Ensure you have completed the installation steps to get started with XCENA SDK.
- Check device interface
xcena_cli num-device
xcena_cli device-info 0
- Check library installation
Find the following in your system.pkg-config
will be supported in the future./usr/local/lib/libpxl.so /usr/local/include/pxl/ /usr/local/lib/cmake/pxl/
Get Started with XCENA SDK
This section provides a simple C++ example according to the basic workflow.
For Rust example, refer to Hello Sort (Rust).
0. Setup repository
To set up an application repository, copy the contents of the app/template
folder to your desired project directory.
cp -r <sdk_root>/app/template /path/to/your/project
Template folder contains the following structure:
app/template/
├── CMakeLists.txt
├── Makefile
├── build.sh
├── main.cpp
└── mu_kernel
├── CMakeLists.txt
├── Makefile
└── kernel.cpp
CMakeLists.txt
: CMake configurations specifying how the host application and its dependencies should be compiled and linked.Makefile
: Build configurations for users who prefermake
over CMake. It provides commands to build both the kernel and host application.build.sh
: Script to automate the entire build process, including compiling the kernel and host application.main.cpp
: Source code for host application. Modify this file to implement your application logic and integrate compute kernels.kernel.cpp
: Source code for compute kernel. Modify this file to implement your compute kernels.
By using this template, you can quickly start to develop applications.
1. Build Your Compute Kernel
- Prepare source code to offload
Write a simple Compute Kernel code for the tasks to be offloaded inkernel.cpp
.
For example, a kernel to bypass data in parallel can be written as follows:#include "mu/mu.hpp" void data_copy(int* input, int* output, int size) { uint32_t taskIdx = mu::getTaskIdx(); for (int i = 0; i < size; i++) { int idx = taskIdx * size + i; output[idx] = input[idx]; } } MU_KERNEL_ADD(data_copy);
Note: MU_KERNEL_ADD must be included to initialize the kernel properly.
2. Create Your Host Application
- Prepare source code of host application
- Include API header.
#include "pxl/pxl.hpp"
- Configure kernel path, and arguments.
size
is the number of elements to process per task.numTasks
is the number of parallel tasks.const int size = 64; const int numTasks = 4; const char* kernelFile = "mu_kernel/mu_kernel.mubin";
- Setup PXL runtime instances.
uint32_t deviceId = 0; auto ctx = pxl::runtime::createContext(deviceId); auto job = ctx->createJob(); job->load(kernelFile);
- Prepare execution.
auto map = job->buildMap(numTasks);
- Allocate device memory.
int* input = reinterpret_cast<int*>(ctx->memAlloc(size * numTasks * sizeof(int))); int* output = reinterpret_cast<int*>(ctx->memAlloc(size * numTasks * sizeof(int)));
- Execute the kernel. After initializing input data as needed, execute the
map
.auto ret = map->execute(input, output, size); if (ret == false) { // failed to execute parallel return 1; } if (!map->synchronize()) { // failed to synchronize return 1; }
You can find a full example source code in
examples/data_copy/main.cpp
. - Include API header.
3. Build Your Application
The template provides multiple build options:
- Using the build.sh script (Recommended for beginners)
The simplest way to build your application is using the provided script:./build.sh
This script automatically builds both the MU kernel and host application in one step.
- Using CMake
If you need more control over the build process:mkdir -p build cd build cmake .. -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo ninja ninja install cd -
The CMakeLists.txt has been pre-configured to handle all necessary dependencies, including PXL library and the MU kernel.
4. Run and Check
- Run executable
./data_copy
- Verify results
Check the output data to confirm that the offloading process works correctly.Loaded kernel. [0] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 [1] 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 [2] 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 [3] 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 Compute time: 360.132000 ms
Next Steps
Now that you’ve learned the XCENA SDK workflow, explore the examples to design more complex applications.