Error checking (and adding a print) after each CUDA call makes the code difficult to read. For example, here is the code to initialize 4 variables on the GPU with error handling:
int *d_x, *d_y, *d_x_mean, *d_y_mean;
cudaError_t err;
cudaMalloc((void**)&d_x, N * sizeof(int));
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("1CUDA error: %sn", cudaGetErrorString(err));
}
cudaMalloc((void**)&d_y, N * sizeof(int));
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("2CUDA error: %sn", cudaGetErrorString(err));
}
cudaMalloc((void**)&d_x_mean, sizeof(int));
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("3CUDA error: %sn", cudaGetErrorString(err));
}
cudaMalloc((void**)&d_y_mean, sizeof(int));
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("4CUDA error: %sn", cudaGetErrorString(err));
}
cudaMemcpy(d_x, h_x, N * sizeof(int), cudaMemcpyHostToDevice);
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("5CUDA error: %sn", cudaGetErrorString(err));
}
cudaMemcpy(d_y, h_y, N * sizeof(int), cudaMemcpyHostToDevice);
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("6CUDA error: %sn", cudaGetErrorString(err));
}
cudaMemset(d_x_mean, 0, sizeof(int));
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("7CUDA error: %sn", cudaGetErrorString(err));
}
cudaMemset(d_y_mean, 0, sizeof(int));
err = cudaGetLastError();
if (err != cudaSuccess) {
printf("8CUDA error: %sn", cudaGetErrorString(err));
}
vs here is what it looks like without error checking at each call:
int *d_x, *d_y, *d_x_mean, *d_y_mean;
cudaMalloc((void**)&d_x, N * sizeof(int));
cudaMalloc((void**)&d_y, N * sizeof(int));
cudaMalloc((void**)&d_x_mean, sizeof(int));
cudaMalloc((void**)&d_y_mean, sizeof(int));
cudaMemcpy(d_x, h_x, N * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_y, h_y, N * sizeof(int), cudaMemcpyHostToDevice);
cudaMemset(d_x_mean, 0, sizeof(int));
cudaMemset(d_y_mean, 0, sizeof(int));
Is there a way to add proper error checking without having it clutter the code?
While looking at the CUDA samples, I found that this is the way they handle errors:
checkCudaErrors(cudaMallocHost((void **)&a, nbytes));
This requires the CUDA Common library which is not considered production ready.
Tyler Hilbert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.