Search code examples
g++openmp

OpenMP with Segmentation fault (core dumped)


I encountered a problem when using OpenMP to parallelize my code. I have attached the simplest code that can reproduce my problem.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n = 10;
    int size = 1;

    vector<double> vec(1, double(1.0));
    double sum = 0.0;

#pragma omp parallel for private(vec) reduction(+: sum)
    for (int i = 0; i != n; ++i)
    {
        /* in real case, complex operations applied on vec here */

        sum += vec[0];
    }

    cout << "sum: " << sum << endl;

    return 0;
}

I compile with g++ with flag -fopenmp, and the error message from g++ prompts "Segmentation fault (core dumped)". I am wondering what's wrong with the code.

Note that vec should be set to private since in the real code a complex operation is applied on vec in the for-loop.


Solution

  • The problem indeed comes from the private(vec) clause. There are two issues with this code.

    First, from a semantics perspective, the private(vec) should be shared(vec), as the intent seems to be to work on the same std::vector instance in parallel. So, the code should look like this:

    #pragma omp parallel for shared(vec), reduction(+: sum)
    for (int i = 0; i != n; ++i)
    {
        sum += vec[0];
    }
    

    In the previous code, the private(vec) made a private instance of std::vector for each thread and was supposed to initialize these instances by calling the default constructor of std::vector.

    Second, the segmentation fault then arises from the fact that there's no vec[0] element in any of the private instances. This can be confirmed by calling vec.size() fro the threads.

    PS: shared(vec) would be been the default sharing for vec as per the OpenMP specification anyways.