Initializing help system before first use

XPRD C and XPRD Java


Type: Distributed Computing
Rating: 3 (intermediate)
Description:
  • runprimedistr.c, runprimedistr.java, runprimedistr.mos: Running a model on a remote Mosel instance (requires prime.mos)
  • runprimeiodistr.c, runprimeiodistr.java: Running a model on a remote Mosel instance and retrieving output data: data saved to file or in memory on the remote machine (requires primeio.mos)
  • runprimeiodistr2.c, runprimeiodistr2.java: Running a model on a remote Mosel instance and retrieving output data: data saved in memory on the local machine. Implementation of a file manager for data exchange in memory (requires primeio.mos)
  • runprimeiodistr3.c, runprimeiodistr3.java: Running a model on a remote Mosel instance and retrieving output data: data saved to a file on the local machine (requires primeio.mos)
  • runprimeiodistr.mos: mmjobs implementation. Running a model on a remote Mosel instance and retrieving output data (requires primeio.mos)
File(s): runprimedistr.c, runprimedistr.java, runprimedistr.mos, prime.mos (submodel), runprimeiodistr.c, runprimeiodistr.java, runprimeiodistr.mos, primeio.mos (submodel), runprimeiodistr2.c, runprimeiodistr2.java, runprimeiodistr3.c, runprimeiodistr3.java

runprimedistr.c
/*******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file runprimedistr.c
   ````````````````````
   Distributed computing: 
   Running a model on a remote Mosel instance.

   Before running this model, the server 
   "xprmsrv" must have been started on this machine.
       
   (c) 2012 Fair Isaac Corporation
       author: S. Heipcke, Apr 2012
*******************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "xprd.h"

int main(int argv,char *args[]) 
{
  XPRDcontext xprd;
  XPRDmosel moselInst;
  XPRDmodel modPrime, evsender;
  double evvalue;
  int evclass;

  xprd=XPRDinit();              /* Create an XPRD context */
                                /* Open connection to a remote node:
			           "" means the node running this program */
  moselInst=XPRDconnect(xprd, "", NULL, NULL, NULL, 0);
                                /* Compile the model file */
  XPRDcompmod(moselInst, "", "rmt:prime.mos", "rmt:prime.bim", "");
                                /* Load the bim file into the remote instance */
  modPrime=XPRDloadmod(moselInst, "rmt:prime.bim"); 

  XPRDrunmod(modPrime, "LIMIT=50000");  /* Start execution and */
  XPRDwaitevent(xprd,2);        /* wait 2 seconds for an event */

  if (XPRDqueueempty(xprd)==1)  /* No event has been sent... */
  {
   printf("Model too slow: stopping it!\n");
   XPRDstoprunmod(modPrime);    /* ... stop the model, then wait */
   XPRDwaitevent(xprd,-1);
  }

  XPRDgetevent(xprd, &evsender, &evclass, &evvalue);    /* Get the event */
  printf("Event value: %g\n", evvalue);
/*printf("Event value: %g sent by model %d\n", evvalue, XPRDgetnum(evsender));*/
  printf("Exit status: %d\n", XPRDgetstatus(modPrime));
  printf("Exit code  : %d\n", XPRDgetexitcode(modPrime));

  XPRDunloadmod(modPrime);       /* Unload the model */
  XPRDdisconnect(moselInst);     /* Disconnect remote instance */
  XPRDfinish(xprd);              /* Terminate XPRD */

  remove("prime.bim");           /* Clean up temporary files */

  return 0;
} 

runprimedistr.java
/******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file runprimedistr.java 
   ```````````````````````
   Distributed computing: 
   Running a model on a remote Mosel instance.
  
   (c) 2012 Fair Isaac Corporation
       author: S. Heipcke, Feb 2012
*******************************************************/

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

public class runprimedistr
{
 public static void main(String[] args) throws Exception
 {
  XPRD xprd=new XPRD();
  XPRDMosel moselInst;
  XPRDModel modPrime;
  XPRDEvent event;

  moselInst=xprd.connect("");    // Open connection to remote nodes
                                 // "" means the node running this program
    
                                 // Compile the model file on remote instance
  moselInst.compile("", "rmt:prime.mos", "rmt:prime.bim");
  
	                         // Load the bim file into remote instance
  modPrime=moselInst.loadModel("rmt:prime.bim"); 

  modPrime.execParams = "LIMIT=50000";
  modPrime.run();                // Start execution and
  xprd.waitForEvent(2);          // wait 2 seconds for an event

  if (xprd.isQueueEmpty())       // No event has been sent...
  {
   System.out.println("Model too slow: stopping it!");
   modPrime.stop();              // ... stop the model, then wait
   xprd.waitForEvent();
  }
                                 // An event is available: model finished
  event=xprd.getNextEvent();
  System.out.println("Event value: " + event.value +
                     " sent by model " + event.sender.getNumber());
  System.out.println("Exit status: " + modPrime.getExecStatus());
  System.out.println("Exit code  : " + modPrime.getResult());

  moselInst.unloadModel(modPrime);  // Unload the submodel
  moselInst.disconnect();           // Terminate the connection

  new File("prime.bim").delete();   // Clean up temporary files
 }
} 


