CUDA C++ LabGPU architecture

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);
  • &value supplies the address of the CPU destination. output supplies the address of the GPU source.
  • sizeof(value) gives the number of bytes for this one float. cudaMemcpyDeviceToHost selects 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.

Follow the result from launch to console
  1. CPU · main()Reserve space in GPU memory and launch hello.
  2. GPU · one threadAn SM runs hello and performs the assignment.
  3. GPU memory · output[0]The kernel stores one float.
  4. 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.

Computer / system · host and GPU device

CPU side · CUDA host

CPUmain() and CUDA calls.
System RAMCPU memory for arrays.

GPU device

GPU compute chip

SM · streaming multiprocessorThis lesson
Assigned blocks → threads
Logical groups using this SM’s resources
Other SMs

Can run other blocks.

Blocks may be assigned to different SMs.

Device memoryGPU arrays; separate from the compute chip.

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.

GPU setup and tools

Browser simulation checks program behaviour; physical GPU timing requires a hardware measurement.

Enable JavaScript to save completion.

App status

Course status

Checking for updates

Version and download details

Downloads include all lessons, recordings, diagrams, examples, browser simulators and the CUDA compiler. Browser storage overhead is additional. External links need internet access.

Running examples outside the browser needs separate PyTorch or CUDA/GPGPU-Sim tools.

Add CUDA C++ Lab to your apps

Use your browser’s install option for an app icon and a separate window. Course status shows whether the course is downloaded and ready to use offline.