I'm working on a parallel matrix-matrix multiplier in MPI. I've got the calculation part working but I also want to calculate CPU time. I'm getting stuck because it looks like some processes are reporting start and end times of 0 and for a task that should take under a second (small matrices), the program reports 1000+ second CPU times (even though I know that it runs in under a second from observation). Here's what I'm currently doing:
#include <time.h>
#include "mpi.h"
// other includes
int main()
{
int start, end, min_start, min_end;
if (rank == 0)
{
// setup stuff
start = clock();
MPI_Reduce(&min_start, &start, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
// master computation stuff
end = clock();
MPI_Reduce(&max_end, &end, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
cout << "CPU time was "
<< (double)(max_end - min_start) / CLOCKS_PER_SEC
<< " seconds" << endl;
}
else if (rank != 0)
{
// setup stuff
start = clock();
MPI_Reduce(&min_start, &start, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
// slave computation stuff
end = clock();
MPI_Reduce(&max_end, &end, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
}
}
I'm not sure what the source of the error is. When I added in this debugging output (after the if (rank == 0)
and else if (rank != 0)
statement)
MPI_Barrier(MPI_COMM_WORLD);
for (int i=0; i<size; i++)
{
if (rank == i)
cout << "(" << i << ") CPU time = "
<< end << " - " << start
<< " = " << end - start << endl;
MPI_Barrier(MPI_COMM_WORLD);
}
I get the following output
CPU time was 1627.91 seconds
(1) CPU time = 0 - 0 = 0
(2) CPU time = 0 - 0 = 0
(0) CPU time = 1627938704 - 32637 = 1627906067
(3) CPU time = 10000 - 0 = 10000