【发布时间】:2018-05-04 22:33:39
【问题描述】:
我正在尝试将OpenMP 2.0 用于parallel for 用于2 个for 循环。我找到了一些示例,我如何使用parallel for 并选择了一些变体。
这就是为什么我要尝试这种方式:
#pragma omp parallel
for(int i=1; i<n; i++) {
#pragma omp for nowait
for(int j=0; j<n; j++) {
//some code here..
}
}
好的。但我是OpenMP world 的新手,我知道如果我有一些构造,例如:if {...},我需要找到另一个变体来并行化我的循环,并考虑到我的if constructions。
您能帮我找到使用OpenMP 并行化我的函数的正确方法吗?谢谢。
string readTextFromImage(Mat image) {
string result = "";
int red;
int green;
int blue;
int ascii;
char ch;
#pragma omp parallel for
for (int i = 0; i < 100; i++)
{
#pragma omp for nowait
for (int j = 0; j < 100; j++)
{
if (i == 0 && j < 3)
{
continue;
}
red = (image.at<Vec3b>(i, j)[2] + 1 - 1) % 10;
green = (image.at<Vec3b>(i, j)[1] + 1 - 1) % 10;
blue = (image.at<Vec3b>(i, j)[0] + 1 - 1) % 10;
if (red == 0 && green == 0 && blue == 0)
{
return result;
}
ascii = red * 100 + green * 10 + blue;
ch = ascii;
result += ch;
}
}
return result;
}
作为我使用上面代码的结果。我有一个错误,我的代码甚至无法编译:
C1001 编译器发生内部错误。
但如果我使用没有if 构造的代码,我就有了编译后的代码。
【问题讨论】:
-
我不明白这个问题。你的代码有一些
#pragma omp parallel for。它没有按预期工作吗? -
您能否详细说明一下“我已经明白,如果我有一些构造,比如:”?如果您能提供minimal reproducible example,那就太好了,它可以更轻松地具体演示如何正确执行此操作。
-
@user463035818 不,它不起作用。我猜,这是因为没有使用
critical section。因为我在没有if的情况下对其进行了测试,这没关系。但可以肯定的是,我需要这些条件。我正在尝试找到一种方法来正确并行化。 -
请将错误逐字添加到问题中(确保是您发布的代码导致该错误)
-
似乎还没有人提到这一点:
C1001 An internal error has occurred in the compiler.之类的消息是编译器错误的证据。有时,编译器错误是由试图做一些非凡的事情的程序暴露出来的,所以“错误”以至于编译器编写者以前从未遇到过或期望永远不会发生,因此没有做出任何规定。至于 OP 的代码,minimal reproducible example 的要求很高。