【发布时间】:2013-05-11 03:54:10
【问题描述】:
我遇到了无法理解的内存分配问题。我正在尝试在 GPU 中分配一个 char 数组(我猜这可能是内存碎片问题)。
这是我的代码,
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<cuda.h>
inline void gpuAssert(cudaError_t code, char *file, int line,
int abort=1)
{
if (code != cudaSuccess) {
printf("GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
__global__ void calc(char *k,char *i)
{
*i=*k;
}
int main()
{
char *dev_o=0;
char *i;
i = (char*)malloc(10*sizeof(char));
cudaMalloc((void**)&dev_o,10*sizeof(char)); //Line 31
calc<<<1,1>>>("arun",dev_o);
gpuErrchk(cudaMemcpy(&i,dev_o,10*sizeof(char),cudaMemcpyDeviceToHost));
cudaFree(dev_o);
printf("string : %s \n",i);
return 0;
}
但我得到的输出是,
GPUassert: 内存不足 sample2.cu 31
在同样的情况下,我尝试在 GPU 中分配整数并且它工作正常。
我的 GPU 设备信息显示为,
--- General Information for device 0 ---
Name:GeForce GTX 460 SE
Compute capability:2.1
Clock rate:1296000
Device copy overlap:Enabled
Kernel execition timeout :Enabled
--- Memory Information for device 0 ---
Total global mem:1073283072
Total constant Mem:65536
Max mem pitch:2147483647
Texture Alignment:512
--- MP Information for device 0 ---
Multiprocessor count:6
Shared mem per mp:49152
Registers per mp:32768
Threads in warp:32
Max threads per block:1024
Max thread dimensions:(1024, 1024, 64)
Max grid dimensions:(65535, 65535, 65535)
谁能告诉我问题出在哪里以及如何克服它?
【问题讨论】:
-
您的错误之一是
cudaMemcpy()中的&i。应该是i。 -
另外,您没有检查内核调用可能产生的错误。错误出现在那里,你只能在以后发现它。
-
另一个错误是将主机指针传递给 char 数组作为内核调用的第一个参数。并检查
cudaMalloc的返回值并调用cudaDeviceSynchronizeafter kernel to pinpoint error location。 -
我可以使用 cudaDeviceReset() 重置设备内存吗?
-
我建议先阅读编程指南,然后尝试编写代码。 docs.nvidia.com/cuda/cuda-c-programming-guide/index.html
标签: memory-management cuda gpu