cuda: memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar f c when one thread...

26
CUDA: Memory Mark Greenstreet CpSc 418 – November 06, 2020 Midterm Meditation Architecture Snapshot Registers Shared Memory Global Memory Other Memory: texture memory, constant memory, caches Summary , preview review Unless otherwise noted or cited, these slides are copyright 2020 by Mark Greenstreet and are made available under the terms of the Creative Commons Attribution 4.0 International license http://creativecommons.org/licenses/by/4.0/ Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 1 / 26

Upload: others

Post on 21-Mar-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

CUDA: Memory

Mark Greenstreet

CpSc 418 – November 06, 2020

Midterm MeditationArchitecture SnapshotRegistersShared MemoryGlobal MemoryOther Memory: texture memory, constant memory, cachesSummary, preview review

Unless otherwise noted or cited, these slides are copyright 2020 by Mark Greenstreet and aremade available under the terms of the Creative Commons Attribution 4.0 International licensehttp://creativecommons.org/licenses/by/4.0/

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 1 / 26

Page 2: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Midterm Meditation

Grading done!Original grades: mean=65, median=67, std=17. Ouch!Regrade: mean = 72.26, median = 74, 14.3

I Main difference on coding questions.I Small change Benes diameter question.

Scaled regrade: mean = 76.25, median = 75, std=12.24.

Scaled = Raw + max(100 − Raw ,0)/7.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 2 / 26

Page 3: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Midterm SpeculationOnly about half of the students answered the “list your sources”question, even though it said:

Anything else must be listed in your answer to thisquestion. If you did not use any other sources, then enter“No additional sources used” as your answer.

If everyone had answered this, the regraded mean would havebeen around 75.Obviously there wasn’t enough time for many (but far from all) ofthe students.

I What if I were to count the Benes network question as extra credit (Iwon’t)?

I The average goes up to 95% !I believe that time was a bigger issue for canvas than handing.

I Canvas solutions showed a greater evidence of getting “lost” whenstuck on a problem.

I This could be from having 25 questions (12 of which weretrue/false, and two were “free”).

F With canvas, students had to do a linear search to figure out where tocontinue if they were stuck.

F With q.pdf, the question structure showed you where to continue ifyou got stuck.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 3 / 26

Page 4: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

GPU Architecture Review

(high−end, "Pascal" generation)

GDDR

GDDR

GD

DR

GD

DR

SM

SM

SM

SM

SM

SM

SM

SM

SM

SM

SM

SM

SM

SM

SM

SML2$

SM

SM

SM

SM

SM

SM

SM

SM

SM

SM

SM

SM

SM

SM

SMSM

GTX 1080 Architecture

GPUs in the thelinXX.ugrad.cs.ubc.camachines

GeForce GTX 10609 SMs (10 on chip, 9reported by the device):

I 128 SPs/SM.I That’s 1152 SPs on the

chip.I Each SM can schedule

4 warps in a singlecycle.

∼ 1.6GHz clockfrequency.3 GBytes of GDDR5memory,∼ 192GBytes/sec. memorybandwidth.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 4 / 26

Page 5: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

A Streaming Multiprocessor (SM)

warp

F

Iregisters

lots ofdeep pipeline(20−30 stages)

registerslots of

deep pipeline(20−30 stages)

registerslots of

deep pipeline(20−30 stages)

sharedmemory

sharedmemory

sharedmemory

I$PC

DEC

ctrl

scheduler

inst inst

crossbarswitch

I omitted the connections from the pipelines to thecrossbar because the figure is already quite full.

Each of the pipelines is an SP (streaming processor)Lots of deep pipelines.Lots of threads: when we encounter an architectural challenge:

I Raising throughput is easy, lowering latency is hard.I Solve problems by increasing latency and adding threads.I Make the programmer deal with it.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 5 / 26

Page 6: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Why do we need a memory hierarchy

global void saxpy(uint n, float a, float *x, float *y) {uint myId = blockDim.x*blockIdx.x + threadIdx.x;if(myId < n)

y[myId] = a*x[myId] + y[myId];}

