2014-11-23 2 views
-3

저는 CUDA를 처음 사용합니다. 난 무작위로 초기화 된 행렬을 장치 메모리에 복사하려고 시도하는 간단한 코드를 작성하고 각 행렬 항목의 값을 하나씩 증분 한 다음 호스트 메모리로 다시 전송합니다.CUDA 커널 출시

코드를 컴파일하거나 실행하는 동안 오류가 없습니다. 그러나 커널을 시작한 후에 행렬 항목의 값이 같기 때문에 커널이 시작되지 않는 것으로 보입니다.

어떤 아이디어가 있습니까?

#include <iostream> 

using namespace std; 

#define SIZE 2 

void print_matrix (int size, float *array); 
void matrix_initialize(int size, float *array); 

__global__ void LU(float * m, int size){ 
m[threadIdx.y*size + threadIdx.x] ++ ; 
} 


int main(){ 
    srand(0); 
    //variables 
    float *a = new float[SIZE*SIZE]; 
    dim3 blockdim(2,2,0); 
    dim3 griddim(1,0,0); 

    //initialize 
    matrix_initialize(SIZE, a); 
    print_matrix (SIZE, a); 


    //allocate space on device memory: 
    float * Ad; 
    int size = SIZE * SIZE; 
    cudaMalloc ((void **)&Ad, size); 

    //transfer data to device memory: 
    cudaMemcpy(Ad , a, size, cudaMemcpyHostToDevice); 

    //run the kernel 
    LU<<<griddim,blockdim>>>(Ad, SIZE); 


    // transfer the data back to the host memory 
    cudaMemcpy(a , Ad, size, cudaMemcpyDeviceToHost); 

    //test if the kernel runing the kernel has changed the value 
    print_matrix (SIZE, a); 


    // free device memory : 
    cudaFree (Ad); 



return 0; 
} 


void print_matrix (int size, float *array){ 
    for (int i=0; i < size*size ; i++){ 

     if(i % size == 0) 
     cout << endl; 
     cout << array [i] << " "; 

    } 

} 

void matrix_initialize(int size, float *array){ 

    for (int i = 0; i< SIZE*SIZE; i++){ 
      array[i] = rand()/(float) RAND_MAX; 
    } 
} 
+0

런타임 오류 검사는 좋았을 것입니다 : http://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda-runtime-api . –

답변

2

않는 치수는 0 대신에 1로 설정되어야한다

dim3 blockdim(2, 2, 1); 
dim3 griddim(1, 1, 1); 

코드는 2 × 2 × 0 = 0 인 블록, 1 X 0 X 0 = 0의 스레드 각각의 실행.

귀하의 크기 계산이 잘못되었습니다 :

int size = SIZE * SIZE * sizeof(float); 

에 코드를 계정에 배열 요소의 크기를 고려하지 않습니다.

+0

덕분에 많은 도움이되었습니다. –

+2

또한, 사용되지 않는 차원을 전혀 지정할 필요가 없으며, 'dim3'의 생성자는 지정되지 않은 차원에 대해 기본값 1을가집니다. – talonmies