[英]How can I use openMP for loop parallel in this for loop?
可以在此代码中将openMP用于并行循环吗? 我试过了,它显示'break'有错误。 谁能帮助我指导如何与openMP并行? 该代码的目的是为值生成算术表达式的可能置换。 例如5 + 5 + 1 = 11,可能还有更多的表达式可以得到11。
问题是我想将openMP用作Paralle ..但我不知道如何使其成为OpenMp和C ++的新手。
vector<string> solve(vector<int> question, vector<char> operands)
{
int targetAnswer = question.back(); //Get the final answer
question.pop_back(); //remove the final answer from question list
long int totalLoopingCount_Operands = pow(operands.size(),question.size()); // Calculate total looping numbers(operands)
bool isRedundantAnswer;
vector<string> answer;
sort(question.begin(), question.end());
do{
isRedundantAnswer = false;
vector<int> operationSequence;
//Fill up the operation sequence with first priority operands (int this case '*')
for (int i = 0; i < question.size(); i++) {
operationSequence.push_back(0);
}
//Start the answer seeking algorithm here
for (long int i = 0; i < totalLoopingCount_Operands-1; i++) {
if (operands[operationSequence[0]] == '*' || operands[operationSequence[0]] == '/') {
operationSequence[0]++;
continue;
}
string checkResult = checkAnswer(targetAnswer, operands, question, operationSequence); //Check the current equation
//check redundant answer
for (vector<string>::iterator it = answer.begin(); it != answer.end();it++) {
if (*it == checkResult) {
isRedundantAnswer = true;
}
}
//if result == -1, means this is not a solution
if (checkResult != "-1" && !isRedundantAnswer) {
answer.push_back(checkResult); //insert the answer into the list
}
//increment the operationSequence will change the equation
operationSequence[0]++;
for (int j = 0; j < question.size() - 1; j++) {
if (operationSequence[j] == operands.size()) {
operationSequence[j] = 0;
operationSequence[j + 1] ++;
}
}
if (operationSequence[i % (question.size() - 1)] == 5) {
cout << "error" << endl;
break;
}
}
} while (next_permutation(question.begin(),question.end()));
return answer;
}
在此解决方案中,错误是由foundError
布尔值控制的,该值会尽快被修改,并且会发现错误并阻止线程选择其他迭代来继续处理。 #pragma omp flush
确保对boolen变量的修改在修改后或读取之前立即发送到主内存。
这基于在http://www.thinkingparallel.com/2007/06/29/breaking-out-of-loops-in-openmp中讨论的解决方案。 还要注意,该解决方案需要详细研究哪些变量应在#pragma omp parallel for
私有化或保持共享,以#pragma omp parallel for
bool foundError = false;
#pragma omp parallel for
for (long int i = 0; i < totalLoopingCount_Operands-1; i++) {
#pragma omp flush(foundError)
if (!foundError)
{
if (operands[operationSequence[0]] == '*' || operands[operationSequence[0]] == '/') {
operationSequence[0]++;
continue;
}
string checkResult = checkAnswer(targetAnswer, operands, question, operationSequence); //Check the current equation
//check redundant answer
for (vector<string>::iterator it = answer.begin(); it != answer.end();it++) {
if (*it == checkResult) {
isRedundantAnswer = true;
}
}
//if result == -1, means this is not a solution
if (checkResult != "-1" && !isRedundantAnswer) {
answer.push_back(checkResult); //insert the answer into the list
}
//increment the operationSequence will change the equation
operationSequence[0]++;
for (int j = 0; j < question.size() - 1; j++) {
if (operationSequence[j] == operands.size()) {
operationSequence[j] = 0;
operationSequence[j + 1] ++;
}
}
if (operationSequence[i % (question.size() - 1)] == 5) {
foundError = true;
#pragma omp flush(foundError)
}
}
}
if (foundError) {
cout << "error" << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.