2

关于在并行区域内使用关键的 OpenMP,我似乎严重缺乏了解。我的问题很简单:为什么下面的代码会产生带有 valgrind drd 的警告?

#include <stdio.h>
#include <unistd.h>

void A(int* a)
{
   printf("a++\n");
   (*a)++;
}

void B(int* a)
{
   printf("a--\n");
   (*a)--;
}

void f(int* a)
{
#pragma omp critical
   A(a);

   sleep(1); /* work done here */

#pragma omp critical
   B(a);
}

int main(int argc, char** argv)
{
   int i;
   int a = 0;

#pragma omp parallel for
   for(i = 0; i < 4; ++i)
   {
      f(&a);
   }

   return 0;
}

它是用以下方式编译的:

gcc -fopenmp -g -o omptest omptest.c

valgrind 调用是

valgrind --tool=drd  --check-stack-var=yes ./omptest

我的理解是关键部分应该保护我免受我收到的警告的影响。我现在花了 2 天时间试图找出错误,但我没有找到它。如果有人能给我一个关于我到底不明白的东西的提示,那就太好了。

任何帮助表示赞赏。

编辑: 我的 2 CPU 机器上的(重复)警告是:

Thread 2:
Conflicting load by thread 2 at 0x7fefffecc size 4
   at 0x4007DE: A (omptest.c:7)
   by 0x40082E: f (omptest.c:19)
   by 0x400902: main._omp_fn.0 (omptest.c:35)
   by 0x4E45EE9: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
   by 0x4C2D9E1: ??? (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
   by 0x5053E99: start_thread (pthread_create.c:308)
   by 0x535B39C: clone (clone.S:112)
Allocation context: unknown.
Other segment start (thread 1)
   at 0x4C2DF29: pthread_create@* (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
   by 0x4E4631B: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
   by 0x400889: main (omptest.c:32)
Other segment end (thread 1)
   at 0x5326F1D: ??? (syscall-template.S:82)
   by 0x5326DBB: sleep (sleep.c:138)
   by 0x40083D: f (omptest.c:21)
   by 0x400902: main._omp_fn.0 (omptest.c:35)
   by 0x400895: main (omptest.c:32)

Conflicting store by thread 2 at 0x7fefffecc size 4
   at 0x4007E7: A (omptest.c:7)
   by 0x40082E: f (omptest.c:19)
   by 0x400902: main._omp_fn.0 (omptest.c:35)
   by 0x4E45EE9: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
   by 0x4C2D9E1: ??? (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
   by 0x5053E99: start_thread (pthread_create.c:308)
   by 0x535B39C: clone (clone.S:112)
Allocation context: unknown.
Other segment start (thread 1)
   at 0x4C2DF29: pthread_create@* (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
   by 0x4E4631B: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
   by 0x400889: main (omptest.c:32)
Other segment end (thread 1)
   at 0x5326F1D: ??? (syscall-template.S:82)
   by 0x5326DBB: sleep (sleep.c:138)
   by 0x40083D: f (omptest.c:21)
   by 0x400902: main._omp_fn.0 (omptest.c:35)
   by 0x400895: main (omptest.c:32)

Thread 1:
Conflicting load by thread 1 at 0x7fefffecc size 4
   at 0x400805: B (omptest.c:13)
   by 0x40084E: f (omptest.c:24)
   by 0x400902: main._omp_fn.0 (omptest.c:35)
   by 0x400895: main (omptest.c:32)
Allocation context: unknown.
Other segment start (thread 2)
   at 0x535B361: clone (clone.S:84)
Other segment end (thread 2)
   at 0x5326F1D: ??? (syscall-template.S:82)
   by 0x5326DBB: sleep (sleep.c:138)
   by 0x40083D: f (omptest.c:21)
   by 0x400902: main._omp_fn.0 (omptest.c:35)
   by 0x4E45EE9: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
   by 0x4C2D9E1: ??? (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
   by 0x5053E99: start_thread (pthread_create.c:308)
   by 0x535B39C: clone (clone.S:112)

Conflicting store by thread 1 at 0x7fefffecc size 4
   at 0x40080E: B (omptest.c:13)
   by 0x40084E: f (omptest.c:24)
   by 0x400902: main._omp_fn.0 (omptest.c:35)
   by 0x400895: main (omptest.c:32)
Allocation context: unknown.
Other segment start (thread 2)
   at 0x535B361: clone (clone.S:84)
Other segment end (thread 2)
   at 0x5326F1D: ??? (syscall-template.S:82)
   by 0x5326DBB: sleep (sleep.c:138)
   by 0x40083D: f (omptest.c:21)
   by 0x400902: main._omp_fn.0 (omptest.c:35)
   by 0x4E45EE9: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
   by 0x4C2D9E1: ??? (in /usr/lib/valgrind/vgpreload_drd-amd64-linux.so)
   by 0x5053E99: start_thread (pthread_create.c:308)
   by 0x535B39C: clone (clone.S:112)

我将警告理解为 a 上的数据竞争,第 7 行和第 13 行是 (*a)-- 和 (*a)++ 调用。

4

1 回答 1

1

我只是在阅读 drd 的文档,特别是第 8.2.8 节:

DRD 支持由 GCC 生成的 OpenMP 共享内存程序。GCC 从 4.2.0 版开始支持 OpenMP。GCC 对 OpenMP 程序的运行时支持由一个名为 libgomp 的库提供。此库中实现的同步原语直接使用 Linux 的 futex 系统调用,除非该库已配置 --disable-linux-futex 选项。DRD 仅支持已使用此选项配置且存在符号信息的 libgomp 库。对于大多数 Linux 发行版,这意味着您必须重新编译 GCC。另请参阅 Valgrind 源代码树中的脚本 drd/scripts/download-and-build-gcc 以获取如何编译 GCC 的示例。您还必须确保在启动 OpenMP 程序时加载了新编译的 libgomp.so 库。

如果您没有重新编译libgomp,这可能是您遇到的奇怪行为的可能解释。

于 2012-09-29T20:32:25.567 回答