You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.0 KiB
71 lines
2.0 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#ifdef __APPLE__
|
|
#include <OpenCL/opencl.h>
|
|
#else
|
|
#include <CL/cl.h>
|
|
#endif
|
|
|
|
#define KERNEL "part4.clbin"
|
|
|
|
int main() {
|
|
|
|
cl_platform_id platform; cl_device_id device; cl_context context;
|
|
cl_program program; cl_kernel kernel; cl_command_queue queue;
|
|
cl_mem kernelBuffer;
|
|
|
|
FILE* programHandle; char *programBuffer; char *programLog;
|
|
size_t programSize; char hostBuffer[32];
|
|
|
|
// get first available sdk and gpu and create context
|
|
clGetPlatformIDs(1, &platform, NULL);
|
|
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
|
|
context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);
|
|
|
|
// get size of kernel source
|
|
programHandle = fopen(KERNEL, "rb");
|
|
fseek(programHandle, 0, SEEK_END);
|
|
programSize = ftell(programHandle);
|
|
rewind(programHandle);
|
|
|
|
// read kernel source into buffer
|
|
programBuffer = (char*) malloc(programSize + 1);
|
|
programBuffer[programSize] = '\0';
|
|
assert (programSize == fread(programBuffer, sizeof(char), programSize, programHandle));
|
|
|
|
fclose(programHandle);
|
|
|
|
// create and build program
|
|
program = clCreateProgramWithBinary(context, 1, &device,
|
|
(const size_t*)&programSize, (const unsigned char **) &programBuffer, NULL, NULL);
|
|
free(programBuffer);
|
|
|
|
// create kernel and command queue
|
|
kernel = clCreateKernel(program, "hello", NULL);
|
|
queue = clCreateCommandQueue(context, device, 0, NULL);
|
|
|
|
// create kernel argument buffer and set it into kernel
|
|
kernelBuffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY,
|
|
32 * sizeof(char), NULL, NULL);
|
|
clSetKernelArg(kernel, 0, sizeof(cl_mem), &kernelBuffer);
|
|
|
|
// execute kernel, read back the output and print to screen
|
|
clEnqueueTask(queue, kernel, 0, NULL, NULL);
|
|
clEnqueueReadBuffer(queue, kernelBuffer, CL_TRUE, 0,
|
|
32 * sizeof(char), hostBuffer, 0, NULL, NULL);
|
|
puts(hostBuffer);
|
|
|
|
clFlush(queue);
|
|
clFinish(queue);
|
|
clReleaseKernel(kernel);
|
|
clReleaseProgram(program);
|
|
clReleaseMemObject(kernelBuffer);
|
|
clReleaseCommandQueue(queue);
|
|
clReleaseContext(context);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// vim: set ft=c ts=4 sw=4:
|