在C ++ OpenMP代码中测量执行时间 [英] Measure execution time in C++ OpenMP code

查看:915
本文介绍了在C ++ OpenMP代码中测量执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以顺序方式运行.cpp代码(i)和(ii)使用OpenMP语句。我试图看到时间差异。对于计算时间,我使用这个:

I am running a .cpp code (i) in sequential style and (ii) using OpenMP statements. I am trying to see the time difference. For calculating time, I use this:

#include <time.h>
.....
main()
{

  clock_t start, finish;
  start = clock();
  .
  .
  .
  finish = clock();

  processing time = (double(finish-start)/CLOCKS_PER_SEC);

 }

代码。运行此过程大约需要8秒钟。当我在代码中插入OpenMP语句,然后计算时间,我得到减少的时间,但在控制台上显示的时间约为8-9秒,实际上它只是3-4秒的实时!

The time is pretty accurate in sequential (above) run of the code. It takes about 8 seconds to run this. When I insert OpenMP statements in the code and thereafter calculate the time I get a reduction in time, but the time displayed is about 8-9 seconds on the console, when actually its just 3-4 seconds in real time!

这里是我的代码看起来抽象:

Here is how my code looks abstractly:

#include <time.h>
.....
main()
{

  clock_t start, finish;
  start = clock();
  .
  .
  #pragma omp parallel for
  for( ... )
     for( ... )
       for (...)
    {           
      ...;      
    }
  .
  .
  finish = clock();

  processing time = (double(finish-start)/CLOCKS_PER_SEC);

 }

当我运行上面的代码时,但是所显示的时间在实时方面不准确。在我看来,就像clock()函数计算每个线程的单独时间,并将它们相加并显示出来。

When I run the above code, I get the reduction in time but the time displayed is not accurate in terms of real time. It seems to me as though the clock () function is calculating each thread's individual time and adding up them up and displaying them.

有人可以告诉原因或建议我可以使用任何其他计时函数来测量OpenMP程序中的时间。

Can someone tell the reason for this or suggest me any other timing function to use to measure the time in OpenMP programs?

谢谢。

推荐答案

我看过clock()报告CPU时间,而不是实时时间。

I've seen clock() reporting CPU time, instead of real time.

您可以使用

struct timeval start, end;
gettimeofday(&start, NULL);

// benchmark code

gettimeofday(&end, NULL);

delta = ((end.tv_sec  - start.tv_sec) * 1000000u + 
         end.tv_usec - start.tv_usec) / 1.e6;

要时间

这篇关于在C ++ OpenMP代码中测量执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