gettimeofday崩溃了MPI?

时间:2013-02-08 22:39:29

标签: c linux mpi gettimeofday

我有一个mpi程序,我正在尝试测量它的运行时间。所以我添加了2次调用gettimeofday,这就是一切都停止工作的地方。出于某种原因,如果我在那里第二次调用gettimeofday,它只会崩溃。这是我得到的信息:

MPI应用程序等级0在具有信号11的MPI_Finalize()之前被杀死 srun:错误:n32:task0:退出退出代码245

这是代码

   struct timeval starttime;
   struct timeval endtime;

   gettimeofday(&starttime, NULL);

   int numDarts = 1000000000;
   int numWorkers = 2;

   char* args[1];
   if(argc >= 2)
   {
      numWorkers = atoi(argv[1]);
   }
   if(argc >= 3)
        numDarts = atoi(argv[2]);

   MPI_Init(&argc, &argv);
   MPI_Comm_size(MPI_COMM_WORLD, &world_size);

   printf("world size = %i\n", world_size);
   if (world_size != 1)
        printf("Top heavy with management\n");

   int numDartsWorker = numDarts/numWorkers;
   int numDartsMaster = numDarts/numWorkers + (numDarts % numWorkers); //the master computes the leftover
   args[0] = malloc(256 * sizeof(char));
   sprintf(args[0], "%i", numDartsWorker);
  // printf("argument passing to workers: %s\n", args[0]);
   /*
    * Now spawn the workers. Note that there is a run-time determination
    * of what type of worker to spawn, and presumably this calculation must
    * be done at run time and cannot be calculated before starting
    * the program. If everything is known when the application is
    * first started, it is generally better to start them all at once
    * in a single MPI_COMM_WORLD.
    */
  // printf("About to call MPI_Comm_spawn with %i workers...\n", numWorkers);
   int resultLen = 0;

  //the master counts as a worker, hence the -1
   MPI_Comm_spawn("piworker", args, numWorkers-1, MPI_INFO_NULL, 0, MPI_COMM_SELF,
                   &everyone, MPI_ERRCODES_IGNORE);

   double pisum = 0;
   double myresult = dboard(numDartsMaster);
   printf("parent result is %.9f\n", myresult);

   int rc = MPI_Reduce(&myresult, &pisum, 1, MPI_DOUBLE, MPI_SUM, MPI_ROOT, everyone);

   if (rc != MPI_SUCCESS)
        printf("failure on mpi_reduce\n");

   free(args[0]);
   /*
    * Parallel code here. The communicator "everyone" can be used
    * to communicate with the spawned processes, which have ranks 0,..
    * MPI_UNIVERSE_SIZE-1 in the remote group of the intercommunicator
    * "everyone".
    */
   //receive the results
   int i=1;
   MPI_Status status;

   double avgpi = pisum;
   avgpi += myresult; //include master's average in the result.
   avgpi /= numWorkers;

   printf("startTime = %d secs, %d microsecs\n", starttime.tv_sec);


//   gettimeofday(&endtime, NULL);

//   double totalTime = ((double)endtime.tv_sec + (double)endtime.tv_usec/1000000.0f) -
//                    ((double)starttime.tv_usec + (double)starttime.tv_usec/1000000.0f);

 //  printf("Total time: %.8f\n", totalTime);
   printf("With %i workers, %i darts, estimated value of pi is: %.9f\n", numWorkers, numDarts, avgpi);

       MPI_Finalize();


   return 0;
}

我在第二次调用gettimeofday之前输入了一个printf调用。如果第二个呼叫被注释掉,它只打印任何东西,否则它会崩溃。我在这个例子中注释了gettimeofday,但那是导致mpi崩溃的调用。如果我取消注释它,它会再次因我提到的错误消息而崩溃。

我想知道是否有人有任何见解,为什么gettimeofday会这样做。

2 个答案:

答案 0 :(得分:1)

在你的代码中,有一个线程产生,如

//the master counts as a worker, hence the -1
MPI_Comm_spawn("piworker", args, numWorkers-1, MPI_INFO_NULL, 0, MPI_COMM_SELF,
               &everyone, MPI_ERRCODES_IGNORE);

随后args被释放,如

free(args[0]);

这是预期的吗?这种记忆的释放不会影响个别线程吗?

答案 1 :(得分:0)

感谢nos,我评论了我的问题,我找到了解决方案。我摆脱了对sprintf的调用,sprintf被用来写入MPI_Comm_Spawn调用的参数字符串,该调用告诉工人要使用多少“飞镖”。相反,我使用MPI_Send向他们发送信息。似乎在我调用gettimeofday之前,sprintf不是一个问题。