Ship the Data, Then the Work
A GPU is a second computer inside the node, with its own memory. Before it can touch a byte, that byte has to get there. One method, four steps, decides whether the trip is worth it. You will run it on one loop, compare it with what Cardinal measured, and then run it yourself.
Concept map
One idea, one worked example, then your turn. Every box is a section of this page.
Two memories, one road
One Cardinal GPU node. Your arrays are created in the CPU's memory. The GPU has its own memory, and the only way in is the PCIe bus: the road.
| What | Speed used in the model | Where the number comes from |
|---|---|---|
| The CPU's memory | 200 GB/s | one socket's published peak is 307 GB/s; real code gets about two thirds |
| The GPU's own memory | 3,000 GB/s | the vendor lists 3,350 GB/s for the H100 |
| The road between them | 50 GB/s | the bus peaks at 64 GB/s in each direction |
GB/s means gigabytes moved per second. These three are vendor numbers, rounded. They are a model. A later section replaces them with what Cardinal measured.
Drag the amount of data. The bars show how long it takes to move it once at each of the three speeds (model).
The method: should this loop go to the GPU?
Four steps. The trip is copying the arrays across the road to the GPU, and copying the results back. A pass is one full run of the loop over the array.
Count the bytes that cross the road.
Read the map clauses:
to= copied to the GPU,from= copied back,tofrom= both.Time three things.
The trip: the copy over and the copy back. One pass of the loop on the CPU. One pass on the GPU.
Add it up for your number of passes.
GPU = one trip + every pass on the GPU. CPU = every pass on the CPU.
Measure.
Run it with the trip and the passes timed separately. Compare with steps 2 and 3.
Steps 1 and 2: the loop, sent once
Branch lecture14, src/main.cpp, saxpy_once(). The loop is SAXPY: y = a·x + y for every element. n = 100 million floats, 4 bytes each.
float* x = new float[n]; // CPU memory float* y = new float[n]; init(x, y, n); #pragma omp target teams distribute parallel for \ map(to: x[0:n]) map(tofrom: y[0:n])for (long i = 0; i < n; i++) { y[i] = A * x[i] + y[i]; } check("saxpy_once", y, n, reps); // y is back on the CPU
| Word | What it does |
|---|---|
target | run the loop on the GPU, not on the CPU's threads |
teams distribute parallel for | Week 9's parallel for, split across the GPU's threads |
map(to: x[0:n]) | copy x to the GPU before the loop; x[0:n] is "start at 0, n elements" |
map(tofrom: y[0:n]) | copy y to the GPU before the loop, and back after it |
Each array is 100 million × 4 bytes = 0.4 GB. to sends x over once. tofrom sends y over and brings it back. Three crossings of 0.4 GB: 1.2 GB.
For each element the loop reads x, reads y and writes y: 12 bytes for one multiply and one add. So one pass moves 1.2 GB through memory, on either machine, and its time is bytes ÷ memory speed.
| Time this | Bytes | At this speed | Time (model) |
|---|---|---|---|
| The trip: x and y over, y back | 1.2 GB | 50 GB/s (the road) | 24 ms |
| One pass on the CPU, 48 cores | 1.2 GB | 200 GB/s (the CPU's memory) | 6 ms |
| One pass on the GPU | 1.2 GB | 3,000 GB/s (the GPU's memory) | 0.4 ms |
| GPU total, one pass: trip + pass | 24.4 ms, 4× slower than 6 ms |
The GPU's pass is 15× faster than the CPU's. The trip there and back costs as much as 60 GPU passes.
Step 3: more passes, one trip
saxpy_resident(). Same loop, same n, same node. The map moved out of the pass loop, onto target data.
#pragma omp target data map(to: x[0:n]) map(tofrom: y[0:n]){ for (long r = 0; r < reps; r++) { // no map clause: x and y are already on the GPU #pragma omp target teams distribute parallel for for (long i = 0; i < n; i++) { y[i] = A * x[i] + y[i]; } } } // y comes back here, once
x and y go over at the opening brace. y comes back at the closing brace. Inside, nothing crosses the road. It is Week 6's rule again: build the datatype once, outside the loop.
GPU: 24 + 100 × 0.4 = 64 ms. CPU: 100 × 6 = 600 ms. 600 ÷ 64 = 9.4. "15" forgets the trip. "0.25" pays it a hundred times, which is what happens if the map clause stays inside the loop.
Drag the number of passes. Switch between the model's speeds and the pieces Cardinal measured, and between the two places the map clause can sit.
Step 4: measured on Cardinal
The same loop, run on one NVIDIA H100 with 48 CPU threads on Mon Nov 23, minimum of five trials. saxpy_split times the trip over, the passes, and the trip back separately.
Both memory speeds were close. Both halves of the trip were about five times slower than the model.
| Piece | Model | Measured | The speed the measurement implies |
|---|---|---|---|
| One pass on the CPU, 48 cores | 6 ms | 4.9 ms | close: 247 GB/s (assumed 200) |
| The trip over: x and y to the GPU | 16 ms | 92 ms | wrong, 5.7× slower: 8.7 GB/s (assumed 50) |
| One pass on the GPU | 0.4 ms | 0.6 ms | close: 2,000 GB/s (assumed 3,000) |
| The trip back: y to the CPU | 8 ms | 35 ms | wrong, 4.4× slower: 11 GB/s (assumed 50) |
5 ON_DEVICE 1 saxpy_cpu, one pass (s): 0.0048511 T_IN (s): 0.0918832 T_KERNEL, one pass (s): 0.000599 T_OUT (s): 0.0352299
Why the road is slow is not established yet. The likely reason: x and y are ordinary memory from new[], and a copy to the GPU from ordinary memory is staged through a second buffer. The 50 GB/s figure is for memory that has been locked in place. The trip over also includes setting aside 0.8 GB on the GPU. Mon Nov 30 times the two kinds of memory against each other.
Step 3 again, with the measured pieces
| Question | The model said | From the measured pieces |
|---|---|---|
| Sent once | 4× slower | 26× slower: 127.7 ms against 4.9 ms |
| The GPU is ahead from pass | 5 | 30: 127 ms of trip, and each pass saves 4.25 ms |
| Speedup at 100 passes | 9.4× | 2.6×: 187 ms against 485 ms |
| The most it can ever win | 15× | 8×: 4.85 ms ÷ 0.6 ms |
The right-hand column is arithmetic on the four measured pieces, not four more timed runs. Go back to the explorer in the last section and switch it to "measured on Cardinal" to see every pass count.
No GPU at all
The same program and the same pragmas, on the course's test machine: 2 cores, g++ 13, no GPU. This is real output, and it is not a Cardinal timing.
$ OMP_NUM_THREADS=2 ./Default/OMP saxpy_split 1e8 1 ON_DEVICE 0 T_IN 6.08e-07 s (0.8 GB, 1.32e+06 GB/s) T_KERNEL 0.100872 s (1 reps, 0.101 s each, 11.9 GB/s) T_OUT 1.078e-06 s (0.4 GB, 3.71e+05 GB/s) CHECK saxpy_split ok
ON_DEVICE 0 is the program asking, inside the target loop, whether it is on a GPU. With no GPU, OpenMP is allowed to run a target loop on the CPU instead. The standard calls it host fallback. The "GPU" and the CPU are then the same memory, the map clauses copy nothing, and dividing 0.8 GB by a microsecond gives a silly speed. The answer is right either way.
One environment variable turns the silent CPU run into an error:
$ OMP_TARGET_OFFLOAD=MANDATORY ./Default/OMP saxpy_split 1e8 1
libgomp: OMP_TARGET_OFFLOAD is set to MANDATORY,
but only the host device is availableOMP_TARGET_OFFLOAD=MANDATORY, and read ON_DEVICE, before you trust a number. Week 7 had the same lesson: a permissive runtime hides the bug.Ship it or stay home?
Run steps 1 to 3 on three new cases. Same node, and the model's three speeds: road 50 GB/s, one CPU pass and one GPU pass as given. Work each one out on paper before you choose.
Trip: 1.2 GB ÷ 50 GB/s = 24 ms, once. GPU: 24 + 3 × 0.4 = 25.2 ms. CPU: 3 × 6 = 18 ms. 0.71×. Three passes do not pay for the trip. It takes five.
Over, once: 0.8 GB ÷ 50 = 16 ms. Back, every pass: 0.4 GB ÷ 50 = 8 ms. GPU: 16 + 100 × (0.4 + 8) = 856 ms. CPU: 100 × 6 = 600 ms. 0.70×. The data lives on the GPU, but y still makes the trip 100 times. Step 1 counts every crossing, every time.
Save every tenth pass instead: 16 + 100 × 0.4 + 10 × 8 = 136 ms, which is 4.4× faster than the CPU. Same loop; one decision about how often the bytes cross.
Trip: 0.8 GB ÷ 50 GB/s = 16 ms. GPU: 16 + 18 = 34 ms. CPU: 180 ms. 5.3×, in a single pass.
Why the opposite answer from SAXPY sent once: count the work each trip buys. SAXPY ships 1.2 GB to do 2 operations on each of 100 million elements, 200 million operations. The matrix multiply ships 0.8 GB to do 2 × 8,192 × 8,192 × 8,192 = 1.1 trillion. A loop that does a lot of work per byte shipped can win in one pass.
With the road Cardinal measured (about 10 GB/s) the three verdicts hold and the margins move: A and B lose by more, and C still ships, at about 1.7×. C's 180 ms and 18 ms are still a model; that program has not been run.
The stencil: can you even do step 1?
A different program, legacy_ptrptr(): an older stencil, kept the way it was written. Each cell becomes the average of its four neighbours.
int** A = new int*[n]; // n row pointers int** Anew = new int*[n]; for (long j = 0; j < n; j++) { // n separate rows A[j] = new int[m]; Anew[j] = new int[m](); } #pragma omp target teams distribute parallel for reduction(max : error)for (long j = 1; j < n - 1; j++) { for (long i = 1; i < m - 1; i++) { Anew[j][i] = 0.25 * (A[j][i + 1] + A[j][i - 1] + A[j - 1][i] + A[j + 1][i]); error = std::fmax(error, std::fabs(double(Anew[j][i] - A[j][i]))); } }
There are no map clauses. You can count what the loop touches: it reads the n × m ints of A and writes the n × m ints of Anew, 2 × n × m × 4 bytes. What crosses the road is a different question, and the code is silent on it.
With no map clause the compiler falls back on its rule for a bare pointer: ship the pointer, and nothing behind it. It has no choice. An int** carries no length. n and m are two unrelated variables as far as the compiler knows. And the rows are n separate blocks scattered through memory, not one run of bytes.
On Cardinal it ran anyway, and the answer was right. The compiler's report shows it built a GPU version of the loop and copied no rows, so the GPU fetched rows out of the CPU's memory as it touched them: thousands of small unplanned trips instead of one planned one.
ON_DEVICE 1 CHECK legacy_ptrptr ok (error 51) ELAPSED 0.0283501
The fix: one block, and map clauses that say what crosses
stencil_flat(). Each grid is one block of n × m ints. Cell (j, i) lives at A[j * m + i].
int* A = new int[n * m]; // one block, every row int* Anew = new int[n * m](); #pragma omp target teams distribute parallel for \ map(to: A[0:n * m]) map(tofrom: Anew[0:n * m]) \ map(tofrom: error) reduction(max : error)for (long j = 1; j < n - 1; j++) { for (long i = 1; i < m - 1; i++) { Anew[j * m + i] = 0.25 * (A[j * m + i + 1] + A[j * m + i - 1] + A[(j - 1) * m + i] + A[(j + 1) * m + i]); error = std::fmax(error, std::fabs(double(Anew[j * m + i] - A[j * m + i]))); } }
Now step 1 has an answer. A goes over. Anew goes over and comes back: the loop never writes its edge cells, so their zeros have to make the trip. Choose a grid size.
| Version, n = m = 2,000, on Cardinal | Best of five | Answer |
|---|---|---|
legacy_ptrptr: rows fetched as they are touched | 28 ms | CHECK ok (error 51) |
stencil_flat: one block, 48 MB mapped | 5.1 ms | CHECK ok (error 51) |
The prediction for the fixed version was made before the run: 48 MB at the measured road speed of about 10 GB/s is a trip of about 5 ms. It measured 5.1 ms, trip included. The method could predict it because the code finally said what crosses.
Run it on Cardinal
Measure the three pieces yourself, then the two stencils. Branch lecture14. Everything runs as a batch job on one GPU; about 15 minutes, most of it waiting in the queue.
-
The lecture branch
git clone -o class git@gitlab.com:cs4540-spring-2019/cs-4170-5170-fall-2026/openmp.git openmpif you have not cloned yet, then:ssh <your-username>@cardinal.osc.edu cd openmp git fetch class git checkout -b lecture14 class/fa2026-openmp/lecture14
-
Predict first
Write down four numbers before you submit anything: one pass on the CPU, the trip over, one pass on the GPU, the trip back. Use the model or the measured table; say which.
Experiments
A · The three pieces
The job script loads the GPU compiler, builds, sets OMP_TARGET_OFFLOAD=MANDATORY, and runs every SAXPY method five times.
cd ~/openmp sbatch jobScript.slurm squeue -u $USER
When the job has left the queue:
cd ~/openmp
R=lec14/results
grep -h ON_DEVICE $R/saxpy_split_1e8_100.txt | sort | uniq -c
echo "saxpy_cpu, one pass (s): $(grep ELAPSED $R/saxpy_cpu_1e8_1.txt | awk '{print $2}' | sort -g | head -1)"
echo "T_IN (s): $(grep T_IN $R/saxpy_split_1e8_100.txt | awk '{print $2}' | sort -g | head -1)"
echo "T_KERNEL, one pass (s): $(grep T_KERNEL $R/saxpy_split_1e8_100.txt | awk '{print $6}' | sort -g | head -1)"
echo "T_OUT (s): $(grep T_OUT $R/saxpy_split_1e8_100.txt | awk '{print $2}' | sort -g | head -1)"What you should see
ON_DEVICE 1 five times: it ran on the GPU. A 0 means it ran on the CPU and the numbers mean nothing about a GPU. On Nov 23 the four lines were 0.0048511, 0.0918832, 0.000599 and 0.0352299 seconds. Yours will differ a little. Whichever piece is furthest from your prediction, name the speed you assumed wrong.
B · The stencil, two ways
Predict the fixed version's time first: 48 MB at the road speed you measured in A.
cd ~/openmp sbatch <<'EOF' #!/bin/bash #SBATCH --nodes=1 --ntasks=1 --cpus-per-task=48 --gpus-per-node=1 #SBATCH --time=00:10:00 -A PCS0313 --job-name=lec14_stencil --output=stencil.out module load nvhpc/25.1 cd Default && make GPU && cd .. export OMP_TARGET_OFFLOAD=MANDATORY OMP_NUM_THREADS=48 for t in 1 2 3 4 5; do ./Default/OMP_GPU legacy_ptrptr 2000 ./Default/OMP_GPU stencil_flat 2000 done EOF
When it has finished:
grep -E "CHECK|ELAPSED|BYTES_MAPPED|ON_DEVICE" ~/openmp/stencil.out
What you should see
Both versions print CHECK ... ok (error 51). Take the smallest ELAPSED of each. On Nov 23: legacy_ptrptr 0.0284 s, stencil_flat 0.0051 s with BYTES_MAPPED 0.048 GB. Same answer, about 5.5× apart.
C · No GPU, on purpose
On a machine with no GPU, such as your laptop. Do not time anything on a Cardinal login node.
cd Default && make all && cd .. OMP_NUM_THREADS=2 ./Default/OMP saxpy_split 1e7 1 OMP_TARGET_OFFLOAD=MANDATORY ./Default/OMP saxpy_split 1e7 1
What you should see
First run: ON_DEVICE 0, a transfer speed in the hundreds of thousands of GB/s or more, and CHECK saxpy_split ok. Nothing moved. Second run: the runtime refuses to start, because the only device is the CPU.
Check yourself
Six questions, instant feedback, nothing recorded.
map(to: x[0:n]) map(tofrom: y[0:n]), with n floats of 4 bytes each. How many bytes cross the road?target data around the pass loop change?ON_DEVICE 0, a transfer speed of a million GB/s, and CHECK ok. What happened?OMP_TARGET_OFFLOAD=MANDATORY makes that an error instead.int** rows has no map clause, ran on the GPU, and got the right answer. Why can the method not predict its time?