









































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










































Kyu Ho Park Mar. 7, 2017
Shared Memory Thread( ,0) Thread( ,0) Thread( ,0) Thread( ,0)
Global Memory
cudaMalloc cudaMemcpy cudaMemset cudaFree Kernel
Block ā¦. Grid Software Hardware CUDA core S M
Thread
SM a block
8 thread block grid
Simultaneous Thread Processing of a GPU
ļ 768x16= 12,288 threads in parallel
ļ 1,024x30=30,720 threads in parallel
the most recent one(2016 )
thread block t threadIdx.x = t1 t2 t3 t4 t threadIdx.x= 5
global void kernel<<<1,512>>>( )
#include <cuda.h> #include <cuda_runtime.h> #include <stdio.h> global void VectorAdd( inta, intb, intc) { int tid = blockIdx.x * blockDim.x+ threadIdx.x; c[tid] = a[tid] + b[tid]; } int main() { const int size = 51265535; const int BufferSize = sizesizeof(int); int A; int* B; int* Sum; //Allocation of the Host memory A = (int)malloc(BufferSize); B = (int)malloc(BufferSize); Sum = (int*)malloc(BufferSize); int i = 0; //Data input for( int i = 0; i < size; i++) { A[i] = i; B[i] = i; Sum[i] = 0; }
int* d_A; (gpu memory convention) int* d_B; int* d_Sum; //Allocation of the Device memory cudaMalloc((void)&d_A, sizesizeof(int)); cudaMalloc((void)&d_B, sizesizeof(int)); cudaMalloc((void)&d_Sum, sizesizeof(int)); //Data transfer from the Host to the Device cudaMemcpy(d_A, A, sizesizeof(int), cudaMemcpyHostToDevice); cudaMemcpy(d_B, B, sizesizeof(int), cudaMemcpyHostToDevice); //Add VectorAdd<<<65535,512>>>(d_A, d_B, d_Sum); //Device to Host cudaMemcpy(Result, d_Sum, sizesizeof(int), cudaMemcpyDeviceToHost); //Print out the result for( i = 0; i < 5; i++){ printf(" Result[%d] : %d
n",i,Sum[i]);} printf(" ......\n"); for( i = size-5; i < size; i++){ printf(" Result[%d] : %d
n",i,Sum[i]);} cudaFree(d_A); cudaFree(d_B); cudaFree(d_Sum); free(A); free(B); free(Sum); return 0; }