1

I got the following output with error message while executing the program that uses openmp in VS2008 C++

thread number: 0

thread number: 1

Fatal User Error 1002: 'for' loop executed with inconsistent parameters between threads

My program does the following:

    omp_set_dynamic(0);
    omp_set_num_threads(2);

    int i = 0;
    int start_pos   = 0;
    int end_pos     = 0;

#pragma omp parallel default(none) private(i, start_pos, end_pos)
    {
        int nThreadNum = omp_get_thread_num();
        printf("thread number: %d\n", nThreadNum);
        start_pos   = 0;
        end_pos     = (number_of_model_points / 2 + 1);
        if (nThreadNum != 0) {
            start_pos   = (number_of_model_points / 2) + 1;
            end_pos     = (number_of_model_points);
        }

#pragma omp for
        for(i = start_pos; i < end_pos; i++) {
                      ...some code here...
        }
}

What is the problem here? Please correct me if I made a mistake.

2 Answers 2

1

So why there is an additional pragma for the for loop? You already did fork your processes. If you really want to fork again (don't do that, please!) then "i" needs to be set private again. However I think you just would want to remove the "omp for" pragma.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the right answer for the wrong reason. You normally do want to do a #pragma omp for inside the #pragma omp parallel to take advantage of work sharing - the omp for will split up the loop for you, otherwise you'd have all the threads (not processes) doing the same loop. But, here you have already done the work of manually breaking up the loop yourself; sometimes that's advantageous, although I don't see the benefit here. So you do want all the threads to do the same for loop, with each thread doing it's own region. So you don't want the omp for.
0

Jonathan Dursi is right, so in summary, for loop bounding parameters can not be private. In your example, for(i = start_pos; i < end_pos; i++), start_pos and end_pos are private per omp thread, hence consistency is not guaranteed and you get error 1002. Otherwise you can definitely have #pragma omp for inside #pragma omp parallel.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.