gpt4 book ai didi

c++ - OpenACC 嵌套循环依赖错误

转载 作者:行者123 更新时间:2023-11-30 05:18:07 26 4
gpt4 key购买 nike

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

vairables W、hbias、vbias(它应该得到每次迭代的更新)和 propup 和 propdown 函数内部的计算方法之间存在依赖关系,但是通过在不可用的代码上使用 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;
}

最佳答案

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

关于c++ - OpenACC 嵌套循环依赖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41885101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com