1

根据 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”。一开始,库没有加载,所以它们处于挂起状态。当 a 在 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”首先出现,这怎么可能。

谢谢你。

4

1 回答 1

2

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) ();
}

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

于 2017-10-16T13:29:15.583 回答