Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #ifdef __APPLE__
  5. #include <OpenCL/opencl.h>
  6. #else
  7. #include <CL/cl.h>
  8. #endif
  9.  
  10. #define fileName "kernel.cl"
  11.  
  12. char openKernelFile(char* fn);
  13.  
  14.  
  15. //General Steps for OpenCL Programming
  16. //  1. Get the Platform and Devices
  17. //  2. Create the context for the device or devices you want to use
  18. //  3. Create the command queue for the context
  19. //  4. Create the program with source. Ie. Open the file containing the kernel functions this program will use
  20. //  5. Build the program
  21. //  6. Create the kernel you want to use in this program
  22.  
  23.  
  24. int main() {
  25.  
  26.     cl_int err;
  27.     cl_platform_id platformID;
  28.     cl_device_id deviceID;
  29.  
  30.     cl_context context;
  31.     cl_command_queue commandQ;
  32.     cl_program program;
  33.     cl_kernel kernel;
  34.  
  35.      char *initializeKernel = openKernelFile(fileName);
  36.  
  37.    
  38.     /* 1. Getting information about the platform and device to use */
  39.     err = clGetPlatformIDs(1, &platformID, NULL);
  40.     err = clGetDeviceIDs(platformID, CL_DEVICE_TYPE_GPU, 1, &deviceID, NULL);
  41.  
  42.     /* 2. Creating and checking the context */
  43.     context = clCreateContext(NULL, 1, &deviceID, NULL, NULL, &err);
  44.  
  45.     if (err == 0)
  46.         printf("Context created successfully\n");
  47.     else
  48.         printf("Context failed to create with error code: %d\n", err);
  49.    
  50.     /* 3. Creating and checking the Queue */
  51.  
  52.     commandQ = clCreateCommandQueue(context, deviceID, NULL, &err);
  53.  
  54.     if (err == 0)
  55.         printf("Command Queue created successfully\n");
  56.     else
  57.         printf("Command Queue failed to create with error code: %d\n", err);
  58.  
  59.     /* 4. Creating the program with source from file */
  60.  
  61.     program = clCreateProgramWithSource(context, 1, (const char**)&initializeKernel, NULL, &err);
  62.  
  63.     if (err == 0)
  64.         printf("Program creation from source file was done successfully\n");
  65.     else
  66.         printf("Program creating from source file has failed with error code: %d\n", err);
  67.  
  68.  
  69.     /* 5. Building the program */
  70.  
  71.     err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
  72.  
  73.     if (err == 0)
  74.         printf("Building the program was done successfully\n");
  75.     else
  76.         printf("Building the program has failed with error code: %d\n", err);
  77.  
  78.     /* 6. Create the kernel */
  79.  
  80.     kernel = clCreateKernel(program, "helloWorld", &err);
  81.  
  82.     if (err = 0)
  83.         printf("Creating the kernel was done successfully\n");
  84.     else
  85.         printf("Creating the kernel has failed with error code: %d", err);
  86.  
  87.  
  88.     return 0;
  89.  
  90.  
  91. }
  92.  
  93. char openKernelFile(char* fn) {
  94.  
  95.     FILE* kernelFile;
  96.     char* programSource;
  97.     size_t programSize;
  98.  
  99.     kernelFile = fopen(fn, "rb"); //Change this to r if ! using windows
  100.    
  101.     if (!kernelFile) {
  102.  
  103.         printf("Kernel file loading failed\nExiting Program\n");
  104.         exit(1);
  105.     }
  106.  
  107.     fseek(kernelFile, 0, SEEK_END);
  108.     programSize = ftell(kernelFile);
  109.     rewind(kernelFile);
  110.     programSource = (char*)malloc(programSize + 1);
  111.     programSource[programSize] = '\0';
  112.     fread(programSource, sizeof(char), programSize, kernelFile);
  113.    
  114.     fclose(kernelFile);
  115.  
  116.     return programSource;
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement