Isma Haro
Isma Haro

Reputation: 263

MPI Total run time

I have one program (which calculates the matrix multiplication with cannon algorithm), is implemented in MPI for C, I have set a clock to see the TOTAL time of this program, with total I mean all the process sum.

But as a result I get the time of each process.

Part of my code at the beginning of my main:

 clock_t begin, end;
 double time_spent;
 begin = clock();

 /* Initializing */
 MPI_Init (&argc, &argv);

Then at the end of my code I have:

  MPI_Finalize();

  end = clock();
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("\n\nTIME: %f SECONDS\n\n", time_spent);

Upvotes: 3

Views: 3040

Answers (2)

Gilles
Gilles

Reputation: 9489

Assuming what you want is the sum of the individual times for each of your processes, you will need to:

  1. Move your measure of the end of time before the call to MPI_Finalize(), as you'll need an extra communication;
  2. Collect the individual times via an MPI_Reduce() to process #0 (for example); and finally
  3. Print this time with process 0.

Aside from this, unless you have a compelling reason for doing otherwise, I would encourage you to use MPI_Wtime() as timer rather than the somewhat misleading clock() timer.

The code could then look as follow:

int main( int *argc, char* argv[] ) {
    MPI_Init( &argc, &argv );
    double tbeg = MPI_Wtime();

    // a lot of stuff here

    // you might want a barrier here...
    // MPI_Barrier( MPI_COMM_WORLD );
    double elapsedTime = MPI_Wtime() - tbeg;
    double totalTime;
    MPI_Reduce( &elapsedTime, &totalTime, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD );
    if ( rank == 0 ) {
        printf( "Total time spent in seconds id %f\n", totalTime );
    }
    MPI_Finalize();
    return 0;
}

Upvotes: 7

gsamaras
gsamaras

Reputation: 73366

Yes of course what you get is correct.

If you want the TOTAL TIME, i.e. the sum of the time spent in every process, then send the local time_spent of every node in the master node, perform the summation of the local time_spent's there and print it.

Upvotes: 0

Related Questions