Ich muss Berechnungen anstellen: A[x][y] = Summe{von z=0 bis z=n}{B[x][y][z]+C[x][y][z]}, wobei Matrix A die Dimensionen [Höhe][Breite] und Matrix B,C die Dimensionen [Höhe][Breite][n] hat.
Die Werte werden dem Speicher etwa mit der folgenden Formel zugeordnet:
index = 0;
for (z = 0; z<n; ++z)
for(y = 0; y<width; ++y)
for(x = 0; x<height; ++x) {
matrix[index] = value;
index++;
}
Q1: Ist dieser Cuda-Kernel in Ordnung?
idx = blockIdx.x*blockDim.x + threadIdx.x;
idy = blockIdx.y*blockDim.y + threadIdx.y;
for(z=0; z<n; z++){
A[idx*width+idy] += B[idx*width+idy+z*width*height] + C[idx*width+idy+z*width*height];
}
F2: Kann die Berechnung so schneller durchgeführt werden?
idx = blockIdx.x*blockDim.x + threadIdx.x;
idy = blockIdx.y*blockDim.y + threadIdx.y;
idz = blockIdx.z*blockDim.z + threadIdx.z;
int stride_x = blockDim.x * gridDim.x;
int stride_y = blockDim.y * gridDim.y;
int stride_z = blockDim.z * gridDim.z;
while ( idx < height && idy < width && idz < n ) {
atomicAdd( &(A[idx*width+idy]), B[idx*width+idy+idz*width*height] + C[idx*width+idy+idz*width*height] );
idx += stride_x;
idy += stride_y;
idz += stride_z;
}