Advertisement
Guest User

Untitled

a guest
Sep 14th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.25 KB | None | 0 0
  1. #include "include/types.h"
  2. #include "include/gaussian.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6.  
  7. #include <sys/types.h> // mkdir
  8. #include <sys/stat.h> // mkdir
  9.  
  10. #ifdef __APPLE__
  11. #include <OpenCL/opencl.h>
  12. #else
  13. #include <CL/cl.h>
  14. #endif
  15.  
  16. // -load kernel
  17. bool loadKernels(char ** path, FILES * files, int count)
  18. {
  19.  
  20.     cl_int err;//the openCL error code/s
  21.     int c; // number of file
  22.  
  23.     /*********************************************
  24.             1. OPENCL PREPARATION
  25.     *********************************************/
  26.     //Get platform and device information
  27.     cl_platform_id platformID;//will hold the ID of the openCL available platform
  28.     cl_uint platformsN;//will hold the number of openCL available platforms on the machine
  29.     cl_context_properties cprops[3];
  30.     cl_device_id deviceID;
  31.  
  32.     if(clGetPlatformIDs(1, &platformID, &platformsN) != CL_SUCCESS)
  33.     {
  34.         printf("Could not get the OpenCL Platform IDs\n");
  35.         return false;
  36.     }
  37.  
  38.     cprops[0] = CL_CONTEXT_PLATFORM;
  39.     cprops[1] = (cl_context_properties)platformID;
  40.     cprops[2] = (cl_context_properties)NULL;
  41.  
  42.     cl_program program;
  43.  
  44.     /***************************************************
  45.             2. OPENCL CREATE CONTEXT
  46.     ***************************************************/
  47.  
  48.     cl_context context; // Create an OpenCL context
  49.  
  50.     /* create a context with all of the available devices. */
  51.     context = clCreateContextFromType( cprops, CL_DEVICE_TYPE_ALL, NULL, NULL, &err );
  52.  
  53.     // cl_context context = clCreateContext( NULL, 1, &deviceID, NULL, NULL, &err);
  54.     if(err != CL_SUCCESS)
  55.     {
  56.         printf("Could not create a valid OpenCL context\n");
  57.         return false;
  58.     }
  59.  
  60.  
  61.     /* STARTING files LOOP */
  62.  
  63.  
  64.     for( c=0; c < count; c++ )
  65.     {
  66.  
  67.     /***************************************************
  68.             3. READ BINARY DATA & close file
  69.     ***************************************************/
  70.  
  71.     FILE* f;
  72.     char* kernelBinary;
  73.     size_t kernelBinarySize;
  74.     kernelBinary = malloc(MAX_BINARY_SIZE);
  75.     char kernel_path [KERNEL_TOTAL_FILE_PATH_SIZE];
  76.     if (strlen(*path) + 1 + strlen(files[c].filename) + 3 > KERNEL_TOTAL_FILE_PATH_SIZE-1 )
  77.     {
  78.        sprintf("WARNNING: File path to kernel binary is too long %s/%s. 511 bytes total is maximum. This kernel cannot be compiled.\n",kernel_path,files[c].filename);
  79.        continue;
  80.     }
  81.     sprintf(kernel_path, "%s/%s", *path, files[c].filename);
  82.     // sprintf(files[count].filename,"%s_%d.bin",files[count].filename,settings->load_device_id);
  83.  
  84.     if( (f = fopen(kernel_path, "r")) == NULL)
  85.     {
  86.         fprintf(stderr, "Failed to load OpenCL binary kernel %s.\n",*kernel_path);
  87.         return false;
  88.     }
  89.     kernelBinarySize = fread( kernelBinary, 1, MAX_BINARY_SIZE, f);
  90.     fclose(f);
  91.  
  92.  
  93.     /**************************************************************
  94.             4. OPENCL - LOAD BINARY PROGRAM
  95.     **************************************************************/
  96.  
  97.     //Create a program object and associate it with the kernel's source code.
  98.  
  99.     /******************************************
  100.     Load the generated binary kernels
  101.     into an OpenCL program. */
  102.     /* If you are loading multiple binary
  103.     kernels at the same time, the binary
  104.     kernels in the array must be in the
  105.     same order as the matching devices
  106.     in the device array. */
  107.     /*******************************************/
  108.  
  109.     cl_program program;
  110.     // arg. 5: expected const unsigned char **
  111. //    program = clCreateProgramWithBinary( context, 1, &deviceID, &kernelBinarySize, &(*kernelBinary), NULL, &err );
  112.     program = clCreateProgramWithBinary( context, 1, &deviceID, &kernelBinarySize, &(*kernelBinary), NULL, &err );
  113.     if ( program == NULL )
  114.         {
  115.             puts("Cannot create program: clCreateProgramWithBinary fail. program is NULL");
  116.             return -5;
  117.         }
  118.     /**************************************************************
  119.             5. OPENCL - BUILD PROGRAM
  120.     **************************************************************/
  121.  
  122.     // setup the necessary internal program state:
  123.     err = clBuildProgram( program, 1, &deviceID, NULL, NULL, NULL );
  124.  
  125.     /**************************************************************
  126.             6. OPENCL - CALL PROGRAM
  127.     **************************************************************/
  128.  
  129.     } /* end of FOR files loop */
  130.     /*
  131.     // @DELETE:
  132.     ///Set the arguments of the kernel
  133.     if(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&gpuImg) != CL_SUCCESS)
  134.     {
  135.         printf("Could not set the kernel's \"gpuImg\" argument\n");
  136.         return false;
  137.     }
  138.     if(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&gpuGaussian) != CL_SUCCESS)
  139.     {
  140.         printf("Could not set the kernel's \"gpuGaussian\" argument\n");
  141.         return false;
  142.     }
  143.     if(clSetKernelArg(kernel, 2, sizeof(int), (void *)&bmp.imgWidth) != CL_SUCCESS)
  144.     {
  145.         printf("Could not set the kernel's \"imageWidth\" argument\n");
  146.         return false;
  147.     }
  148.     if(clSetKernelArg(kernel, 3, sizeof(int), (void *)&bmp.imgHeight) != CL_SUCCESS)
  149.     {
  150.         printf("Could not set the kernel's \"imgHeight\" argument\n");
  151.         return false;
  152.     }
  153.     if(clSetKernelArg(kernel,4,sizeof(int),(void*)&size) != CL_SUCCESS)
  154.     {
  155.         printf("Could not set the kernel's \"gaussian size\" argument\n");
  156.         return false;
  157.     }
  158.     if(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *)&gpuNewImg) != CL_SUCCESS)
  159.     {
  160.         printf("Could not set the kernel's \"gpuNewImg\" argument\n");
  161.         return false;
  162.     }
  163.     */
  164.  
  165.     /*
  166.     @DELETE:
  167.     ///enqueue the kernel into the OpenCL device for execution
  168.     size_t globalWorkItemSize = imgSize;//the total size of 1 dimension of the work items. Basically the whole image buffer size
  169.     size_t workGroupSize = 64; //The size of one work group
  170.     err = clEnqueueNDRangeKernel(cmdQueue, kernel, 1, NULL, &globalWorkItemSize, &workGroupSize,0, NULL, NULL);
  171.     */
  172.  
  173.     ///Clean up everything
  174.     // clReleaseProgram(program);
  175.     clReleaseContext(context);
  176.     ///save the new image and return success
  177.     // @DELETE
  178.     // bmp.imgData = newData;
  179.     // meImageBMP_Save(&bmp,"gpu_blur.bmp");
  180.     return true;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement