Write your first GPU value
Launch one thread, write one value, and read it back in main().
Which value does the CPU print?
The program writes a value in GPU memory, copies the result to CPU memory and prints it. The function below, hello, performs the GPU write. In CUDA C++, __global__ marks hello as a kernel—a GPU function the CPU can launch. The CPU launches one GPU thread to run hello. That thread writes 42.0f to output[0].
Read-only CUDA C++ · Kernel definition
__global__ void hello(float* output) {
output[0] = 42.0f;
}In float* output, output holds the address of the reserved GPU storage. output[0] is the float at that address. The f in 42.0f marks a float constant.
Launch the kernel from main()
The CPU code in main() reserves space for one float in GPU memory. It launches hello and passes the address of that space as output.
Read-only CUDA C++ · Launch inside main()
hello<<<1, 1>>>(output);The first 1 asks for one block, a group of GPU threads. The second 1 asks for one thread in that block.
Copy the result back before printing
The kernel writes output[0] in GPU memory. The CPU has a separate variable, value, initially set to 0.0f. Writing the GPU result does not change that CPU variable. The supplied cudaMemcpy call copies the result into value.
Read-only CUDA C++ · Copy and print inside main() (error checks omitted)
float value = 0.0f;
cudaMemcpy(&value, output, sizeof(value),
cudaMemcpyDeviceToHost);
std::printf("%.1f\n", value);&valuesupplies the address of the CPU destination.outputsupplies the address of the GPU source.sizeof(value)gives the number of bytes for this onefloat.cudaMemcpyDeviceToHostselects a copy from GPU memory to CPU memory.
For this copy, main() waits until the result is available in value, then std::printf prints it in Console. The complete program tests the status returned by allocation, launch and copy calls and reports any CUDA error. Leave those checks unchanged for this experiment. Lesson 4: From CPU array to GPU result explains allocation and copies for arrays.
The CUDA source editor opens with the complete starter or your saved draft. To run the starter again, use Editor tools → Restore starter. Choose Undo restore there to recover the previous draft.
Click Compile and run with the starter. Find 42.0 in Console.
Change the value
In hello in the CUDA source editor, change output[0] = 42.0f to output[0] = 7.0f. Predict the value in Console and click Compile and run again. Can the launch still use one thread?
Where does the kernel run?
A streaming multiprocessor (SM) is a part of the GPU chip that runs GPU threads. Here, the SM runs the thread that writes 42.0f in hello.
- CPU · main()Reserve space in GPU memory and launch hello.
- GPU · one threadAn SM runs hello and performs the assignment.
- GPU memory · output[0]The kernel stores one float.
- CPU · consolemain() copies back the float and prints it.
Which operation in main() moves the result across the CPU–GPU boundary?
Hardware context
The CPU launches hello. An SM runs the thread that writes output[0] in GPU memory. The CPU copies the stored value into its own memory before printing.
CPU side · CUDA host
Host ↔ device · copies move data; launches request execution
GPU device
GPU compute chip
SM · streaming multiprocessorThis lesson
Logical groups using this SM’s resources
Can run other blocks.
Blocks may be assigned to different SMs.
Chip ↔ device memory · data transfers
Simplified NVIDIA-style architecture. Boundaries show containment; arrows name relationships. Unit counts, block placement and timing vary by GPU. A thread is not a dedicated physical processor.
In the next lesson, threads run one kernel to process five values. How will each thread choose its own array element?
Follow the value through the GPU and back 2 min 16 sec · Video walkthrough, captions and transcript
Use fullscreen to inspect the code and diagram labels.
Read the transcript
Why is a copy needed?
The lesson supplies a complete starter program. This walkthrough explains how its GPU result reaches the Console. The GPU writes 42.0, but the CPU prints it. What connects those two steps?
The kernel runs on the GPU
hello is a kernel: a function that runs on the GPU. Its body writes 42.0 into output[0].
The starter prepares GPU storage
The supplied setup reserves space for that value in GPU memory. You can leave this setup unchanged.
The CPU launches hello
In main, the CPU launches hello. The two ones request one block containing one thread.
One thread executes on an SM
A thread is one execution of the kernel. An SM, or streaming multiprocessor, is GPU hardware that executes its instructions.
The thread writes GPU memory
The thread writes 42.0 into output[0] in GPU memory.
The CPU has a separate variable
The program also has a CPU variable named value. main initialises it to 0.0.
Pause before the copy
Suppose the GPU write and the CPU initialisation have both finished. The copy has not started. Pause here. Is the CPU variable value still 0.0, or has the GPU write changed it to 42.0?
The GPU write did not change value
value is still 0.0. The GPU wrote to output[0], a different location in GPU memory.
The copy transfers the result
The cudaMemcpy call copies the GPU result into value in CPU memory.
The copy completes before printing
For this call, main waits for the copy to complete before continuing. value now holds 42.0.
The CPU prints its copy
The CPU reads value and prints 42.0 in Console. The kernel itself did not print that line.
Execution, storage and transfer
The SM executes the thread. GPU memory holds its result. The copy makes that result available to CPU code.
Try tracing the same path
Return to the lesson. Find the assignment inside hello, then the copy and print calls inside main.
References kernel definitions · launching a kernel · explicit memory management
The browser exercises need no installed CUDA documentation version. For local execution, use the documentation matching your toolkit. External references need internet access.
- CUDA · kernel definitions (new tab)
__global__ declares a function that runs on the GPU.
Does main() run on the GPU because it launches hello?
Compare your answer
No. main() runs on the CPU; the launched kernel body runs on GPU threads.
- CUDA · launching a kernel (new tab)
hello<<<1, 1>>>(output) launches one block with one thread and passes the GPU storage address.
In hello<<<1, 1>>>(output), which parts request the thread count, and which part supplies the storage address?
Compare your answer
The first 1 requests one block and the second requests one thread in that block. output in parentheses passes the address of the GPU result storage to the kernel.
- CUDA · explicit memory management (new tab)
cudaMalloc reserves GPU storage; cudaMemcpy with cudaMemcpyDeviceToHost copies the result into CPU memory.
If hello stores 7.0f, can the CPU print that value from its own result variable before the copy back?
Compare your answer
No. The kernel writes GPU storage. The device-to-host copy places the result in the CPU variable before main prints it.
Further GPU investigation Run this CUDA example on an NVIDIA GPU
In the CUDA source editor, open Editor tools and choose Export .cu to save the complete program. Use an NVIDIA GPU and a matching CUDA toolkit to run it outside the browser.
Browser simulation checks program behaviour; physical GPU timing requires a hardware measurement.
Enable JavaScript to save completion.