2011-02-17 4 views
15

CUDA를 처음 사용했습니다. 크기 MXN의 2D 배열을 할당하는 방법? CUDA에서 배열을 트래버스하는 방법? 샘플 코드를주세요. ................................................. ...........................................CUDA에서 2D 어레이를 사용하는 방법?

안녕하세요. 답변 감사합니다. 나는 당신의 코드를 다음 프로그램에 사용했다. 그러나 나는 정확한 결과를 얻지 못하고있다.

__global__ void test(int A[BLOCK_SIZE][BLOCK_SIZE], int B[BLOCK_SIZE][BLOCK_SIZE],int C[BLOCK_SIZE][BLOCK_SIZE]) 
{ 

    int i = blockIdx.y * blockDim.y + threadIdx.y; 
    int j = blockIdx.x * blockDim.x + threadIdx.x; 

    if (i < BLOCK_SIZE && j < BLOCK_SIZE) 
     C[i][j] = A[i][j] + B[i][j]; 

} 

int main() 
{ 

    int d_A[BLOCK_SIZE][BLOCK_SIZE]; 
    int d_B[BLOCK_SIZE][BLOCK_SIZE]; 
    int d_C[BLOCK_SIZE][BLOCK_SIZE]; 

    int C[BLOCK_SIZE][BLOCK_SIZE]; 

    for(int i=0;i<BLOCK_SIZE;i++) 
     for(int j=0;j<BLOCK_SIZE;j++) 
     { 
     d_A[i][j]=i+j; 
     d_B[i][j]=i+j; 
     } 


    dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); 
    dim3 dimGrid(GRID_SIZE, GRID_SIZE); 

    test<<<dimGrid, dimBlock>>>(d_A,d_B,d_C); 

    cudaMemcpy(C,d_C,BLOCK_SIZE*BLOCK_SIZE , cudaMemcpyDeviceToHost); 

    for(int i=0;i<BLOCK_SIZE;i++) 
     for(int j=0;j<BLOCK_SIZE;j++) 
     { 
     printf("%d\n",C[i][j]); 

     } 
} 

도와주세요.

+16

더 정중하고 더 해칠 수는 없습니다. – karlphillip

+1

당신은 cudaMemcpy로 2D 배열의 가치를 되 찾을 수 없지만 cudaMallocPitch 또는 cudaPitchPtr을 @Dave가 말한대로 – ardiyu07

답변

16

2 차원 배열을 할당하는 방법 :

__global__ void YourKernel(int d_A[BLOCK_SIZE][BLOCK_SIZE], int d_B[BLOCK_SIZE][BLOCK_SIZE]){ 
int row = blockIdx.y * blockDim.y + threadIdx.y; 
int col = blockIdx.x * blockDim.x + threadIdx.x; 
if (row >= h || col >= w)return; 
/* whatever you wanna do with d_A[][] and d_B[][] */ 
} 

내가이

도움이 또한 당신이 매트릭스에 대해 CUDA Programming Guide 22 페이지를 참조 할 수 있기를 바랍니다 : 해당 배열을 통과하는 방법

int main(){ 
#define BLOCK_SIZE 16 
#define GRID_SIZE 1 
int d_A[BLOCK_SIZE][BLOCK_SIZE]; 
int d_B[BLOCK_SIZE][BLOCK_SIZE]; 

/* d_A initialization */ 

dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE); // so your threads are BLOCK_SIZE*BLOCK_SIZE, 256 in this case 
dim3 dimGrid(GRID_SIZE, GRID_SIZE); // 1*1 blocks in a grid 

YourKernel<<<dimGrid, dimBlock>>>(d_A,d_B); //Kernel invocation 
} 

곱셈

+3

@user621508으로 사용하는 대신 cudaMalloc3D와 함께 사용해야합니다. 이것이 작동하는 동안 장치 메모리에 하나의 거대한 선형 배열이 생성됩니다. 또한 [cudaMalloc3D] (http://developer.download.nvidia.com/compute/cuda/3_2/toolkit/docs/online/group__CUDART__MEMORY_g04a7553c90322aef32f8544d5c356a10.html#g04a7553c90322aef32f8544d5c356a10)를 사용하여 2D 기반 최적화에 최적화 된 2 차원 배열을 할당 할 수 있습니다. 데이터 접근. 2D 배열의 인덱싱 또는 성능을 원하는지 여부는 알지 못했습니다. –

+6

여기서 id는 cudaMalloc이고 aboce 코드는 무엇입니까? –

+2

@ username_4567, 그게/* d_A 초기화 * /의 약자입니다. 그러나 메모리 해제는 없습니다. –

4

가장 좋은 방법은, 그것의 벡터 형태로 2 차원 배열 A. 당신이 원 - 사용

A[i*n+j] (with i=0..n-1 and j=0..m-1). 

을 작성할 수 있습니다 예를 들어, 당신은 매트릭스 크기 n × m 개의 있고,이 벡터 형태로

A[i][j] (with i=0..n-1 and j=0..m-1). 

될 것 포인터 표현 포인터 (I, J) 요소의 이 경우 2 차원 배열은 복사 프로세스를 단순화합니다. 간단합니다.

double *A,*dev_A; //A-hous pointer, dev_A - device pointer; 
A=(double*)malloc(n*m*sizeof(double)); 
cudaMalloc((void**)&dev_A,n*m*sizeof(double)); 
cudaMemcpy(&dev_A,&A,n*m*sizeof(double),cudaMemcpyHostToDevice); //In case if A is double 
관련 문제