runprimedistr.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file runprimedistr.mos 
   ``````````````````````
   Distributed computing: 
   Running a model on a remote Mosel instance.

   *** ATTENTION: This model will return an error if ***
   *** no more than one Xpress licence is available. ***
  
   (c) 2010 Fair Isaac Corporation
       author: S. Heipcke, July 2010
*******************************************************!)

model "Run model prime remotely"
 uses "mmjobs"

 declarations
  moselInst: Mosel
  modPrime: Model
  event: Event
 end-declarations
                               ! Compile 'prime.mos' locally
 if compile("prime.mos")<>0 then exit(1); end-if

                               ! Start a remote Mosel instance: 
                               ! "" means the node running this model
 if connect(moselInst, "")<>0 then exit(2); end-if

                               ! Load bim file into remote instance
 load(moselInst, modPrime, "rmt:prime.bim")

 run(modPrime, "LIMIT=50000")  ! Start execution and
 wait(2)                       ! wait 2 seconds for an event

 if isqueueempty then          ! No event has been sent...
  writeln("Model too slow: stopping it!")
  stop(modPrime)               ! ... stop the model, then wait
  wait
 end-if
                               ! An event is available: model finished
 event:=getnextevent
 writeln("Exit status: ", getvalue(event))
 writeln("Exit code  : ", getexitcode(modPrime))

 unload(modPrime)              ! Unload the submodel
end-model 

prime.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file prime.mos 
   `````````````
   Working with sets.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001
*******************************************************!)

model Prime 

 parameters
  LIMIT=100                     ! Search for prime numbers in 2..LIMIT
 end-parameters

 declarations
  SNumbers: set of integer      ! Set of numbers to be checked
  SPrime: set of integer        ! Set of prime numbers
 end-declarations

 SNumbers:={2..LIMIT} 
 
 writeln("Prime numbers between 2 and ", LIMIT, ":")

 n:=2
 repeat
   while (not(n in SNumbers)) n+=1
   SPrime += {n}                ! n is a prime number
   i:=n
   while (i<=LIMIT) do          ! Remove n and all its multiples
     SNumbers-= {i}
     i+=n
   end-do
 until SNumbers={}    
 
 writeln(SPrime)
 writeln(" (", getsize(SPrime), " prime numbers.)")
 
end-model


runprimeiodistr.c
/*******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file runprimeiodistr.c
   ``````````````````````
   Distributed computing: 
   Running a model on a remote Mosel instance
   and retrieving output data.

   - Data saved on the remote machine running the Mosel model -
     (Submodel writing to "bin:shmem:" or "bin:tmp:")
       
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke, Jan. 2013
*******************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xprd.h"
#include "bindrv.h"

#define STOPMOD 2            /* Identifier for "Stop submodel" user event */
#define MODREADY 3           /* Identifier for "Submodel ready" user event */

/*********************************/
/* Wrapper function for 'bindrv' */
/*********************************/
static size_t my_read(void *buf, size_t size, size_t nmemb, void *ctx)
{
 size_t s,a;
 long t;
 
 s=size*nmemb;
 a=0;
 while(s>0)
 {
  t=XPRDfread(ctx,(char*)buf+a,s);
  if(t<=0) break;
  else
  {
   a+=t;
   s-=t;
  }
 }
 return a/size;
}

/********************************************************/
/* Using bindrv 
   Decode the binary file and display its content       */
/********************************************************/
void show_solution(size_t (*doread)(void *,size_t,size_t,void*), void *rctx)
{
 s_bindrvctx bdrv;
 int *solarr;
 int size,i,n;
 char *str;

 bdrv=bindrv_newreader(doread,rctx);  /* Initialize binreader */

 i=size=0;
 solarr=NULL;
 while(bindrv_nexttoken(bdrv)>=0)
 {
  bindrv_getctrl(bdrv,&n);         /* 'label'  (marker) */
  bindrv_getstring(bdrv,&str);        /* Read a string */
  if(strcmp(str,"NumP")==0)
  {
   free(str);
   bindrv_getint(bdrv,&size);         /* Read an integer */
   printf("( %d prime numbers)\n", size);
   if(size>0)                         /* Prepare array to receive values */
    solarr=malloc(sizeof(int)*size);
   else
    break;
  } 
  else
  if(strcmp(str,"SPrime")==0)
  {
   free(str);
   bindrv_getctrl(bdrv,&n);       /* [  (start marker) */
   while(bindrv_nexttoken(bdrv)==BINDRV_TYP_INT)
   {                                  /* Read integers */
    bindrv_getint(bdrv,&(solarr[i++]));
   }
   bindrv_getctrl(bdrv,&n);        /* ]  (end marker) */
  }
  else
  {
   printf("Unexpected label: %s\n", str);
   free(str);
   exit(1);
  }
 }

 bindrv_delete(bdrv);              /* Release bin reader */

/* Print the set of prime numbers */
 printf("Prime numbers={");
 for(i=0;i<size;i++)
  printf(" %d",solarr[i]);
 printf(" }\n");
 
 free(solarr);                     /* Clean up */
}

/********************************************************/
int main(int argv,char *args[]) 
{
  XPRDcontext xprd;
  XPRDmosel moselInst;
  XPRDmodel modPrime, evsender;
  double evvalue;
  int evclass;
  XPRDfile f;

  xprd=XPRDinit();              /* Create an XPRD context */
                                /* Open connection to a remote node:
			           "" means the node running this program */
  moselInst=XPRDconnect(xprd, "", NULL, NULL, NULL, 0);
                                /* Compile the model file */
  XPRDcompmod(moselInst, "", "rmt:primeio.mos", "tmp:primeio.bim", "");
                                /* Load the bim file into the remote instance */
  modPrime=XPRDloadmod(moselInst, "tmp:primeio.bim"); 

                                /* Disable submodel output */
  XPRDsetdefstream(moselInst, modPrime, XPRD_F_WRITE, "null:");

                                /* Start execution */
  XPRDrunmod(modPrime, "LIMIT=50000,OUTPUTFILE=bin:shmem:resdata"); 
  XPRDwaitevent(xprd,0);        /* Wait for an event */
  XPRDgetevent(xprd, &evsender, &evclass, &evvalue);    /* Get the event */
  if (evclass != MODREADY)      /* Check the event class */
  {
    printf("Problem with submodel run");
    return 1;
  } 

  XPRDwaitevent(xprd,2);        /* Wait 2 seconds for an event */

  if (XPRDqueueempty(xprd)==1)  /* No event has been sent */
  {
   printf("Model too slow: stopping it!\n");
   XPRDsendevent(modPrime, STOPMOD, 0);    /* Stop the model, then  */
   XPRDwaitevent(xprd,-1);      /* wait for its termination */
  } 

  /* Open the output file, retrieve and display the solution data */
  f=XPRDfopen(moselInst, "shmem:resdata", XPRD_F_BINARY|XPRD_F_INPUT, NULL,0);
  show_solution(my_read,f);
  XPRDfclose(f);

  XPRDunloadmod(modPrime);      /* Unload the model */
  XPRDdisconnect(moselInst);    /* Disconnect remote instance */
  XPRDfinish(xprd);             /* Terminate XPRD */

  return 0;
} 

runprimeiodistr.java
/******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file runprimeiodistr.java 
   `````````````````````````
   Distributed computing: 
   Running a model on a remote Mosel instance
   and retrieving output data.
   
   - Data saved on the remote machine running the Mosel model -
     (Submodel writing to "bin:shmem:" or "bin:tmp:")
  
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke & Y. Colombani, Jan 2013
*******************************************************/

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

