c++ - valgrind 和 openmp,仍然可以访问并可能丢失,这很糟糕吗?

标签 c++ openmp valgrind

这里是 C++ 新手。

在过去的几天里,我一直在提高我的内存管理技能,根据 valgrind,我的程序不再泄漏内存。事实上,我根本没有收到来自 valgrind 的警告。

但是,当我将 openmp 循环添加到我的代码中时,我开始在 valgrind (memcheck) 中出现以下错误:(但没有绝对丢失的 block )

==6417== 304 bytes in 1 blocks are possibly lost in loss record 3 of 4
==6417==    at 0x4C279FC: calloc (vg_replace_malloc.c:467)
==6417==    by 0x4011868: _dl_allocate_tls (dl-tls.c:300)
==6417==    by 0x6649871: pthread_create@@GLIBC_2.2.5 (allocatestack.c:570)
==6417==    by 0x62263DF: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==6417==    by 0x42A2BB: Blade::updatePanels() (blade.cpp:187)
==6417==    by 0x418677: VLMsolver::initialiseBlade() (vlmsolver.cpp:590)
==6417==    by 0x415A1B: VLMsolver::start(std::string) (vlmsolver.cpp:80)
==6417==    by 0x40B28C: main (charybdis.cpp:176)

和:

==6417== 1,568 bytes in 1 blocks are still reachable in loss record 4 of 4
==6417==    at 0x4C28FAC: malloc (vg_replace_malloc.c:236)
==6417==    by 0x6221578: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==6417==    by 0x6226044: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==6417==    by 0x622509B: GOMP_parallel_start (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==6417==    by 0x41AF58: VLMsolver::segmentCirculations() (vlmsolver.cpp:943)
==6417==    by 0x415E4B: VLMsolver::solveManager() (vlmsolver.cpp:177)
==6417==    by 0x415A4B: VLMsolver::start(std::string) (vlmsolver.cpp:91)
==6417==    by 0x40B28C: main (charybdis.cpp:176)

这是 valgrind 不理解 openmp 的情况吗?或者它可能会变得险恶?

请注意,当我使用 helgrind 运行 valgrind 时,我收到了数以千计的“读取期间可能存在数据竞争”(和写入)消息。然而,我的程序(流体动力学求解器)对 openmp 和串行代码给出了相同的结果。如果您对此问题感兴趣,我可以提供 helgrind 错误和相关部分。

否则现在,这里是第二条消息的违规代码:第 943 行是 pragma 行。

for (int b = 0;b < sNumberOfBlades;++b) {
*VLMSOLVER.CPP LINE 943 is next*:
#pragma omp parallel for collapse(2) num_threads(2) firstprivate(b) 
    for (int i = 0;i<numX;++i) {
        for (int j = 0;j<numY;++j) {
            if (j == 0) {
                blades[b].line[i*numNodesY+j].circulation = blades[b].panel[i*numY+j].circulation;
            } else {
                blades[b].line[i*numNodesY+j].circulation =  blades[b].panel[i*numY+j].circulation - blades[b].panel[i*numY+j-1].circulation;
            }
            if (j==numY-1) {
                blades[b].line[i*numNodesY+j+1].circulation = -1 * blades[b].panel[i*numY+j].circulation;
            }

        }
    }
    if (sBladeSymmetry) {
        break;
    }
}

int k = numX*numNodesY;
for (int b = 0;b < sNumberOfBlades;++b) {
    for (int i = 0;i<numX;++i) {
        for (int j = 0;j<numY;++j) {
            if (i == 0) {
                blades[b].line[k+i*numY+j].circulation = - 1 * blades[b].panel[i*numY+j].circulation;
            } else {
                blades[b].line[k+i*numY+j].circulation = -1 * blades[b].panel[i*numY+j].circulation + blades[b].panel[(i-1)*numY+j].circulation;
            }
            if (i==numX-1) {
                blades[b].line[k+(i+1)*numY+j].circulation =  blades[b].panel[i*numY+j].circulation;
            }
        }
    }
    if (sBladeSymmetry) {
        break;
    }
}

最佳答案

Still reachable 不是内存泄漏。

Still reachable意味着一 block 内存尚未被释放,但在尚未释放的寄存器或内存中仍有指向该 block 开始的有效指针。

你需要看看 the Valgrind FAQ libgomp的实际原因 here 解释了导致警告的原因

关于c++ - valgrind 和 openmp,仍然可以访问并可能丢失,这很糟糕吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43471287/

相关文章:

c# - 识别行计数程序中的 C# 或 C++ 函数开始

c++ - openMP 嵌套并行 for 循环与内部并行 for

c - 蒙特卡罗 pi 近似的并行化

c - 如何使用 libbmp 编译我的项目?

c++ - Valgrind 下 Mac OS 上的 std::thread.join() SIGSEGV

c++ - SSE 指令在实践中优化了什么,编译器如何启用和使用它们?

c++ - constexpr 与 const 引用交互的奇怪行为

c - Valgrind:未初始化的值是由堆分配创建的

c++ - 调用函数的动态函数

c - C 中的 GeoIP_new API 可能存在内存泄漏