本文介绍了OpenMP time和clock()给出两个不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些顺序代码可以通过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.

奇怪的是,通过cpu_time()omp_get_wtime()计算的经过时间不同.为什么?

The weird thing is the elapsed time calculated via cpu_time() and omp_get_wtime() is different. Why?

根据cpu_time()的经过时间类似于连续时间.

The elapsed time according to cpu_time() is similar to the sequential time.

在计算开始之前:

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

计算结束后:

ctime2_ = cpu_time();
#ifdef _OPENMP
ctime2 = omp_get_wtime();
#endif

cpu_time()函数定义:

cpu_time() function definition:

double cpu_time(void)
{
  double value;
  value = (double) clock () / (double) CLOCKS_PER_SEC;
  return value;
}

打印结果:

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

抽样结果:

7.009537 - 11.575277 seconds.

推荐答案

clock函数测量cpu时间,即您在CPU上花费的时间,OMP函数测量执行过程中经过的时间,两个完全不同的东西.

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.

这篇关于OpenMP time和clock()给出两个不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 12:00