Reputation: 2154
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
Upvotes: 0
Views: 2424
Reputation: 8238
First, man 3 clock
says that "the clock() function returns an approximation of processor time used by the program". So to determine the time you do not need to compute the difference. This misconception is the source of the error. You just need to call it after your intensive computations and neglect the time consumed by setup stuff
.
If you do not want to take setup time into account, then you really need the difference. So just use simple and robust MPI_Wtime function which gets precise number of seconds since a fixed moment in the past.
The value you are getting by subtraction minimal start time from maximal end time is not overall CPU time in generally accepted terms (i.e. in terms of time
utility). That time is real
time. To get indeed CPU time, you should sum up all processing times, i.e. call MPI_Reduce
with time differences and MPI_SUM
operation.
Upvotes: 1