OPENMP Parallel For Problem with saving data on a vector at a specific position

I'm trying to save the results from "sum" variable at the specific position "i" in a vector, but the code isn't working. The code aims to save the result of calculating the vertex distances of a graph (which was done using BFS) in an output file, containing the value obtained for each of the graph's nodes. The BFS function returns "dist". The code is below:

  double sum;
  vector<double>sum2;
  
  #pragma omp parallel for num_threads(8) reduction(+:sum) shared(sum2)
   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 < dist.size(); ++j) {
      if (dist[j] > 0)
        distinv[j] = 1 / dist[j];
      else
        distinv[j] = 0;
    }
    sum = accumulate(distinv.cbegin(), distinv.cend(), 0.0);
    vector<double>::iterator iter = sum2.begin() + i;
    sum2.insert(iter, sum);

  }
  
  for (size_t i = 0; i <= N; ++i) {
    outFile << fixed << setprecision(6) << sum2[i] / N << "\n";
  }
  
  outFile.close(); 


Read more here: https://stackoverflow.com/questions/64961672/openmp-parallel-for-problem-with-saving-data-on-a-vector-at-a-specific-position

Content Attribution

This content was originally published by Matheus Fogliatto at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: