我有一個內存分配問題,我不明白。我試圖在GPU中分配一個char數組(我猜測它可能是內存碎片問題)。cuda內存碎片
這裏是我的代碼,
#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)
誰能告訴我是什麼問題以及如何克服呢?
哪條線是31? – KiaMorot
你的錯誤之一是'cudaMemcpy()'中的'&i'。它應該是'我'。 – BenC
另外,您沒有檢查內核調用可能導致的錯誤。錯誤出現在那裏,你以後只能捕捉它。 – BenC