0

The goal is to measure run-time vs #of processes.

I am just a beginner in MPI and get stuck somewhere.

I wrote a hello world program and want to test global run-time.

I tried using barrier, to make sure all processes terminate before measuring the system time, but I get a segmentation fault.

My code:

#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;
}

Help me figure out why I am getting segmentation fault?

Himanshu
  • 4,327
  • 16
  • 31
  • 39

1 Answers1

1

The first thing that I can notice from your code is that you've measured the time before the MPI_Barrier, which means the runtime might be measured even before all the processes print "hello world". To ensure the correctness measure the time after a MPI_Barrier.

Also you might want to use MPI_Wtime() to measure the time elapsed in an MPI Process.

Your code will only print the runtime at each machine, to compute the global runtime, you will have to use MPI_Reduce. This function will compute the specified operation (MAX in this case) and store the result at the root.

So here is what your code should look like:

#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;
}
Pooja Nilangekar
  • 1,419
  • 13
  • 20