Initializing help system before first use

Basic tasks: remote connection, coordination, communication, and parallelization


Type: Programming
Rating: 3
Description: A series of examples of basic tasks that typically need to be performed when working with remote models in Mosel:
  • Check for available remote Mosel servers: findservers.*
  • Run a single model on a remote machine: runrtdistr.* (executing rtparams.mos)
  • Running parallel submodels in a distributed architecture: runrtpardistr.* (executing rtparams3.mos)
  • Queuing submodels for parallel execution in a distributed architecture with one or several models per node: runrtparqueued.* (executing rtparams3.mos)
File(s): findservers.c, findservers.java, runrtdistr.c, runrtdistr.java, rtparams.mos, runrtpardistr.c, runrtpardistr.java, rtparams3.mos, runrtparqueued.c, runrtparqueued.java


findservers.c
/*******************************************************
   Mosel Example Problems 
   ======================

   file findservers.c
   ``````````````````
   Find Mosel servers that are able to accept remote model runs

   This file only produces output on the local node, 
   it does not start any remote runs.
       
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke, Jan. 2013
*******************************************************/

#include 
#include 
#ifdef _WIN32
#include 
#else
#include 
#include 
#include 
#endif
#include "xprd.h"

#define M 20                   /* Max. number of servers to be sought */

int main(int argv,char *args[]) 
{
  XPRDcontext xprd;
  XPRDmosel mosInst;
  struct in_addr Hosts[M];
  char buf[200];
  int hsize, i;

  xprd=XPRDinit();             /* Create an XPRD context */
 
  hsize=XPRDfindxsrvs(xprd, 1,M, (unsigned int *)Hosts);
  printf("%d servers found.\n", hsize); 
   
  for(i=0;i

findservers.java
/*******************************************************
   Mosel Example Problems 
   ======================

   file findservers.java
   `````````````````````
   Find Mosel servers that are able to accept remote model runs

   This file only produces output on the local node, 
   it does not start any remote runs.
       
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke, Jan. 2013
*******************************************************/

import com.dashoptimization.*;
import java.lang.*;
import java.io.*;
import java.util.*;


public class findservers
{
 static final int M=20;
 
 public static void main(String[] args) throws Exception
 {
  XPRD xprd=new XPRD();
  XPRDMosel mosInst=null;
  Set Hosts=new HashSet();

  xprd.findXsrvs(1, M, Hosts);
  System.out.println(Hosts.size() + " servers found.");
  
  for(Iterator h=Hosts.iterator(); h.hasNext();)
  {
   String i=h.next();
                               // Open connection to a remote node
   try {
    mosInst=xprd.connect(i);
    
    System.out.println("Server " + i + ": " + mosInst.getSystemInformation());
    mosInst.disconnect();        // Disconnect remote instance
   }
   catch(IOException e) {
    System.out.println("Connection to " + i + " failed");
   }
  }
  
 }
} 

runrtdistr.c
/*******************************************************
   Mosel Example Problems 
   ======================

   file runrtdistr.c
   `````````````````
   Running a model on a remote machine,
   passing runtime parameters to the submodel.

   Before running this model, you need to set up the 
   NODENAME with a machine name/address of your local network.
   The node that is used needs to have the same version of
   Xpress installed and suitably licensed, and the server 
   "xprmsrv" must have been started on this machine.
       
   (c) 2012 Fair Isaac Corporation
       author: S. Heipcke, Jan 2012
*******************************************************/

#include 
#include 
#include "xprd.h"

int main(int argv,char *args[]) 
{
  XPRDcontext xprd;
  XPRDmosel mosInst;
  XPRDmodel modRP;
  char params[200];

       /* Use the name or IP address of a machine in
        * your local network, or "" for current node */
  char *NODENAME = "";

  xprd=XPRDinit();             /* Create an XPRD context */
                               /* Open connection to a remote node */
  mosInst=XPRDconnect(xprd, NODENAME, NULL, NULL, NULL, 0);
                               /* Compile the model file */
  XPRDcompmod(mosInst, "", "rmt:rtparams.mos", "tmp:rp.bim", "");
                               /* Load the bim file into the remote instance */
  modRP=XPRDloadmod(mosInst, "tmp:rp.bim"); 
                               /* Run-time parameters */
  sprintf(params, "PARAM1=%d,PARAM2=%g,PARAM3='a string',PARAM4=true", 2, 3.4);
  XPRDrunmod(modRP, params);   /* Run the model */
  XPRDwaitevent(xprd,-1);      /* Wait for model termination */
  XPRDdropevent(xprd);         /* Ignore termination event message */

  printf("`rtparams' returned: %d\n", XPRDgetexitcode(modRP));

  XPRDunloadmod(modRP);        /* Unload the model */
  XPRDdisconnect(mosInst);     /* Disconnect remote instance */
  XPRDfinish(xprd);            /* Terminate XPRD */
  return 0;
} 

runrtdistr.java
/*******************************************************
   Mosel Example Problems 
   ======================

   file runrtdistr.java
   ````````````````````
   Running a model on a remote machine,
   passing runtime parameters to the submodel.

   Before running this model, you need to set up the 
   NODENAME with a machine name/address of your local network.
   The node that is used needs to have the same version of
   Xpress installed and suitably licensed, and the server 
   "xprmsrv" must have been started on this machine.
       
   (c) 2012 Fair Isaac Corporation
       author: S. Heipcke, Jan 2012
*******************************************************/

import com.dashoptimization.*;
import java.lang.*;
import java.io.*;

public class runrtdistr
{
 public static void main(String[] args) throws Exception
 {
  XPRD xprd=new XPRD();
  XPRDMosel mosInst=null;
  XPRDModel modRP=null;

       // Use the name or IP address of a machine in
       // your local network, or "" for current node
  String NODENAME = "";
                               // Open connection to a remote node
  mosInst=xprd.connect(NODENAME);
                               // Compile the model file
  mosInst.compile("", "rmt:rtparams.mos", "tmp:rp.bim"); 
                               // Load the bim file into the remote instance
  modRP=mosInst.loadModel("tmp:rp.bim"); 
                               // Run-time parameters
  modRP.execParams = "PARAM1=" + 2 + ",PARAM2=" + 3.4 + 
                     ",PARAM3='a string'" + ",PARAM4=true";
  modRP.run();                 // Run the model
  xprd.waitForEvent();         // Wait for model termination
  xprd.dropNextEvent();        // Ignore termination event message

  System.out.println("`rtparams' returned: " + modRP.getResult());

  mosInst.disconnect();        // Disconnect remote instance
 }
} 

rtparams.mos
(!*******************************************************
   Mosel Example Problems 
   ======================

   file rtparams.mos
   `````````````````
   Model with different runtime parameter types;
   can be run as submodel from models 'runrtpar*.mos'.
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006
*******************************************************!)

model "Runtime parameters"
 uses "mmjobs"
 parameters
  PARAM1 = 0
  PARAM2 = 0.5
  PARAM3 = ''
  PARAM4 = false
 end-parameters

 wait(round(5*random))
 writeln(PARAM1, "  ", PARAM2, "  ", PARAM3, "  ", PARAM4)
end-model 

runrtpardistr.c
/*******************************************************
   Mosel Example Problems 
   ======================

   file runrtpardistr.c
   ````````````````````
   Running several instances of a model remotely from
   a host program.
   - Parallel submodels in distributed architecture -

   Before running this model, you need to set up the 
   NODENAME with a machine name/address of your local network.
   The node that is used needs to have the same version of
   Xpress installed and suitably licensed, and the server 
   "xprmsrv" must have been started on this machine.
       
   (c) 2012 Fair Isaac Corporation
       author: S. Heipcke, Apr 2012, rev. Jan. 2013
*******************************************************/

#include 
#include 
#include "xprd.h"

#define A 10 
#define B 5

int main(int argv,char *args[]) 
{
  XPRDcontext xprd;
  XPRDmosel mosInst[B];
  XPRDmodel modRP[A];
  char params[200];
  char *NODENAME[B];
  int i,j;
  char bufd[200],bufn[200];

       /* Use the name or IP address of a machine in
        * your local network, or "" for current node */
  for(j=0;j

runrtpardistr.java
/*******************************************************
   Mosel Example Problems 
   ======================

   file runrtpardistr.java
   ```````````````````````
   Running several instances of a model remotely from
   a host program.
   - Parallel submodels in distributed architecture -

   Before running this model, you need to set up the 
   NODENAMES with a machine name/address of your local network.
   All nodes that are used need to have the same version of
   Xpress installed and suitably licensed, and the server 
   "xprmsrv" must have been started on this machine.
       
   (c) 2012 Fair Isaac Corporation
       author: S. Heipcke, Feb 2012, rev. Jan. 2013
*******************************************************/

import com.dashoptimization.*;
import java.lang.*;
import java.io.*;

public class runrtpardistr
{
 static final int A=10;
 static final int B=5;

 public static void main(String[] args) throws Exception
 {
  XPRD xprd=new XPRD();
  XPRDMosel[] mosInst=new XPRDMosel[B];
  XPRDModel[] modRP=new XPRDModel[A];
  String[] NODENAMES=new String[B];
  int i,j;

       // Use the name or IP address of a machine in
       // your local network, or "" for current node
  for(j=0;j

rtparams3.mos
(!*******************************************************
   Mosel Example Problems 
   ======================

   file rtparams3.mos
   ``````````````````
   Model with different runtime parameter types;
   can be run as submodel from models 'runrtpar*.mos'.
       
   (c) 2010 Fair Isaac Corporation
       author: S. Heipcke, May 2010
*******************************************************!)

model "Runtime parameters"
 uses "mmjobs", "mmsystem"
 parameters
  PARAM1 = 0
  PARAM2 = 0.5
  PARAM3 = ''
  PARAM4 = false
 end-parameters

 sleep(1000*round(5*random))
 writeln("Node: ", getparam("NODENUMBER"), " ", getsysinfo(SYS_NODE), 
         ". Parent: ", getparam("PARENTNUMBER"),
	 ". Model number: ", getparam("JOBID"), 
         ". Parameters: ", 
         PARAM1, "  ", PARAM2, "  ", PARAM3, "  ", PARAM4)
end-model 

runrtparqueued.c
/*******************************************************
   Mosel Example Problems 
   ======================

   file runrtparqueued.c
   `````````````````````
   Running several instances of a model from another
   Mosel model.
   - Queuing submodels for parallel execution in a
     distributed architecture (one or several models per node) -

   Before running this model, you need to set up the list
   NodeList with machine names/addresses of your local network.
   All nodes that are used need to have the same version of
   Xpress installed and suitably licensed, and the server 
   "xprmsrv" must have been started on these machines.

   The maximum number of models per node in array MaxMod needs
   to be adapted to the number of executions licensed on 
   the corresponding nodes.
   
   All files are local to the root node, no write access is
   required at remote nodes.
       
   (c) 2012 Fair Isaac Corporation
       author: S. Heipcke & Y. Colombani, Apr. 2012
*******************************************************/

#include 
#include 
#include 
#include "xprd.h"

#define J 10         /* Number of jobs to run */
#define NUMPAR 2     /* Number of parallel model executions */
                     /* (preferrably <= no. of processors) */

#define MAXQSIZE 20  /* Maximum number of elements in a queue (>=J) */

                     /* Use the name or IP address of a machine in
                        your local network, or "" for current node */
const char *NodeList[]={"localhost","localhost"};
#define NodeListSize (sizeof(NodeList)/sizeof(NodeList[0]))
#define nbNodes (NodeListSizelastId) lastId=XPRDgetnumber(modPar[nct]);
   nct++;
  }

 jobid=malloc(sizeof(int)*(lastId+1));
 modid=malloc(sizeof(int)*(lastId+1));
 modNode=malloc(sizeof(char *)*(lastId+1));

 for(j=0;j0)     /* Start a new run if queue not empty */
    start_next_job(&JobList,sender);
  }
 }

 for(n=0;nsize>=MAXQSIZE) return -1;
 else
 {
  q->buf[q->head++]=e;
  if(q->head>=MAXQSIZE) q->head=0;
  return ++q->size;
 }
}

/**********************************/
/* Remove an element from a queue */
/**********************************/
int queue_get(s_queue *q)
{
 int rts;

 if(q->size<1) return -1;
 else
 {
  rts=q->buf[q->tail++];
  if(q->tail>=MAXQSIZE) q->tail=0;
  q->size--;
  return rts;
 }
}

/***************************/
/* Get the size of a queue */
/***************************/
int queue_size(s_queue *q)
{ return q->size; }

runrtparqueued.java
/*******************************************************
   Mosel Example Problems 
   ======================

   file runrtparqueued.java
   ````````````````````````
   Running several instances of a model from another
   Mosel model.
   - Queuing submodels for parallel execution in a
     distributed architecture (one or several models per node) -

   Before running this model, you need to set up the list
   NodeList with machine names/addresses of your local network.
   All nodes that are used need to have the same version of
   Xpress installed and suitably licensed, and the server 
   "xprmsrv" must have been started on these machines.

   The maximum number of models per node in array MaxMod needs
   to be adapted to the number of executions licensed on 
   the corresponding nodes.
   
   All files are local to the root node, no write access is
   required at remote nodes.
       
   (c) 2012 Fair Isaac Corporation
       author: S. Heipcke & Y. Colombani, Apr. 2012
*******************************************************/

import com.dashoptimization.*;
import java.lang.*;
import java.util.*;
import java.io.*;

public class runrtparqueued
{
 static final int J=10;             // Number of jobs to run
 static final int NUMPAR=2;         // Number of parallel model executions
                                    // (preferrably <= no. of processors)
 static int[] jobid;
 static int[] modid;
 static String[] modNode;

 public static void main(String[] args) throws Exception
 {
  XPRD xprd=new XPRD();
                            // Use the name or IP address of a machine in
                            // your local network, or "" for current node
  String[] NodeList={"localhost","localhost"};
  final int nbNodes=(NodeList.length JobList=new ArrayList();
  List JobsRun=new ArrayList();
  int JobSize;
  XPRDEvent event;
  int lastId=0;

  //**** Setting up remote Mosel instances ****
  for(int n=0;nlastId) lastId=modPar[nct].getNumber();
    nct++;
   }

  jobid=new int[lastId+1];
  modid=new int[lastId+1];
  modNode=new String[lastId+1];
  for(int j=0;j jobList,XPRDModel model) throws Exception
 {
  Integer job;
  int i;

  job=jobList.remove(0);      // Retrieve and remove first entry from job list
  i=job.intValue();
  System.out.println("Start job "+ job + " (model " + modid[model.getNumber()]+
                          ") on "+ modNode[model.getNumber()] );
  model.execParams = "PARAM1=" + i + ",PARAM2=" + (0.1*i) + 
                          ",PARAM3='a string " + i + "',PARAM4=" + (i%2==0);
  model.run();
  jobid[model.getNumber()]=i;
 }

}