0Pricing
CUDA Academy · Lesson

Anatomy of a Kernel

Signature, return type, and the void rule.

Anatomy of a Kernel is a free CUDA Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What a Kernel Is

A kernel is a single C++ function that runs on the GPU, executed at once by thousands of threads. You write it once, the hardware fans it out. 🚀

The __global__ Marker

You mark a kernel with the __global__ qualifier. It tells nvcc this function is called from the CPU but actually runs on the device.

__global__ void myKernel() {
    // runs on the GPU
}

Kernels Return void

Every kernel must return void. There is no return value to hand back, so results travel out through pointers to device memory instead.

__global__ void k() { /* void only */ }

Why Not Return a Value?

Thousands of threads run your kernel together, so a single return value would make no sense. Each thread writes its own slot of an output array.

Parameters Are by Value

Kernel parameters are copied to every thread, so pass small things: ints, sizes, and pointers. Never pass big objects by value.

__global__ void add(float* out, float* a, int n) {}

Pass Pointers, Not Arrays

To give a kernel an array, you pass a device pointer. The kernel reads and writes the GPU memory that pointer addresses.

__global__ void scale(float* data, float k) {}

Each Thread Picks Its Work

The same code runs in every thread, so each one uses its index to decide which element to touch. That is how one function covers a whole array.

int i = threadIdx.x; // who am I?

A Tiny Complete Kernel

Here is a full kernel: it doubles one element per thread. Notice the __global__ marker, the void return, and the pointer parameter all together.

__global__ void doubleIt(float* x) {
    int i = threadIdx.x;
    x[i] = x[i] * 2.0f;
}

Host Code Stays Separate

Your normal CPU function, often main, is host code. It sets things up and then asks the GPU to run the kernel.

int main() {
    // host side: prepare and launch
}

No Recursion or I/O Surprises

A kernel runs on hardware with tight rules, so keep it simple: avoid deep recursion and heavy standard-library calls inside it.

Naming Your Kernels

Treat a kernel like any function: give it a clear, verb-based name like vectorAdd. Good names make launches far easier to read.

__global__ void vectorAdd(float* c, float* a, float* b);

Quick Check

Let us check the kernel signature rules.

Recap: Kernel Anatomy

A kernel is a __global__ void function run by many threads. Pass pointers and small values, and let each thread use its index. Nicely done! 🎉

Frequently asked questions

Is the “Anatomy of a Kernel” lesson free?

Yes — the full text of “Anatomy of a Kernel” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.

What will I learn in “Anatomy of a Kernel”?

Signature, return type, and the void rule. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CUDA Academy?

No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Anatomy of a Kernel” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CUDA Academy lesson?

Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Anatomy of a Kernel
  2. The Triple-Angle-Bracket Launch
  3. printf Inside a Kernel
  4. cudaDeviceSynchronize Explained
← Back to CUDA Academy