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
import mosek import sys def streamprinter(msg): sys.stdout.write(msg) sys.stdout.flush() if len(sys.argv) <= 3: print("Missing argument, syntax is:") print(" opt_server_sync inputfile host port") else: inputfile = sys.argv host = sys.argv port = sys.argv # Create the mosek environment. with mosek.Env() as env: # Create a task object linked with the environment env. # We create it with 0 variables and 0 constraints initially, # since we do not know the size of the problem. with env.Task(0, 0) as task: task.set_Stream(mosek.streamtype.log, streamprinter) # We assume that a problem file was given as the first command # line argument (received in `argv') task.readdata(inputfile) # Solve the problem remotely task.optimizermt(host, port) # Print a summary of the solution 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:
Task.asyncoptimize: Offload the optimization task to a solver server.
Task.asyncpoll: Request information about the status of the remote job.
Task.asyncgetresult: Request the results from a completed remote job.
Task.asyncstop: Terminate a remote job.
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.
import mosek import sys import time def streamprinter(msg): sys.stdout.write(msg) sys.stdout.flush() if len(sys.argv) != 5: print("Missing argument, syntax is:") print(" opt-server-async inputfile host port numpolls") else: filename = sys.argv host = sys.argv port = sys.argv numpolls = int(sys.argv) token = None with mosek.Env() as env: with env.Task(0, 0) as task: print("reading task from file") task.readdata(filename) print("Solve the problem remotely (async)") token = task.asyncoptimize(host, port) print("Task token: %s" % token) with env.Task(0, 0) as task: task.readdata(filename) task.set_Stream(mosek.streamtype.log, streamprinter) i = 0 while i < numpolls: time.sleep(0.1) print("poll %d..." % i) respavailable, trm, res = task.asyncpoll(host, port, token) print("done!") if respavailable: print("solution available!") respavailable, trm, res = task.asyncgetresult(host, port, token) task.solutionsummary(mosek.streamtype.log) break i = i + 1 if i == numpolls: print("max number of polls reached, stopping host.") task.asyncstop(host, port, token)