[英]Issues with a cuda program
我正在編寫一個簡單的cuda程序,我在設備中創建一個2D數組,然后我在內核函數中進行非常基本的操作,在操作之后我將它復制回主機的2D數組。 我在遵循stackoverlow和cuda論壇的幾個主題之后編寫了這段代碼。 我按照建議但是我得到的代碼的輸出是0,而我期望數組的所有成員輸出10。 我在下面發布我的代碼:
__global__ void test_kernel(int *dev_ptr[])
{
int tidx = threadIdx.x;
int tidy = threadIdx.y;
dev_ptr[tidx][tidy] = dev_ptr[tidx][tidy] +10;
}
int main(int argc,char *argv[])
{
int env_end =50;
int **h_ptr ;
int **d_ptr ;
int **env_t;
int i,k,j;
/************************************************************************/
/* cpu
/************************************************************************/
env_t =(int **) malloc(env_end * sizeof *env_t);
for(k=0;k<env_end;k++)
{env_t[k] = (int *)malloc(env_end* env_end* sizeof *env_t[0]);
}
for (k = 1; k < env_end; ++k)
env_t[k] = env_t[k - 1] + env_end;
memset(*env_t, 0, env_end * env_end* sizeof **env_t);
for (i=0;i<env_end;i++)
{ for(j=0;j<env_end;j++)
{printf("%d\t",env_t[i][j]); }
if (j==env_end-1)
{printf("\n"); }
}
/************************************************************************/
/* gpu
/************************************************************************/
h_ptr = (int **)malloc(env_end*sizeof(int *));
for (i=0;i<env_end;i++)
{ cudaMalloc((void **)&h_ptr[i],env_end*sizeof(int));
cudaMemcpy(h_ptr[i],&env_t[i][0],env_end*sizeof(int),cudaMemcpyHostToDevice);
}
cudaMalloc((void ***)d_ptr,env_end*sizeof(int));
cudaMemcpy(d_ptr,h_ptr,env_end*sizeof(int),cudaMemcpyHostToDevice);
/************************************************************************/
/* kernel function and declaration
/************************************************************************/
dim3 blockDim(env_end,env_end,1);
test_kernel<<<1,blockDim>>>(d_ptr);
/************************************************************************/
/* Copying data back to host
************************************************************************/
for (i=0;i<env_end;i++)
{cudaMemcpy(env_t[i],h_ptr[i],env_end*sizeof(int),cudaMemcpyDeviceToHost);
}
for (i=0;i<env_end;i++)
{ for(j=0;j<env_end;j++)
{printf("%d\t",env_t[i][j]); }
if (j==env_end-1)
{printf("\n"); }
}
/************************************************************************/
/* Freeing the memory locations
/************************************************************************/
for (i=0;i<env_end;i++)
{cudaFree(h_ptr[i]);
}
cudaFree(d_ptr);
free(h_ptr);
for (i=0;i<env_end;i++)
{ free(env_t[i]);
}
free(env_t);
}
還有一件事是我在MS visual studio 2010中編寫代碼並且我收到調試斷言失敗通知。 我不確定我做錯了什么以及為什么這個通知即將來臨。 感謝你的幫助。
此代碼存在一些問題。 包含:
h_ptr
, env_t
和d_ptr
之間的大小不匹配。 &
而不是(***void)
來表示cudaMalloc
。 cudaMalloc
分配主機內存。 這是完整的代碼:
#include <stdio.h>
#define SIZE 10
#define INDEX(i,j,k) i*k+j
__global__ void test_kernel(int *dev_ptr, int row_size)
{
int tidx = threadIdx.x;
int tidy = threadIdx.y;
dev_ptr[INDEX(tidx,tidy,row_size)] = dev_ptr[INDEX(tidx,tidy,row_size)] +10;
}
int main(int argc,char *argv[])
{
int env_end =SIZE;
int *d_ptr=NULL;
int *env_t;
int i,j;
/************************************************************************/
// cpu
/************************************************************************/
env_t =(int *) malloc(env_end * env_end * sizeof(int));
memset(env_t, 0, env_end * env_end* sizeof(int));
printf("Input Array:\n");
for (i=0;i<env_end;i++)
{ for(j=0;j<env_end;j++)
{printf("%d\t",env_t[INDEX(i,j,env_end)]); }
printf("\n");
}
printf("\n");
/************************************************************************/
// gpu
/************************************************************************/
cudaMalloc(&d_ptr,env_end*env_end*sizeof(int));
cudaMemcpy(d_ptr,env_t,env_end*env_end*sizeof(int),cudaMemcpyHostToDevice);
/************************************************************************/
// kernel function and declaration
/************************************************************************/
dim3 blckDim(env_end,env_end,1);
test_kernel<<<1,blckDim>>>(d_ptr, env_end);
/************************************************************************/
// Copying data back to host
/************************************************************************/
cudaMemcpy(env_t,d_ptr,env_end*env_end*sizeof(int),cudaMemcpyDeviceToHost);
printf("Output Array:\n");
for (i=0;i<env_end;i++)
{ for(j=0;j<env_end;j++)
{printf("%d\t",env_t[INDEX(i,j,env_end)]); }
printf("\n");
}
printf("\n");
/************************************************************************/
// Freeing the memory locations
/************************************************************************/
cudaFree(d_ptr);
free(env_t);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.