VectorAdd
2015-03-30 20:35:57

//Ref : https://youtu.be/Ed_h2km0liI

#include <stdio.h>

#define SIZE 1024

//1. parallelize this function

//3. modify function call as gpu

__global__ void VectorAdd(int *a, int *b, int *c, int n){

    

    int i = threadIdx.x;

    //for( i=0;i<n;i++){

    //    c[i] = a[i] + b[i];

    //}

    if(i < n)

        c[i] = a[i] + b[i];

    

}

//2. allocate memory on GPU memory

//2.1) Data copied from CPU to GPu

//2.2) Launch VectorAdd kernel on the GPU

//2.3) Resulting data copied from GPU to CPU

int main(){

    int *a, *b, *c, i;

    int *d_a, *d_b, *d_c; // device

    a = (int*)malloc(sizeof(int)*SIZE);

    b = (int*)malloc(sizeof(int)*SIZE);

    c = (int*)malloc(sizeof(int)*SIZE);

    //gpu side memory

    cudaMalloc(&d_a, SIZE*sizeof(int));

    cudaMalloc(&d_b, SIZE*sizeof(int));

    cudaMalloc(&d_c, SIZE*sizeof(int));

    for(i=0;i<SIZE;++i){

            a[i] = i;

            b[i] = i;

            c[i] = 0;

    }

    cudaMemcpy(d_a,a,SIZE*sizeof(int),cudaMemcpyHostToDevice); // => 2.1

    cudaMemcpy(d_b,b,SIZE*sizeof(int),cudaMemcpyHostToDevice); // => 2.1

    cudaMemcpy(d_c,c,SIZE*sizeof(int),cudaMemcpyHostToDevice); // => 2.1

    VectorAdd<<<1, SIZE>>>(d_a, d_b, d_c, SIZE);//1 block, SIZE threads => 2.2

    cudaMemcpy(c,d_c,SIZE*sizeof(int),cudaMemcpyDeviceToHost); // => 2.3

    for(i=0;i<10;++i){

        printf("c[%d] = %d

",i,c[i]);

    }

    free(a);

    free(b);

    free(c);

    cudaFree(d_a);

    cudaFree(d_b);

    cudaFree(d_c);

    return 0;

}

▼ more
Naive Memoization 적용
2015-03-21 08:23:49

1) 완전탐색 재귀함수로 문제해결

2) Parameters caching

* 위 방식은 Parameter 의 구간, 개수를 어떻게 든 줄여 제한 메모리에 넣으면 된다.

cons1. 메모리 제한내 다 들어오지 않을 수도 있고 겹치지 않으면 속도도 줄지 않는다.

cons2. sum 같은 중간에 알아야하는 것을 계속 기억해야한다.

▼ more
왜 다른 기기에서는 접속이 안되는거지?
2015-03-08 00:36:11

음;;

▼ more
Kingdom of Heaven - 2005
2015-02-22 02:25:32

King Baldwin IV: Remember that howsoever you are played or by whom,

     your soul is in your keeping alone,

     even though those who presume to play you be kings or men of power.

     When you stand before God, you cannot say,

     "But I was told by others to do thus," or that virtue was not convenient at the time.

     This will not suffice.

     Remember that.

url : http://youtu.be/iCC8tjrXYE8

기억에 남는 구절은 적어두는 것이 좋을 것 같다.

▼ more