A GPU with 1152 SPs, and a 1.7GHz clock rate (see slide 4 canperform over 3900 single-precision GFlops.

I With a main memory bandwidth of 192 GBytes/sec., and 4 bytesper float, a CUDA kernel needs to perform 1152∗1.7∗2∗4

192 ≈ 82floating point operations per memory read or write.

I Otherwise, memory bandwidth becomes the bottleneck.Registers and shared memory let us use a value many timeswithout going to the off-chip, GDDR memory.

I But, we need to program carefully to make this work.

Is saxpy a good candidate for GPU execution?

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 6 / 26

Page 7: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

CGMA – calculation detailsCGMA: Compute-to-Global-Memory-Access ratio

I Compute: number of floating point operationsI Global memory access: number of 32-bit words read and/or written

to/from the GPU’s DRAM

The example from the previous slide:

CGMA =1152SPs∗1.7×109 instructions

SP·sec ∗2 flopsinstruction

192 bytessec ∗1 32−bitword

byte

≈ 82 flopsmemoryaccess(32−bitword)

whereI 1152 SPs: GTX 1060 architecture detailsI 1.7 × 109 instructions

SP·sec : GTX 1060 clock frequencyI 2 flops

instruction : fused multiply-addI 192 bytes

sec : GTX 1060 off-chip memory bandwidthI 1 32−bitword

byte : sizeof(float)

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 7 / 26

Page 8: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Matrix Multiplication and Memory

for(int i = 0; i < M; i++)for(int j = 0; j < N; j++)

for(int k = 0; k < L; k++)c[i,j] += a[i,k]*b[k,j];

Focus on the innermost loop: for(k . . .)I Why?

How many floating point operations per iteration?How many memory reads?How many memory writes?What is the “Compute-to-Global-Memory-Access” ratio (CGMA)?

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 8 / 26

Page 9: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Registers

block0

warp0 warp3

registerspipelines

warp2warp1

more blocks

ctrl

F

II$

PCDEC

scheduler

inst

warp

inst

Each SP has its own register file.The register file is partitioned between threads executing on theSP.Local variables are placed in registers.

I The compiler in-lines functions when it canF A kernel with recursive functions or deeply nested calls can cause

register spills to main memory – this is slow.I Local array variables are mapped to global memory – watch out.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 9 / 26

Page 10: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

More Registers

block0

warp0 warp3

registerspipelines

warp2warp1

more blocks

ctrl

F

II$

PCDEC

scheduler

inst

warp

inst

In recent versions of CUDA, threads in the same warp can swapregisters.

I Provides very efficient intra-warp communication.I For example: to implement the various strides of for

compare-and-swap in bitonic sort.Performance trade-offs

I A thread can avoid slow, global memory accesses by keeping datain registers.

I But, using too many registers reduces the number of threads thatcan run at the same time.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 10 / 26

Page 11: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Registers and Memory Bandwidth

The GPU on slide 4 has 9 SMs, each with 128 SPs.Each SP has access to a register file.I’ll guess two register reads and one write per clock cycle, per SP.I’ll assume 4-byte registers.We get

9SM∗128SPSM

∗3RW

SP ∗ cycle∗1.7×109 cycle

sec.∗4

ByteRW

≈ 23500GByte

sec.

122 times faster than main memory bandwidth!

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 11 / 26

Page 12: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Registers and Thread Scheduling

block0

warp0 warp3

registerspipelines

warp2warp1

more blocks

ctrl

F

II$

PCDEC

scheduler

inst

warp

inst

Each SM has a 256Kbyte register file, and 64 active warps, with32 threads/warp.

I That’s 32 4-byte registers per thread.If a thread uses more registers

I The CUDA run-time will reduce the number of blocks running on anSM to satisfy the register limit.

I This means that the warp scheduler may not have enough warps tochoose from to keep the SM fully utilized.

The numbers are smaller for older GPUs.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 12 / 26

Page 13: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Registers and Matrix Multiply

Matrix multiplication can be broken into blocks.We can load blocks of A and B into registers and compute theresult block for C.

I E.g. if we load 2 × 2 blocks of A, B into registers, we can compute(part of) a 2 × 2 block of C.

I This involves 8 loads, 4 stores, 8 multiplies, and 4 adds.I CGMA = 1. Better than the brute-force algorithm, but we need to

take this idea further.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 13 / 26

Page 14: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Shared Memory

pipeline

pipelineIaddr

data

addr

data

shared

memory

addr

data

addr

data

shared

memory

I$PC

DE

scheduler

inst

warp

inst ctrl

crossbar

CF

On-chip, one bank per SP.Banks are interleaved by:

I Early CUDA GPUs: 4-byte wordI Later GPUs: programmer configurable 4-byte or 8-byte wordsI Why?

Shared memory is a limited resource: 48KBytes to 96Kbytes/SM.I Each SM has more registers than shared-memory.I Shared memory demands limit how many blocks can execute

concurrently on a SM.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 14 / 26

Page 15: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Shared Memory Example: Matrix Multiply

Running example from the textbook: C = A BEach thread-block loads a 16 × 16 block from A and B.

I The threads to these loads “cooperatively”:I Read AI,K and BK ,J from global memory with “coalesced” loads.I Write these blocks to shared-memory in a way that avoids bank

conflicts.Compute: CI,J += AI,K BK ,J .

I This takes 163 = 4096 fused multiply-adds.I Loading AI,K fetches 162 = 256 floats from global memory.I Likewise for BK ,J . Total of 512 floats fetched.I CGMA = 2 ∗ 4096/512 = 16.

Note: the L2 cache may help here: A and B are read-only.I Need to try more experiments.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 15 / 26

Page 16: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Matrix Multiply: Notes

CGMA of 16 is much better, but still not the 82 that we need.Many graphics and machine learning applications work withlow-precision arithmetic.

I Use 16-bit floating point numbers.I Can load twice as many float16’s per second.I Can do twice as many float16 operations.I Can hold more in shared memory. We’ve improved CGMA some.

Tune the algorithm: the blocks we use for A and B don’t have to besquare.nVidia GPUs seem to be designed to the point that matrixmultiplication balances floating point throughput with memorybandwidth for a carefully optimized implementation.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 16 / 26

Page 17: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Shared Memory: Collisions

pipeline

pipelineIaddr

data

addr

data

shared

memory

addr

data

addr

data

shared

memory

I$PC

DE

scheduler

inst

warp

inst ctrl

crossbar

CF

When one thread in a warp accesses shared memory, all active threads inthe warp access shared memory.If each thread accesses a different bank, then all accesses are performedin a single cycle.

I Otherwise, the load or store can take multiple cycles.I Multiple accesses to the same bank are called collisions.I The worst-case occurs when all threads access different locations in

the same bank.The programmer needs to think about the index calculations to avoidcollisions.

I When programming GPUs, the programmer needs to think about indexcalculations a lot.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 17 / 26

Page 18: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Global Memory

Off-chip DRAMI GDDR supports higher-bandwidth than than regular DDR.I A GPU can have multiple memory interfaces.I Total bandwidth 80 to 484+ GBytes/sec

Memory accesses can be a big bottleneck.I CGMA: compute to global memory access ratio

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 18 / 26

Page 19: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Now for a word about DRAM

The memory that you plug into your computer is mounted on DIMMs(dual-inline memory modules).

A DIMM typically has 16 or 18 chips

E.g. each chip of an 8Gbyte DIMM holds 512MBytes = 4Gbits.

Each chip consists of many “tiles”,

I a typical chip has 1Mbit/tileI that’s 4096 tiles for a 4Gbit chip.

Each tile is an array of capacitors.

I each capacitor holds 1 bit.I a typical tile could have 1024 rows and 1024 columns.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 19 / 26

Page 20: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Writing and reading DRAMWriting: easy

I drive all 1024 column-lines to the values you want to write.I open up all the valves for one row.I the drinking cups for each column in that row get filled or emptied.I note: you end up writing every column in the row; so writes are often

preceded by reads.Reading: hard

I drive all 1024 column-lines to “half-way”, and let them “float”.I open up all the valves for one row.I if the level in the pipe goes up a tiny amount, that cup held a 1.I if the level in the pipe goes down a tiny amount, that cup held a 0.I it’s a delicate measurement – it takes time to set it up.I This is why DRAM is slow.

But: we just read 1024 bits, from each chip of the DIMM.I That’s 16Kbits = 2Kbytes total.I Conclusion: DRAM has awful latency, but we can get very good bandwidth.

F The bandwidth bottleneck is the wires from the DIMM to the CPU or GPU.F But I’m pretty sure that Ian won’t let me give a lecture on transmission lines,

phase-locked loops, equalizers, and all the other cool stuff in the DDR (orGDDR) interface.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 20 / 26

Page 21: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

GPUs meet DRAMDRAM summary: terrible latency (60-200ns or more), fairly highbandwidth.The GPU lets the program take advantage of high bandwidth.

I If the 32 loads from a warp access 32 consecutive memorylocation,

F The GPU does one GDDR access,F and it transfers a large block of data.

I The same optimization is applied to stores, and to loads from theon-chip caches.

In CUDA-speak, if the loads from a warp access consecutivelocations, we say that the memory accesses are coalesced.It’s a big deal to make sure that your memory accesses arecoalesced.

I Note that the memory optimizations are exposed to theprogrammer.

I You can get the performance by considering the memory model.I But, it’s not automatic.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 21 / 26

Page 22: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Example: Matrix Multiplication

In C, matrices are usually stored in row-major order.I A[i,k] and A[i,k+1] are at adjacent locations, butI B[k,j] and B[k+1,j] are N words apart (for N × N matrices).

For matrix multiplication, accesses to A are naturally coalesced,but accesses to B.The optimized code loads a block of B into shared memory.

I This allows accesses to be coalesced.I But we need to be careful about how we store the data in the

shared memory to avoid bank conflicts.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 22 / 26

Page 23: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Other Memory

Constant memory: cached, read-only access of global memory.Texture memory: global memory with special access operations.L1 and L2 caches: only for memory reads.

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 23 / 26

Page 24: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

SummaryGPUs can have thousands of execution units, but only a fewoff-chip memory interfaces.

I This means that the GPU can perform 10-50 floating pointoperations for every memory read or write.

I Arithmetic operations are very cheap compared with memoryoperations

To mitigate the off-chip memory bottleneckI GPUs have, limited on-chip memoryI Registers and the per-block, shared-memory will be our main

concerns in this class.Moving data between different kinds of storage is theprogrammer’s responsibility.

I The programmer explicitly declares variables to be stored in sharedmemory.

I The programmer needs to be aware of the per-thread registerusage to achieve good SM utilization.

I The only way to communicate between thread blocks is to write toglobal memory, end the kernel, and start a new kernel (ouch!)

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 24 / 26

Page 25: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

Preview

Nov 9: GPU Memory (part 2)Nov 11: Rembrance Day – no lectureNov 13: GPU Memory (part 3?)Nov 16: GPU Performance (part 1)Nov 18: GPU Performance (part 2)

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 25 / 26

Page 26: CUDA: Memorycs-418/2020-1/lecture/11-06/slides.pdfinst warp inst ctrl crossbar F C When one thread in a warp accesses shared memory, all active threads in the warp access shared memory

ReviewWhat is CGMA?On slide 15 we computed the CGMA for matrix-multiplicationusing 16 × 16 blocks of the A, B, and C matrices.

I How many such thread-blocks can execute concurrently on an SMwith 48KBytes of memory?

I How does the CGMA change if we use 32 × 32 blocks?I If we use the larger matrix-blocks, how many thread blocks can

execute concurrently on an SM with 48Kbytes of memory?I If we use the larger matrix-blocks, how many thread blocks can

execute concurrently on an SM with 96Kbytes of memory?

What are bank conflicts?How can increasing the number of registers used by a threadimprove performance?How can increasing the number of registers used by a threaddegrade performance?What is a “coalesced memory access”?

Greenstreet CUDA: Memory CpSc 418 – Nov. 06, 2020 26 / 26