[英]Why do malloc() and calloc() not seem work when using CUDA?
在CUDA中使用時,使用malloc()
/ calloc()
進行的動態內存分配似乎無法正常工作。
至於檢查,我使用calloc()
編寫了以下代碼。 該數組似乎分配了所需的內存,我也可以分配一些值。 但是,當我從內核中打印矩陣元素時,我只能看到垃圾值。 我認為這可能是cudaMemcpy()
的問題,但如果我將A[5][5]
放在**A
,則代碼可以完美運行。
使用memset()
會導致“核心轉儲”錯誤。
任何人都可以幫助與malloc()
/ calloc()
相處嗎?
#include<stdio.h>
__global__ void threads(int* dA)
{
int gi=threadIdx.x+(blockIdx.x*blockDim.x);
int gj=threadIdx.y+(blockIdx.y*blockDim.y);
printf("global Id in X= %d, in Y =%d, E= %d\n", gi,gj,dA[gi*5+gj]);
}
int main(int argc, char** argv)
{
int **A, *dA;
int R=5, C=4;
int size=R*C*sizeof(int);
A=(int **)calloc(R, sizeof(int*));
for(int i=0; i<R; i++)
A[i]=(int *)calloc(C, sizeof(int));
// memset(A, 0, size);
for(int i=0; i<R; i++)
{
for(int j=0; j<C; j++)
A[i][j]=i*C+j;
}
printf(" \n Before \n");
for(int i=0; i<R; i++)
{
for(int j=0; j<C; j++)
printf("%d ",A[i][j]);
printf("\n");
}
cudaMalloc((int**) &dA, size);
cudaMemcpy(dA, A, size, cudaMemcpyHostToDevice);
dim3 nblocks(R,C);
dim3 nthreads(1);
threads<<<nblocks, nthreads>>>(dA);
cudaDeviceSynchronize();
cudaFree(dA);
free(A);
return 0;
}
您的代碼問題與宿主函數malloc
和calloc
的使用無關。 問題是您沒有正確處理雙指針以及如何將雙指針傳遞給CUDA內核。 正如羅伯特·克羅維拉(Robert Crovella)指出的那樣,適當的錯誤檢查將使您對實現中缺少的內容有更好的了解。
下面是程序的有效版本。 它僅是由talonmies提供的答案在CUDA 2D陣列問題中的應用 。
#include<stdio.h>
#include<conio.h>
inline void GPUassert(cudaError_t code, char * file, int line, bool Abort=true)
{
if (code != 0) {
fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code),file,line);
if (Abort) exit(code);
}
}
#define GPUerrchk(ans) { GPUassert((ans), __FILE__, __LINE__); }
__global__ void threads(int* dA[]) {
int gi=blockIdx.x;
int gj=blockIdx.y;
printf("global Id in X= %i, in Y =%i, E= %i\n", gi, gj, dA[gi][gj]);
}
int main(int argc, char** argv)
{
int **A, *dA;
int R=5, C=4;
int size=R*C*sizeof(int);
A=(int**)calloc(R,sizeof(int*));
for(int i=0; i<R; i++) A[i]=(int*)calloc(C,sizeof(int));
for(int i=0; i<R; i++) for(int j=0; j<C; j++) A[i][j]=i*C+j;
printf("Before transfer \n");
for(int i=0; i<R; i++) { for(int j=0; j<C; j++) { printf("%d ",A[i][j]); } printf("\n"); }
printf("\n");
// --- Create an array of R pointers on the host
int** h_A = (int**)malloc(R*sizeof(int*));
for(int i=0; i<R;i++){
// --- For each array pointer, allocate space for C ints on the device
GPUerrchk(cudaMalloc((void**)&h_A[i], C*sizeof(int)));
// --- Copy the rows of A from host to device at the address determined by h_A[i]
GPUerrchk(cudaMemcpy(h_A[i], &A[i][0], C*sizeof(int), cudaMemcpyHostToDevice));
}
// --- Create an array of R pointers on the device
int **d_A; GPUerrchk(cudaMalloc((void***)&d_A, R*sizeof(int*)));
// --- Copy the addresses of the rows of the device matrix from host to device
GPUerrchk(cudaMemcpy(d_A, h_A, R*sizeof(int*), cudaMemcpyHostToDevice));
dim3 nblocks(R,C);
dim3 nthreads(1);
printf("After transfer \n");
threads<<<nblocks, nthreads>>>(d_A);
GPUerrchk(cudaPeekAtLastError());
cudaDeviceSynchronize();
getch();
return 0;
}
正如cuda 2D數組問題中強調的那樣,將2D數組展平為1D總是更好,以避免這種麻煩的數組處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.