Docsity

CUDA Memory Architecture: A Comprehensive Guide for Developers, Lecture notes of Advanced Computer Architecture

Partial preview of the text

Download CUDA Memory Architecture: A Comprehensive Guide for Developers and more Lecture notes Advanced Computer Architecture in PDF only on Docsity!

Lecture 5 CUDA Memory Architecture

Kyu Ho Park Mar. 14, 2017

Grid 공공공공공 (Shared Memory) Register Block 0 Register Thread 0 Thread 1 공공공공공 (Shared Memory) Register Block 1 Register Thread 0 Thread 1 Global Memory Constant Memory Texture Memory Host CUDA Memory Architecture

CUDA Memory

Characteristics

  1. Registers : RW a thread
  2. Local Memory : RW a thread
  3. Shared Memory : RW all threads in a block
  4. Global Memory : RW all threads in a device(grid) and the host
  5. Constant Memory : R all threads in a device and the host
  6. Texture Memory : R all threads in a device and the host

Registers

  1. An automatic variable is a local variable which is allocated and deallocated automatically when program flow enters and leaves the variable's scope.
  2. An automatic variable declared in a kernel without any other type qualifiers is stored in a register.
  3. Arrays declared in a kernel may be stored in registers. (but only if the indices used to reference the array are constant and can be determined at compile time. )
  4. A kernel uses registers to hold frequently accessed thread- private variables.
  5. Fermi: limit of 63 registers per thread,
  6. Kepler: 255 registers per thread.
  7. Register are partitioned among threads
  8. If a kernel uses more registers than the hardware limit, the excess registers will spill over to local memory. This register spilling can have adverse performance

Locality of Memory Access

Temporal locality: If at one point a

particular memory location is

referenced, then it is likely that the

same location will be referenced

again in the near future

Spatial locality: If a particular

memory location is referenced at a

particular time, then it is likely that

nearby memory locations will be

referenced in the near future

Local Memory

 (^) Local arrays referenced with indices whose values cannot be determined at compile time.  (^) Large local structures or array that would consume too much register space.  (^) Any variable that does not fit within the kernel register limit.  (^) Local memory is actually a part of Global memory that has high latency and low bandwidth and are subject to the requirements for efficient memory access.  (^) With compute capability 2.0 and higher, local memory data is also cached in a per-SM L1 and per-device L2cache

Shared Memory(SMem)

On Chip memory,

 Partitioned among thread blocks resident

on the SM

access speed is the same as the L

cache(but is also programmable)

 the size is 16KiB per SM, and easily used

by CUDA.

 static allocation

shared int a[1024];

dynamic allocation

extern shared int b[ ];

SMem Read/Write

  1. global void LoadStoreSharedMemory(int *In, int *Out)
  2. {
  3. shared int SharedMemory[512];
  4. SharedMemory[threadIdx.x]=In[threadIdx.x];
  5. __syncthreads( );
  6. Out[threadIdx.x]=SharedMemory[threadIdx.x];
  7. __syncthreads();
  8. }
  9. int main() 10.{ 11.….. 12.LoadStoreSharedMemory<<<1,512>>>(dev_In,dev_Out); 13.…. 14.return 0; 15.}

__syncthread()

A __syncthread() must be executed

by all threads in a block. (they should

__syncthread() at the same

instruction address)

What will happen?! not correct the

instruction addres different!!!!!!

if( ) then{ A;

__syncthread();}

else {B;

__syncthread();}

__syncthread()

The CUDA can execute blocks in any

order relative each other because

none of them must wait for each

other.

Constant Memory(CMem)

CMem:

some part of DRAM is used for CMem ,and it is read only and Cache is supported (constant cache). The max size is 64kB. The host can write Cmem and the device can read it only. All threads can share it like the global memory. Constant variables must be declared with global scope, outside of any kernels. Constant memory is statically declared and visible to all kernels in the same compilation unit Constant memory performs best when all threads in a warp read from the same memory address constant int cData[6];

Constant Memory(CMem)

Constant memory performs best when all threads in a warp read from the same memory address. For example, a coeffcient for a mathematical formula is a good use case for constant memory because all threads in a warp will use the same coeffcient to conduct the same calculation on different data. If each thread in a warp reads from a different address, and only reads once, then constant memory is not the best choice because a single read from constant memory broadcasts to all threads in a warp.

Texture

XX