我正在创建一个测试程序,它将创建一个设备和一个大小为n的主机数组,然后启动一个内核,该内核创建n 个线程,将常量值 0.95f 分配给设备数组中的每个位置。完成后,将设备数组复制到主机数组,并对所有条目进行总计,并显示最终总计。
下面的程序似乎可以很好地处理大约 6000 万浮点数的数组,并很快返回正确的结果,但是在达到 7000 万时,程序似乎会挂起一段时间,并最终返回总数的 NAN 结果。在运行 6000 万次后检查主机阵列显示它正确填充了 0.95f,但在运行 7000 万次后检查它显示它填充了 NAN。据我所知,没有一个 CUDA 调用返回错误。
我使用的是 2GB GT640m(Compute 3.0),最大块大小为 1024,最大网格尺寸为 2147483647。
我确信有更好的方法来实现类似的目标,我想听听建议。但我也想了解这里出了什么问题,以便从中吸取教训。
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <fstream>
void cudaErrorHandler(cudaError_t status)
{
// Cuda call returned an error, just print error for now
if(status != cudaSuccess)
{
printf("Error");
}
}
__global__ void addKernel(float* _Results, int _TotalCombinations)
{
// Get thread Id
unsigned int Id = (blockDim.x * blockDim.y * blockIdx.x) + (blockDim.x * threadIdx.y) + threadIdx.x;
//If the Id is within simulation range, log it
if(Id < _TotalCombinations)
{
_Results[Id] = 0.95f;
}
}
#define BLOCK_DIM_X 32
#define BLOCK_DIM_Y 32
#define BLOCK_SIZE BLOCK_DIM_X * BLOCK_DIM_Y // Statc block size of 32*32 (1024)
#define CUDA_CALL(x) cudaErrorHandler(x)
int main()
{
// The number of simulations to run
unsigned int totalCombinations = 45000000;
int gridsize = 1;
// Work out how many blocks of size 1024 are required to perform all of totalCombinations
for(unsigned int totalsize = gridsize * BLOCK_SIZE; totalsize < totalCombinations;
gridsize++, totalsize = gridsize * BLOCK_SIZE)
;
// Allocate host memory
float* host_results = new float[totalCombinations];
memset(host_results, 0, sizeof(float) * totalCombinations);
float *dev_results = 0;
cudaSetDevice(0);
// Allocate device memory
CUDA_CALL(cudaMalloc((void**)&dev_results, totalCombinations * sizeof(float)));
dim3 grid, block;
block = dim3(BLOCK_DIM_X, BLOCK_DIM_Y);
grid = dim3(gridsize);
// Launch kernel
addKernel<<<gridsize, block>>>(dev_results, totalCombinations);
// Wait for synchronize
CUDA_CALL(cudaDeviceSynchronize());
// Copy device data back to host
CUDA_CALL(cudaMemcpy(host_results, dev_results, totalCombinations * sizeof(float), cudaMemcpyDeviceToHost));
double total = 0.0;
// Total the results in the host array
for(unsigned int i = 0; i < totalCombinations; i++)
total+=host_results[i];
// Print results to screen
printf("Total %f\n", total);
delete[] host_results;
return 0;
}