如何在此 for 循环中并行使用 openMP for 循环?

How can I use openMP for loop parallel in this for loop?

是否可以在此代码中使用 openMP for loop parallel?我试过了,它显示 'break' 有错误。谁能帮我指导如何使它与 openMP 并行? 此代码的目的是为一个值生成算术表达式的可能排列。例如5+5+1=11 可能还有很多表达式可以得到 11.
问题是我想使用 openMP 来并行..但我不知道如何实现它,因为我是 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

中保持共享
    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;
    }