public class runprimeiodistr
{
 static final int STOPMOD = 2;    // Identifier for "Stop submodel" user event
 static final int MODREADY = 3;   // Identifier for "Submodel ready" user event

// **** Decode the binary stream and display its contents ****
 static void showSolution(InputStream inbuf) throws Exception
 {
  BinDrvReader bdrv=new BinDrvReader(inbuf);  // Initialize binreader
  String label;
  ArrayList<Integer> setP=new ArrayList<Integer>();

  while(bdrv.nextToken()>=0)
  {
   bdrv.getControl();          // 'label'  (marker)
   label=bdrv.getString();        // Read a string
   if(label.equals("NumP"))
   {                              // Read an integer
    System.out.println("(" + bdrv.getInt() + " prime numbers.)");
   }
   else
   if(label.equals("SPrime"))
   {
    bdrv.getControl();         // [  (start marker)
    while(bdrv.nextToken()==BinDrvReader.TYP_INT) // or ] at end of list
    {                             // Read integers
     setP.add(new Integer(bdrv.getInt()));
    }
    bdrv.getControl();         // ]  (end marker)
   }
   else
   {
    System.out.println("Unexpected label: "+label);
    System.exit(0);
   }
  }

 // Display the contents of the set 'SPrime'
  Iterator<Integer> iprime=setP.iterator();
  System.out.print("Prime numbers={");
  while(iprime.hasNext())
  {
   Integer p=iprime.next();
   System.out.print(" "+p);
  }
  System.out.println(" }");
 }

/*******************************************************/
 public static void main(String[] args) throws Exception
 {
  XPRD xprd=new XPRD();          // Initialize XPRD
  XPRDMosel moselInst;
  XPRDModel modPrime;
  XPRDEvent event;
  InputStream resdata;

  moselInst=xprd.connect("");    // Open connection to remote nodes
                                 // "" means the node running this program
    
                                 // Compile the model file on remote instance
  moselInst.compile("", "rmt:primeio.mos", "tmp:primeio.bim");
  
	                         // Load the bim file into remote instance
  modPrime=moselInst.loadModel("tmp:primeio.bim"); 

                                 // Disable submodel output
  modPrime.setDefaultStream(modPrime.F_OUTPUT, "null:");

  modPrime.execParams = "LIMIT=50000,OUTPUTFILE=bin:shmem:resdata";
  modPrime.run();                     // Start execution and
  xprd.waitForEvent();                // ...wait for an event
  event=xprd.getNextEvent();          // Retrieve the event
  if (event.eventClass != MODREADY)   // Check the event class 
  {
    System.out.println("Problem with submodel run");
    System.exit(1);
  } 

  xprd.waitForEvent(2);               // Let the submodel run for 2 seconds

  if (xprd.isQueueEmpty())            // No event has been sent...
  {
   System.out.println("Model too slow: stopping it!");
   modPrime.sendEvent(STOPMOD, 0);    // ... stop the model, then 
   xprd.waitForEvent();               // wait for its termination
  }

  // Open the output file, retrieve and display the solution data
  resdata=moselInst.openForReading("shmem:resdata", moselInst.F_BINARY);
  showSolution(resdata);
  resdata.close();

  moselInst.unloadModel(modPrime);    // Unload the submodel
  moselInst.disconnect();             // Terminate the connection
 }
} 


