Skip to content

Instantly share code, notes, and snippets.

@simark
Created May 5, 2025 14:44
Show Gist options
  • Save simark/6c1a7cc15cd72efc3511ea1e84b9e035 to your computer and use it in GitHub Desktop.
Save simark/6c1a7cc15cd72efc3511ea1e84b9e035 to your computer and use it in GitHub Desktop.
Illustration of OpenCL's all() function
// g++ host.cpp -lOpenCL && ./a.out
#include <CL/cl.h>
#include <stdio.h>
#include <stdlib.h>
#define VECTOR_SIZE 4
typedef struct {
int x, y, z, w;
} int4;
const char *kernelSource = "__kernel void test_all_function(__global const "
"int4* input, __global int* result) {\n"
" int gid = get_global_id(0);\n"
" int4 vec = input[gid];\n"
" result[gid] = all(vec);\n"
"}\n";
int main() {
// Input data
int4 inputData[] = {
{-1, -1, -1, -1},
{0x7fffffff, -1, -1, -1},
};
const int numElements = sizeof(inputData) / sizeof(int4);
int results[numElements];
// OpenCL setup
cl_platform_id platform;
cl_device_id device;
cl_context context;
cl_command_queue queue;
cl_program program;
cl_kernel kernel;
cl_int err;
cl_mem inputBuffer =
clCreateBuffer(NULL, 0, 0, NULL, NULL); // avoid uninitialized warning
err = clGetPlatformIDs(1, &platform, NULL);
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_DEFAULT, 1, &device, NULL);
context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
queue = clCreateCommandQueue(context, device, 0, &err);
inputBuffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
sizeof(int4) * numElements, inputData, &err);
cl_mem resultBuffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY,
sizeof(int) * numElements, NULL, &err);
program = clCreateProgramWithSource(context, 1, &kernelSource, NULL, &err);
err = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
// Show build log if error
if (err != CL_SUCCESS) {
char log[2048];
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(log),
log, NULL);
printf("Build Log:\n%s\n", log);
return 1;
}
kernel = clCreateKernel(program, "test_all_function", &err);
err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &inputBuffer);
err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &resultBuffer);
size_t globalSize = numElements;
err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &globalSize, NULL, 0,
NULL, NULL);
clFinish(queue);
err = clEnqueueReadBuffer(queue, resultBuffer, CL_TRUE, 0,
sizeof(int) * numElements, results, 0, NULL, NULL);
// Print results
for (int i = 0; i < numElements; i++) {
printf("int4(0x%x, 0x%x, 0x%x, 0x%x) => all() = %d\n", inputData[i].x,
inputData[i].y, inputData[i].z, inputData[i].w, results[i]);
}
// Cleanup
clReleaseMemObject(inputBuffer);
clReleaseMemObject(resultBuffer);
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseCommandQueue(queue);
clReleaseContext(context);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment