Actual source code: launch.c

      1: /*
      2:   Usage: A = launch(programname,number processors);
      3:   Modified Sept 28, 2003 RFK: updated obsolete mx functions.
      4: */

      6: #include <stdio.h>
      7: #include <errno.h>
     10:  #include src/sys/src/viewer/impls/socket/socket.h
     11: #include "mex.h"
     12: #define PETSC_MEX_ERROR(a) {fprintf(stdout,"LAUNCH: %s \n",a); return ;}
     13: /*-----------------------------------------------------------------*/
     14: /*                                                                 */
     15: /*-----------------------------------------------------------------*/
     18: void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
     19: {
     20:   int  np,child;
     21:   char program[PETSC_MAX_PATH_LEN],executable[PETSC_MAX_PATH_LEN];

     23:   if (nlhs == 1) {
     24:     plhs[0]  = mxCreateDoubleMatrix(1,1,mxREAL);
     25:     *mxGetPr(plhs[0]) = 1;
     26:   }

     28:   /* check output parameters */
     29:   if (nlhs > 1) PETSC_MEX_ERROR("Open requires at most one output argument.");
     30:   if (!nrhs) PETSC_MEX_ERROR("Open requires at least one input argument.");
     31:   if (!mxIsChar(prhs[0])) PETSC_MEX_ERROR("First arg must be string.");

     33:   if (nrhs == 1) np = 1;
     34:   else           np = (int)*mxGetPr(prhs[1]);

     36:   /* attempt a fork */
     37:   child = fork();
     38:   if (child < 0) {
     39:     PETSC_MEX_ERROR("Unable to fork.");
     40:   } else if (!child) {  /* I am child, start up MPI program */
     41:     mxGetString(prhs[0],program,1000);
     42:     sprintf(executable,"mpirun -np %d %s",np,program);
     43:     printf("About to execute %s\n",executable);
     44:     system(executable);
     45:     printf("Completed subprocess\n");
     46:     fflush(stdout);
     47:     exit(0);
     48:   }
     49:

     51:
     52:   if (nlhs == 1) {
     53:     *mxGetPr(plhs[0]) = 0;
     54:   }
     55:   return;
     56: }

     58:
     59: