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() and omp_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.
The
clockfunction 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.