Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #ifdef __APPLE__
- #include <OpenCL/opencl.h>
- #else
- #include <CL/cl.h>
- #endif
- #define fileName "kernel.cl"
- char openKernelFile(char* fn);
- //General Steps for OpenCL Programming
- // 1. Get the Platform and Devices
- // 2. Create the context for the device or devices you want to use
- // 3. Create the command queue for the context
- // 4. Create the program with source. Ie. Open the file containing the kernel functions this program will use
- // 5. Build the program
- // 6. Create the kernel you want to use in this program
- int main() {
- cl_int err;
- cl_platform_id platformID;
- cl_device_id deviceID;
- cl_context context;
- cl_command_queue commandQ;
- cl_program program;
- cl_kernel kernel;
- char *initializeKernel = openKernelFile(fileName);
- /* 1. Getting information about the platform and device to use */
- err = clGetPlatformIDs(1, &platformID, NULL);
- err = clGetDeviceIDs(platformID, CL_DEVICE_TYPE_GPU, 1, &deviceID, NULL);
- /* 2. Creating and checking the context */
- context = clCreateContext(NULL, 1, &deviceID, NULL, NULL, &err);
- if (err == 0)
- printf("Context created successfully\n");
- else
- printf("Context failed to create with error code: %d\n", err);
- /* 3. Creating and checking the Queue */
- commandQ = clCreateCommandQueue(context, deviceID, NULL, &err);
- if (err == 0)
- printf("Command Queue created successfully\n");
- else
- printf("Command Queue failed to create with error code: %d\n", err);
- /* 4. Creating the program with source from file */
- program = clCreateProgramWithSource(context, 1, (const char**)&initializeKernel, NULL, &err);
- if (err == 0)
- printf("Program creation from source file was done successfully\n");
- else
- printf("Program creating from source file has failed with error code: %d\n", err);
- /* 5. Building the program */
- err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
- if (err == 0)
- printf("Building the program was done successfully\n");
- else
- printf("Building the program has failed with error code: %d\n", err);
- /* 6. Create the kernel */
- kernel = clCreateKernel(program, "helloWorld", &err);
- if (err = 0)
- printf("Creating the kernel was done successfully\n");
- else
- printf("Creating the kernel has failed with error code: %d", err);
- return 0;
- }
- char openKernelFile(char* fn) {
- FILE* kernelFile;
- char* programSource;
- size_t programSize;
- kernelFile = fopen(fn, "rb"); //Change this to r if ! using windows
- if (!kernelFile) {
- printf("Kernel file loading failed\nExiting Program\n");
- exit(1);
- }
- fseek(kernelFile, 0, SEEK_END);
- programSize = ftell(kernelFile);
- rewind(kernelFile);
- programSource = (char*)malloc(programSize + 1);
- programSource[programSize] = '\0';
- fread(programSource, sizeof(char), programSize, kernelFile);
- fclose(kernelFile);
- return programSource;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement