OpenMP time and clock() give two different results
Solution 1
The clock
function measures cpu time, the time you spend actively on the CPU, the OMP function measures the time as it has passed during execution, two completely different things.
Your process seems to be blocked in waiting somewhere.
Solution 2
What you observe is a perfectly valid result for any parallel application - the combined CPU time of all threads as returned by clock()
is usually more than the wallclock time measured by omp_get_wtime()
except if your application mostly sleeps or waits.
Solution 3
The clock()
function returns CPU time, not wall time. Instead, use gettimeofday()
.
mert
Sofware Engineer by day and night. M.Sc. and B.Sc. in Computer Engineering at Bilkent University, Ankara, Turkey.
Updated on June 17, 2022Comments
-
mert about 2 years
I have sequential code to parallelize via OpenMP. I have put in the corresponding pragmas and tested it. I measure the performance gain by checking the time spent in the main function.
The weird thing is the elapsed time calculated via
cpu_time()
andomp_get_wtime()
is different. Why?The elapsed time according to
cpu_time()
is similar to the sequential time.Before computation starts:
ctime1_ = cpu_time(); #ifdef _OPENMP ctime1 = omp_get_wtime(); #endif
After computation ends:
ctime2_ = cpu_time(); #ifdef _OPENMP ctime2 = omp_get_wtime(); #endif
cpu_time() function definition:
double cpu_time(void) { double value; value = (double) clock () / (double) CLOCKS_PER_SEC; return value; }
Printing result:
printf("%f - %f seconds.\n", ctime2 - ctime1, ctime2_ - ctime1_);
Sample result:
7.009537 - 11.575277 seconds.