Initializing help system before first use

Working with multiple models: submodels, coordination, communication, and parallelization


Type: Programming
Rating: 3 (intermediate)
Description: The Mosel module mmjobs enables the user to work with several models concurrently. We show here a series of examples of basic tasks that typically need to be performed when working with several models in Mosel:
Parallel computing:
  • Running a submodel from another Mosel model: runtestsub.mos (master model executing testsub.mos)
  • Retrieving termination status from submodels (means of coordination of different models): runsubevnt.mos (master model executing testsub.mos)
  • Retrieving user event sent by the submodel: runsubevnt2.mos (master model executing testsubev.mos)
  • Stopping a submodel: runsubwait.mos (master model executing testsub.mos)
  • Compiling to memory: runsubmem.mos (master model executing testsub.mos)
  • Setting runtime parameters: runrtparam.mos (master model executing rtparams.mos)
  • Sequential execution of submodels: runrtparseq.mos (master model executing rtparams.mos)
  • Parallel execution of submodels: runrtparprl.mos (master model executing rtparams.mos)
  • Job queue for parallel execution of submodels: runrtparqueue.mos (master model executing rtparams.mos)
  • Using the shmem (shared memory) I/O driver for data exchange (bin format): runsubshm.mos (master model executing testsubshm.mos)
  • Using the shmem (shared memory) I/O driver for data exchange (raw format): runsubshmr.mos (master model executing testsubshmr.mos)
  • Using the mempipe (memory pipe) I/O driver for data exchange: runsubpip.mos (master model executing testsubpip.mos)
Distributed computing:
  • Check for available remote Mosel servers: findservers.mos
  • Run a single model on a remote machine: runrtdistr.mos (master model executing rtparams.mos)
  • Run a single model on a remote machine with configuration options: runrtdistrconf.mos (master model executing rtparams.mos)
  • Running parallel submodels in a distributed architecture: runrtpardistr.mos (master model executing rtparams3.mos)
  • Queuing submodels for parallel execution in a distributed architecture with one or several models per node: runrtparqueued.mos (master model executing rtparams3.mos)
  • 3-level tree of (parallel) submodels: runrtpartree.mos (master model executing rtparams2.mos)
  • Running a submodel that detaches itself from the master: runrtdetach.mos (master model executing rtparams4.mos)
  • Using the shmem (shared memory) I/O driver for data exchange (bin format): runsubshmdistr.mos (master model executing testsubshm.mos)
File(s): runtestsub.mos, testsub.mos, runsubevnt.mos, runsubwait.mos, runsubmem.mos, runrtparam.mos, runrtparam2.mos, rtparams.mos, runrtparseq.mos, runrtparprl.mos, runrtparqueue.mos, runsubshm.mos, testsubshm.mos, runsubshmr.mos, testsubshmr.mos, runsubpip.mos, testsubpip.mos


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

   file runtestsub.mos
   ```````````````````
   Running a model from another Mosel model,
   waiting for the submodel termination.
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006
*******************************************************!)

model "Run model testsub"
 uses "mmjobs"

 declarations
  modSub: Model
 end-declarations
                                   ! Compile the model file
 if compile("testsub.mos")<>0 then exit(1); end-if
 load(modSub, "testsub.bim")       ! Load the bim file

! Uncomment one of the following lines to redirect the submodel output
! setdefstream(modSub, F_OUTPUT, "testout.txt") ! Output to file
! setdefstream(modSub, F_OUTPUT, "tee:testout.txt&")  
                                                ! Output to file and on screen
! setdefstream(modSub, F_OUTPUT, "null:")       ! Disable all output

 run(modSub)                       ! Start model execution
 wait                              ! Wait for model termination
 dropnextevent                     ! Ignore termination event message

end-model 

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

   file testsub.mos
   ````````````````
   Simple model printing some output.
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006
*******************************************************!)

model "Test submodel"

! Uncomment one of the following lines to redirect output to a file
! fopen("testout.txt", F_OUTPUT+F_APPEND) ! Output to file (in append mode)
! fopen("tee:testout.txt&", F_OUTPUT)     ! Output to file and on screen
! fopen("null:", F_OUTPUT)                ! Disable all output
 
 forall(i in 10..20) write(i^2, " ")
 writeln

! Uncomment if output is redirected
! fclose(F_OUTPUT)

end-model 

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

   file runsubevnt.mos
   ```````````````````
   Running a model from another Mosel model,
   getting the submodel termination status.
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006
*******************************************************!)

