首先,我认为talmonmies在回答你提到的上一个问题时,并没有打算让那个代码代表良好的编程。因此,想要将其扩展到三维可能不是你时间的最佳利用方式。例如,为什么我们要编写只使用一个线程的程序?虽然这种内核可能有合法的用途,但这不是其中之一。你的内核有可能进行大量独立的工作并行处理,但你却强制将它们全部放在一个线程上,从而使其串行化。并行工作的定义如下:
a[i][j][k]=i+j+k;
让我们想办法在GPU上并行处理它。
我还想提出另一个入门观察,由于我们正在处理已知大小的问题,因此让我们使用C语言来尽可能地从该语言中获得好处。在某些情况下,可能需要嵌套循环以执行cudaMalloc,但我不认为这是其中之一。
以下是一个可以并行完成工作的代码:
#include <stdio.h>
#include <stdlib.h>
#define DATAXSIZE 100
#define DATAYSIZE 100
#define DATAZSIZE 20
#define BLKXSIZE 32
#define BLKYSIZE 4
#define BLKZSIZE 4
#define cudaCheckErrors(msg) \
do { \
cudaError_t __err = cudaGetLastError(); \
if (__err != cudaSuccess) { \
fprintf(stderr, "Fatal error: %s (%s at %s:%d)\n", \
msg, cudaGetErrorString(__err), \
__FILE__, __LINE__); \
fprintf(stderr, "*** FAILED - ABORTING\n"); \
return 1; \
} \
} while (0)
__global__ void set(int a[][DATAYSIZE][DATAXSIZE])
{
unsigned idx = blockIdx.x*blockDim.x + threadIdx.x;
unsigned idy = blockIdx.y*blockDim.y + threadIdx.y;
unsigned idz = blockIdx.z*blockDim.z + threadIdx.z;
if ((idx < (DATAXSIZE)) && (idy < (DATAYSIZE)) && (idz < (DATAZSIZE))){
a[idz][idy][idx] = idz+idy+idx;
}
}
int main(int argc, char *argv[])
{
typedef int nRarray[DATAYSIZE][DATAXSIZE];
const dim3 blockSize(BLKXSIZE, BLKYSIZE, BLKZSIZE);
const dim3 gridSize(((DATAXSIZE+BLKXSIZE-1)/BLKXSIZE), ((DATAYSIZE+BLKYSIZE-1)/BLKYSIZE), ((DATAZSIZE+BLKZSIZE-1)/BLKZSIZE));
const int nx = DATAXSIZE;
const int ny = DATAYSIZE;
const int nz = DATAZSIZE;
nRarray *c;
nRarray *d_c;
if ((c = (nRarray *)malloc((nx*ny*nz)*sizeof(int))) == 0) {fprintf(stderr,"malloc1 Fail \n"); return 1;}
cudaMalloc((void **) &d_c, (nx*ny*nz)*sizeof(int));
cudaCheckErrors("Failed to allocate device buffer");
set<<<gridSize,blockSize>>>(d_c);
cudaCheckErrors("Kernel launch failure");
cudaMemcpy(c, d_c, ((nx*ny*nz)*sizeof(int)), cudaMemcpyDeviceToHost);
cudaCheckErrors("CUDA memcpy failure");
for (unsigned i=0; i<nz; i++)
for (unsigned j=0; j<ny; j++)
for (unsigned k=0; k<nx; k++)
if (c[i][j][k] != (i+j+k)) {
printf("Mismatch at x= %d, y= %d, z= %d Host= %d, Device = %d\n", i, j, k, (i+j+k), c[i][j][k]);
return 1;
}
printf("Results check!\n");
free(c);
cudaFree(d_c);
cudaCheckErrors("cudaFree fail");
return 0;
}
由于您在评论中要求,这里是我能够使您的代码运行所做的最少更改数量。让我们也回想一下talonmies在您参考的上一个问题中的一些评论:
"出于代码复杂性和性能原因,您真的不希望这样做,在CUDA代码中使用指针数组比使用线性内存更加困难且更慢。"
"与使用线性内存相比,这是一个非常糟糕的想法。"
我不得不在纸上绘制图表,以确保我正确地复制了所有指针。
#include <cstdio>
inline void GPUassert(cudaError_t code, char * file, int line, bool Abort=true)
{
if (code != 0) {
fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code),file,line);
if (Abort) exit(code);
}
}
#define GPUerrchk(ans) { GPUassert((ans), __FILE__, __LINE__); }
__global__ void doSmth(int*** a) {
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
for(int k=0; k<2; k++)
a[i][j][k]=i+j+k;
}
int main() {
int*** h_c = (int***) malloc(2*sizeof(int**));
for(int i=0; i<2; i++) {
h_c[i] = (int**) malloc(2*sizeof(int*));
for(int j=0; j<2; j++)
GPUerrchk(cudaMalloc((void**)&h_c[i][j],2*sizeof(int)));
}
int ***h_c1 = (int ***) malloc(2*sizeof(int **));
for (int i=0; i<2; i++){
GPUerrchk(cudaMalloc((void***)&(h_c1[i]), 2*sizeof(int*)));
GPUerrchk(cudaMemcpy(h_c1[i], h_c[i], 2*sizeof(int*), cudaMemcpyHostToDevice));
}
int*** d_c;
GPUerrchk(cudaMalloc((void****)&d_c,2*sizeof(int**)));
GPUerrchk(cudaMemcpy(d_c,h_c1,2*sizeof(int**),cudaMemcpyHostToDevice));
doSmth<<<1,1>>>(d_c);
GPUerrchk(cudaPeekAtLastError());
int res[2][2][2];
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
GPUerrchk(cudaMemcpy(&res[i][j][0], h_c[i][j],2*sizeof(int),cudaMemcpyDeviceToHost));
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
for(int k=0; k<2; k++)
printf("[%d][%d][%d]=%d\n",i,j,k,res[i][j][k]);
}
简单来说,我们需要执行以下一系列步骤:
- 在主机上分配一个多维指针数组,比问题规模少一维,最后一维是指向cudaMalloc分配在设备上的区域的指针集合。
- 创建另一个多维指针数组,与前一步所创建的类别相同,但比前一步少一维。该数组必须也将其最终排名cudaMalloc分配在设备上。
- 将第二步中最后一组主机指针复制到前一步中cudaMalloc在设备上的区域中。
- 重复执行2-3步,直到最终得到一个指向多维指针数组的单个(主机)指针,这些指针现在都驻留在设备上。