Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 272
Isma Haro Avatar asked Jan 31 '26 13:01

Isma Haro


1 Answers

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;
}
like image 196
Gilles Avatar answered Feb 03 '26 06:02

Gilles