I'm interested in parallelize a for loop using OpenMP where global std vectors are involved.
In this specific example, the order in which the operation vec[1] = vec[1] + i
is executed doesn't matter however the results appears to be wrong sometimes due to race conditions. What is the proper way to handle this?
The REDUCTION clause seems not working for containers.
#include <iostream>
#include <vector>
std::vector<double> vec = {1,1};
void func(int i){
vec[1] = vec[1] + i;
}
int main(int argc, char *argv[]) {
int k;
#pragma omp parallel for
for(k=1; k<10; k++){
func(k);
}
std::cout << vec[0] << ", " << vec[1] << std::endl;
}
Aucun commentaire:
Enregistrer un commentaire