【发布时间】:2021-08-19 06:06:39
【问题描述】:
我有以下想要并行的串行代码。我理解在嵌套循环中使用 collapse 子句时,在 for(i) 循环之前和之后不要有代码是很重要的,因为这是不允许的。那么我如何将嵌套的 for 循环与这样的 if 语句并行:
void foo2D(double Lx, double Ly, double KX[], double KY[], double ksqu[], double ninvksqu[], int FourierMeshType){
// Make a variable to account for way wavenumbers are set in FFTW3.
int k_counter = 0;
// kx corresponds to modes [0:n/2-1 , -n/2:-1]. This is why there is an additional step, just due to FFTW3's structuring
#pragma omp parallel for collapse(2)
for(int i = 0; i < nx ; i++){
for(int j = 0; j < nyk; j++){
if (i < nx/2){ // i<nx/2 --> 0 to 127
KX[j + nyk*i] =2.*M_PI*i/Lx; //K = 2pi/L* [0:nx/2-1 , -n/2:-1]' : creates a column vector with nx/2 elements starting from 0 ( from 0 to 127 then -128 to -1) 256/2 is 128
}
if( i >= nx/2){ // i >= nx/2 --> from -128 to -1
KX[j + nyk*i] = 2.* M_PI * (-i + 2.*k_counter) / Lx;
}
}
if( i >= nx/2){ // error here
k_counter++;
}
}
}
其中 nx, ny, nyk定义为:
static const int nx = 256;
static const int ny = 256;
static const int nyk = ny/2 + 1;
有没有更好的方法来重写这个?
【问题讨论】:
标签: c++ multithreading parallel-processing openmp nested-loops