I have this code in C++ that calculates the average of each column of a matrix. I want to parallelize the code using OpenMP.
#include <vector>
#include <cstdlib>
#include <chrono>
#include <iostream>
using namespace std;
vector<double> average(const vector<vector<unsigned char>>& original){
vector<vector<double>> result(original.size(), vector<double>(original[0].size()));
vector<double> average(original[0].size(), 0.0);
for (int i=0; i<original.size(); i++) {
const vector<unsigned char>& vector = original[i];
for (int k = 0; k < vector.size(); ++k) {
average[k] += vector[k];
}
}
for (double& val : average) {
val /= original.size();
}
return average;
}
Adding #pragma omp parallel for before the outer for loop gets me bogus results. Do you have any pointers? I thought I would find tons of examples of this online but wasn't able to find much. This is my first time using OpenMP.