0

目標は、実行時間とプロセス数を測定することです。

私は 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 に答える