如何在OpenMP中测量每个线程的执行时间?

6
我希望能够测量每个线程在执行代码块时所花费的时间。我想知道我的负载均衡策略是否能够平均地分配工作任务。通常,我的代码如下所示:
#pragma omp parallel for schedule(dynamic,chunk) private(i)
for(i=0;i<n;i++){
//loop code here
}

更新 我正在使用带有gcc的openmp 3.1


1
你使用的编译器是什么(gcc/linux,icc,windows,macos),以及OpenMP实现方式?有一些OpenMP分析/跟踪解决方案... 你想要测量每个“for”循环中线程的时间,还是只需要关于线程的汇总信息?(为什么不在你的“循环代码”周围添加timer_start和timer_stop,并使用线程本地存储?) - osgx
1
这更多地是测量每个线程完成每个迭代块所花费的时间。在循环周围编码似乎很有趣,你能发展一下吗? - Marouen
1
你应该使用明确支持OpenMP的性能分析工具,例如Score-P / Vampir、Allinea MAP、HPCToolkit。 - Zulan
1个回答

8
你可以通过以下方式打印每个线程的时间(未经测试,甚至未编译):
#pragma omp parallel
{
    double wtime = omp_get_wtime();
    #pragma omp for schedule( dynamic, 1 ) nowait
    for ( int i=0; i<n; i++ ) {
        // whatever
    }
    wtime = omp_get_wtime() - wtime;
    printf( "Time taken by thread %d is %f\n", omp_get_thread_num(), wtime );
}

NB,nowait参数可以在for循环结束时移除barrier,否则这个过程将没有任何意义。

当然,使用适当的性能分析工具是更好的方法...


工作得很好,只需更正wtime中的小写t即可。谢谢。 - Marouen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接