CUDA Global Memory
Parallel Algorithms
David Mayerich
Scalable Tissue Imaging and Modeling (STIM) Laboratory
Department of Electrical and Computer Engineering
Cullen College of Engineering
University of Houston
David Mayerich
STIM Laboratory, University of Houston

Thread Scheduling
-
Threads within a block are divided into warps
-
this is a feature of modern devices - it is not part of the CUDA specification
-
currently 32 threads per warp
-
each warp executes a single instruction
-
this instruction is provided by the warp scheduler
-
-
Warps are sequential based on
threadIdx-
threads 0 to 31 are assigned to a single warp (as are 32 to 63, etc.)
-
numbering is in lexicographic \((x, y, z)\) order
-
warps belonging to the same block execute on the same SM
-
-
More warps are assigned to an SM than the SM can process simultaneously
-
aka the SM is a hyperthreading processor
-
as with CPU hyperthreading, this hides memory latency
-
David Mayerich
STIM Laboratory, University of Houston

Latency Tolerance
-
Hiding latency by executing other threads provides latency tolerance
-
A large number of executions are required to hide a \(\approx 300\) clock cycle global memory fetch
-
If there are a large number of global fetches in a kernel, the chance of stalling increases substantially
-
Your first step in optimization is minimizing global memory fetches
David Mayerich
STIM Laboratory, University of Houston
Global Memory Instructions
-
Transactions are composed of global memory instructions
-
Global memory instructions request word sizes of 1, 2, 4, 8, or 16 bytes
-
They must be naturally aligned:
-
an \(n\)-byte read/write instruction must start at an address divisible by \(n\)
-
if the data is not naturally aligned, it requires multiple memory instructions
-
a warp can only execute one memory instruction from each thread at a time
-
-
Multiple memory instructions from a single thread cannot be coalesced into a transaction!
David Mayerich
STIM Laboratory, University of Houston
Global Memory Instructions
-
4 byte instructions for each thread are ideal, but they aren't naturally aligned
-
Multiple instructions (1 and 4 byte) can be used, but this introduces stalls
David Mayerich
STIM Laboratory, University of Houston
There's often misalignment between data a thread needs and the compatible instructions
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
unaligned
4 byte
4 byte
4 byte
4 byte
1
1
Global Memory Instructions
-
4 byte instructions for each thread are ideal, but they aren't naturally aligned
-
Multiple instructions (1 and 4 byte) can be used, but this introduces stalls
David Mayerich
STIM Laboratory, University of Houston
There's often misalignment between data a thread needs and the compatible instructions
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
unaligned
2
1
1
1
2
1
2
1
1
2
1
1
Aligning Memory Instructions
-
The GPU handles alignment of standard data types:
char, short, int, long, size_t, float, double -
Pointers returned from cudaMalloc() are aligned to 256 byte words
-
As a programmer, you will only have to worry about structures that you use or create:
David Mayerich
STIM Laboratory, University of Houston
__device__ struct badAlign {
float a; // the first float in an array will be aligned to 4 bytes
char b; // but this will throw off the rest
}Enforcing Alignment
David Mayerich
STIM Laboratory, University of Houston
__device__ struct badAlign {
float a; // the first float in an array will be aligned to 4 bytes
char b; // but this will throw off the rest
}Force alignment with the __align__(n) directive, where n is the word size to align to
__device__ struct __align__(8) goodAlign {
float a;
char b;
// 3 bytes of padding will be added (ex. when sizeof() is used)
}All global memory instructions coalesce into a single device memory transaction issued by the warp scheduler:
Device Memory Transactions
-
Global memory is part of device memory
-
large chunk of off-chip memory (VRAM)
-
this is where
cudaMemcpy()copies data
-
-
Device memory accesses occur via transactions for 32-byte sectors
- Transactions must be naturally aligned: the address must be divisible by 32 bytes
- Global memory fetches within a warp are automatically combined into transactions
- the fetches are coalesced into one or more transactions
- the GPU will attempt to minimize the number of transactions created
- The CUDA standard also supports 64 and 128 byte transactions, but those have been phased out of modern Nvidia GPUs
David Mayerich
STIM Laboratory, University of Houston
Device Memory Transactions
David Mayerich
STIM Laboratory, University of Houston
Assume that the specified threads within a warp require the following global data:
264
272
280
288
296
304
312
320
328
336
344
352
360
368
376
384
392
400
408
416
424
How many global memory instructions will be required to fetch the required data?
How many transactions will be required?
Will there be any stalls due to divergence?
16
16
16
16
8
8
8
8
Device Memory Transactions
David Mayerich
STIM Laboratory, University of Houston
Assume that the specified threads within a warp require the following global data:
264
272
280
288
296
304
312
320
328
336
344
352
360
368
376
384
392
400
408
416
424
16
16
16
16
8
8
8
8
instruction 1
instruction 2
How many global memory instructions will be required to fetch the required data?
How many transactions will be required?
Will there be any stalls due to divergence?
Device Memory Transactions
David Mayerich
STIM Laboratory, University of Houston
How many global memory instructions will be required to fetch the required data?
How many transactions will be required?
Will there be any stalls due to divergence?
Assume that the specified threads within a warp require the following global data:
264
272
280
288
296
304
312
320
328
336
344
352
360
368
376
384
392
400
408
416
424
16
16
16
16
8
8
8
8
instruction 1
instruction 2
transactions
\(1\times 16\) byte and an \(1\times 8\) byte instruction (\(2\) total)
\(4\times\) for the first instruction, \(3\times\) for the second
no, but probably for latency
Device Memory Transactions
David Mayerich
STIM Laboratory, University of Houston
How many instructions will be required for each thread?
How many transactions will be required for each thread?
How much unused data will be transferred from global memory?
The user is processing the red channel of an RGBA image represented by double values:
\(1\times 8\) byte instruction per thread
\(1\times\) transaction per thread
\(24\) bytes per thread
264
272
280
288
296
304
312
320
328
336
344
352
360
368
376
384
392
400
408
416
424
8
8
8
8
8
8
Performance
-
Consider this matrix multiplication kernel:
David Mayerich
STIM Laboratory, University of Houston
-
The primary inner loop instruction is
c += a[ni * m + i] * b[j * n + ni];-
performs 2 floating point operations (mul and add)
-
requires two 32-bit (4-byte) global memory accesses: 8 B / 2 FLOPS = 4 B / FLOP
-
-
The maximum 32-bit floating point performance for a Tesla P100 is 5304 GFLOPS
-
The maximum global memory bandwidth for a P100 is 720 GB/s
__global__ void kernelMatrixMult(float* c, float* a, float* b, size_t m, size_t n) {
size_t i = blockIdx.y * blockDim.y + threadIdx.y; // calculate the row index
size_t j = blockIdx.x * blockDim.x + threadIdx.x; // calculate the column index
if(i >= m || j >= m) return; // return if (i,j) is outside C
float dot = 0; // initialize an accumulator
for(size_t ni = 0; ni < n; ni++) // for each element in the dot product
dot += a[ni * m + i] * b[j * n + ni]; // perform a mutliply-add
c[i * m + j] = dot; // save the register to global memory
}Caching
-
This approximation slightly under-estimates performance
David Mayerich
STIM Laboratory, University of Houston
-
Modern GPUs have an L1 cache per streaming multiprocessor (SM)
-
There is also a single L2 cache on the chip
The experimental throughput of a P100 was measured at:
global: \(510 \text{ GB/s }\)
L2: \(1624 \text{ GB/s }\)
L1: \(2300 \text{ GB/s }\)
What's the best performance we could hope for?
Jia, Maggioni, Staiger, and Scarpazza
Dissecting the NVIDIA Volta GPU Architecture via Microbenchmarking (2018)
Constant Memory
Constant memory (specified using the
__constant__decorator in CUDA compiled files)faster than L1 if all threads in a warp access the same address
accessible to all
__device__and__global__functionsaccessible in
__host__functions through the API:
David Mayerich
STIM Laboratory, University of Houston
__constant__ float c_coeffs[NUM_COEFFS]; // specify the symbol in a CUDA file
__global__ func(...) { // use the symbol in a kernel function
//...
x = y * c_coeffs[i];
//...
}
int main(int argv, char* argc[]) {
float h_coeffs[NUM_COEFFS] = {1.0f, 2.0f, 3.0f, 4.0f};
cudaMemcpyToSymbol(c_coeffs, h_coeffs, sizeof(h_coeffs)); // copy to constant memory
//call a the kernel function...
}cudaMemcpyToSymbol(...)
cudaMemcpyFromSymbol(...)
Texture Memory
-
Texture memory is global memory accessed through texture units
-
Specialized caching that is specifically useful in computer graphics
-
Allows floating point indexing in 1D, 2D, and 3D:
David Mayerich
STIM Laboratory, University of Houston
-
Provides linear interpolation between values
0.00
0.05
0.10
0.15
0.20
0.25
0.30
0.35
0.40
0.45
0.50
0.55
0.60
0.65
0.70
0.75
0.80
0.85
0.90
0.95
1.00
1.25
1.50
1.75
x[0.57]=1.35
x[0.64]=1.70
hardware calculation
(texture units)
Texture Memory
David Mayerich
STIM Laboratory, University of Houston
0.10
0.20
0.30
0.40
0.50
0.60
0.70
0.80
0.90
1.00
1.10
1.20
1.30
1.40
1.50
1.60
1.70
1.80
1.90
2.00
2.10
2.20
2.30
2.40
2.50
2.60
2.70
2.80
2.90
3.00
3.10
3.20
3.30
3.40
3.50
3.60
standard layout (row-major)
0.70
0.80
0.90
1.00
1.10
1.20
1.30
1.40
1.50
1.60
1.70
1.80
-
Caching based on spatial locality
-
Methods are proprietary but generally some combination of tiling and swizzling
Texture Memory
David Mayerich
STIM Laboratory, University of Houston
standard layout (row-major)
tiling
0.10
0.20
0.30
0.40
0.50
0.60
0.70
0.80
0.90
1.00
1.10
1.20
1.30
1.40
1.50
1.60
1.70
1.80
1.90
2.00
2.10
2.20
2.30
2.40
2.50
2.60
2.70
2.80
2.90
3.00
3.10
3.20
3.30
3.40
3.50
3.60
0.70
0.80
0.90
1.00
1.10
1.20
1.30
1.40
1.50
1.60
1.70
1.80
0.10
0.20
0.30
0.40
0.70
0.80
0.90
1.00
1.50
1.30
1.40
1.50
-
Caching based on spatial locality
-
Methods are proprietary but generally some combination of tiling and swizzling
Texture Memory
David Mayerich
STIM Laboratory, University of Houston
standard layout (row-major)
tiling
0.10
0.20
0.30
0.40
0.50
0.60
0.70
0.80
0.90
1.00
1.10
1.20
1.30
1.40
1.50
1.60
1.70
1.80
1.90
2.00
2.10
2.20
2.30
2.40
2.50
2.60
2.70
2.80
2.90
3.00
3.10
3.20
3.30
3.40
3.50
3.60
0.70
0.80
0.90
1.00
1.10
1.20
1.30
1.40
1.50
1.60
1.70
1.80
0.10
0.20
0.30
0.40
0.70
0.80
0.90
1.00
1.50
1.30
1.40
1.50
0.20
0.30
0.80
0.90
0.40
0.50
1.00
1.10
1.40
1.50
2.00
2.10
swizzling (z-curve)
-
Caching based on spatial locality
-
Methods are proprietary but generally some combination of tiling and swizzling
Current Global Memory Bandwidth
David Mayerich
STIM Laboratory, University of Houston
| GPU | Release | Bus | Bandwidth |
|---|---|---|---|
| Arc Pro B70 | 03/2026 | 256-bit | 608 GB/s |
| RX 9070 | 03/2025 | 256-bit | 640 GB/s |
| RTX 5090 | 01/2025 | 512-bit | 1,792 GB/s |
| RTX 5080 | 01/2025 | 256-bit | 960 GB/s |
| RX 7900 | 12/2022 | 384-bit | 800 GB/s |
| RTX 4080 | 11/2022 | 256-bit | 716.8 GB/s |
-
Bandwidth is dependent on the memory data rate and bus width:
bandwidth (GB/s) = bus (bits)\(\times\)data (GB/s)
-
Theoretical and practical limits are usually different:
-
architecture (ex. AMD's caching) can increase practical performance
-
bandwidth tests tend to
produce worse results due
to access patterns, etc.
-
1,500 GB/s effective
-
AMD's infinity cache can increase effective bandwidth
2,900 GB/s effective
Summary
-
Global memory is the principle (and largest) bank of GPU memory
-
The host interface to global memory is provided by the CUDA API:
-
cudaMalloc(),cudaMemcpy(),cudaFree(), etc.
-
-
The device interface includes:
-
global memory instructions of 1, 2, 4, 8, and 16 bytes issued by warp schedulers
-
global memory transactions of 32 bytes issued by the SM
(64 and 128 byte transactions are supported by the CUDA standard)
-
-
Global memory is frequently the biggest bottleneck in GPU processing due to its high latency and bandwidth
-
Nvidia GPUs minimize latency with an L2 (global) cache and an L1 (SM-level) cache
David Mayerich
STIM Laboratory, University of Houston
E.5 CUDA Global Memory
By STIM Laboratory
E.5 CUDA Global Memory
- 194