首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >线程私有向量出现openMP C++错误

线程私有向量出现openMP C++错误
EN

Stack Overflow用户
提问于 2020-04-13 04:55:37
回答 1查看 178关注 0票数 0
代码语言:javascript
复制
#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 |#杂注omp threadprivate(reduction)

错误:“复制”的“reduction”必须是“threadprivate”

82 |#杂注omp并行复制(缩减)

是什么导致了这些错误?我只是声明了这个变量,没有做其他任何事情。

错误是可以避免的吗?

是否有其他方法可以跨递归函数的openMP任务进行缩减

或者解决这个问题的替代方法?

我尝试编写代码的方式是否存在根本性的错误?

我用的是gcc 9.3

,抱歉,如果我的问题不清楚,请提出改进建议。

EN

回答 1

Stack Overflow用户

发布于 2020-04-14 05:18:49

这是一种只能与gcc一起使用的解决方案,不幸的是,这只是一种变通办法,而不是一个适当的解决方案。

代码语言:javascript
复制
#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线程,而gcc的openMP实现只是POSIX的幕后黑手。来源:Using C++11 thread_local with other parallel libraries

因此,对于gcc来说,如果一个变量以这种方式被发送到线程本地存储,那么它就相当于openMP的一个'threadprivate‘变量。

其他人请建议一个适当的解决方案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61178038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档