#include #include #include #include #include #include #include #include static const int max_command_len=1048576; /* Limit command length to avoid buffer overflow attacks */ static int mpi_inited=0; void die(const char *format,...) { va_list ap; va_start(ap,format); vfprintf(stderr,format,ap); va_end(ap); if(mpi_inited) MPI_Abort(MPI_COMM_WORLD,2); exit(2); abort(); } void mpicall(int ret,const char *name) { char error[MPI_MAX_ERROR_STRING+1]=""; int len=-1,err2; if(ret!=MPI_SUCCESS) { if((err2=MPI_Error_string(ret,error,&len))==MPI_SUCCESS) { error[len]='\0'; die("MPI error %d when %s: %s\n",ret,name,error); } else { die("MPI error %d when %s, and MPI_Error_string could not process that error number (it said %d) so I don't even know what went wrong!\n", ret,name,err2); } } } int is_spmd(const int argc,const int rank) { int spmd,mpmd; char *pgmmodel; if(rank==0) { spmd=(argc>1); mpmd=(!spmd); pgmmodel=getenv("MP_PGMMODEL"); if(pgmmodel) { if(!strcasecmp(pgmmodel,"MPMD")) { spmd=0 ; mpmd=1; } else if(!strcasecmp(pgmmodel,"SPMD")) { spmd=1 ; mpmd=0; } else { die("Invalid value \"%s\" for $MP_PGMMMODEL\n",pgmmodel); } } } mpicall(MPI_Bcast(&spmd,1,MPI_INTEGER,0,MPI_COMM_WORLD),"calling MPI_Bcast"); return spmd; } char *append_args(const int argc,const char **argv) { char *buf; int iarg; size_t len,len1,buflen; len=0; for(iarg=1;iarg1) die("ERROR. Do not specify arguments in MPMD mode.\n"); rc=mpmd_run(argc,argv,rank); } /* Determine the maximum return code */ mycode=rc; maxcode=255; mpicall(MPI_Allreduce(&mycode,&maxcode,1,MPI_INTEGER,MPI_MAX,MPI_COMM_WORLD),"using an mpi_allreduce to get the maximum return code"); mpicall(MPI_Finalize(),"finalizing MPI library"); return maxcode; }