Docsity

CUDA Execution Model: Memory Hierarchy, Thread-Block-Grid, and Parallel Processing, Lecture notes of Advanced Computer Architecture

Partial preview of the text

Download CUDA Execution Model: Memory Hierarchy, Thread-Block-Grid, and Parallel Processing and more Lecture notes Advanced Computer Architecture in PDF only on Docsity!

Lecture 3 CUDA Execution

Model

Kyu Ho Park Mar. 7, 2017

Memory Hierarchy and

Thread-Block-Grid

GRI

D

Shared Memory Thread( ,0) Thread( ,0) Thread( ,0) Thread( ,0)

Block(

Global Memory

Host

cudaMalloc cudaMemcpy cudaMemset cudaFree Kernel

Devic

e

Thread-Block-Grid

Grid: All threads spawned by a single

kernel launch are collectively called a

grid.

All threads in a grid share the same

global memory space.

Block: A thread block is a group of

threads that can cooperate with each

other.

Threads from different blocks cannot

cooperate.

Thread-Block-Grid & Core-SM-Device

Block …. Grid Software Hardware CUDA core S M

Device

Thread

a block and warps

warp

SM a block

Thread-Block-Grid Model

8 thread block grid

Simultaneous Thread Processing of a GPU

G80 16 SMs , max 768 threads/SM

ļ‚„ 768x16= 12,288 threads in parallel

GT200 30 SM, max 1,024 threads/

SM

ļ‚„ 1,024x30=30,720 threads in parallel

GTX 1080,

the most recent one(2016 )

CUDA block and 1-D thread

thread block t threadIdx.x = t1 t2 t3 t4 t threadIdx.x= 5

The thread index is represented by the given
variable ā€˜ threadIdx ’.
kernel function:
global void kernel<<<1,6>>>(int a, int b, int c);
global void kernel<<<1,512>>>(int a, int b, int
c);

thread0: threadIdx.x=

thread1: threadIdx.x=

thread511: threadIdx.x=

G80: 16 SM

global void kernel<<<1,512>>>( )

Only one SM will be utilized and 15 SMs
will be idle.
To utilize all SMs,
global void
kernel<<<16,32>>>( );

kernel<<<16,32>>>

thread_indexing

block0 : threadIdx.x=0,

…….,threadIdx.x=

block1 : threadIdx.x=0,

…….,threadIdx.x=

block15: threadIdx.x=0,

…….,threadIdx.x=

GT200: 30 SMs,

16SMs are active and 14 SMs are

idle.

Vector Sum

#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; }