OpenACC嵌套循环依赖性错误

时间:2017-01-26 23:55:00

标签: c++ parallel-processing cuda openacc pgi

拜托,我需要一些使用OpenACC并行计算模型(C ++)的帮助。问题如下:

vairables W,hbias,vbias之间存在依赖关系(它应该更新每次迭代)并且计算平均和支持函数内部的方法,但是通过在不可用的代码上使用OpenACC。因此,每个迭代函数都会得到W的偏见和偏差。请注意,如果并行化发生在较低级别,它将无法获益。这是代码:

    void RBM::contrastive_divergence(int ** train_X, double learning_rate, int k) {


    int * input = new int[n_visible];

    double *ph_mean = new double[n_hidden];
    int *ph_sample = new int[n_hidden]; // CALUCLATED WITHIN COMPLETE CODE
    double *nv_means = new double[n_visible];
    int *nv_samples = new int[n_visible]; //CALUCLATED WITHIN COMPLETE CODE
    double *nh_means = new double[n_hidden];
    int *nh_samples = new int[n_hidden]; //CALUCLATED WITHIN COMPLETE CODE


    #pragma acc parallel 
    {

    #pragma acc loop gang private(input[0:n_visible],ph_mean[0:n_hidden],ph_sample[0:n_hidden], \
    nv_means[0:n_visible], nv_samples[0:n_visible], nh_means[0:n_hidden], \
    nh_samples[0:n_hidden])

    for (int ii = 0; ii<train_N; ii++) {

    #pragma acc loop vector
    for (int j = 0; j< n_visible; j++)
    input[j] = train_X[ii][j];


    sample_h_given_v(input, ph_mean);

    sample_v_given_h(h0_sample, nv_means);
    sample_h_given_v(nv_samples, nh_means);

        #pragma acc loop vector
        for (int i = 0; i<n_hidden; i++) {
            for (int j = 0; j<n_visible; j++) {
            #pragma acc atomic update
            W[i][j] += learning_rate * (ph_mean[i] * input[j] - nh_means[i] * nv_samples[j]) / N;
            }
        #pragma acc atomic update
        hbias[i] += learning_rate * (ph_sample[i] - nh_means[i]) / N;
        }

        #pragma acc loop vector
        for (int i = 0; i<n_visible; i++) {
        #pragma acc atomic update
        vbias[i] += learning_rate * (input[i] - nv_samples[i]) / N;
        }

    }

    }

    delete[] input;
    delete[] ph_mean;
    delete[] ph_sample;
    delete[] nv_means;
    delete[] nv_samples;
    delete[] nh_means;
    delete[] nh_samples;

    }

    #pragma acc routine vector
    void RBM::sample_h_given_v(int *v0_sample, double *mean){

        #pragma acc loop vector
        for (int i = 0; i<n_hidden; i++) {
            mean[i] = propup(v0_sample, W[i], hbias[i]);
        }
    }




    #pragma acc routine vector
    void RBM::sample_v_given_h(int *h0_sample, double *mean){

            #pragma acc loop vector
            for (int i = 0; i < n_visible; i++) {
                mean[i] = propdown(h0_sample, i, vbias[i]);
            }
    }


    #pragma acc routine seq
    double RBM::propup(int *v, double *w, double b) {
    double pre_sigmoid_activation = 0.0;
    for (int j = 0; j<n_visible; j++) {
    pre_sigmoid_activation += w[j] * v[j];
    }
    pre_sigmoid_activation += b;

    double x;
    x = 1.0 / (1.0 + exp(-pre_sigmoid_activation));

    return x;
    }

    #pragma acc routine seq
    double RBM::propdown(int *h, int i, double b) {
    double pre_sigmoid_activation = 0.0;
    for (int j = 0; j<n_hidden; j++) {
    pre_sigmoid_activation += W[j][i] * h[j];
    }
    pre_sigmoid_activation += b;

    double x;
    x = 1.0 / (1.0 + exp(-pre_sigmoid_activation));
    return x;
    }

1 个答案:

答案 0 :(得分:1)

如何在“#pragma acc loop vector”中添加“independent”子句? 您在顶部使用“#pragma acc parallel”。 因此,您有责任表达依赖关系或确保结果的正确性。