runprimeiodistr.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file runprimeiodistr.mos 
   ````````````````````````
   Distributed computing: 
   Running a model on a remote Mosel instance.

   *** ATTENTION: This model will return an error if ***
   *** no more than one Xpress licence is available. ***
  
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke, Jan. 2013
*******************************************************!)

model "Run model primeio remotely"
 uses "mmjobs"

 declarations
  moselInst: Mosel
  modPrime: Model
  event: Event
  NumP: integer                ! Number of prime numbers found
  SetP: set of integer         ! Set of prime numbers
  STOPMOD = 2                  ! "Stop submodel" user event
  MODREADY = 3                 ! "Submodel ready" user event
 end-declarations
                               ! Compile 'prime.mos' locally
 if compile("primeio.mos")<>0 then exit(1); end-if

                               ! Start a remote Mosel instance: 
                               ! "" means the node running this model
 if connect(moselInst, "")<>0 then exit(2); end-if

                               ! Load bim file into remote instance
 load(moselInst, modPrime, "rmt:primeio.bim")

                               ! Disable submodel output
 setdefstream(modPrime,"","null:","null:")
                               ! Start execution and
 run(modPrime, "LIMIT=50000,OUTPUTFILE=bin:rmt:shmem:resdata")
 wait                          ! ... wait for an event
 if getclass(getnextevent) <> MODREADY then
  writeln("Problem with submodel run")
  exit(1)
 end-if 

 wait(2)                       ! Let the submodel run for 2 seconds

 if isqueueempty then          ! No event has been sent...
  writeln("Model too slow: stopping it!")
  send(modPrime, STOPMOD, 0)   ! ... stop the model, then wait
  wait
 end-if

 initializations from "bin:shmem:resdata"
  NumP  SetP as "SPrime"
 end-initializations
 
 writeln(SetP)                 ! Output the result
 writeln(" (", NumP, " prime numbers.)")

 unload(modPrime)              ! Unload the submodel
end-model 

primeio.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file primeio.mos 
   ```````````````
   Data exchange via memory.

   *** Not intended to be run standalone - run from runprimeio*  ***
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2005, rev. Jan. 2013
*******************************************************!)

model "Prime IO" 
 uses "mmjobs"

 parameters
  LIMIT=100                     ! Search for prime numbers in 2..LIMIT
  OUTPUTFILE = "bin:shmem:resdata"
 end-parameters

 declarations
  SNumbers: set of integer      ! Set of numbers to be checked
  SPrime: set of integer        ! Set of prime numbers
  STOPMOD = 2                   ! "Stop submodel" user event
  MODREADY = 3                  ! "Submodel ready" user event
 end-declarations

 send(MODREADY,0)               ! Send "model ready" event 

 SNumbers:={2..LIMIT} 
 
 writeln("Prime numbers between 2 and ", LIMIT, ":")

 n:=2
 repeat
   while (not(n in SNumbers)) n+=1
   SPrime += {n}                ! n is a prime number
   i:=n
   while (i<=LIMIT) do          ! Remove n and all its multiples
     SNumbers-= {i}
     i+=n
   end-do
 until (SNumbers={} or not isqueueempty)     
 
 NumP:= getsize(SPrime)
 
 initializations to OUTPUTFILE
  NumP  SPrime 
 end-initializations
 
end-model

runprimeiodistr2.c
/*******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file runprimeiodistr2.c
   ```````````````````````
   Distributed computing: 
   Running a model on a remote Mosel instance
   and retrieving output data.
   
   - Data saved in memory on the local machine running XPRD -
   - Implementation of a file manager for data exchange in memory -
     (Submodel writing to "bin:rmt:")
       
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke & Y. Colombani, Jan. 2013
*******************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xprd.h"
#include "bindrv.h"

#define STOPMOD 2            /* Identifier for "Stop submodel" user event */
#define MODREADY 3           /* Identifier for "Submodel ready" user event */

static char datafile[20*1024];	 /* Memory block to store the output data */
                                 /* (using static size for simplicity's sake) */
static int datafilesize;

typedef struct
	{
	 char *buffer;
	 int mode;
	 int bufsize;
	 int curpos;
	} s_memfile;

/********************************************************
   Implementation of a file manager 
   This file manager accesses directly file 'resdata'
   in local memory block 
   Warning: concurrency not handled here!!!
 ********************************************************/

void* XPRD_CC my_open(void *ctx, char *filename, int, XPRDfct_data* fct_data, 
       XPRDfct_close* fct_close, XPRDfct_skip *fct_skip, char *msg, int msglen);
int XPRD_CC my_close(void *data);
int XPRD_CC my_read(void *data, char *buf, int size);
int XPRD_CC my_write(void *data, char *buf, int size);

void* XPRD_CC my_open(void *ctx, char *filename, int mode, 
                      XPRDfct_data* fct_data, XPRDfct_close* fct_close, 
                      XPRDfct_skip *fct_skip, char *msg, int msglen)
{
 s_memfile *memfile;

 if(strcmp(filename,"resdata")==0)
 {
  if((memfile=malloc(sizeof(s_memfile)))==NULL)
  {
   strncpy(msg,"memory error",msglen);
   return XPRD_FMGR_ERR;
  }
  else
  {
   memfile->buffer=datafile;
   memfile->mode=mode&(XPRD_F_READ|XPRD_F_WRITE);      
   memfile->bufsize=(memfile->mode==XPRD_F_READ)?datafilesize:sizeof(datafile);
   memfile->curpos=0;
   *fct_data=(memfile->mode==XPRD_F_READ)?my_read:my_write;
   *fct_close=my_close;
   *fct_skip=NULL;
   return memfile;
  }
 }
 else
  return NULL;               /*  Use default behaviour (open local file) */
}

/**** Release resources used by a memory file ****/
int XPRD_CC my_close(void *data)
{
 s_memfile *memfile;

 memfile=data;
 /* save size of datafile */
 if((memfile->mode==XPRD_F_WRITE)&&(memfile->buffer==datafile))
  datafilesize=memfile->curpos;
 free(data);
 return 0;
}

/**** Send data from a block of memory ****/
int XPRD_CC my_read(void *data,char *buf,int size)
{
 s_memfile *memfile;
 int l;

 memfile=data;
 if(memfile->curpos>=memfile->bufsize)           /* end of file */
  return 0;
 else
 if((l=memfile->bufsize-memfile->curpos)<=size)  /* last block */
 {
  memcpy(buf,memfile->buffer+memfile->curpos,l);
  memfile->curpos=memfile->bufsize;
  return l;
 }
 else
 {
  memcpy(buf,memfile->buffer+memfile->curpos,size);
  memfile->curpos+=size;
  return size;
 }
}

/**** Reading function with signature as required by the bin driver ****/
size_t my_fread(void *buf,size_t l,size_t nm,void *fd)
{
 return (my_read(fd,buf,nm*l)==l*nm)?nm:0;
}

/**** Store data in a block of memory ****/
int XPRD_CC my_write(void *data,char *buf,int size)
{
 s_memfile *memfile;
 memfile=data;
 if(memfile->curpos+size>=memfile->bufsize)     /* buffer overflow */
  return -1;
 else
 {
  memcpy(memfile->buffer+memfile->curpos,buf,size);
  memfile->curpos+=size;
  return size;
 }
}

/********************************************************/
/* Using bindrv 
   Decode the binary file and display its content       */
/********************************************************/
void show_solution(size_t (*doread)(void *,size_t,size_t,void*),void *rctx)
{
 s_bindrvctx bdrv;
 int *solarr;
 int size,i,n;
 char *str;

 bdrv=bindrv_newreader(doread,rctx);  /* Initialize binreader */

 i=size=0;
 solarr=NULL;
 while(bindrv_nexttoken(bdrv)>=0)
 {
  bindrv_getctrl(bdrv,&n);         /* 'label'  (marker) */
  bindrv_getstring(bdrv,&str);        /* Read a string */
  if(strcmp(str,"NumP")==0)
  {
   free(str);
   bindrv_getint(bdrv,&size);         /* Read an integer */
   printf("( %d prime numbers)\n", size);
   if(size>0)                         /* Prepare array to receive values */
    solarr=malloc(sizeof(int)*size);
   else
    break;
  } 
  else
  if(strcmp(str,"SPrime")==0)
  {
   free(str);
   bindrv_getctrl(bdrv,&n);       /* [  (start marker) */
   while(bindrv_nexttoken(bdrv)==BINDRV_TYP_INT)
   {                                  /* Read integers */
    bindrv_getint(bdrv,&(solarr[i++]));
   }
   bindrv_getctrl(bdrv,&n);        /* ]  (end marker) */
  }
  else
  {
   printf("Unexpected label: %s\n", str);
   free(str);
   exit(1);
  }
 }

 bindrv_delete(bdrv);              /* Release bin reader */

/* Print the set of prime numbers */
 printf("primes={");
 for(i=0;i<size;i++)
  printf(" %d",solarr[i]);
 printf(" }\n");

 free(solarr);                     /* Clean up */
}

/********************************************************/
int main(int argv,char *args[]) 
{
  XPRDcontext xprd;
  XPRDmosel moselInst;
  XPRDmodel modPrime, evsender;
  double evvalue;
  int evclass;
  s_memfile *f;
  XPRDfct_data fct_data; 
  XPRDfct_close fct_close;
  XPRDfct_skip fct_skip;

  xprd=XPRDinit();              /* Create an XPRD context */
                                /* Open connection to a remote node:
			           "" means the node running this program */
  moselInst=XPRDconnect(xprd, "", my_open, NULL, NULL, 0);
                                /* Compile the model file */
  XPRDcompmod(moselInst, "", "rmt:primeio.mos", "tmp:primeio.bim", "");
                                /* Load the bim file into the remote instance */
  modPrime=XPRDloadmod(moselInst, "tmp:primeio.bim"); 

                                /* Disable submodel output */
 XPRDsetdefstream(moselInst, modPrime, XPRD_F_WRITE, "null:");

                                /* Start execution */
  XPRDrunmod(modPrime, "LIMIT=50000,OUTPUTFILE=bin:rmt:resdata"); 
  XPRDwaitevent(xprd,0);        /* Wait for an event */
  XPRDgetevent(xprd, &evsender, &evclass, &evvalue);    /* Get the event */
  if (evclass != MODREADY) 
  {
    printf("Problem with submodel run");
    return 1;
  } 

  XPRDwaitevent(xprd,2);        /* Wait 2 seconds for an event */

  if (XPRDqueueempty(xprd)==1)  /* No event has been sent... */
  {
   printf("Model too slow: stopping it!\n");
   XPRDsendevent(modPrime, STOPMOD, 0);    /* ... stop the model, then wait */
   XPRDwaitevent(xprd,-1);
  }

  /* Open the output file, retrieve and display the solution data */
  f=my_open(NULL,"resdata",XPRD_F_READ,&fct_data,&fct_close,&fct_skip,NULL,0);
  show_solution(my_fread,f);
  my_close(f);

  XPRDunloadmod(modPrime);       /* Unload the model */
  XPRDdisconnect(moselInst);     /* Disconnect remote instance */
  XPRDfinish(xprd);              /* Terminate XPRD */

  return 0;
} 

runprimeiodistr2.java
/******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file runprimeiodistr2.java 
   ``````````````````````````
   Distributed computing: 
   Running a model on a remote Mosel instance
   and retrieving output data.
   
   - Data saved in memory on the local machine running XPRD -
   - Implementation of a file manager for data exchange in memory -
     (Submodel writing to "bin:rmt:")
  
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke & Y. Colombani, Jan 2013
*******************************************************/

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

public class runprimeiodistr2
{
 static byte solbuf[]=null;
 static final int STOPMOD = 2;    // Identifier for "Stop submodel" user event
 static final int MODREADY = 3;   // Identifier for "Submodel ready" user event

/**
  * An extension of 'ByteArrayOutputStream' for saving the array on closing.
  */
 static class myByteArrayOutputStream extends ByteArrayOutputStream
 {
  public void close()
  {
   solbuf=toByteArray();
  }
 }

/**
  * This file manager will directly access the file 'resdata'
  * to a local ArrayStream object.
  * Warning: concurrency not handled here!!!
  */
 static class FileManager implements XPRDFileManager
 {
  public OutputStream openForWriting(String fname,int mode) throws IOException
  {
   if(fname.equals("resdata"))
   {
    return new myByteArrayOutputStream();
   }
   else
    return null;    // Name not found: use default behaviour (open local file)
  }
  public InputStream openForReading(String fname,int mode) throws IOException
  {
   return null;     // Use default behaviour (open local file)
  }
 }


// **** Decode the binary stream and display its contents ****
 static void showSolution(InputStream inbuf) throws Exception
 {
  BinDrvReader bdrv=new BinDrvReader(inbuf);  // Initialize binreader
  String label;
  ArrayList<Integer> setP=new ArrayList<Integer>();

  while(bdrv.nextToken()>=0)
  {
   bdrv.getControl();          // 'label'  (marker)
   label=bdrv.getString();        // Read a string
   if(label.equals("NumP"))
   {                              // Read an integer
    System.out.println("(" + bdrv.getInt() + " prime numbers.)");
   }
   else
   if(label.equals("SPrime"))
   {
    bdrv.getControl();         // [  (start marker)
    while(bdrv.nextToken()==BinDrvReader.TYP_INT) // or ] at end of list
    {                             // Read integers
     setP.add(new Integer(bdrv.getInt()));
    }
    bdrv.getControl();         // ]  (end marker)
   }
   else
   {
    System.out.println("Unexpected label: "+label);
    System.exit(0);
   }
  }

 // Display the contents of the set 'SPrime'
  Iterator<Integer> iprime=setP.iterator();
  System.out.print("primes={");
  while(iprime.hasNext())
  {
   Integer p=iprime.next();
   System.out.print(" "+p);
  }
  System.out.println(" }");
 }

/*******************************************************/
 public static void main(String[] args) throws Exception
 {
  XPRD xprd=new XPRD();                // Initialize XPRD
  XPRDMosel moselInst;
  XPRDModel modPrime;
  XPRDEvent event;
  InputStream resdata;
  FileManager fmgr=new FileManager();  // Initialize the file manager

  moselInst=xprd.connect("",fmgr); // Open connection to remote nodes
                                   // "" means the node running this program
    
                                   // Compile the model file on remote instance
  moselInst.compile("", "rmt:primeio.mos", "tmp:primeio.bim");
  
	                           // Load the bim file into remote instance
  modPrime=moselInst.loadModel("tmp:primeio.bim"); 

                                   // Disable submodel output
  modPrime.setDefaultStream(modPrime.F_OUTPUT,"null:");

  modPrime.execParams = "LIMIT=50000,OUTPUTFILE=bin:rmt:resdata";
  modPrime.run();                     // Start execution and
  xprd.waitForEvent();                // ...wait for an event
  event=xprd.getNextEvent();          // Retrieve the event
  if (event.eventClass != MODREADY)   // Check the event class
  {
    System.out.println("Problem with submodel run");
    System.exit(1);
  } 

  xprd.waitForEvent(2);               // Let the submodel run for 2 seconds

  if (xprd.isQueueEmpty())            // No event has been sent...
  {
   System.out.println("Model too slow: stopping it!");
   modPrime.sendEvent(STOPMOD, 0);    // ... stop the model, then
   xprd.waitForEvent();               // wait for its termination
  }

  // Open the output file, retrieve and display the solution data
  resdata=new ByteArrayInputStream(solbuf);
  showSolution(resdata);
  resdata.close();

  moselInst.unloadModel(modPrime);    // Unload the submodel
  moselInst.disconnect();             // Terminate the connection
  
  solbuf=null;                        // Cleaning up
 }
} 


