简体   繁体   English

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

[英]How can I use openMP for loop parallel in this for loop?

Is it possible to use openMP for loop parallel here in this code? 可以在此代码中将openMP用于并行循环吗? I tried and it shows 'break' has error. 我试过了,它显示'break'有错误。 can anyone help me guide how to make this parallel with openMP? 谁能帮助我指导如何与openMP并行? The purpose of this code is to generate possible permutations of arithmetic expressions for a value. 该代码的目的是为值生成算术表达式的可能置换。 eg 5+5+1=11 and there may have many more expressions to get 11. 例如5 + 5 + 1 = 11,可能还有更多的表达式可以得到11。
The problem is I want to use openMP to be paralle..but I don't know how can make it coz I am newbie in openMp and C++. 问题是我想将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;
}

In this solution the error is controlled by the foundError boolean which is modified ASAP the error is found and prevents threads picking other iterations to continue processing. 在此解决方案中,错误是由foundError布尔值控制的,该值会尽快被修改,并且会发现错误并阻止线程选择其他迭代来继续处理。 The #pragma omp flush ensures that the modifications to the boolen variable is sent to main memory as soon as it is modified or before reading. #pragma omp flush确保对boolen变量的修改在修改后或读取之前立即发送到主内存。

This is based from the solution discussed in http://www.thinkingparallel.com/2007/06/29/breaking-out-of-loops-in-openmp . 这基于在http://www.thinkingparallel.com/2007/06/29/breaking-out-of-loops-in-openmp中讨论的解决方案。 Notice also, that the solution needs a detailed study of which variables should be privatized or kept shared in the #pragma omp parallel for 还要注意,该解决方案需要详细研究哪些变量应在#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM