gpt4 book ai didi

c++ - 我可以将 Thread Sanitizer 用于 OpenMP 程序吗?

转载 作者:可可西里 更新时间:2023-11-01 17:39:03 25 4
gpt4 key购买 nike

考虑以下示例:

#include <iostream> 

int main () {
int i = 0;
#pragma omp parallel
{
#pragma omp critical
{
++i;
}
}
std::cout << i;
}

使用 g++ -fopenmp -fsanitize=thread 编译并运行 yield

WARNING: ThreadSanitizer: data race (pid=9576)
Read of size 4 at 0x7ffdc170f600 by thread T1:
#0 main._omp_fn.0 (a.out+0x000000400d20)
#1 gomp_thread_start /build/gcc/src/gcc-5.2.0/libgomp/team.c:118 (libgomp.so.1+0x00000000f42d)

Previous write of size 4 at 0x7ffdc170f600 by thread T2:
#0 main._omp_fn.0 (a.out+0x000000400d35)
#1 gomp_thread_start /build/gcc/src/gcc-5.2.0/libgomp/team.c:118 (libgomp.so.1+0x00000000f42d)

Location is stack of main thread.

Thread T1 (tid=9578, running) created by main thread at:
#0 pthread_create /build/gcc/src/gcc-5.2.0/libsanitizer/tsan/tsan_interceptors.cc:895 (libtsan.so.0+0x000000027a37)
#1 gomp_team_start /build/gcc/src/gcc-5.2.0/libgomp/team.c:796 (libgomp.so.1+0x00000000f98f)
#2 __libc_start_main (libc.so.6+0x00000002060f)

Thread T2 (tid=9579, running) created by main thread at:
#0 pthread_create /build/gcc/src/gcc-5.2.0/libsanitizer/tsan/tsan_interceptors.cc:895 (libtsan.so.0+0x000000027a37)
#1 gomp_team_start /build/gcc/src/gcc-5.2.0/libgomp/team.c:796 (libgomp.so.1+0x00000000f98f)
#2 __libc_start_main (libc.so.6+0x00000002060f)

SUMMARY: ThreadSanitizer: data race ??:0 main._omp_fn.0

据我所知,这是误报。有没有办法避免这种情况?

(使用 clang 和 libomp 也可以。)

最佳答案

是的,至少对于 Clang,这是相对容易的。您需要构建具有 ThreadSanitizer 支持的 libomp(Clang 使用它而不是 libgomp)。这不会花那么长时间:

git clone https://github.com/llvm/llvm-project
cd llvm-project
mkdir build
cd build
cmake -DLIBOMP_TSAN_SUPPORT=1 ../openmp
sudo cmake --build . --target install

(sudo--target install 是可选的,如果你调整路径到下面的 libomp.so)

如果您使用此 libomp.so 而不是系统一,现在运行您的示例可以正常运行:

clang++ -fsanitize=thread -fopenmp main.cpp
env LD_PRELOAD=/usr/local/lib/libomp.so ./a.out

关于c++ - 我可以将 Thread Sanitizer 用于 OpenMP 程序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33004809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com