当我使用 OpenMP 进行并行 for 循环时,为什么我的程序会收到 SIGABRT?

发布于 2024-12-15 12:59:36 字数 1197 浏览 3 评论 0原文

我正在编写一个科学程序来用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还给你自由 2024-12-22 12:59:37

您可能在 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.

笙痞 2024-12-22 12:59:37

程序崩溃的最可能原因是访问 FEMSecondMeshElement2D* secondaryMeshElement、currentSecondMeshIndex 或 nextFirstMeshIndex 时内存损坏
取决于 if 子句中的其他函数对它们执行的操作。

我建议仔细检查变量的访问,并事先将它们正确声明为线程私有/共享。
例如

FEMSecondMeshElement2D *secondMeshElement = NULL;

#pragma omp parallel for private(secondMeshElement)
...

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.

FEMSecondMeshElement2D *secondMeshElement = NULL;

#pragma omp parallel for private(secondMeshElement)
...
我不是你的备胎 2024-12-22 12:59:37

您是否尝试使用调试和所有警告(即使用 -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, the RLIMIT_CORE). Once you have a core file, gdb can be used for post-mortem analysis. And there is also gcore(1) to force a core dump.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文