For the following code, I only want to parallelize its last part which calculates the second norm of each vector (length of each vector is different) but I am getting an error of segmentation fault. Also, I am not sure whether I am using reduction for the sum in the right place or not.
Another point is that I think I only need to parallelize the outer loop and there is no need to do this for an inner loop. Right?
#include <iostream>
#include <vector>
#include <random>
#include <cmath>
#include <omp.h>
#include <fstream>
#include <cfloat>
#include <chrono>
using namespace std;
int main()
{
int N = 1000000;
unsigned size;
vector<vector<double>> c;
default_random_engine g(0);
uniform_real_distribution<double> d(0.0f, nextafter(1.0f, DBL_MAX));
vector<double> b;
for (int i = 0; i < N; i++) {
size = pow(10, i % 4);
vector<double> a;
for (int j = 0; j < size; j++) {
double number = d(g);
a.push_back(number);
}
c.push_back(a);
}
int i, j;
double sum;
#pragma omp parallel for num_threads(4) shared(N) private(i,j,c,b) reduction (+: sum)
for (int i = 0; i <N ; i++) {
double sum = 0;
for (int j = 0; j < c[i].size();j++) {
sum = sum + pow(c[i][j],2);
}
double n = sqrt(sum);
b.push_back(n);
}
}