runprimeiodistr3.c
/*******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file runprimeiodistr3.c
   ```````````````````````
   Distributed computing: 
   Running a model on a remote Mosel instance
   and retrieving output data.
   
   - Data saved to a file on the local machine running XPRD -
     (Submodel writing to "bin:rmt:")
       
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke, Jan. 2013
*******************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xprd.h"
#include "bindrv.h"

#define STOPMOD 2            /* Identifier for "Stop submodel" user event */
#define MODREADY 3           /* Identifier for "Submodel ready" user event */


/********************************************************/
/* Using bindrv 
   Decode the binary file and display its content       */
/********************************************************/
void show_solution(size_t (*doread)(void *,size_t,size_t,void*),void *rctx)
{
 s_bindrvctx bdrv;
 int *solarr;
 int size,i,n;
 char *str;

 bdrv=bindrv_newreader(doread,rctx);  /* Initialize binreader */

 i=size=0;
 solarr=NULL;
 while(bindrv_nexttoken(bdrv)>=0)
 {
  bindrv_getctrl(bdrv,&n);         /* 'label'  (marker) */
  bindrv_getstring(bdrv,&str);        /* Read a string */
  if(strcmp(str,"NumP")==0)
  {
   free(str);
   bindrv_getint(bdrv,&size);         /* Read an integer */
   printf("( %d prime numbers)\n", size);
   if(size>0)                         /* Prepare array to receive values */
    solarr=malloc(sizeof(int)*size);
   else
    break;
  } 
  else
  if(strcmp(str,"SPrime")==0)
  {
   free(str);
   bindrv_getctrl(bdrv,&n);        /* [  (start marker) */
   while(bindrv_nexttoken(bdrv)==BINDRV_TYP_INT)
   {                                  /* Read integers */
    bindrv_getint(bdrv,&(solarr[i++]));
   }
   bindrv_getctrl(bdrv,&n);        /* ]  (end marker) */
  }
  else
  {
   printf("Unexpected label: %s\n", str);
   free(str);
   exit(1);
  }
 }

 bindrv_delete(bdrv);              /* Release bin reader */

/* Print the set of prime numbers */
 printf("primes={");
 for(i=0;i<size;i++)
  printf(" %d",solarr[i]);
 printf(" }\n");

 free(solarr);                     /* Clean up */
}

/********************************************************/
int main(int argv,char *args[]) 
{
  XPRDcontext xprd;
  XPRDmosel moselInst;
  XPRDmodel modPrime, evsender;
  double evvalue;
  int evclass;
  FILE *f;

  xprd=XPRDinit();              /* Create an XPRD context */
                                /* Open connection to a remote node:
			           "" means the node running this program */
  moselInst=XPRDconnect(xprd, "", NULL, NULL, NULL, 0);
                                /* Compile the model file */
  XPRDcompmod(moselInst, "", "rmt:primeio.mos", "tmp:primeio.bim", "");
                                /* Load the bim file into the remote instance */
  modPrime=XPRDloadmod(moselInst, "tmp:primeio.bim"); 

                                /* Disable submodel output */
 XPRDsetdefstream(moselInst, modPrime, XPRD_F_WRITE, "null:");

                                /* Start execution */
  XPRDrunmod(modPrime, "LIMIT=50000,OUTPUTFILE=bin:rmt:resdata"); 
  XPRDwaitevent(xprd,0);        /* Wait for an event */
  XPRDgetevent(xprd, &evsender, &evclass, &evvalue);    /* Get the event */
  if (evclass != MODREADY) 
  {
    printf("Problem with submodel run");
    return 1;
  } 

  XPRDwaitevent(xprd,2);        /* Wait 2 seconds for an event */

  if (XPRDqueueempty(xprd)==1)  /* No event has been sent... */
  {
   printf("Model too slow: stopping it!\n");
   XPRDsendevent(modPrime, STOPMOD, 0);    /* ... stop the model, then wait */
   XPRDwaitevent(xprd,-1);
  }

  /* Open the output file, retrieve and display the solution data */
  f=fopen("resdata","rb");      /* Open file for reading in binary format */
  show_solution(fread,f);
  fclose(f);

  XPRDunloadmod(modPrime);      /* Unload the model */
  XPRDdisconnect(moselInst);    /* Disconnect remote instance */
  XPRDfinish(xprd);             /* Terminate XPRD */

  remove("resdata");            /* Clean up temporary files */

  return 0;
} 

runprimeiodistr3.java
/******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file runprimeiodistr3.java 
   ``````````````````````````
   Distributed computing: 
   Running a model on a remote Mosel instance
   and retrieving output data.
   
   - Data saved to a file on the local machine running XPRD -
     (Submodel writing to "bin:rmt:")
  
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke & Y. Colombani, Jan 2013
*******************************************************/

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

public class runprimeiodistr3
{
 static final int STOPMOD = 2;    // Identifier for "Stop submodel" user event
 static final int MODREADY = 3;   // Identifier for "Submodel ready" user event

// **** Decode the binary stream and display its contents ****
 static void showSolution(InputStream inbuf) throws Exception
 {
  BinDrvReader bdrv=new BinDrvReader(inbuf);  // Initialize binreader
  String label;
  ArrayList<Integer> setP=new ArrayList<Integer>();

  while(bdrv.nextToken()>=0)
  {
   bdrv.getControl();          // 'label'  (marker)
   label=bdrv.getString();        // Read a string
   if(label.equals("NumP"))
   {                              // Read an integer
    System.out.println("(" + bdrv.getInt() + " prime numbers.)");
   }
   else
   if(label.equals("SPrime"))
   {
    bdrv.getControl();         // [  (start marker)
    while(bdrv.nextToken()==BinDrvReader.TYP_INT) // or ] at end of list
    {                             // Read integers
     setP.add(new Integer(bdrv.getInt()));
    }
    bdrv.getControl();         // ]  (end marker)
   }
   else
   {
    System.out.println("Unexpected label: "+label);
    System.exit(0);
   }
  }

 // Display the contents of the set 'SPrime'
  Iterator<Integer> iprime=setP.iterator();
  System.out.print("Prime numbers={");
  while(iprime.hasNext())
  {
   Integer p=iprime.next();
   System.out.print(" "+p);
  }
  System.out.println(" }");
 }

/*******************************************************/
 public static void main(String[] args) throws Exception
 {
  XPRD xprd=new XPRD();          // Initialize XPRD
  XPRDMosel moselInst;
  XPRDModel modPrime;
  XPRDEvent event;
  InputStream resdata;

  moselInst=xprd.connect("");    // Open connection to remote nodes
                                 // "" means the node running this program
    
                                 // Compile the model file on remote instance
  moselInst.compile("", "rmt:primeio.mos", "tmp:primeio.bim");
  
	                         // Load the bim file into remote instance
  modPrime=moselInst.loadModel("tmp:primeio.bim"); 

                                 // Disable submodel output
  modPrime.setDefaultStream(modPrime.F_OUTPUT, "null:");

  modPrime.execParams = "LIMIT=50000,OUTPUTFILE=bin:rmt:resdata";
  modPrime.run();                     // Start execution and
  xprd.waitForEvent();                // ...wait for an event
  event=xprd.getNextEvent();          // Retrieve the event
  if (event.eventClass != MODREADY)   // Check the event class 
  {
    System.out.println("Problem with submodel run");
    System.exit(1);
  } 

  xprd.waitForEvent(2);               // Let the submodel run for 2 seconds

  if (xprd.isQueueEmpty())            // No event has been sent...
  {
   System.out.println("Model too slow: stopping it!");
   modPrime.sendEvent(STOPMOD, 0);    // ... stop the model, then 
   xprd.waitForEvent();               // wait for its termination
  }

  // Open the output file, retrieve and display the solution data
  resdata=new FileInputStream("resdata");
  showSolution(resdata);
  resdata.close();

  moselInst.unloadModel(modPrime);    // Unload the submodel
  moselInst.disconnect();             // Terminate the connection

  new File("resdata").delete();       // Clean up temporary files
 }
} 


© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.