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.
3. Run-time library functions This section describes the OpenMP C and C++ run-time library functions. The header <omp.h> declares two types, several functions that can be used to control and query the parallel execution environment, and lock functions that can be used to synchronize access to data.
The routine’s return value is not guaranteed to be consistent across any set of threads. Effect The omp_get_wtime routine returns a value equal to the elapsed wall clock time in seconds since some time-in-the-past. The actual time-in-the-past is arbitrary, but it is guaranteed not to change during the execution of the application program.
Therefore, it isn't necessary for an OpenMP program to include explicit flush directives to make sure that the lock variable's value is consistent among different threads. (There may be a need for flush directives to make the values of other variables consistent.)
The omp_unset_nest_lock function releases a nestable lock. The omp_test_nest_lock function tests a nestable lock. The OpenMP lock functions access the lock variable in such a way that they always read and update the most current value of the lock variable.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With