Search code examples
cmpitime-measurement

Why do we use MPI_Wtime() function instead of time() function?


Since there are already several timers in C, such as time(), clock(), we can use C library function to determine how long a piece of code have been running, for example

start_time=time(NULL);
{...}
end_time=time(NULL);
walltime=end_time-start_time;

If I would like to determine the wall time each processor takes to run the same piece of code, can I use the above code instead of replacing time() with MPI_Wtime()? If it is the wrong way, why should I use MPI_Wtime() in MPI code. Is there any difference between the time() and MPI_Wtime() when I write the MPI code?

By the way, I have no idea about the difference bewteen the time() and MPI_Wtime(), and why does MPI_Wtime() exist because we already have some timers in C standard library.


Solution

  • MPI is designed to support C, C++ and Fortran. It was created in the 1990s when Fortran had no standard support for timers (see the LAPACK source code for a few examples of implementation-specific options).

    Even though gettimeofday exists, it is often extremely slow, because of how it is required to work. MPI does not have to provide the same behavior and can be faster. As Gilles notes, MPI_Wtime returns a double, which is often what users wants, not a struct containing seconds and microseconds as integers.

    For example, on the IBM Blue Gene/P system, MPI_Wtime() read the system clock from a register. The overhead was 220 cycles. In contrast, omp_get_wtime() from OpenMP invoked gettimeofday and had multiple orders of magnitude greater overhead (until I filed the bug and IBM fixed it).

    As a side note, MPI_Wtime won't have the Y2038 problem (https://en.wikipedia.org/wiki/Year_2038_problem) that time_t does, until they fix it by breaking ABI.