Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "include/types.h"
- #include "include/gaussian.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <sys/types.h> // mkdir
- #include <sys/stat.h> // mkdir
- #ifdef __APPLE__
- #include <OpenCL/opencl.h>
- #else
- #include <CL/cl.h>
- #endif
- // -load kernel
- bool loadKernels(char ** path, FILES * files, int count)
- {
- cl_int err;//the openCL error code/s
- int c; // number of file
- /*********************************************
- 1. OPENCL PREPARATION
- *********************************************/
- //Get platform and device information
- cl_platform_id platformID;//will hold the ID of the openCL available platform
- cl_uint platformsN;//will hold the number of openCL available platforms on the machine
- cl_context_properties cprops[3];
- cl_device_id deviceID;
- if(clGetPlatformIDs(1, &platformID, &platformsN) != CL_SUCCESS)
- {
- printf("Could not get the OpenCL Platform IDs\n");
- return false;
- }
- cprops[0] = CL_CONTEXT_PLATFORM;
- cprops[1] = (cl_context_properties)platformID;
- cprops[2] = (cl_context_properties)NULL;
- cl_program program;
- /***************************************************
- 2. OPENCL CREATE CONTEXT
- ***************************************************/
- cl_context context; // Create an OpenCL context
- /* create a context with all of the available devices. */
- context = clCreateContextFromType( cprops, CL_DEVICE_TYPE_ALL, NULL, NULL, &err );
- // cl_context context = clCreateContext( NULL, 1, &deviceID, NULL, NULL, &err);
- if(err != CL_SUCCESS)
- {
- printf("Could not create a valid OpenCL context\n");
- return false;
- }
- /* STARTING files LOOP */
- for( c=0; c < count; c++ )
- {
- /***************************************************
- 3. READ BINARY DATA & close file
- ***************************************************/
- FILE* f;
- char* kernelBinary;
- size_t kernelBinarySize;
- kernelBinary = malloc(MAX_BINARY_SIZE);
- char kernel_path [KERNEL_TOTAL_FILE_PATH_SIZE];
- if (strlen(*path) + 1 + strlen(files[c].filename) + 3 > KERNEL_TOTAL_FILE_PATH_SIZE-1 )
- {
- 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);
- continue;
- }
- sprintf(kernel_path, "%s/%s", *path, files[c].filename);
- // sprintf(files[count].filename,"%s_%d.bin",files[count].filename,settings->load_device_id);
- if( (f = fopen(kernel_path, "r")) == NULL)
- {
- fprintf(stderr, "Failed to load OpenCL binary kernel %s.\n",*kernel_path);
- return false;
- }
- kernelBinarySize = fread( kernelBinary, 1, MAX_BINARY_SIZE, f);
- fclose(f);
- /**************************************************************
- 4. OPENCL - LOAD BINARY PROGRAM
- **************************************************************/
- //Create a program object and associate it with the kernel's source code.
- /******************************************
- Load the generated binary kernels
- into an OpenCL program. */
- /* If you are loading multiple binary
- kernels at the same time, the binary
- kernels in the array must be in the
- same order as the matching devices
- in the device array. */
- /*******************************************/
- cl_program program;
- // arg. 5: expected const unsigned char **
- // program = clCreateProgramWithBinary( context, 1, &deviceID, &kernelBinarySize, &(*kernelBinary), NULL, &err );
- program = clCreateProgramWithBinary( context, 1, &deviceID, &kernelBinarySize, &(*kernelBinary), NULL, &err );
- if ( program == NULL )
- {
- puts("Cannot create program: clCreateProgramWithBinary fail. program is NULL");
- return -5;
- }
- /**************************************************************
- 5. OPENCL - BUILD PROGRAM
- **************************************************************/
- // setup the necessary internal program state:
- err = clBuildProgram( program, 1, &deviceID, NULL, NULL, NULL );
- /**************************************************************
- 6. OPENCL - CALL PROGRAM
- **************************************************************/
- } /* end of FOR files loop */
- /*
- // @DELETE:
- ///Set the arguments of the kernel
- if(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&gpuImg) != CL_SUCCESS)
- {
- printf("Could not set the kernel's \"gpuImg\" argument\n");
- return false;
- }
- if(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&gpuGaussian) != CL_SUCCESS)
- {
- printf("Could not set the kernel's \"gpuGaussian\" argument\n");
- return false;
- }
- if(clSetKernelArg(kernel, 2, sizeof(int), (void *)&bmp.imgWidth) != CL_SUCCESS)
- {
- printf("Could not set the kernel's \"imageWidth\" argument\n");
- return false;
- }
- if(clSetKernelArg(kernel, 3, sizeof(int), (void *)&bmp.imgHeight) != CL_SUCCESS)
- {
- printf("Could not set the kernel's \"imgHeight\" argument\n");
- return false;
- }
- if(clSetKernelArg(kernel,4,sizeof(int),(void*)&size) != CL_SUCCESS)
- {
- printf("Could not set the kernel's \"gaussian size\" argument\n");
- return false;
- }
- if(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *)&gpuNewImg) != CL_SUCCESS)
- {
- printf("Could not set the kernel's \"gpuNewImg\" argument\n");
- return false;
- }
- */
- /*
- @DELETE:
- ///enqueue the kernel into the OpenCL device for execution
- size_t globalWorkItemSize = imgSize;//the total size of 1 dimension of the work items. Basically the whole image buffer size
- size_t workGroupSize = 64; //The size of one work group
- err = clEnqueueNDRangeKernel(cmdQueue, kernel, 1, NULL, &globalWorkItemSize, &workGroupSize,0, NULL, NULL);
- */
- ///Clean up everything
- // clReleaseProgram(program);
- clReleaseContext(context);
- ///save the new image and return success
- // @DELETE
- // bmp.imgData = newData;
- // meImageBMP_Save(&bmp,"gpu_blur.bmp");
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement