在CUDA动态并行执行父内核的其余部分之前,我如何等待子内核在父内核中完成?



所以我需要runParatron子程序在for循环的下一次迭代发生之前完全完成。根据我得到的结果,我很确定这不会发生。例如,我在runParatron中有一个print语句,在第一个"["在for循环外打印。我尝试运行cudadevicesynchrize,但它不会编译,说明主机代码不能在设备代码上执行,并且cudadevicesynchrize在设备代码中未定义。有什么办法可以等到孩子们的玉米粒完成吗?我看到其他的帖子,例子和教程使用cudadevicesynize内核,所以也许我错过了一些基本的?非常感谢您的帮助。

__global__ void runMLP(double* x, double* outputs, double* weights, activation_function* A_Fs, int*     CIL, int layers, int bias, int* WLO, int* OLO) {
if (CIL[0] > 511) {
copyElements << <CIL[0] / 32, 32 >> > (outputs, x, CIL[0]);
//I WOULD ALSO LIKE TO WAIT HERE
}
else
for (int i = 0;i < CIL[0];i++) {
outputs[i] = x[i];
}
for (int i = 1;i < layers;i++) {
printf("----------------------Layer %d :: InputSize %d :: Layer weight offset %d ::     Layer output offset %d----------------------n", i, CIL[i-1], WLO[i-1], OLO[i]);
runParatron << < (CIL[i] / 32) + 1, 32 >> > (outputs + OLO[i - 1], outputs +     OLO[i], weights + WLO[i - 1], A_Fs[i], CIL[i - 1], CIL[i], bias);
//cudaDeviceSynchronize(); //THIS IS WHERE I NEED TO WAIT UNTIL NEXT ITERATION
}
if (A_Fs[layers - 1] == SOFTMAX) {
double* temp = outputs + OLO[layers - 1];
printf("[");
for (int i = 0;i < CIL[layers-1];i++) {
printf("% d, ", temp[i]);
}
printf("]n");
double denom = 0;
for (int i = 0;i < CIL[layers - 1];i++) {
denom += temp[i];
}
if (denom < DBL_MIN)
denom = DBL_MIN;
for (int i = 0;i < CIL[layers - 1];i++) {
temp[i] /= denom;
}
}
}

例如,下面的输出是"["出现在子内核输出之前:

//All Cell: starting lines are produced from child kernel
[Cell: 0 :: weightOffset 0 :: AF 2 //As you can see, there is the "[" here when it should be
Cell: 1 :: weightOffset 6 :: AF 2
Cell: 2 :: weightOffset 12 :: AF 2
Cell: 3 :: weightOffset 18 :: AF 2
-502657059,  2118981138,  1645236453, ] //Down here!

所以我添加了一个原子计数器,并在每个子内核的末尾加1。然后,我在子内核调用检查之后放置一个while循环,以查看计数器是否已达到我想要完成的调用数量。这就解决了问题。如果有人需要代码或澄清,请告诉我。