7.7 MOSEK OptServer

MOSEK provides an easy way to offload optimization problem to a remote server in both synchronous or asynchronous mode. This section describes related functionalities from the client side, i.e. sending optimization tasks to the remote server and retrieving solutions.

Setting up and configuring the remote server is described in a separate manual for the OptServer.

7.7.1 Synchronous Remote Optimization

In synchronous mode the client sends an optimization problem to the server and blocks, waiting for the optimization to end. Once the result has been received, the program can continue. This is the simplest mode and requires very few modifications to existing code: instead of Task.optimize the user must invoke Task.optimizermt with the host and port where the server is running and listening as additional arguments. The rest of the code remains untouched.

Note that it is impossible to recover the job in case of a broken connection.

Source code example

Listing 7.6 Using the OptServer in synchronous mode. Click here to download.
using System;

namespace mosek.example
{
  class msgclass : mosek.Stream
  {
    public override void streamCB (string msg)
    {
      Console.Write ("{0}", msg);
    }
  }

  public class simple
  {
    public static void Main (string[] args)
    {
      if (args.Length == 0)
      {
        Console.WriteLine ("Missing arguments, syntax is:");
        Console.WriteLine ("  opt_server_sync inputfile host port");
      }
      else
      {
        String inputfile = args[0];
        String host      = args[1];
        String port      = args[2];

        mosek.rescode trm;

        using (mosek.Env env = new mosek.Env())
        {
          using (mosek.Task task = new mosek.Task(env))
          {
            task.set_Stream (mosek.streamtype.log, new msgclass ());

            task.readdata (inputfile);

            task.optimizermt (host, port, out trm);

            task.solutionsummary (mosek.streamtype.log);
          }
        }
      }
    }
  }
}

7.7.2 Asynchronous Remote Optimization

In asynchronous mode the client sends a job to the remote server and the execution of the client code continues. In particular, it is the client’s responsibility to periodically check the optimization status and, when ready, fetch the results. The client can also interrupt optimization. The most relevant methods are:

Source code example

In the example below the program enters in a polling loop that regularly checks whether the result of the optimization is available.

Listing 7.7 Using the OptServer in asynchronous mode. Click here to download.
using System;
using System.Threading;

namespace mosek.example
{
  class msgclass : mosek.Stream
  {
    public override void streamCB (string msg)
    {
      Console.Write ("{0}", msg);
    }
  }

  public class opt_server_async
  {
    public static void Main (string[] args)
    {
      if (args.Length == 0)
      {
        Console.WriteLine ("Missing argument, syntax is:");
        Console.WriteLine ("  opt_server inputfile host port numpolls");
      }
      else
      {

        string inputfile = args[0];
        string host      = args[1];
        string port      = args[2];
        int numpolls     = Convert.ToInt32(args[3]);

        using (mosek.Env env = new mosek.Env())
        {

          string token;

          using (mosek.Task task = new mosek.Task(env))
          {
            task.readdata (inputfile);
            token = task.asyncoptimize (host, port);
          }

          using (mosek.Task task = new mosek.Task(env))
          {
            task.readdata (inputfile);
            task.set_Stream (mosek.streamtype.log, new msgclass ());
            Console.WriteLine("Starting polling loop...");

            int i = 0;

            while ( true )
            {
              Thread.Sleep(500);
              Console.WriteLine("poll {0}...\n", i);

              mosek.rescode trm;
              mosek.rescode resp;

              int respavailable = task.asyncpoll( host,
                                                  port,
                                                  token,
                                                  out resp,
                                                  out trm);


              Console.WriteLine("polling done");

              if (respavailable != 0)
              {
                Console.WriteLine("solution available!");
                task.asyncgetresult(host,
                                    port,
                                    token,
                                    out resp,
                                    out trm);

                task.solutionsummary (mosek.streamtype.log);
                break;
              }

              if (i == numpolls)
              {
                Console.WriteLine("max num polls reached, stopping host.");
                task.asyncstop (host, port, token);
                break;
              }
              i++;
            }

            task.solutionsummary (mosek.streamtype.log);
          }
        }
      }
    }
  }
}