Certainly! I'll walk you through a working example of using the cl_arm_shared_virtual_memory extension in OpenCL to set a flag on the host, then read it on the GPU.
✅ Goal:
- Allocate a flag (an
int) using shared virtual memory (SVM).
- Set the flag to
1 on the host.
- The GPU kernel checks the flag and sets it to
1234 if it's 1.
- The host reads back the flag to verify that the kernel executed as expected.
ℹ️ Requirements
🧠 Concept Overview
Key API calls (extension):
clSharedMemAllocARM — allocates shared virtual memory.
clEnqueueMigrateMemARM — sync memory between host and device.
clSetKernelArgSVMPointerARM — pass a pointer directly to a kernel as an argument (SVM).
clMemFreeARM — frees the memory.
🧪 Full Example
🔧 Host Code (C):
#include <CL/cl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Extension function pointers
typedef void* (*clSharedMemAllocARM_fn)(cl_context, const cl_mem_properties*, size_t, cl_uint, cl_int*);
typedef cl_int (*clEnqueueMigrateMemARM_fn)(cl_command_queue, const void*, size_t, cl_mem_migration_flags, cl_uint, const cl_event*, cl_event*);
typedef cl_int (*clSetKernelArgSVMPointerARM_fn)(cl_kernel, cl_uint, const void*);
typedef cl_int (*clMemFreeARM_fn)(cl_context, void*);
const char* kernelSource =
"__kernel void check_flag(__global int* flag) {\n"
" if (flag[0] == 1) {\n"
" flag[0] = 1234;\n"
" }\n"
"}\n";
int main() {
cl_int err;
// 1. Platform, device, context, queue
cl_platform_id platform;
cl_device_id device;
cl_context context;
cl_command_queue queue;
err = clGetPlatformIDs(1, &platform, NULL);
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
queue = clCreateCommandQueue(context, device, 0, &err);
// 2. Get extension function pointers
clSharedMemAllocARM_fn clSharedMemAllocARM = (clSharedMemAllocARM_fn)
clGetExtensionFunctionAddressForPlatform(platform, "clSharedMemAllocARM");
clEnqueueMigrateMemARM_fn clEnqueueMigrateMemARM = (clEnqueueMigrateMemARM_fn)
clGetExtensionFunctionAddressForPlatform(platform, "clEnqueueMigrateMemARM");
clSetKernelArgSVMPointerARM_fn clSetKernelArgSVMPointerARM = (clSetKernelArgSVMPointerARM_fn)
clGetExtensionFunctionAddressForPlatform(platform, "clSetKernelArgSVMPointerARM");
clMemFreeARM_fn clMemFreeARM = (clMemFreeARM_fn)
clGetExtensionFunctionAddressForPlatform(platform, "clMemFreeARM");
if (!clSharedMemAllocARM || !clEnqueueMigrateMemARM || !clSetKernelArgSVMPointerARM || !clMemFreeARM) {
printf("Error: Failed to get extension function pointers.\n");
return 1;
}
// 3. Allocate shared memory for the flag
cl_int svm_err;
int* flag_ptr = (int*)clSharedMemAllocARM(context, NULL, sizeof(int), 0, &svm_err);
if (svm_err != CL_SUCCESS || !flag_ptr) {
printf("Error: Failed to allocate shared memory.\n");
return 1;
}
// Set the flag to 1 (host-side)
*flag_ptr = 1;
// Migrate memory host -> device
clEnqueueMigrateMemARM(queue, flag_ptr, sizeof(int), CL_MIGRATE_MEM_OBJECT_HOST, 0, NULL, NULL);
// 4. Create and build the kernel
cl_program program = clCreateProgramWithSource(context, 1, &kernelSource, NULL, &err);
err = clBuildProgram(program, 1, &device, "-cl-std=CL1.2 -cl-ext=+arm_shared_virtual_memory", NULL, NULL);
if (err != CL_SUCCESS) {
// Get build log
char log[2048];
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(log), log, NULL);
printf("Build error:\n%s\n", log);
return 1;
}
cl_kernel kernel = clCreateKernel(program, "check_flag", &err);
// 5. Set kernel argument via SVM pointer
clSetKernelArgSVMPointerARM(kernel, 0, flag_ptr);
const size_t global_work_size = 1;
clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_work_size, NULL, 0, NULL, NULL);
clFinish(queue);
// 6. Migrate memory device -> host to read the result
clEnqueueMigrateMemARM(queue, flag_ptr, sizeof(int), CL_MIGRATE_MEM_OBJECT_DEVICE, 0, NULL, NULL);
clFinish(queue);
printf("Flag value after kernel execution: %d\n", *flag_ptr); // Expected: 1234
// 7. Cleanup
clMemFreeARM(context, flag_ptr);
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseCommandQueue(queue);
clReleaseContext(context);
return 0;
}
✅ Output (Expected):
Flag value after kernel execution: 1234
🔍 Summary
| Step | Action |
|---|
| ✅ 1 | Allocate shared memory (clSharedMemAllocARM) |
| ✅ 2 | Write flag = 1 on host |
| ✅ 3 | Migrate memory HOST -> DEVICE |
| ✅ 4 | Run kernel that reads and modifies the flag |
| ✅ 5 | Migrate memory DEVICE -> HOST |
| ✅ 6 | Read and print the result |
🧰 Tips
- Make sure your Mali driver supports this extension (
cl_arm_shared_virtual_memory). You can use clGetPlatformInfo and clGetDeviceInfo to query supported extensions.
- If your device supports standard OpenCL 2.0 SVM, this code can be ported using
clSVMAlloc and standard APIs.
Would you like a version using OpenCL C++ wrappers, or an Android (NDK) compatible one?