gpt4 book ai didi

c++ - 与DRD一起误报 “Conflicting load”?

转载 作者:行者123 更新时间:2023-12-01 14:51:07 24 4
gpt4 key购买 nike

使用DRD(valgrind)分析我的C++代码会发现“冲突的负载”,但我看不出为什么。代码如下:

int* x;
int Nt = 2;

x = new int[Nt];

omp_set_num_threads(Nt);

#pragma omp parallel for
for (int i = 0; i < Nt; i++)
{
x[i] = i;
}

for (int i = 0; i < Nt; i++)
{
printf("%d\n", x[i]);
}

程序运行良好,但是当主线程打印出x [1]的值时,DRD会遇到问题。除了由于x数组的分配方式可能导致的错误共享外,我看不出为什么应该存在任何冲突以及如何避免冲突...请提供任何见解?

编辑这是上述代码的DRD输出(第47行对应于printf语句):
==2369== Conflicting load by thread 1 at 0x06031034 size 4
==2369== at 0x4008AB: main (test.c:47)
==2369== Address 0x6031034 is at offset 4 from 0x6031030. Allocation context:
==2369== at 0x4C2DCC7: operator new[](unsigned long) (vg_replace_malloc.c:363)
==2369== by 0x400843: main (test.c:37)
==2369== Other segment start (thread 2)
==2369== at 0x4C31EB8: pthread_mutex_unlock (drd_pthread_intercepts.c:703)
==2369== by 0x4C2F00E: vgDrd_thread_wrapper (drd_pthread_intercepts.c:236)
==2369== by 0x5868D95: start_thread (in /lib64/libpthread-2.15.so)
==2369== by 0x5B6950C: clone (in /lib64/libc-2.15.so)
==2369== Other segment end (thread 2)
==2369== at 0x5446846: ??? (in /usr/lib64/gcc/x86_64-pc-linux-gnu/4.7.3/libgomp.so.1.0.0)
==2369== by 0x54450DD: ??? (in /usr/lib64/gcc/x86_64-pc-linux-gnu/4.7.3/libgomp.so.1.0.0)
==2369== by 0x4C2F014: vgDrd_thread_wrapper (drd_pthread_intercepts.c:355)
==2369== by 0x5868D95: start_thread (in /lib64/libpthread-2.15.so)
==2369== by 0x5B6950C: clone (in /lib64/libc-2.15.so)

最佳答案

GNU OpenMP运行时(libgomp)使用线程池实现OpenMP线程团队。创建线程后,线程将停靠在障碍处,等待唤醒以执行特定任务。在GCC中,这些任务以概述的代码段(与内联代码相反)的形式出现,即,并行区域(或显式OpenMP任务)的代码被提取到单独的函数中,并提供给某些等待线程作为执行任务。然后提起对接屏障,线程开始执行任务。一旦完成,线程将再次对接-它们没有连接,而只是被搁置。因此,从DRD的 Angular 来看,在并行区域之后执行代码的串行部分的主线程正在访问而没有其他线程可能写入的保护资源。由于其他线程已停靠并等待新任务,因此这当然不会发生。

这种误报在不理解OpenMP特定语义的通用工具(如DRD)中很常见。因此,这些工具不适合分析OpenMP程序。您应该改用专门的工具,例如Sun / Oracle Solaris Studio for Linux或商业Intel Inspector提供的免费线程分析器。后者可以免费使用,并具有用于非商业开发目的的许可证。两种工具都了解OpenMP的细节,不会出现可能的数据争用的情况。

关于c++ - 与DRD一起误报 “Conflicting load”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425388/

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