How to run the same MPI program multiple times

1.9k Views Asked by At

I have an MPI program to calculate a sorting time. I run it with mpirun -np 2 mpiSort. So this gives me the sorting time by 2 processes.

I want to get the sorting time for 5 times to average them. How do I do that automatically?

If I do a loop in the mpiSort program. It actually executes 5(times) x 2(processes) = 10 times.

Edit: The mpiSort does the sort in parallel. Basically, I'm trying to do mpirun -np 2 mpiSort without typing it 5 times. Because I want to do the same for 4 cores, 8 cores.

2

There are 2 best solutions below

4
On

You could run on five cores using mpirun -np 5 mpiSort and add an MPI_gather at the end. Is the sort code actually using MPI (i.e. calls MPI_init at the beginning?). Assuming you are, you can run on 5 cores and simply average at the end with a reduce,

# include <mpi.h>
#include <iostream>
using namespace std;

int main ( int argc, char *argv[] )
{

    int ierr, rank, nprocs, root=0;
    double time, buf;
    ierr = MPI_Init ( &argc, &argv );
    ierr = MPI_Comm_rank (MPI_COMM_WORLD, &rank);
    ierr = MPI_Comm_size (MPI_COMM_WORLD, &nprocs);
    time = 0.5;
    ierr = MPI_Reduce (&time, &buf, 1, MPI_DOUBLE_PRECISION, 
                          MPI_SUM, root, MPI_COMM_WORLD);
    if (rank == root){
        buf = buf / nprocs;
        cout << buf << "\n";
    }

  MPI_Finalize ( );

}

where time is each processes sort time.

0
On

Put in a loop is the way to go. I was confused because I got 10 values of endTime = MPI_Wtime(), and I only used 5 of them from the root process. Thanks to @EdSmith with his MPI_Reduce code, the correct calculated time is the average of the two processes by using MPI_Reduce.

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &nProcs);

for (int run=0; run<5; run++) {
  ...
  endTime = MPI_Wtime();
  totalTime = endTime - startTime;
  MPI_Reduce (&totalTime, &workTime, 1, MPI_DOUBLE_PRECISION, MPI_SUM, root, MPI_COMM_WORLD);
  if (rank == root) {
    paraTime = workTime/nProcs;
  }
  ...
}

MPI_Finalize();