XPRD C
The program example runprimedistr.c below shows how to run the model prime.mos remotely using the XPRD C library (NB: this program corresponds to the mmjobs distributed computing example runprimedistr.mos from Section Distributed computing). At first sight, the reader might be reminded of the Mosel C libraries presented in Chapter C interface. However, there are two major additions besides the change of the prefixes from XPRM to XPRD:
- We need to connect to a remote machine and create a new Mosel instance (XPRDmosel) prior to working with any Mosel models. Remote machines are specified by their name or IP address, the empty string in the present example indicates that we want to use the local machine.
- The submodel is executed in an independent process and we therefore need to wait for its termination.
In summary, the standard execution sequence for Mosel models of compile load run is augmented to connect – compile load run wait in the context of distributed computing (this remark equally applies to submodels launched via mmjobs).
Instead of simply waiting for the submodel to terminate, the program below waits for 2 seconds and if no termination event message has been received from the Mosel model, it is stopped by the application. After termination of the submodel (either by finishing its calculations within less than 2 seconds or stopped by the master model) the application reports the full event information and also displays the termination status and the exit value of the Mosel model. Unloading a model explicitly as shown here is only really necessary in larger applications that continue after the termination of the submodel, so as to free the memory used by it.
#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 BIM 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 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; }
In this example, we assume that the model source is saved on the local machine running XPRD, and the BIM file is written back to this machine (indicated by the I/O driver prefix rmt: in the 'compile' and 'load' functions that are executed on the remote instance). Alternatively, we might choose to save the BIM file on the remote machine, e.g. in memory (shmem:primebim) or in Mosel's temporary directory (tmp:prime.bim).
© 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.