Kernel Programming Guide

A kernel is a function that runs on an MU core. The following five sections cover kernel declarations and memory use.

  1. Include the Essential Header Include mu/mu.hpp for kernel registration and the mu:: functions.
    #include "mu/mu.hpp"
    
  2. Declare the Kernel Entry Point Mark each kernel with __pxl_kernel__ and compile the source with pxcc++. The annotation identifies the function that pxcc compiles for the device. The host launches that function by its C++ symbol through pxl::Launcher.

    __pxl_kernel__ void add_one(int* data)
    {
        data[mu::getTaskIdx()] += 1;
    }
    
  3. Keep Parameters in Check A kernel entry point accepts at most nine parameters.

  4. Declare Buffer Access Declare directional buffer parameters by value as mu::input_ptr<T> or mu::output_ptr<T>. Reference forms such as const mu::input_ptr<T>& are compile errors. An unmarked T* keeps Launcher’s bidirectional behavior. See Declare input and output buffers for a complete example and directional pointer arguments for synchronization and NDArray rules.

  5. Memory Constraints The kernel heap is limited to 3 MB, and the kernel stack is limited to 64 KB. Allocate a large task buffer on the host, and pass the device pointer to the kernel.

Writing MU kernels

Write the kernel as an ordinary C++ function, and mark it with __pxl_kernel__. Build the source with pxcc++, and the kernel then runs on the MU cores.

#include <algorithm>
#include "mu/mu.hpp"
#include "pxl/pxl.hpp"

__pxl_kernel__ void sort_with_ptr(int* arr, int size)
{
    int idx = mu::getTaskIdx();
    int* base = arr + idx * size;
    std::sort(base, base + size);
}

The host can launch this kernel by its C++ symbol with pxl::Launcher:

auto result = pxl::Launcher()
                  .execute<sort_with_ptr>(data, sortSize)
                  .tasks(testCount)
                  .run();

Launcher configures the runtime objects for the annotated kernel. The host therefore selects the kernel by its C++ symbol.

See Hello Sort for a complete walkthrough, and Compiler for annotations (__pxl_kernel__, __mu_device__, __mu_shared__) and multi-file builds.