为什么 cudaMalloc 返回非法内存访问

why is cudaMalloc returning an illegal memory access

我正在为 MATLAB 编写一个 mexFunction,并且我有 CUDA MEX 功能 运行 MATLAB 示例没问题。

下面是一个简单的 "load the data to the device" 脚本。它 returns 3 个消息,第一个在 cudaMalloc 之前,另外两个在 cudMalloc 函数之后。

没有错误
cudaMalloc 失败!遇到非法内存访问
遇到非法内存访问

系统:Win7 64Bit,MATLAB 2015a,VS2012 Professional,CUDA 6.5.

有什么想法吗?在我看来是正确的。

代码:

void process(double *x, double *y, size_t n)
{
float *d_x, *d_y; // Pointers to data on Device.
cudaError_t cudaStatus;

cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
    mexPrintf("cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");        
}   

// Check If no Errors with GPU so far
mexPrintf(cudaGetErrorString(cudaGetLastError()));  mexPrintf("\n");

// Allocate Memory on Device    
cudaStatus = cudaMalloc( (void**)&d_x, n * sizeof(float) );
if (cudaStatus != cudaSuccess) {
    mexPrintf("cudaMalloc failed!  ");        
}
mexPrintf(cudaGetErrorString(cudaGetLastError()));  mexPrintf("\n");

cudaStatus = cudaMalloc( (void**)&d_y, n * sizeof(float) );
mexPrintf(cudaGetErrorString(cudaGetLastError()));  mexPrintf("\n");

// free the memory allocated on the GPU
cudaFree( d_x );
cudaFree( d_y ); }

我重启电脑解决了问题,上面问题中的代码没问题

我只能假设代码早期迭代中的错误内存分配导致 GPU 出现故障。

如果有人有任何洞察力发现这一点,或者在不重新启动的情况下重新初始化设备,我将很高兴听到它!

谢谢