无法理解 libgomp 如何实现 FOR 构造

Cannot understand how libgomp implements the FOR construct

根据 libgomp 手册,代码格式为:

#pragma omp parallel for
for (i = lb; i <= ub; i++)
  body;

变成

void subfunction (void *data)
{
  long _s0, _e0;
  while (GOMP_loop_static_next (&_s0, &_e0))
  {
    long _e1 = _e0, i;
    for (i = _s0; i < _e1; i++)
      body;
  }
  GOMP_loop_end_nowait ();
}

GOMP_parallel_loop_static (subfunction, NULL, 0, lb, ub+1, 1, 0);
subfunction (NULL);
GOMP_parallel_end ();

我做了一个非常小的程序来调试,只是为了看看这个实现是如何工作的:

int main(int argc, char** argv)
{
  int res, i;
  # pragma omp parallel for num_threads(4)
  for(i = 0; i < 400000; i++) 
      res = res*argc;

  return 0;
} 

接下来,我 运行 gdb 并将断点设置为 "GOMP_parallel_loop_static" 和 "GOMP_parallel_end"。一开始,库没有加载,所以它们处于挂起状态。当运行 gdb中的测试程序时,我得到如下结果:

(gdb) run 2 1 6 5 4 3 8 7
Starting program: ./test 2 1 6 5 4 3 8 7
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-
gnu/libthread_db.so.1".
[New Thread 0x7ffff73c9700 (LWP 5381)]
[New Thread 0x7ffff6bc8700 (LWP 5382)]
[New Thread 0x7ffff63c7700 (LWP 5383)]

 Thread 1 "test" hit Breakpoint 2, 0x00007ffff7bc0c00 in GOMP_parallel_end () from /usr/lib/x86_64-linux-gnu/libgomp.so.1

如您所见,它到达了 "GOMP_parallel_end" 中的第二个断点,但不是第一个。我想知道如果 libgomp 手册清楚地表明 "GOMP_parallel_loop_static" 排在第一位,这怎么可能。

谢谢。

GCC 文档的这一部分并没有真正定期更新,因此最好只阅读它作为实际发生情况的近似值。如果您对这种详细程度感兴趣,我建议您查看由 -fdump-tree-all 和类似选项生成的调试文件。

使用最新版本的 GCC,您的示例生成对 __builtin_GOMP_parallel 的调用,它映射到 GOMP_parallel。那个在最后内部调用 GOMP_parallel_end,所以这就是你所看到的,我想。

void
GOMP_parallel (void (*fn) (void *), void *data, unsigned num_threads, unsigned int flags)
{ 
  num_threads = gomp_resolve_num_threads (num_threads, 0);
  gomp_team_start (fn, data, num_threads, flags, gomp_new_team (num_threads));
  fn (data);
  ialias_call (GOMP_parallel_end) ();
}

当然,我们很乐意接受更新文档的补丁。 :-)