📄 cmnargs.c
字号:
/*@ MPIE_CheckEnv - Check the environment for parameters and default values Output Parameter:. pUniv - Process universe structure; some fields are set (see notes) Notes: @*/int MPIE_CheckEnv( ProcessUniverse *pUniv, int (*processEnv)( ProcessUniverse *, void * ), void *extraData ){ int rc = 0; const char *s; /* A negative universe size is none set */ pUniv->size = GetIntValue( "MPIEXEC_UNIVERSE_SIZE", -1 ); /* A negative timeout is infinite */ pUniv->timeout = GetIntValue( "MPIEXEC_TIMEOUT", -1 ); if (getenv( "MPIEXEC_DEBUG" )) { /* Any value of MPIEXEC_DEBUG turns on debugging */ MPIE_Debug = 1; PMISetDebug( 1 ); } /* Check for stdio buffering controls. Set the default to none as that preserves the behavior of the user's program. */ s = getenv( "MPIEXEC_STDOUTBUF" ); if (s) { rc = MPIE_StdioSetMode( stdout, s ); } else { MPIE_StdioSetMode( stdout, "none" ); } s = getenv( "MPIEXEC_STDERRBUF" ); if (s) { rc = MPIE_StdioSetMode( stderr, s ); } else { MPIE_StdioSetMode( stderr, "none" ); } if (processEnv) { rc = (*processEnv)( pUniv, extraData ); } return rc;}/*@ MPIE_ArgDescription - Return a pointer to a description of the arguments handled by MPIE_Args This includes the handling of the env arguments @*/const char *MPIE_ArgDescription( void ){ return "-usize <universesize> -maxtime <seconds> -exitinfo -l\\\n\ -n <numprocs> -soft <softness> -host <hostname> \\\n\ -wdir <working directory> -path <search path> \\\n\ -file <filename> -configfile <filename> \\\n\ -genvnone -genvlist <name1,name2,...> -genv name value\\\n\ -envnone -envlist <name1,name2,...> -env name value\\\n\ execname <args>\\\n\ [ : -n <numprocs> ... execname <args>]\n";}/*@ MPIE_PrintProcessUniverse - Debugging routine used to print out the results from MPIE_Args Input Parameters:+ fp - File for output- pUniv - Process Univers @*/void MPIE_PrintProcessUniverse( FILE *fp, ProcessUniverse *pUniv ){ ProcessWorld *pWorld; int nWorld = 0; pWorld = pUniv->worlds; while (pWorld) { fprintf(fp,"Apps for world %d\n", nWorld ); MPIE_PrintProcessWorld( fp, pWorld ); pWorld = pWorld->nextWorld; nWorld++; }}/*@ MPIE_PrintProcessWorld - Print a ProcessWorld structure Input Parameters:+ fp - File for output- pWorld - Process World @*/void MPIE_PrintProcessWorld( FILE *fp, ProcessWorld *pWorld ){ int j; ProcessApp *pApp; ProcessSoftSpec *sSpec; pApp = pWorld->apps; while (pApp) { fprintf( fp, "App %d:\n\ exename = %s\n\ hostname = %s\n\ arch = %s\n\ path = %s\n\ wdir = %s\n", pApp->myAppNum, pApp->exename ? pApp->exename : "<NULL>", pApp->hostname ? pApp->hostname : "<NULL>", pApp->arch ? pApp->arch : "<NULL>", pApp->path ? pApp->path : "<NULL>", pApp->wdir ? pApp->wdir : "<NULL>" ); fprintf(fp, " args (%d):\n", pApp->nArgs ); for (j=0; j<pApp->nArgs; j++) { fprintf(fp, " %s\n", pApp->args[j] ? pApp->args[j] : "<NULL>" ); } sSpec = &(pApp->soft); if (sSpec->nelm > 0) { fprintf(fp, " Soft spec with %d tuples\n", sSpec->nelm ); for (j=0; j<sSpec->nelm; j++) { fprintf(fp, " %d:%d:%d\n", sSpec->tuples[j][0], sSpec->tuples[j][1], sSpec->tuples[j][2]); } } else { fprintf(fp, " n = %d\n", pApp->nProcess ); fprintf(fp, " No soft spec\n"); } pApp = pApp->nextApp; } fflush( fp );}/* ------------------------------------------------------------------------- *//* Internal Routines *//* ------------------------------------------------------------------------- *//* Return the int-value of the given argument. If there is no argument, or it is not a valid int, exit with an error message */static int getInt( int argnum, int argc, char *argv[] ){ char *p; long i; if (argnum < argc) { p = argv[argnum]; i = strtol( argv[argnum], &p, 0 ); if (p == argv[argnum]) { MPIU_Error_printf( "Invalid parameter value %s to argument %s\n", argv[argnum], argv[argnum-1] ); mpiexec_usage( NULL ); /* Does not return */ } return (int)i; } else { MPIU_Error_printf( "Missing argument to %s\n", argv[argnum-1] ); mpiexec_usage( NULL ); /* Does not return */ } /* Keep compiler happy */ return 0;}/* FIXME: Move this routine else where; perhaps a pmutil.c? *//* * Try to get an integer value from the enviroment. Return the default * if the value is not available or invalid */static int GetIntValue( const char name[], int default_val ){ const char *env_val; int val = default_val; env_val = getenv( name ); if (env_val) {#ifdef HAVE_STRTOL char *invalid_char; /* Used to detect invalid input */ val = (int) strtol( env_val, &invalid_char, 0 ); if (*invalid_char != '\0') val = default_val;#else val = atoi( env_val );#endif } return val;}/* * Process a "soft" specification. Returns the maximum of the * number of requested processes, or -1 on error * Format is in pseudo BNF: * soft -> element[,element] * element -> number | range * range -> number:number[:number] */int MPIE_ParseSoftspec( const char *str, ProcessSoftSpec *sspec ){ const char *p = str, *p1, *p2; int s, e, incr; int nelm; int maxproc = 1; /* First, count the number of commas to preallocate the SoftSpec tuples array */ nelm = 1; p1 = p; while ( (p1 = strchr(p1,',')) != NULL ) { nelm ++; p1++; } sspec->nelm = nelm; sspec->tuples = (int (*)[3]) MPIU_Malloc( nelm * sizeof(int [3])); nelm = 0; while ( *p ) { p1 = strchr(p,','); if (!p1) { /* Use the rest of the string */ p1 = p + strlen(p); } /* Extract the element between p and p1-1 */ /* FIXME: handle sign, invalid input */ s = 0; e = 0; incr = 1; p2 = p; while (p2 < p1 && *p2 != ':') { s = 10 * s + (*p2 - '0'); p2++; } if (*p2 == ':') { /* Keep going (end) */ p2++; while (p2 < p1 && *p2 != ':') { e = 10 * e + (*p2 - '0'); p2++; } if (*p2 == ':') { /* Keep going (stride) */ p2++; incr = 0; while (p2 < p1 && *p2 != ':') { incr = 10 * incr + (*p2 - '0'); p2++; } } } else { e = s; } /* Save the results */ sspec->tuples[nelm][0] = s; sspec->tuples[nelm][1] = e; sspec->tuples[nelm][2] = incr; /* FIXME: handle negative increments, and e not s + k incr */ if (e > maxproc) maxproc = e; nelm++; p = p1; if (*p == ',') p++; } return maxproc;}/* * Read a file of mpiexec arguments, with a newline between groups. * Initialize the values in plist, and return the number of entries. * Return -1 on error. */#define MAXLINEBUF 2048#define MAXARGV 256static int LineToArgv( char *buf, char *(argv[]), int maxargc );static int ReadConfigFile( const char *filename, ProcessUniverse *pUniv){ FILE *fp = 0; int curplist = 0; char linebuf[MAXLINEBUF]; char *(argv[MAXARGV]); /* A kind of dummy argv */ int argc, newplist; fp = fopen( filename, "r" ); if (!fp) { MPIU_Error_printf( "Unable to open configfile %s\n", filename ); return -1; } /* Read until we get eof */ while (fgets( linebuf, MAXLINEBUF, fp )) { /* Convert the line into an argv array */ argc = LineToArgv( linebuf, argv, MAXARGV ); /* Process this argv. We can use the same routine as for the command line (this allows slightly more than the standard requires for configfile, but the extension (allowing :) is not prohibited by the standard */ newplist = MPIE_Args( argc, argv, pUniv, 0, 0 ); if (newplist > 0) curplist += newplist; else /* An error occurred */ break; } fclose( fp ); return curplist;}/* Convert a line into an array of pointers to the arguments, which are all null-terminated. The argument values copy the values in linebuf so that the line buffer may be reused.*/static int LineToArgv( char *linebuf, char *(argv[]), int maxargv ){ int argc = 0; char *p; p = linebuf; while (*p) { while (isspace(*p)) p++; if (argc >= maxargv) { MPIU_Error_printf( "Too many arguments in configfile line\n" ); return -1; } argv[argc] = p; /* Skip over the arg and insert a null at end */ while (*p && !isspace(*p)) p++; /* Convert the entry into a copy */ argv[argc] = MPIU_Strdup( argv[argc] ); argc++; *p++ = 0; } return 0;}/* Set the buffering mode for the specified FILE descriptor */int MPIE_StdioSetMode( FILE *fp, const char *mode ){ int rc = 0; /* Set the default to none (makes the buffering mimic the users program) */ setvbuf( fp, NULL, _IONBF, 0 ); if (strcmp( mode, "none" ) == 0 || strcmp( mode, "NONE" ) == 0) { DBG_PRINTF(("Setting buffer mode to unbuffered\n")); setvbuf( fp, NULL, _IONBF, 0 ); } else if (strcmp( mode, "line" ) == 0 || strcmp( mode, "LINE" ) == 0) { DBG_PRINTF(("Setting buffer mode to line buffered\n")); setvbuf( fp, NULL, _IOLBF, 0 ); } else if (strcmp( mode, "block" ) == 0 || strcmp( mode, "BLOCK" ) == 0) { DBG_PRINTF(("Setting buffer mode to block buffered\n")); setvbuf( fp, NULL, _IOFBF, 0 ); } else { rc = 1; } return rc;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -