当我使用 OpenMP 进行并行 for 循环时,为什么我的程序会收到 SIGABRT?
我正在编写一个科学程序来用 C++ 求解麦克斯韦方程。数据并行的任务,我想使用OpenMP使程序并行。但是当我使用 OpenMP 在函数内部并行化 for 循环时。当我运行代码时,程序会收到 SIGABRT。我没发现哪里出了问题。请帮忙。
for循环如下:
#pragma omp parallel for
for (int i = 0; i < totalNoOfElementsInSecondMesh; i++) {
FEMSecondMeshElement2D *secondMeshElement = (FEMSecondMeshElement2D *)mesh->secondMeshFEMElement(i);
if (secondMeshElement->elementType == FEMDelectricElement) {
if (solutionType == TE)
calculateEzFieldForDielectricElement(secondMeshElement, i, currentSecondMeshIndex, nextFirstMeshIndex);
else
calculateHzFieldForDielectricElement(secondMeshElement, i, currentSecondMeshIndex, nextFirstMeshIndex);
} else if (secondMeshElement->elementType == FEMXPMLDielectricElement) {
if (solutionType == TE)
calculateEzFieldForDielectricPMLElement((FEMPMLSecondMeshElement2D *)secondMeshElement, i, currentSecondMeshIndex, nextFirstMeshIndex);
else
calculateHzFieldForDielectricPMLElement((FEMPMLSecondMeshElement2D *)secondMeshElement, i, currentSecondMeshIndex, nextFirstMeshIndex);
}
}
编译器是Xcode 4.2默认自带的llvm-gcc。
请帮忙。
I'm writing a scientific program to solve Maxwell's equation with C++. The task in data parallel and I want to use OpenMP to make the program parallel. But when I use OpenMP to parallelise a for loop in side a function it. When I run my code the program gets SIGABRT. I couldn't find out went wrong. Please help.
The for loop is as follows:
#pragma omp parallel for
for (int i = 0; i < totalNoOfElementsInSecondMesh; i++) {
FEMSecondMeshElement2D *secondMeshElement = (FEMSecondMeshElement2D *)mesh->secondMeshFEMElement(i);
if (secondMeshElement->elementType == FEMDelectricElement) {
if (solutionType == TE)
calculateEzFieldForDielectricElement(secondMeshElement, i, currentSecondMeshIndex, nextFirstMeshIndex);
else
calculateHzFieldForDielectricElement(secondMeshElement, i, currentSecondMeshIndex, nextFirstMeshIndex);
} else if (secondMeshElement->elementType == FEMXPMLDielectricElement) {
if (solutionType == TE)
calculateEzFieldForDielectricPMLElement((FEMPMLSecondMeshElement2D *)secondMeshElement, i, currentSecondMeshIndex, nextFirstMeshIndex);
else
calculateHzFieldForDielectricPMLElement((FEMPMLSecondMeshElement2D *)secondMeshElement, i, currentSecondMeshIndex, nextFirstMeshIndex);
}
}
The compiler is llvm-gcc which came with Xcode 4.2 by default.
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能在 Lion 上遇到了编译器问题。请参阅此链接:
https://plus.google.com/101546077160053841119/posts/9h35WKKqffL
你可以下载gcc 4.7从该页面上的链接为 Lion 预编译,这似乎工作正常。
It is possible you've run into a compiler problem on Lion. See this link:
https://plus.google.com/101546077160053841119/posts/9h35WKKqffL
You can download gcc 4.7 pre-compiled for Lion from a link on that page, and that seems to work fine.
程序崩溃的最可能原因是访问 FEMSecondMeshElement2D* secondaryMeshElement、currentSecondMeshIndex 或 nextFirstMeshIndex 时内存损坏
取决于 if 子句中的其他函数对它们执行的操作。
我建议仔细检查变量的访问,并事先将它们正确声明为线程私有/共享。
例如
The most likely reason that your program crashes is memory corruption when accessing FEMSecondMeshElement2D* secondMeshElement, currentSecondMeshIndex or nextFirstMeshIndex
depending what the other functions in the if clause do to them.
I recommend to check carefully the access of variables and declare them thread private / shared properly, beforehand.
e.g.
您是否尝试使用调试和所有警告(即使用
-g -Wall
标志)来编译程序?然后您可以使用调试器(即
gdb
) 进行调试。您可以启用 core(5) 转储(通过适当设置,使用 setrlimit(2) 或
ulimit
shell 内置函数 调用它,RLIMIT_CORE
)。有了core
文件后,gdb
可用于事后分析。还有 gcore(1) 强制 <代码>核心转储。Did you try to compile your program with debugging and all warnings, i.e. with
-g -Wall
flags?Then you can use a debugger (that is,
gdb
) to debug it.You can enable core(5) dumps (by setting appropriately, with setrlimit(2) or the
ulimit
shell builtin which calls it, theRLIMIT_CORE
). Once you have acore
file,gdb
can be used for post-mortem analysis. And there is also gcore(1) to force acore
dump.