OpenMP () time and clock computes two different results

I have serial code to parallelize through OpenMP. I put the appropriate pragmas and tested it. During testing, I interpret performance by checking the time spent in the main function. The strangest thing is the elapsed time, calculated through cpu_time() , and omp_get_wtime() returns two different results. What do you think is the reason?

The elapsed time calculated using the cpu_time() function is like sequential time.

Before you start computing

 ctime1_ = cpu_time(); #ifdef _OPENMP ctime1 = omp_get_wtime(); #endif 

After completing the calculations

 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; } 

Print result

 printf("%f - %f seconds.\n", ctime2 - ctime1, ctime2_ - ctime1_); 

Result Example

 7.009537 - 11.575277 seconds. 
+6
source share
3 answers

The clock function measures the processor time, the time you actively spend on the CPU, the OMP function measures the time elapsed at runtime, two completely different things.

Your process seems to be blocked while waiting somewhere.

+12
source

What you are observing is an absolutely true result for any parallel application - the combined processor time of all threads returned by clock() is usually longer than the wall time measured by omp_get_wtime() , unless your application is basically sleeping or waiting.

+6
source

The clock() function returns the processor time, not the time on the wall. Use gettimeofday() .

+4
source

Source: https://habr.com/ru/post/916196/


All Articles