私はOpenMPを使ってforループを並列化しています。私はスレッドIDでC++ Armadilloベクトルにアクセスしようとしていますが、別のスレッドがメモリの別々の領域にアクセスしたとしても、クリティカルセクションにアクセスする必要があるのでしょうか?/OpenMP:共有変数にスレッドIDでアクセスするときにクリティカルセクションが必要です
#include <armadillo>
#include <omp.h>
#include <iostream>
int main()
{
arma::mat A = arma::randu<arma::mat>(1000,700);
arma::rowvec point = A.row(0);
arma::vec distances = arma::zeros(omp_get_max_threads());
#pragma omp parallel shared(A,point,distances)
{
arma::vec local_distances = arma::zeros(omp_get_num_threads());
int thread_id = omp_get_thread_num();
for(unsigned int l = 0; l < A.n_rows; l++){
double temp = arma::norm(A.row(l) - point,2);
if(temp > local_distances[thread_id])
local_distances[thread_id] = temp;
}
// Is it necessary to put a critical section here?
#pragma omp critical
if(local_distances[thread_id] > distances[thread_id]){
distances[thread_id] = local_distances[thread_id];
}
}
std::cout << distances[distances.index_max()] << std::endl;
}
が、それは読んで置くことが必要である私の場合distances
ベクトルへの書き込み: は、これは私のコードですか?
これは重要なセクションを必要としません。 – subzero