deps/kb_meme/meme-5.0.1/src/exec_parallel.c
/*
   Check arguments to see if -p <np> was given.
   If so, use execvp() to run program as input to MPI.
   The particular MPI command is set in the macro MPI_CMD and
   must end just before the number of processors is to be
   specified (<np>).
*/
Check arguments to see if -p  was given.
If so, use execvp() to run program as input to MPI.
The particular MPI command is set in the macro MPI_CMD and
must end just before the number of processors is to be
specified ().
Check arguments to see if - p was given . If so use execvp () to run program as input to MPI . The particular MPI command is set in the macro MPI_CMD and must end just before the number of processors is to be specified () .
int exec_parallel(
  int argc,
  char *argv[]  
) {
  int i;
  char *resolved_path = get_exe_path(argv[0]);
  char **args = (char **)calloc(argc, sizeof(char *));  
  char *np = NULL;
  int iargs = 0;
  for (i=0; i<argc; i++) {
    if (strcmp(argv[i], "-p") == 0) {
      if (++i == argc) die("Illegal use of `-p <np>': missing number of processors\n");
      np = argv[i];                     
    } else {
      args[iargs++] = argv[i];          
    }
  }
  if (np != NULL) {
    char *mpi_cmd = (char*) mm_malloc(strlen(xstr(MPI_CMD))+strlen(np)+2);
    sprintf(mpi_cmd, "%s %s", xstr(MPI_CMD), np);
    if (mpi_cmd != NULL) {
      char **mpi_args = (char **)calloc(strlen(mpi_cmd), sizeof(char *));
      char *token = strtok(mpi_cmd, "\t ");     
      int n = 0;                                
      while (token != NULL) {
        mpi_args[n++] = token; 
        token = strtok(NULL, "\t ");
      }
      int new_iargs = iargs+n;
      args = (char **) realloc(args, (new_iargs+1) * sizeof(char *));
      for (i=iargs-1; i >= 0; i--) args[n+i] = args[i]; 
      args[n] = resolved_path;                          
      for (i=0; i < n; i++) args[i] = mpi_args[i];      
      args[iargs+n] = NULL;                           
    }
    if (mpi_cmd != NULL) {
      execvp(args[0], args);
      return(0);
    }
    free(mpi_cmd);
  }
  free(args);
  free(resolved_path);
  return(0);
}
int exec_parallel ( int argc , char * argv [ ] ) { int i ; char * resolved_path = get_exe_path ( argv [ 0 ] ) ; char * * args = ( char * * ) calloc ( argc , sizeof ( char * ) ) ; char * np = NULL ; int iargs = 0 ; for ( i = 0 ; i < argc ; i ++ ) { if ( strcmp ( argv [ i ] , " " ) == 0 ) { if ( ++ i == argc ) die ( " \n " ) ; np = argv [ i ] ; } else { args [ iargs ++ ] = argv [ i ] ; } } if ( np != NULL ) { char * mpi_cmd = ( char * ) mm_malloc ( strlen ( xstr ( MPI_CMD ) ) + strlen ( np ) + 2 ) ; sprintf ( mpi_cmd , " " , xstr ( MPI_CMD ) , np ) ; if ( mpi_cmd != NULL ) { char * * mpi_args = ( char * * ) calloc ( strlen ( mpi_cmd ) , sizeof ( char * ) ) ; char * token = strtok ( mpi_cmd , " \t " ) ; int n = 0 ; while ( token != NULL ) { mpi_args [ n ++ ] = token ; token = strtok ( NULL , " \t " ) ; } int new_iargs = iargs + n ; args = ( char * * ) realloc ( args , ( new_iargs + 1 ) * sizeof ( char * ) ) ; for ( i = iargs - 1 ; i >= 0 ; i -- ) args [ n + i ] = args [ i ] ; args [ n ] = resolved_path ; for ( i = 0 ; i < n ; i ++ ) args [ i ] = mpi_args [ i ] ; args [ iargs + n ] = NULL ; } if ( mpi_cmd != NULL ) { execvp ( args [ 0 ] , args ) ; return ( 0 ) ; } free ( mpi_cmd ) ; } free ( args ) ; free ( resolved_path ) ; return ( 0 ) ; }
Check arguments to see if -p <np> was given.
Check arguments to see if - p <np > was given .
// Get the real path to the current executable. // don't use printf here! //fputs("argv: \n",stderr); for(i=0;i<argc;i++){fputs(argv[i], stderr); fputs("\n", stderr);} //fputs("Program called as: ", stderr); fputs(argv[0], stderr); fputs("\n", stderr);  //fputs("Real path is: ", stderr); fputs(resolved_path, stderr); fputs("\n", stderr);  // Scan args for -p so we can launch using MPI. // args to MPI run command // Scan for -p and remove from args, and save np.  // If more than one -p given, last one counts. // set number of processes // add arguments (other than -p <np>) to MPI run args //fputs("np is: ", stderr); fputs(np, stderr); fputs("\n", stderr);  // Convert MPI_CMD and np to a string.  //fputs("MPI_CMD is: ", stderr); fputs(mpi_cmd, stderr); fputs("\n", stderr); // Split MPI_CMD and np string on whitespace. // split on whitespace // number tokens in mpi_cmd // Increase the size of the args array and shift arguments down to make room for mpi_cmd // shift n places down // put real path to parallel program here // put in mpi_cmd arguments // required by execvp //for(i=0;i<new_iargs;i++){fputs(args[i], stderr); fputs("\n", stderr);} // Run via MPI. // cleanup