【发布时间】:2017-05-02 22:07:15
【问题描述】:
我的代码如下:
#include <cuda_runtime.h>
#include <cuda.h>
#include <curand.h>
#include <stdio.h>
#define gpuErrorCheckCurand(ans) { gpuAssertCurand((ans), __FILE__, __LINE__); }
#define gpuErrorCheckCuda(ans) { gpuAssert((ans), __FILE__, __LINE__); }
const char* curandGetErrorString(curandStatus_t status)
{
switch(status)
{
case CURAND_STATUS_SUCCESS: return "CURAND_STATUS_SUCCESS";
case CURAND_STATUS_VERSION_MISMATCH: return "CURAND_STATUS_VERSION_MISMATCH";
case CURAND_STATUS_NOT_INITIALIZED: return "CURAND_STATUS_NOT_INITIALIZED";
case CURAND_STATUS_ALLOCATION_FAILED: return "CURAND_STATUS_ALLOCATION_FAILED";
case CURAND_STATUS_TYPE_ERROR: return "CURAND_STATUS_TYPE_ERROR";
case CURAND_STATUS_OUT_OF_RANGE: return "CURAND_STATUS_OUT_OF_RANGE";
case CURAND_STATUS_LENGTH_NOT_MULTIPLE: return "CURAND_STATUS_LENGTH_NOT_MULTIPLE";
case CURAND_STATUS_DOUBLE_PRECISION_REQUIRED: return "CURAND_STATUS_DOUBLE_PRECISION_REQUIRED";
case CURAND_STATUS_LAUNCH_FAILURE: return "CURAND_STATUS_LAUNCH_FAILURE";
case CURAND_STATUS_PREEXISTING_FAILURE: return "CURAND_STATUS_PREEXISTING_FAILURE";
case CURAND_STATUS_INITIALIZATION_FAILED: return "CURAND_STATUS_INITIALIZATION_FAILED";
case CURAND_STATUS_ARCH_MISMATCH: return "CURAND_STATUS_ARCH_MISMATCH";
case CURAND_STATUS_INTERNAL_ERROR: return "CURAND_STATUS_INTERNAL_ERROR";
}
return "Unknown cuRAND error";
}
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=false) {
if (code != cudaSuccess) {
printf("GPU assert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
inline void gpuAssertCurand(curandStatus_t code, const char *file, int line, bool abort=false) {
if (code != CURAND_STATUS_SUCCESS) {
printf("GPU (CURAND) assert: %s %s %d\n", curandGetErrorString(code), file, line);
if (abort) exit(code);
}
}
int main() {
curandGenerator_t generator;
int n = 100;
gpuErrorCheckCurand(curandCreateGenerator(&generator, CURAND_RNG_PSEUDO_DEFAULT));
gpuErrorCheckCurand(curandSetPseudoRandomGeneratorSeed(generator, 1234ULL));
float* uniformDevice; gpuErrorCheckCuda(cudaMalloc(&uniformDevice, n * sizeof(float)));
gpuErrorCheckCurand(curandGenerateUniform(generator, uniformDevice, n));
gpuErrorCheckCurand(curandDestroyGenerator(generator));
gpuErrorCheckCuda(cudaFree(uniformDevice));
}
我用一个命令编译上面的代码:
nvcc rand.cu -lcurand
记录一下,我的显卡是 GTX 1060,我使用的是 CUDA 7.5 和驱动程序版本 375.39。问题是生成随机数期间出现错误状态 CURAND_STATUS_LAUNCH_FAILURE。 cuda-memcheck 的结果是:
Program hit cudaErrorInvalidDeviceFunction (error 8) due to "invalid device function" on CUDA API call to cudaGetLastError.
有人知道怎么回事吗?
【问题讨论】: