7.8 MOSEK OptServer¶
MOSEK provides an easy way to offload optimization problem to a remote server. This section demonstrates 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.
The URL
of the remote server required in all client-side calls should be a string of the form http://host:port
or https://host:port
.
7.8.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 all it takes is to provide the address of the server before starting optimization. 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
#include "mosek.h"
static void MSKAPI printstr(void *handle, const char str[])
{
printf("%s", str);
}
int main(int argc, const char * argv[])
{
MSKenv_t env = NULL;
MSKtask_t task = NULL;
MSKrescodee res = MSK_RES_OK;
MSKrescodee trm = MSK_RES_OK;
if (argc <= 3)
{
fprintf(stderr, "Syntax: opt_server_sync inputfile addr [certpath]\n");
return 0;
}
else
{
const char * taskfile = argv[1];
const char * address = argv[2];
const char * certfile = argc > 3 ? argv[3] : NULL;
// Create the mosek environment.
// The `NULL' arguments here, are used to specify customized
// memory allocators and a memory debug file. These can
// safely be ignored for now.
res = MSK_makeenv(&env, NULL);
// 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.
if (res == MSK_RES_OK)
res = MSK_maketask(env, 0, 0, &task);
// Direct the task log stream to a user specified function
if (res == MSK_RES_OK)
res = MSK_linkfunctotaskstream(task, MSK_STREAM_LOG, NULL, printstr);
// We assume that a problem file was given as the first command
// line argument (received in `argv')
if (res == MSK_RES_OK)
res = MSK_readdata(task, taskfile);
// Set OptServer URL
if (res == MSK_RES_OK)
res = MSK_putoptserverhost(task, address);
// Path to certificate, if any
if (MSK_RES_OK == res && certfile)
res = MSK_putstrparam(task, MSK_SPAR_REMOTE_TLS_CERT_PATH,certfile);
// Solve the problem remotely
if (res == MSK_RES_OK)
res = MSK_optimizetrm(task, &trm);
printf("%s:%d: res = %d\n",__FILE__,__LINE__,res);
// Print a summary of the solution.
if (res == MSK_RES_OK)
res = MSK_solutionsummary(task, MSK_STREAM_LOG);
// Delete task and environment
MSK_deletetask(&task);
MSK_deleteenv(&env);
}
return res;
}
7.8.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:
MSK_asyncoptimize
: Offload the optimization task to a solver server.MSK_asyncpoll
: Request information about the status of the remote job.MSK_asyncgetresult
: Request the results from a completed remote job.MSK_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.
#include "mosek.h"
#ifdef _WIN32
#include "windows.h"
#else
#include "unistd.h"
#endif
static void MSKAPI printstr(void *handle, const char str[])
{
printf("%s", str);
}
int main(int argc, char * argv[])
{
char token[40];
int numpolls = 10;
int i = 0;
MSKbooleant respavailable;
MSKenv_t env = NULL;
MSKtask_t task = NULL;
MSKrescodee res = MSK_RES_OK;
MSKrescodee trm;
MSKrescodee resp;
const char * filename = "../data/25fv47.mps";
const char * addr = "solve.mosek.com:30080";
const char * cert = NULL;
if (argc < 4)
{
fprintf(stderr, "Syntax: opt_server_async filename host:port numpolls [cert]\n");
return 0;
}
filename = argv[1];
addr = argv[2];
numpolls = atoi(argv[3]);
cert = argc < 5 ? NULL : argv[4];
res = MSK_makeenv(&env, NULL);
if (res == MSK_RES_OK)
res = MSK_maketask(env, 0, 0, &task);
if (res == MSK_RES_OK)
res = MSK_linkfunctotaskstream(task, MSK_STREAM_LOG, NULL, printstr);
if (res == MSK_RES_OK)
res = MSK_readdata(task, filename);
if (MSK_RES_OK == res && cert)
res = MSK_putstrparam(task, MSK_SPAR_REMOTE_TLS_CERT_PATH,cert);
res = MSK_asyncoptimize(task,
addr,
"",
token);
MSK_deletetask(&task);
printf("token = %s\n", token);
if (res == MSK_RES_OK)
res = MSK_maketask(env, 0, 0, &task);
if (res == MSK_RES_OK)
res = MSK_readdata(task, filename);
if (MSK_RES_OK == res && cert)
res = MSK_putstrparam(task, MSK_SPAR_REMOTE_TLS_CERT_PATH,cert);
if (res == MSK_RES_OK)
res = MSK_linkfunctotaskstream(task, MSK_STREAM_LOG, NULL, printstr);
for (i = 0; i < numpolls && res == MSK_RES_OK ; i++)
{
#if __linux__ || __APPLE__
sleep(1);
#elif defined(_WIN32)
Sleep(1000);
#endif
puts("+++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("poll %d\n ", i);
res = MSK_asyncpoll(task,
addr,
"",
token,
&respavailable,
&resp,
&trm);
puts("polling done\n");
puts("+++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
if (respavailable)
{
puts("solution available!");
res = MSK_asyncgetresult(task,
addr,
"",
token,
&respavailable,
&resp,
&trm);
MSK_solutionsummary(task, MSK_STREAM_LOG);
break;
}
}
if (i == numpolls)
{
printf("max num polls reached, stopping %s", addr);
MSK_asyncstop(task, addr, "", token);
}
MSK_deletetask(&task);
MSK_deleteenv(&env);
printf("%s:%d: Result = %d\n", __FILE__, __LINE__, res); fflush(stdout);
return res;
}