SirF
SirF

Reputation: 1

OPENMP Parallel Problem Error for Double Loop

I was getting the error: "free(): corrupted unsorted chunks" when trying to run:

#pragma omp parallel for reduction(+:save) shared(save2)
   for (size_t i = 0; i <= N; ++i) {
    vector<float> dist = cdist(i, arestas);
    vector<float> distinv(dist.size());

    for (size_t j = 0; j < N(); ++j) {
      if (arr[j] > 0)
        arrv[j] = (1/N) + (1 / arr[j]);
      else
        arrv[j] = 0;
    }
    save = accumulate(arrv.begin(), arrv.end(), 0.0);
    vector<double>::iterator iter = save2.begin() + i;
    save2.insert(iter, sum);
  }

Upvotes: 0

Views: 103

Answers (1)

Gilles
Gilles

Reputation: 9519

I might miss the point here, but what about just doing it this way (not tested)?

vector<double> sum2(N);

#pragma omp parallel for num_threads(8)
for ( size_t i = 0; i < N; i++ ) {
    double sum = 0;
    for ( size_t j = 0; j < dist.size(); ++j ) {
        if ( dist[j] > 0 ) {
            sum += 1. / dist[j];
        }
    }
    sum2[i] = sum;
}

There is still some room for improving this version (by removing the if statement for example, in order to help the vectorization), but unless you had some unexplained constrains in your code, I think this version is a good starting point.

Upvotes: 2

Related Questions