【发布时间】:2022-11-12 09:07:48
【问题描述】:
目前我在这里有这组代码:
double * currentPlate;
const int innerSize = interiorX * interiorY * sizeof(double);
// creating a matrix with cuda on the GPU
cudaError_t error = cudaMallocManaged(¤tPlate, innerSize);
double * newPlate;
fprintf(stderr, "cudaMatrix returned: (error code %s)!\n",
cudaGetErrorString(error));
cudaError_t error2 = cudaMallocManaged(&newPlate, innerSize);
fprintf(stderr, "cudaMatrix_X returned: (error code %s)!\n", cudaGetErrorString(error2));
error = cudaMallocManaged(¤tPlate, innerSize);
fprintf(stderr, "cudaMatrix returned: (error code %s)!\n", cudaGetErrorString(error));
initializePlateTemp(currentPlate, interiorX);
initializePlateTemp(newPlate, interiorX);
// timer to be outputed to terminal
float time;
// begin running the cuda events
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start, 0);
int dev = 0;
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, dev);
int numThreads = deviceProp.maxThreadsPerBlock;
int blockSize = (((interiorX * interiorY) + numThreads - 1) / numThreads);
for (int i = 0; i < I; i++)
{
iterateTemp << <blockSize, numThreads >> > (currentPlate, newPlate, interiorX);
cudaDeviceSynchronize(); // wait for GPU threads to finish
error=cudaMemcpy(currentPlate, newPlate, innerSize, cudaMemcpyDeviceToDevice);
}
fprintf(stderr, "cudaMatrix returned: (error code %s)!\n", cudaGetErrorString(error));
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&time, start, stop);
std::cout.precision(3);
// output the time to the console
std::cout << "Time: " << time << "ms" << std::fixed << std::endl;
我的问题是,如果我将currentPlate 和newPlate 的结果写入文件,它们看起来完全一样。
我认为问题出在函数iterateTemp 上,但我已经在纸上解决了问题,我认为数学本身没有问题。
该代码是:
__global__ void iterateTemp(double* H, double* Q, int n)
{
int num = blockIdx.x * blockDim.x + threadIdx.x;
int row = num % n;
int col = num / n;
if (num < (n * n) && (col > 0 && col < n - 1) && (row > 0 && row < n - 1))
{
Q[n * row + col] = 0.25 * (H[n * (row - 1) + col] + H[n * (row + 1) + col] + H[n * row + (col - 1)] + H[n * row + (col + 1)]);
}
}
我认为可能发生的情况是结果实际上并未正确复制到新矩阵中,但我不确定为什么会这样。我对使用 cuda 库很陌生,但我认为我使用 blockSize、numThreads 对函数进行了正确的调用。
我该如何解决?
【问题讨论】: