Kernel Programming Guide
A kernel is a function that runs on an MU core. The following five sections cover kernel declarations and memory use.
- Include the Essential Header Include
mu/mu.hppfor kernel registration and themu::functions.#include "mu/mu.hpp" -
Declare the Kernel Entry Point Mark each kernel with
__pxl_kernel__and compile the source withpxcc++. The annotation identifies the function that pxcc compiles for the device. The host launches that function by its C++ symbol throughpxl::Launcher.__pxl_kernel__ void add_one(int* data) { data[mu::getTaskIdx()] += 1; } -
Keep Parameters in Check A kernel entry point accepts at most nine parameters.
-
Declare Buffer Access Declare directional buffer parameters by value as
mu::input_ptr<T>ormu::output_ptr<T>. Reference forms such asconst mu::input_ptr<T>&are compile errors. An unmarkedT*keeps Launcher’s bidirectional behavior. See Declare input and output buffers for a complete example and directional pointer arguments for synchronization and NDArray rules. - 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.