[mpich-discuss] segfault when calling MPI_Init

Martin Siegert siegert at sfu.ca
Thu Apr 3 22:52:36 CDT 2008


Hi,

I am trying to compile GAMESS (Ameslab) using the MPI interface
and mpich2-1.0.6p1. I get it to compile just fine, but I get
a segfault (signal 11) when it calls MPI_Init.

The main program is written in Fortran, but in order to process
command line arguments that then can be passed to MPI_Init a
C routine is used.
Here are the relevant pieces from the code:

ddi_fortran.c:

   void F77_Init() {
      int i,j,lenmax=256,argc=iargc_();
      char **argv = NULL;
      char arg[256];

      STD_DEBUG((stdout," DDI: Entering F77 DDI_Init.\n"))

   /* -------------------------- *\
      Get command line arguments
   \* -------------------------- */
      if(argc) {
         argc++;
         argv = (char **)Malloc(argc*sizeof(char*));
         for(i=0; i<argc; i++) {
           for(j=0; j<256; j++) arg[j]=' ';

         # if defined CRAY
             getarg_(&i,arg,&lenmax);
         # else
             getarg_(&i,arg,lenmax);
         # endif

           for(j=0; j<256 && arg[j] != ' '; j++);
           arg[j] = 0;
           argv[i] = (char *) strdup(arg);
         }
      }

      MAX_DEBUG((stdout," DDI: Calling DDI_Init.\n"))

   /* -------------- *\
      Initialize DDI
   \* -------------- */
      DDI_Init(argc,argv);

   }

ddi_init.c:

   void DDI_Init(int argc,char **argv) {
...
    # if defined DDI_MPI
      Init_mpi(argc,argv);
    # endif
...

   static void Init_mpi(int targc,char *targv[]) {
...
      int argc = targc;
      char **argv = targv;
...
   /* -------------- *\
      Initialize MPI
   \* -------------- */
      if(MPI_Init(&argc,&argv) != MPI_SUCCESS) {
         fprintf(stdout," DDI: MPI_Init failed.\n");
         fflush(stdout); exit(911);
      }

Thus the fortran program calls F77_Init which in turn uses getarg_i
to get the command line arguments and then basically calls
MPI_Init(&argc,&argv).
The segmentation fault happens when MPI_Init is called.

I vaguely remember seeing a discussion (on this list?) about hidden
arguments in MPI_Init (when called from Fortran?) but I failed to
find anything in the mail archive of the list. Anyway, I played
around somewhat with the code (this is pure guess work on my part)
and changed it to:

      if(argc) {
         argc++;
         argv = (char **)Malloc((argc+1)*sizeof(char*));
         for(i=0; i<argc; i++) {
           for(j=0; j<256; j++) arg[j]=' ';

         # if defined CRAY
             getarg_(&i,arg,&lenmax);
         # else
             getarg_(&i,arg,lenmax);
         # endif

           for(j=0; j<256 && arg[j] != ' '; j++);
           arg[j] = 0;
           argv[i] = (char *) strdup(arg);
         }
      }
      arg[0] = 0;
      argv[argc] = (char *) strdup(arg);

i.e., added an additional array element to argv which I set to '\0'.
After that the segmentation fault disappears.

Can somebody explain to me whether this is appropriate or am I totally
off base here? Any hints about why I am getting the segmentation fault
in the first place?

Cheers,
Martin

-- 
Martin Siegert
Head, Research Computing
WestGrid Site Lead
Client and Research Services               phone: 778 782-4691
Simon Fraser University                    fax:   778 782-4242
Burnaby, British Columbia                  email: siegert at sfu.ca
Canada  V5A 1S6




More information about the mpich-discuss mailing list