model "Run model testsub"
 uses "mmjobs"

 declarations
  modSub: Model
  ev: Event
 end-declarations
                                   ! Compile the model file
 if compile("testsub.mos")<>0 then exit(1); end-if
 load(modSub, "testsub.bim")       ! Load the bim file  
 run(modSub)                       ! Start model execution
 wait(1)                           ! Wait 1 second for an event

 if isqueueempty then              ! No event has been sent: model still runs
  stop(modSub)                     ! Stop the model
  wait                             ! Wait for model termination
 end-if
                                   
 ev:=getnextevent                  ! An event is available: model finished
 writeln("Event class: ", getclass(ev))
 writeln("Event value: ", getvalue(ev))
 writeln("Exit code  : ", getexitcode(modSub))

end-model 

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

   file runsubwait.mos
   ```````````````````
   Running a model from another Mosel model,
   waiting for the submodel termination.
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006
*******************************************************!)

model "Run model testsub"
 uses "mmjobs"

 declarations
  modSub: Model
 end-declarations
                                   ! Compile the model file
 if compile("testsub.mos")<>0 then exit(1); end-if
 load(modSub, "testsub.bim")       ! Load the bim file  
 run(modSub)                       ! Start model execution
 wait(1)                           ! Wait 1 second for an event

 if isqueueempty then              ! No event has been sent: model still runs
  writeln("Stopping the submodel")
  stop(modSub)                     ! Stop the model
  wait                             ! Wait for model termination
 end-if
 dropnextevent                     ! Ignore termination event message

end-model 

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

   file runsubmem.mos
   ``````````````````
   Running a model from another Mosel model,
   compilation to memory.
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006
*******************************************************!)

model "Run model testsub"
 uses "mmjobs", "mmsystem"

 declarations
  modSub: Model
 end-declarations
                                   ! Compile the model file
 if compile("", "testsub.mos", "shmem:testsubbim")<>0 then 
  exit(1)
 end-if
 load(modSub, "shmem:testsubbim")  ! Load the bim file from memory
 fdelete("shmem:testsubbim")       ! ... and release the memory block
 run(modSub)                       ! Start model execution
 wait                              ! Wait for model termination
 dropnextevent                     ! Ignore termination event message

end-model 

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

   file runrtparam.mos
   ```````````````````
   Running a model from another Mosel model,
   passing runtime parameters to the submodel.
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006
*******************************************************!)

model "Run model rtparams"
 uses "mmjobs"

 declarations
  modPar: Model
 end-declarations
                                   ! Compile the model file
 if compile("rtparams.mos")<>0 then exit(1); end-if
 load(modPar, "rtparams.bim")      ! Load the bim file
                                   ! Start model execution
 run(modPar, "PARAM1=" + 2 + ",PARAM2=" + 3.4 + 
             ",PARAM3='a string'" + ",PARAM4=" + true)
 wait                              ! Wait for model termination
 dropnextevent                     ! Ignore termination event message

end-model 

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

   file runrtparam2.mos
   ````````````````````
   Running a model from another Mosel model,
   passing runtime parameters to the submodel.
   - Using 'setmodpar' -
       
   (c) 2014 Fair Isaac Corporation
       author: S. Heipcke, Sep. 2014
*******************************************************!)

model "Run model rtparams"
 uses "mmjobs", "mmsystem"

 declarations
  modPar: Model
  params: text
 end-declarations
                                   ! Compile the model file
 if compile("rtparams.mos")<>0 then exit(1); end-if
 load(modPar, "rtparams.bim")      ! Load the bim file
 setmodpar(params, "PARAM1", 2)    ! Set values for model parameters
 setmodpar(params, "PARAM2", 3.4)
 setmodpar(params, "PARAM3", 'a string')
 setmodpar(params, "PARAM4", true)
 run(modPar, params)               ! Start model execution
 params:= ""                       ! Delete the parameters definition
 wait                              ! Wait for model termination
 dropnextevent                     ! Ignore termination event message

end-model 

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"
 parameters
  PARAM1 = 0
  PARAM2 = 0.5
  PARAM3 = ''
  PARAM4 = false
 end-parameters

 writeln(PARAM1, "  ", PARAM2, "  ", PARAM3, "  ", PARAM4)

end-model 

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

   file runrtparseq.mos
   ````````````````````
   Running several instances of a model from another
   Mosel model.
   - Sequential submodels -
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006, rev. May 2010
*******************************************************!)

model "Run model rtparams in sequence"
 uses "mmjobs"

 declarations
  A = 1..10
  modPar: Model
 end-declarations
                                   ! Compile the model file
 if compile("rtparams.mos")<>0 then exit(1); end-if
 load(modPar, "rtparams.bim")      ! Load the bim file

 forall(i in A) do                 ! Start model execution
  run(modPar, "PARAM1=" + i + ",PARAM2=" + 0.1*i +
              ",PARAM3='string " + i + "'" + ",PARAM4=" + isodd(i))
  wait                             ! Wait for model termination
  dropnextevent                    ! Ignore termination event message
 end-do

end-model 

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

   file runrtparprl.mos
   ````````````````````
   Running several instances of a model from another
   Mosel model.
   - Parallel submodels -

   *** ATTENTION: This model will return an error if no  ***
   *** sufficient number of Xpress licences is available ***
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006, rev. May 2010
*******************************************************!)

model "Run model rtparams in parallel"
 uses "mmjobs"

 declarations
  A = 1..10
  modPar: array(A) of Model
 end-declarations
                                   ! Compile the model file
 if compile("rtparams.mos")<>0 then exit(1); end-if

 forall(i in A) do
  load(modPar(i), "rtparams.bim")  ! Load the bim file
                                   ! Start model execution
  run(modPar(i), "PARAM1=" + i + ",PARAM2=" + 0.1*i +
                 ",PARAM3='string " + i + "'" + ",PARAM4=" + isodd(i))
 end-do
 
 forall(i in A) do
  wait                             ! Wait for model termination
  dropnextevent                    ! Ignore termination event message
 end-do

end-model 

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

   file runrtparqueue.mos
   ``````````````````````
   Running several instances of a model from another
   Mosel model.
   - Queuing submodels for parallel execution 
     in a limited number of processes -

   *** ATTENTION: This model will return an error if ***
   *** no more than one Xpress licence is available. ***
   *** With a single license, use setting NUMPAR=1   ***
       
   (c) 2010 Fair Isaac Corporation
       author: S. Heipcke, Apr. 2010, rev. Jun. 2015
*******************************************************!)

model "Run model rtparams with job queue"
 uses "mmjobs"

 parameters
   J=10                             ! Number of jobs to run
   NUMPAR=2                         ! Number of parallel model executions
 end-parameters                     ! (preferrably <= no. of processors)
  
 forward procedure start_next_job(submod: Model)
 
 declarations
   RM = 1..NUMPAR                   ! Model indices
   JOBS = 1..J                      ! Job (instance) indices
   modPar: array(RM) of Model       ! Models
   jobid: array(set of integer) of integer  ! Job index for model UIDs
   JobList: list of integer         ! List of jobs
   JobsRun: set of integer          ! Set of finished jobs
   JobSize: integer                 ! Number of jobs to be executed
   Msg: Event                       ! Messages sent by models
 end-declarations
                                    ! Compile the model file
 if compile("rtparams.mos")<>0 then exit(1); end-if

 forall(m in RM) do
   load(modPar(m), "rtparams.bim")  ! Load the bim file
   modPar(m).uid:= m                ! Store the model ID
 end-do 

 JobList:= sum(i in JOBS) [i]       ! Define the list of jobs (instances)
 JobSize:=JobList.size              ! Store the number of jobs
 JobsRun:={}                        ! Set of terminated jobs is empty

!**** Start initial lot of model runs ****
 forall(m in RM) 
   if JobList<>[] then
     start_next_job(modPar(m))
   end-if

!**** Run all remaining jobs ****
 while (JobsRun.size<JobSize) do
   wait                               ! Wait for model termination
 ! Start next job
   Msg:= getnextevent
   if Msg.class=EVENT_END then        ! We are only interested in "end" events
     m:= Msg.fromuid                  ! Retrieve the model ID
     JobsRun+={jobid(m)}              ! Keep track of job termination
     writeln("End of job ", jobid(m), " (model ", m, ")")
     if JobList<>[] then              ! Start a new run if queue not empty
      start_next_job(modPar(m))
     end-if
   end-if
 end-do 

!*************************************************************************
 procedure start_next_job(submod: Model)
   i:=getfirst(JobList)               ! Retrieve first job in the list
   cuthead(JobList,1)                 ! Remove first entry from job list
   jobid(submod.uid):= i              ! Save job index for this model
   writeln("Start job ", i, " (model ", submod.uid, ")")
   run(submod, "PARAM1=" + i + ",PARAM2=" + 0.1*i +
               ",PARAM3='string " + i + "'" + ",PARAM4=" + isodd(i))
 end-procedure

end-model 

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

   file runsubshm.mos
   ``````````````````
   Running a model from another Mosel model.
   Communication of data from/to master model
   using 'shmem' IO driver with 'bin'.
       
   (c) 2011 Fair Isaac Corporation
       author: S. Heipcke, April 2011
*******************************************************!)

model "Run model testsubshm"
 uses "mmjobs"

 declarations
  modSub: Model
  A = 30..40 
  B: array(A) of real
 end-declarations
                                   ! Compile the model file
 if compile("testsubshm.mos")<>0 then exit(1); end-if
 load(modSub, "testsubshm.bim")    ! Load the bim file

 initializations to "bin:shmem:indata"
  A
 end-initializations

 run(modSub)                       ! Start model execution
 wait                              ! Wait for model termination
 dropnextevent                     ! Ignore termination event message

 initializations from "bin:shmem:resdata"
  B
 end-initializations

 writeln(B)
 
end-model 

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

   file testsubshm.mos
   ```````````````````
   Simple model printing some output.
   Communication of data from/to master model
   using 'shmem' IO driver with 'bin'.
       
   (c) 2011 Fair Isaac Corporation
       author: S. Heipcke, April 2011
*******************************************************!)

model "Test submodel"
 declarations
  A: range 
  B: array(A) of real
 end-declarations

 initializations from "bin:shmem:indata"
  A
 end-initializations
 
 forall(i in A) B(i):= i^2

 initializations to "bin:shmem:resdata"
  B
 end-initializations

end-model 

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

   file runsubshmr.mos
   ```````````````````
   Running a model from another Mosel model.
   Communication of data from/to master model
   using 'shmem' IO driver with 'raw'.
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006
*******************************************************!)

model "Run model testsubshmr"
 uses "mmjobs"

 declarations
  modSub: Model
  A = 30..40 
  B: array(A) of real
 end-declarations
                                   ! Compile the model file
 if compile("testsubshmr.mos")<>0 then exit(1); end-if
 load(modSub, "testsubshmr.bim")   ! Load the bim file

 initializations to "raw:"
  A as "shmem:A"
 end-initializations

 run(modSub)                       ! Start model execution
 wait                              ! Wait for model termination
 dropnextevent                     ! Ignore termination event message

 initializations from "raw:"
  B as "shmem:B"
 end-initializations

 writeln(B)
 
end-model 

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

   file testsubshmr.mos
   ````````````````````
   Simple model printing some output.
   Communication of data from/to master model
   using 'shmem' IO driver with 'raw'.
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006
*******************************************************!)

model "Test submodel"
 declarations
  A: range 
  B: array(A) of real
 end-declarations

 initializations from "raw:"
  A as "shmem:A"
 end-initializations
 
 forall(i in A) B(i):= i^2

 initializations to "raw:"
  B as "shmem:B"
 end-initializations

end-model 

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

   file runsubpip.mos
   ``````````````````
   Running a model from another Mosel model.
   Communication of data from/to master model
   using 'mempipe' IO driver.
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006
*******************************************************!)

model "Run model testsubpip"
 uses "mmjobs"

 declarations
  modSub: Model
  A = 30..40 
  B: array(A) of real
 end-declarations
                                   ! Compile the model file
 if compile("testsubpip.mos")<>0 then exit(1); end-if
 load(modSub, "testsubpip.bim")    ! Load the bim file

 run(modSub)                       ! Start model execution

 initializations to "mempipe:indata"
  A
 end-initializations

 initializations from "mempipe:resdata"
  B
 end-initializations

 wait                              ! Wait for model termination
 dropnextevent                     ! Ignore termination event message

 writeln(B)
 
end-model 

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

   file testsubpip.mos
   ```````````````````
   Simple model printing some output.
   Communication of data from/to master model
   using 'mempipe' IO driver.
       
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, May 2006
*******************************************************!)

model "Test submodel"
 declarations
  A: range 
  B: array(A) of real
 end-declarations

 initializations from "mempipe:indata"
  A
 end-initializations

 forall(i in A) B(i):= i^2

 initializations to "mempipe:resdata"
  B
 end-initializations

end-model