OpenMP 的哪个线程做的工作最多?

Which thread of OpenMP is doing the most of work?

我想测量每个线程在 OpenMP 中执行任务的时间。我该怎么做?

对平行区域执行此操作的基本方法如下:

int total_threads = ...;
double time_taken_by_threads[total_threads];
#pragma omp parallel region num_threads(total_threads)
{
    int threadID = omp_get_thread_num();
    double start = omp_get_wtime();
     // parallel task
    double end = omp_get_wtime();
    time_taken_by_threads[threadID] = end - start;
}  
for(int i = 0; i < total_threads; i++)
   printf("Thread %d took, %ld (s)", time_taken_by_threads[i];

您创建一个数组来存储 每个 线程所用的时间。您在线程执行的并行任务之前和之后使用 omp_get_wtime。最后,将结果保存在数组中。在并行区域之外,您可以检查每个线程所用的时间。

这适用于并行区域,但对于其他 OpenMP 并行构造函数(例如任务)可能不是很好。

最可靠的解决方案是使用 OpenM 分析器,例如 VTune