gpt4 book ai didi

c++ - 嵌套for循环的openmp错误

转载 作者:行者123 更新时间:2023-11-30 05:42:07 24 4
gpt4 key购买 nike

我在面向对象的 cpp 代码中使用了 Eigen 库。我定义了一个主要对象,2DGF,在它的一些方法中,我使用了 openmp。现在我想在另一种方法中使用 openmp,如下所示:

#pragma omp parallel num_threads(NON) 
#pragma omp for
for(unsigned int iE=0;iE<NE;iE++){
for(unsigned int iK=0;iK<NK;iK++){
for(unsigned int ii=0; ii<NL ;ii++){
for(unsigned int jj=0; jj<NL ;jj++){
if(abs(CoorX[ii]-CoorX[jj])<DiogLim){
G.insert(iE*NL+ii,iK*NL+jj)=0;
GR.insert(iE*NL+ii,iK*NL+jj)=0;
S.insert(iE*NL+ii,iK*NL+jj)=0;
SR.insert(iE*NL+ii,iK*NL+jj)=0;
}
}
}
}
}

其中 G、GR、S 和 SR 是稀疏矩阵。如果不使用 openmp,它可以正常工作。但是当我使用 openmp 时,我收到以下错误:
2DGF: malloc.c:2372: sysmalloc: 断言`(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof (size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' 失败。
中止(核心转储)

有人可以帮我解决吗?

最佳答案

我对 Eigen 了解不多。但我在他们的 documentation 中读了一点关于 SparseMatrix 的方法 insert() 它说它是 reserving room 用于其他插入。您是否为所有插入物预留了足够的空间?

如果不是,则可能是以下代码中存在的同一个错误。

#include <iostream>
#include <vector>
#include <omp.h>

int main()
{
std::vector< int > vInts;

#pragma omp parallel for
for (int i = 0; i < 1000; i++) {
vInts.push_back( i );
}

return 0;
}

这也可能以段错误结束。原因是 vector 被重新定位到不同的内存区域。这可以通过为 vector 保留足够的内存来避免。在这种情况下 vInts.reserve(1000)

SparseMatrix 还提供了一种保留方法。试试这个,至少将其排除为可能的错误。

编辑:这是带有 for 循环的代码和最内层循环中的简单赋值。该程序使用 g++ -fopenmp -Wall -Werror main.cpp 编译,没有错误或警告。程序正常执行,输出 i=99。

#include <iostream>
#include <omp.h>
#include <vector>

#define NON 3

int main()
{
int i = 0;

unsigned int NE = 100;
unsigned int NK = 100;
unsigned int NL = 100;

#pragma omp parallel num_threads(NON)
#pragma omp for
for(unsigned int iE=0;iE<NE;iE++){
for(unsigned int iK=0;iK<NK;iK++){
for(unsigned int ii=0; ii<NL ;ii++){
for(unsigned int jj=0; jj<NL ;jj++){
i = jj;
}
}
}
}

return 0;
}

关于c++ - 嵌套for循环的openmp错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30815669/

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