【问题标题】:Cannot understand how libgomp implements the FOR construct无法理解 libgomp 如何实现 FOR 构造
【发布时间】:2017-10-16 06:07:54
【问题描述】:

根据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”是第一位的,这怎么可能。

谢谢。

【问题讨论】:

  • 你得到了main的形参argvargc的顺序错误。

标签: openmp libgomp


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2014-01-25
    相关资源
    最近更新 更多