Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMP time and clock() give two different results

Tags:

c

openmp

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.
like image 658
mert Avatar asked May 20 '12 13:05

mert


People also ask

What are the run time library functions in OpenMP?

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.

Does omp_get_wtime return a consistent value across threads?

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.

Should an OpenMP program include explicit flush directives for lock variables?

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.)

How does the OMP_unset_nest_lock() function work?

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.


2 Answers

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.

like image 75
Hristo Iliev Avatar answered Oct 21 '22 23:10

Hristo Iliev


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.

like image 25
Jens Gustedt Avatar answered Oct 21 '22 23:10

Jens Gustedt