Here's a minimal and clear example of using the cl_arm_shared_virtual_memory extension to set a flag from the host to the GPU, using Fine-Grain System SVM (shared virtual memory) on an ARM-based OpenCL device.
✅ Key Concepts
- Shared Virtual Memory (SVM): Allows the host and device to share a single memory region.
cl_arm_shared_virtual_memory: Enables SVM on ARM devices.
clSVMAllocARM: Allocates SVM memory.
clSetKernelArgSVMArm: Passes SVM pointers to the kernel.
clEnqueueSVMMemFlushARM: Ensures the host's write is visible to the device.
🔹 1. OpenCL Kernel (kernel.cl)
#pragma OPENCL EXTENSION cl_arm_shared_virtual_memory : enable
__kernel void set_flag(__attribute__((svm_ptr)) int* flag) {
if (*flag == 1) {
// Flag is set, perform some action
*flag = 2; // Modify the flag (optional)
}
}
🔹 2. Host Code (main.c)
#include <stdio.h>
#include <CL/cl.h>
#define CHECK_ERR(err) \
if (err != CL_SUCCESS) { \
fprintf(stderr, "OpenCL Error: %d\n", err); \
exit(EXIT_FAILURE); \
}
int main() {
cl_int err;
cl_platform_id platform;
cl_device_id device;
cl_context context;
cl_command_queue queue;
cl_program program;
cl_kernel kernel;
// 1. Get Platform and Device
err = clGetPlatformIDs(1, &platform, NULL);
CHECK_ERR(err);
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
CHECK_ERR(err);
// 2. Check for SVM support
char ext[1024];
err = clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, sizeof(ext), ext, NULL);
CHECK_ERR(err);
if (strstr(ext, "cl_arm_shared_virtual_memory") == NULL) {
fprintf(stderr, "Error: cl_arm_shared_virtual_memory not supported.\n");
return EXIT_FAILURE;
}
// 3. Create Context and Command Queue
context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
CHECK_ERR(err);
queue = clCreateCommandQueue(context, device, 0, &err);
CHECK_ERR(err);
// 4. Load and Build Kernel
const char* kernel_src = "__kernel void set_flag(__attribute__((svm_ptr)) int* flag) { if (*flag == 1) *flag = 2; }";
program = clCreateProgramWithSource(context, 1, &kernel_src, NULL, &err);
CHECK_ERR(err);
err = clBuildProgram(program, 1, &device, "-cl-std=CL2.0", NULL, NULL);
CHECK_ERR(err);
kernel = clCreateKernel(program, "set_flag", &err);
CHECK_ERR(err);
// 5. Allocate SVM Memory
int* flag = (int*)clSVMAllocARM(context, CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_SYSTEM_ARM, sizeof(int), 0, &err);
CHECK_ERR(err);
// 6. Set Flag from Host
*flag = 1; // Host writes to SVM pointer
clEnqueueSVMMemFlushARM(queue, 1, (void**)&flag, NULL, 0, NULL, NULL); // Flush to device
// 7. Set Kernel Argument
err = clSetKernelArgSVMArm(kernel, 0, (void*)flag);
CHECK_ERR(err);
// 8. Run Kernel
size_t global_size = 1;
err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_size, NULL, 0, NULL, NULL);
CHECK_ERR(err);
// 9. Wait for Completion
clFinish(queue);
// 10. Read Result
printf("Flag after kernel: %d\n", *flag); // Should be 2
// 11. Cleanup
clSVMFreeARM(context, flag);
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseCommandQueue(queue);
clReleaseContext(context);
return 0;
}
🧪 How to Compile and Run
- Save the kernel code as
kernel.cl.
- Compile the host code:
gcc main.c -o main -lOpenCL
- Run the executable:
./main
📌 Expected Output
Flag after kernel: 2
📝 Explanation
- The host writes
*flag = 1 directly to the shared memory.
clEnqueueSVMMemFlushARM ensures the device sees the updated value.
- The kernel reads and modifies the flag.
- The host then reads the modified value, confirming the GPU executed the logic.
This example demonstrates direct memory sharing between host and GPU without explicit buffer copies or synchronization.