Presentation is loading. Please wait.

Presentation is loading. Please wait.

CUDA - 2.

Similar presentations


Presentation on theme: "CUDA - 2."— Presentation transcript:

1 CUDA - 2

2 Arrays of Parallel Threads
A CUDA kernel is executed by a grid (array) of threads. All threads in a grid run the same kernel code (SPMD) Each thread has indexes that it uses to compute memory addresses and make control decisions. blockIdx.x*blockDim.x+threadIdx.x

3 Thread Blocks: Scalable Cooperation
Divide thread array into multiple blocks Threads within a block cooperate via: Shared memory, Atomic operations, and Barrier Synchronization Threads in different blocks do not interact.

4 Transparent Scalability
Each block can execute in any order relative to others. Hardware is free to assign blocks to any processor at any time A kernel scales to any number of parallel processors

5 Example: Executing Thread Blocks
Threads are assigned to Streaming Multiprocessors (SM) in block granularity Up to 8 blocks to each SM as resource allows Fermi SM can take up to 1536 threads Could be 256 (threads/block) * 6 blocks Or 512 (threads/block) * 3 blocks, etc. SM maintains thread/block idx #s SM manages/schedules thread execution

6 Warps as Scheduling Units
Each Block is executed as 32-thread Warps An implementation decision, not part of the CUDA programming model Warps are scheduling units on an SM Threads in a warp execute in SIMD

7 Warp Example If 3 blocks are assigned to an SM and each block has 256 threads, how many Warps are there in an SM? Each Block is divided into 256/32 = 8 Warps There are 8 * 3 = 24 Warps

8 Example: Thread Scheduling (Cont.)
SM implements zero-overhead warp scheduling Warps whose next instruction has its operands ready for consumption are eligible for execution Eligible Warps are selected for execution on a prioritized scheduling policy All threads in a warp execute the same instruction when selected

9 How thread blocks are partitioned
Thread blocks are partitioned into warps Thread IDs within a warp are consecutive and increasing Warp 0 starts with Thread ID 0 Partitioning is always the same Thus you can use this knowledge in control flow However, the exact size of warps may change from generation to generation DO NOT rely on any ordering within or between warps If there are any dependencies between threads, you must __syncthreads() to get correct results (more later).

10 Partial Overview of CUDA Memories
Device code can: R/W per-thread registers R/W all shared global memory Host code can: Transfer data to/from per-grid global memory

11 CUDA Device Memory Management API functions
cudaMalloc() Allocates object in the device global memory Two parameters Address of a pointer to the allocated object Size ofallocated object in terms of bytes cudaFree() Frees object from device global memory Pointer to freed object

12 Host-Device Data Transfer API functions
cudaMemcpy() memory data transfer Requires four parameters Pointer to destination Pointer to source Number of bytes copied Type/Direction of transfer Transfer to device is asynchronous

13 INTER-WARP/THREAD LEVEL SYNCHRONIZATION
Finer grained control of execution order within a kernel If they can be avoided easily, they should Sometimes you can’t however

14 SYNCTHREADS The most simple: __synchthreads();
When reached a thread will block until all threads in the block have reached the call Maintain memory access order The CUDA compiler will try to delay memory writes as long as it can

15 Programmer View of CUDA Memories

16 Declaring CUDA Variables
Variable declaration Memory Scope Lifetime int LocalVar; Register Thread __device__ __shared__ int SharedVar; Shared Block __device__ int GlobalVar; Global Grid Application __device__ __constant__ int ConstantVar; Constant __device__ is optional when used with __shared__, or __constant__ Automatic variables reside in a register Except per-thread arrays that reside in global memory

17 Where to Declare Variables?

18 Shared Memory in CUDA A special type of memory whose contents are explicitly declared and used in the source code One in each SM Accessed at much higher speed (in both latency and throughput) than global memory Still accessed by memory access instructions A form of scratchpad memory in computer architecture

19 Hardware View of CUDA Memories

20 A Common Programming Strategy
Partition data into subsets or tiles that fit into shared memory Use one thread block to handle each tile by: Loading the tile from global memory to shared memory, using multiple threads Performing the computation on the subset from shared memory, reducing traffic to the global memory Upon completion, writing results from shared memory to global memory

21


Download ppt "CUDA - 2."

Similar presentations


Ads by Google