0

目标是测量运行时间与#of 进程。

我只是 MPI 的初学者并且被困在某个地方。

我写了一个 hello world 程序并想测试全局运行时。

我尝试使用屏障,以确保所有进程在测量系统时间之前终止,但我遇到了分段错误。

我的代码:

#include <mpi.h> 
#include <stdio.h> 
int main(int argc, char *argv[]) {
  double time1, time2;
  double duration=0.0000;
  int npes, myrank;
  time1 = clock();
  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &npes);
  MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
  printf("From process %d out of %d, Hello World!\n", myrank, npes);
  time2 = clock();
  if (time2-time1>duration) {
    duration = time2-time1;
  }
  duration = time2-time1;
  MPI_BARRIER(MPI_COMM_WORLD);
  printf("runtime is %f ", duration);
  MPI_Finalize();
  return 0;
}

帮我弄清楚为什么会出现分段错误?

4

1 回答 1

1

我从您的代码中注意到的第一件事是您已经测量MPI_Barrier了.hello world"MPI_Barrier

此外,您可能希望用于MPI_Wtime()测量 MPI 进程中经过的时间。

您的代码只会在每台机器上打印运行时,要计算全局运行时,您必须使用MPI_Reduce. 此函数将计算指定的操作(在本例中为 MAX)并将结果存储在根中。

因此,您的代码应如下所示:

#include <mpi.h> 
#include <stdio.h> 
int main(int argc, char *argv[]) {
   double time1, time2,duration,global;
   int npes, myrank;
   MPI_Init(&argc, &argv);
   time1 = MPI_Wtime();
   MPI_Comm_size(MPI_COMM_WORLD, &npes);
   MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
   printf("From process %d out of %d, Hello World!\n", myrank, npes);
   MPI_Barrier(MPI_COMM_WORLD);
   time2 = MPI_Wtime();
   duration = time2 - time1;
   MPI_Reduce(&duration,&global,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);
   if(myrank == 0) {
       printf("Global runtime is %f\n",global);
   }
   printf("Runtime at %d is %f \n", myrank,duration);
   MPI_Finalize();
   return 0;
}
于 2015-09-28T07:59:04.600 回答