CS 4170 / 5170 · Week 14 · Monday, Nov 23

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.

Where do the bytes live, and what does it cost to move them? On a GPU that question decides everything, before the first line of the loop runs.

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.

WhatSpeed used in the modelWhere the number comes from
The CPU's memory200 GB/sone socket's published peak is 307 GB/s; real code gets about two thirds
The GPU's own memory3,000 GB/sthe vendor lists 3,350 GB/s for the H100
The road between them50 GB/sthe 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).


  
Thanksgiving version: a kitchen across town cooks 15× faster than yours. Every ingredient is driven there, and every dish driven back, on a slow road. Is it worth the drive? It depends how much cooking.

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.

  1. Count the bytes that cross the road.

    Read the map clauses: to = copied to the GPU, from = copied back, tofrom = both.

  2. 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.

  3. Add it up for your number of passes.

    GPU = one trip + every pass on the GPU. CPU = every pass on the CPU.

  4. Measure.

    Run it with the trip and the passes timed separately. Compare with steps 2 and 3.

Ship it only if the GPU total is smaller. The next three sections run all four steps on one loop, one size, one machine.

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
WordWhat it does
targetrun the loop on the GPU, not on the CPU's threads
teams distribute parallel forWeek 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
Step 1. n = 100 million floats, 4 bytes each. How many bytes cross the road?

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.

Step 2. The road carries 50 GB/s. The CPU's memory moves 200 GB/s and the GPU's moves 3,000 GB/s. The loop does one multiply and one add per element, so it spends its time waiting on memory. Sent to the GPU once, how does it compare with 48 CPU cores?

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 thisBytesAt this speedTime (model)
The trip: x and y over, y back1.2 GB50 GB/s (the road)24 ms
One pass on the CPU, 48 cores1.2 GB200 GB/s (the CPU's memory)6 ms
One pass on the GPU1.2 GB3,000 GB/s (the GPU's memory)0.4 ms
GPU total, one pass: trip + pass24.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.

The GPU is not slow. Shipping a byte costs more than using it where it is, because the road is slower than the CPU's own memory. You would never scatter an array in MPI to do one add per element and gather it back, for the same reason.

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.

The CPU needs 600 ms for 100 passes (6 ms each). With the data staying on the GPU, what is the GPU's speedup at 100 passes (model)?

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.


  
In the model the GPU is ahead from the 5th pass, and the most it can ever win is 15× = 3,000 ÷ 200: the two memory speeds, not the number of cores. This loop waits on memory on both machines, and more workers do not make memory faster.

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.

Three speeds went into the model: the CPU's memory, the GPU's memory, and the road. Before you look at the measurements: which one is most likely to be wrong, and why?

Both memory speeds were close. Both halves of the trip were about five times slower than the model.

PieceModelMeasuredThe speed the measurement implies
One pass on the CPU, 48 cores6 ms4.9 msclose: 247 GB/s (assumed 200)
The trip over: x and y to the GPU16 ms92 mswrong, 5.7× slower: 8.7 GB/s (assumed 50)
One pass on the GPU0.4 ms0.6 msclose: 2,000 GB/s (assumed 3,000)
The trip back: y to the CPU8 ms35 mswrong, 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

QuestionThe model saidFrom the measured pieces
Sent once4× slower26× slower: 127.7 ms against 4.9 ms
The GPU is ahead from pass530: 127 ms of trip, and each pass saves 4.25 ms
Speedup at 100 passes9.4×2.6×: 187 ms against 485 ms
The most it can ever win15×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.

The model had the right three pieces and the right loser, and one bad number. The run disagreed with the prediction, and the disagreement named exactly one assumption. That is what step 4 is for.

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
The road carries tens of GB/s at best. This run says 0.8 GB crossed at 1,320,000 GB/s, and the answer is right. What happened?

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 available
Build this the ordinary way on a machine with a GPU and you can get a CPU timing that you will call a GPU timing. Set OMP_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.

A. SAXPY, 100 million floats, 3 passes. The data stays on the GPU. 1.2 GB crosses, once. CPU pass 6 ms, GPU pass 0.4 ms.

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.

B. SAXPY, 100 million floats, 100 passes. y comes back to the CPU after every pass, to be saved to a file. 0.8 GB goes over once; then 0.4 GB comes back on every pass. CPU pass 6 ms, GPU pass 0.4 ms.

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.

C. A different program: multiply two 8,192 × 8,192 matrices of floats, once. 0.8 GB crosses: two matrices over, one back. One pass: CPU 180 ms, GPU 18 ms (both are a model).

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])));
    }
}
Step 1 says: read the map clauses. Look at the pragma. How many bytes does this code say will cross the road?

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 CardinalBest of fiveAnswer
legacy_ptrptr: rows fetched as they are touched28 msCHECK ok (error 51)
stencil_flat: one block, 48 MB mapped5.1 msCHECK 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.

A right answer tells you nothing about what moved. What you ship must be one run of bytes, and the code must say how long it is. Week 6 said the same thing about MPI datatypes.

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.

  1. The lecture branch

    git clone -o class git@gitlab.com:cs4540-spring-2019/cs-4170-5170-fall-2026/openmp.git openmp if 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
  2. 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.

Keep your four measured numbers. Mon Nov 30 writes the same three pieces by hand in CUDA and times them again.

Check yourself

Six questions, instant feedback, nothing recorded.

1. map(to: x[0:n]) map(tofrom: y[0:n]), with n floats of 4 bytes each. How many bytes cross the road?
x goes over (4n). y goes over and comes back (8n). 12n in all: 1.2 GB when n is 100 million.
2. In the model the GPU's memory is 15× faster than the CPU's, yet SAXPY sent once is 4× slower on the GPU. Why?
Trip 24 ms, GPU pass 0.4 ms, CPU pass 6 ms. The road (50 GB/s) is slower than the CPU's own memory (200 GB/s), so shipping a byte costs more than using it where it is.
3. What does target data around the pass loop change?
The copies happen at the region's braces. The target loops inside find the data already there. With the map clause left inside the loop, every pass pays the trip and the GPU is 0.25× at every pass count.
4. Cardinal measured the trip at 127 ms where the model said 24 ms, and both passes within a factor of 1.5. What does that tell you?
The pieces were the right pieces. The road ran at about 9 to 11 GB/s, not 50. Timing the pieces separately is what lets a disagreement name its cause. With the measured pieces the GPU is ahead from the 30th pass, not the 5th.
5. A program prints ON_DEVICE 0, a transfer speed of a million GB/s, and CHECK ok. What happened?
Host fallback: with no GPU, a target loop may run on the CPU. Same memory, no copy, right answer. OMP_TARGET_OFFLOAD=MANDATORY makes that an error instead.
6. The stencil on int** rows has no map clause, ran on the GPU, and got the right answer. Why can the method not predict its time?
You can count what the loop touches. You cannot read from the code what crosses the road. The rows came over as they were touched, and it took 28 ms. With one block per grid and explicit map clauses, 48 MB crosses and the loop takes 5.1 ms, as predicted.
← → keys also work