[英]Using the OpenMP threadprivate directive on static instances of C++ STL types
[英]openMP C++ error with threadprivate vector
#include<iostream>
#include<vector>
#include<ctime>
#include<cmath>
#include<omp.h>
#include<array>
using namespace std;
double counter = 0;
vector<int> vec;
vector<double> reduction;
#pragma omp threadprivate(reduction)
vector<double> eet;
int fish;
void mario( int current_node, int current_depth) {
int q = current_node % 4;
#pragma omp simd
for( int i = 3 ; i < current_node; i+=4){
reduction[i]+=4;
mario(i, current_depth);
mario(i - 1,current_depth);
mario(i - 2,current_depth);
mario(i - 3,current_depth);
}
#pragma omp simd
for(int x = 1; x <= q; x++){
reduction[0]++;
mario(current_node - x,current_depth);
}
#pragma omp task firstprivate(current_node,current_depth)
{
if(current_depth > 0){
int new_depth = current_depth - 1;
#pragma omp simd
for(int i = current_node;i <= vec[current_node];i++){
reduction[i]++;
mario(i + 1,new_depth);
}
}
}
}
int main() {
omp_proc_bind_true;
omp_set_dynamic(0);
int nodes;
int timesteps;
int threadz;
cout << "enter number of nodes" << endl;
cin >> nodes;
cout << "enter number of timesteps" << endl;
cin >> timesteps;
cout << "enter number threads" << endl;
cin >> threadz;
omp_set_num_threads(threadz);
int mushroom = nodes - 2;
fish = nodes - 1;
vec.assign( nodes, mushroom );
clock_t t = clock();
vector<double> zed(mushroom + 1 , 0 );
eet = zed;
reduction = eet;
#pragma omp parallel copyin(reduction)
{
#pragma omp single
{
mario(nodes - 1, timesteps - 1);
}
#pragma omp critical
{
#pragma omp simd
for(int x = 0; x < zed.size();x++){
eet[x] += reduction[x];
}
}
#pragma omp barrier
}
for(int j = 0; j < eet.size(); j++){
counter += eet[j];
}
t = clock() - t;
double time_taken = ((double)t) / CLOCKS_PER_SEC;
cout << "mario took " << fixed << counter << " steps" << endl;
cout << "It took him " << time_taken << " seconds" << endl;
return 0;
}
在我的代碼中,我聲明了向量縮減: vector<double> reduction;
然后我立即使用 threadprivate: #pragma omp threadprivate(reduction)
,但是當我編譯我的代碼時,我得到了錯誤。
錯誤:第一次使用后“reduction”聲明為“threadprivate”
13 | #pragma omp 線程私有(減少)
錯誤:對於“copyin”,“reduction”必須是“threadprivate”
82 | #pragma omp 並行復制(減少)
是什么導致了這些錯誤? 我只是聲明了變量而沒有做任何其他事情。
錯誤是可以避免的嗎?
是否有任何其他方法可以減少遞歸函數的 openMP 任務
或解決問題的替代方法?
我嘗試編寫代碼的方式是否存在根本性錯誤?
我正在使用 gcc 9.3
,抱歉,如果我的問題不清楚,請提出改進建議。
僅適用於 gcc 的解決方案的一個組成部分,不幸的是,這只是一種解決方法,而不是適當的解決方案。
#include<iostream>
#include<vector>
#include<ctime>
#include<cmath>
#include<omp.h>
#include<array>
#include<thread>
using namespace std;
double counter = 0;
vector<int> vec;
thread_local vector<double> reduction;
vector<double> eet;
int fish;
void mario( int current_node, int current_depth) {
int q = current_node % 4;
#pragma omp simd
for( int i = 3 ; i < current_node; i+=4){
reduction[i]+=4;
mario(i, current_depth);
mario(i - 1,current_depth);
mario(i - 2,current_depth);
mario(i - 3,current_depth);
}
#pragma omp simd
for(int x = 1; x <= q; x++){
reduction[0]++;
mario(current_node - x,current_depth);
}
#pragma omp task firstprivate(current_node,current_depth)
{
if(current_depth > 0){
int new_depth = current_depth - 1;
#pragma omp simd
for(int i = current_node;i <= vec[current_node];i++){
reduction[i]++;
mario(i + 1,new_depth);
}
}
}
}
int main() {
omp_proc_bind_true;
omp_set_dynamic(0);
int nodes;
int timesteps;
int threadz;
cout << "enter number of nodes" << endl;
cin >> nodes;
cout << "enter number of timesteps" << endl;
cin >> timesteps;
cout << "enter number threads" << endl;
cin >> threadz;
omp_set_num_threads(threadz);
int mushroom = nodes - 2;
fish = nodes - 1;
vec.assign( nodes, mushroom );
clock_t t = clock();
vector<double> zed(mushroom + 1 , 0 );
eet = zed;
#pragma omp parallel
{
reduction = zed;
#pragma omp barrier
#pragma omp single
{
mario(nodes - 1, timesteps - 1);
}
#pragma omp critical
{
for(int x = 0; x < reduction.size();x++){
eet[x] += reduction[x];
}
}
#pragma omp barrier
}
for(int j = 0; j < eet.size(); j++){
counter += eet[j];
}
t = clock() - t;
double time_taken = ((double)t) / CLOCKS_PER_SEC;
cout << "mario took " << fixed << counter << " steps" << endl;
cout << "It took him " << time_taken << " seconds" << endl;
return 0;
}
這是一種很糟糕的方法,顯然 gcc 的 c++ 實現使用 POSIX 線程,而 openMP 的 gcc 實現只是 POSIX 底層。 來源: 將 C++11 thread_local 與其他並行庫一起使用
因此,對於 gcc,如果以這種方式將變量發送到線程本地存儲,則它等效於 openMP 的“threadprivate”變量。
請其他人提出適當的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.