C++和CUDA:为什么代码每次返回不同的结果
C++ and CUDA: why does the code return different results each time?
更新:我发现了错误。由于我之前发布的代码非常复杂,我简化了它们,只在问题出现时保留部分。
if (number >= dim * num_points)
return;
但实际上,我只有num_points,我想使用num_point线程,所以正确的方法应该是
if (number >= num_points)
return;
谢谢大家的帮助。
我正在将一些C++代码从CPU重写到GPU。代码粘贴在下面。抱歉时间太长了,因为我认为用这种方式更容易发现问题。
在代码中,对于每个线程,我都需要一些矩阵格式的中间结果,所以我为这些中间结果分配设备内存,例如d_dir2、d_R、d_Stick、d_PStick。结果并不是我所期望的,所以为了调试,我尝试以这种方式输出一些中间结果R:
如果(k==0){结果[tmp_int1+i*dim+j]=R[tmp_int1+i*dim+j];}
以及以后在C++中,我打印结果。然而,我发现每次的结果都会给出不同的值。有时它给出正确答案R,有时,PStick的值,有时R和PStick组合,有时R与0的组合(结果在开始时初始化为0)。
我很困惑是什么引起了这个问题。知道吗?非常感谢:)
__global__ void stickvote(const int dim, const int num_points, const int gridx, float Sigma, float* input, float* dir2, float* R, float* Stick, float* PStick, float* results) {
float threshold = 4 * Sigma;
float c = (- 16 * log(0.1f) * (sqrt(Sigma) - 1)) / 3.1415926f / 3.1415926f;
int row = blockIdx.y * blockDim.y + threadIdx.y;
int col = blockIdx.x * blockDim.x + threadIdx.x;
int number = row * BLOCK_SIZE * gridx + col;
if (number >= dim * num_points) //// The bug is here!
return;
}
extern "C" void KernelStickVote(int dim, int num_points, float Sigma, float* input, float* results) {
const int totalpoints = num_points;
const int totalpoints_input = (dim + 1)* (dim + 1) * num_points;
const int totalpoints_output = dim * dim * num_points;
size_t size_input = totalpoints_input * sizeof(float);
size_t size_output = totalpoints_output * sizeof(float);
float* d_input;
cutilSafeCall(cudaMalloc((void**)&d_input, size_input));
float* d_result;
cutilSafeCall(cudaMalloc((void**)&d_result, size_output));
// used to save dir, and calculate dir * dir'
float* d_dir2;
cutilSafeCall(cudaMalloc((void**)&d_dir2, dim * num_points * sizeof(float)));
// used to save R: dim * dim * N
float* d_R;
cutilSafeCall(cudaMalloc((void**)&d_R, size_output));
// used to save Stick: dim * dim * N
float* d_Stick;
cutilSafeCall(cudaMalloc((void**)&d_Stick, size_output));
// used to save Stick: dim * dim * N
float* d_PStick;
cutilSafeCall(cudaMalloc((void**)&d_PStick, size_output));
// Copy input data from host to device
cudaMemcpy(d_input, input, size_input, cudaMemcpyHostToDevice);
int totalblock = (totalpoints % BLOCKPOINTS==0 ? totalpoints/BLOCKPOINTS : (int(totalpoints/BLOCKPOINTS) + 1));
int gridx = (65535 < totalblock ? 65535 : totalblock);
int gridy = (totalblock % gridx == 0 ? totalblock/gridx : (int(totalblock/gridx)+1) );
dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);
dim3 dimGrid(gridx, gridy);
stickvote<<<dimGrid, dimBlock>>>(dim, num_points, gridx, Sigma, d_input, d_dir2, d_R, d_Stick, d_PStick, d_result);
cudaMemcpy(results, d_result, size_output, cudaMemcpyDeviceToHost);
cudaFree(d_input);
cudaFree(d_result);
cudaFree(d_dir2);
cudaFree(d_R);
cudaFree(d_Stick);
cudaFree(d_PStick);
}
问题的原始发布者自己进行了一些进一步的代码简化和调试,发现内核中的guard语句:
if (number >= dim * num_points)
return;
事实上是不正确的,应该是
if (number >= num_points)
return;
这就是错误的来源。
此答案已作为社区wiki答案添加,目的是将此问题从未回答的队列中删除
相关文章:
- C++Brute Force攻击函数不会返回结果
- 如何在一个表达式中生成并返回结果?
- 使用std::tie进行类似golang的错误处理,同时返回结果,是否有缺点?(C++11)
- C++ 线程端回调并返回结果
- C++使用存储过程返回结果
- reinterpret_cast总是返回结果C++?
- 尝试以随机字符(长读取)打印字符串返回结果
- 通过C 中的参考返回结果
- 使用枚举确定返回结果的类型(使用宏的黑客)
- 返回结果不可见
- 打开一个 Shell 程序,向其传递参数,然后返回结果
- 如何在递归函数中计算返回结果
- 整数列表的交集-无法返回结果列表
- 来自 open() 的返回结果代码之间的差异:25 与 3
- 使用 c++11 线程返回结果的正确方法是什么
- 传递StringBuilder并从c++dll向c#返回结果
- 如何跳出一些深度递归的函数并直接返回结果
- 使用MPI临时接收数据,然后返回结果
- C++,我不知道为什么我的函数不返回结果
- QDialog show()后面跟着返回结果操作