From 9df36d85f90f425da49005ae127d7360e303ad27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20Alexander=20H=C3=B6fle?= Date: Tue, 29 Jul 2014 09:24:25 -0300 Subject: [PATCH 1/2] Altera SDK for OpenCL implemented Added functiond cl_program ocl_create_program_from_binary Creates a program from a binary file. cl_program ocl_create_program_from_binary_for_fpga Uses the ALTERA SDK for OpenCL to create program for a device on which the binary was loaded. using namespace aocl_utils was added and AOCL_Utils.h is now included for the usage of the ALTERA SDK for OpenCL --- src/ocl.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/ocl.h b/src/ocl.h index a101ac2..e486c6c 100644 --- a/src/ocl.h +++ b/src/ocl.h @@ -21,6 +21,10 @@ #include #include +#include "AOCL_Utils.h" + +using namespace aocl_utils; + typedef struct OclPlatform OclPlatform; #define OCL_CHECK_ERROR(error) { \ @@ -56,6 +60,13 @@ cl_program ocl_create_program_from_source const char *source, const char *options, cl_int *errcode); +cl_program ocl_create_program_from_binary + (OclPlatform *ocl, + const char *source, + cl_int *errcode); +cl_program ocl_create_program_from_binary_for_fpga + (OclPlatform *ocl, + char* binaryfile); int ocl_get_num_devices (OclPlatform *ocl); cl_device_id * ocl_get_devices (OclPlatform *ocl); cl_command_queue * ocl_get_cmd_queues (OclPlatform *ocl); From a3da403cab8074946307c3f1deacfd5957681f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20Alexander=20H=C3=B6fle?= Date: Tue, 29 Jul 2014 09:36:09 -0300 Subject: [PATCH 2/2] Binary funcs added Added two functions - ocl_create_program_from_binary which creates a program from the binary file and... cl_program ocl_create_program_from_binary_for_fpga which uses the ALTERA SDK for OpenCL to create a program which was loaded before onto the device. Furthermore, before every malloc is now a cast according to the type of memory allocation. --- src/ocl.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 97 insertions(+), 7 deletions(-) diff --git a/src/ocl.c b/src/ocl.c index e876722..d5c288d 100644 --- a/src/ocl.c +++ b/src/ocl.c @@ -120,7 +120,7 @@ ocl_read_program (const char *filename) length = ftell (fp); rewind (fp); - buffer = malloc (length + 1); + buffer = (char*) malloc (length + 1); buffer[length] = '\0'; if (buffer == NULL) { @@ -148,10 +148,10 @@ ocl_new (unsigned platform, cl_uint num_platforms; cl_platform_id *platforms; - ocl = malloc (sizeof(OclPlatform)); + ocl = (OclPlatform*) malloc (sizeof(OclPlatform)); OCL_CHECK_ERROR (clGetPlatformIDs (0, NULL, &num_platforms)); - platforms = malloc (sizeof (cl_platform_id) * num_platforms); + platforms = (cl_uint) malloc (sizeof (cl_platform_id) * num_platforms); if (platform >= num_platforms) { fprintf (stderr, "invalid platform %i out of %i platforms\n", platform, num_platforms); @@ -163,7 +163,7 @@ ocl_new (unsigned platform, OCL_CHECK_ERROR (clGetDeviceIDs (ocl->platform, type, 0, NULL, &ocl->num_devices)); - ocl->devices = malloc (ocl->num_devices * sizeof(cl_device_id)); + ocl->devices = (_cl_device_id**) malloc (ocl->num_devices * sizeof(cl_device_id)); OCL_CHECK_ERROR (clGetDeviceIDs (ocl->platform, type, ocl->num_devices, ocl->devices, NULL)); ocl->context = clCreateContext (NULL, ocl->num_devices, ocl->devices, NULL, NULL, &errcode); @@ -195,7 +195,7 @@ ocl_new_with_queues (unsigned platform, return NULL; ocl->own_queues = 1; - ocl->cmd_queues = malloc (ocl->num_devices * sizeof(cl_command_queue)); + ocl->cmd_queues = (_cl_command_queue**) malloc (ocl->num_devices * sizeof(cl_command_queue)); for (cl_uint i = 0; i < ocl->num_devices; i++) { ocl->cmd_queues[i] = clCreateCommandQueue (ocl->context, ocl->devices[i], @@ -306,7 +306,7 @@ ocl_get_platform_info (OclPlatform *ocl, char *result; OCL_CHECK_ERROR (clGetPlatformInfo (ocl->platform, param, 0, NULL, &size)); - result = malloc (size); + result = (char*) malloc (size); OCL_CHECK_ERROR (clGetPlatformInfo (ocl->platform, param, size, result, NULL)); return result; } @@ -336,7 +336,7 @@ ocl_create_program_from_source (OclPlatform *ocl, transfer_error (tmp_err, errcode); OCL_CHECK_ERROR (clGetProgramBuildInfo (program, ocl->devices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size)); - log = malloc (log_size * sizeof(char)); + log = (char*) malloc (log_size * sizeof(char)); OCL_CHECK_ERROR (clGetProgramBuildInfo (program, ocl->devices[0], CL_PROGRAM_BUILD_LOG, log_size, log, NULL)); fprintf (stderr, "\n** Error building program. Build log:\n%s\n", log); @@ -368,6 +368,96 @@ ocl_create_program_from_file (OclPlatform *ocl, return program; } + + + + +cl_program +ocl_create_program_from_binary(OclPlatform *ocl, const char *source, cl_int *errcode) +{ + cl_int tmp_err, binary_status; + cl_program program; + + size_t pos; + FILE *fp; + + if ((fp = fopen(source, "rb")) != NULL) { + if (fseek(fp, 0, SEEK_END) == 0) { + if ((pos = ftell(fp)) != -1L) { + printf("Dateigröße beträgt %lu Bytes.\n", pos); + } else { + perror(source); + } + } else { + perror(source); + } + + } else { + perror(source); + } + + + + unsigned char* programBinary = (unsigned char*) malloc(sizeof(char) * (pos)); + fread(programBinary, 1, pos, fp); + fclose(fp); + + + + program = clCreateProgramWithBinary(ocl->context, ocl->num_devices, ocl->devices, &pos, (const unsigned char**) &programBinary, &binary_status,&tmp_err); + + if (tmp_err != CL_SUCCESS) { + transfer_error (tmp_err, errcode); + return NULL; + } + + + if(binary_status != CL_SUCCESS) + { + transfer_error(binary_status, errcode); + return NULL; + } + + tmp_err= clBuildProgram(program, ocl->num_devices, ocl->devices, NULL,NULL,NULL); + + if (tmp_err != CL_SUCCESS) { + size_t log_size; + char* log; + + transfer_error (tmp_err, errcode); + + OCL_CHECK_ERROR (clGetProgramBuildInfo (program, ocl->devices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size)); + log = (char*)malloc (log_size * sizeof(char)); + + OCL_CHECK_ERROR (clGetProgramBuildInfo (program, ocl->devices[0], CL_PROGRAM_BUILD_LOG, log_size, log, NULL)); + fprintf (stderr, "\n** Error building program. Build log:\n%s\n", log); + free (log); + return NULL; + } + + *errcode = CL_SUCCESS; + free(programBinary); + + + return program; + + + +} + +//This function uses ALTERA SDK for OpenCL +cl_program +ocl_create_program_from_binary_for_fpga(OclPlatform *ocl, const char* binaryfile) +{ + cl_program temp; + temp = createProgramFromBinary(ocl->context, binaryfile, ocl->devices, (unsigned int) ocl->num_devices); + return temp; + +} + + + + cl_context ocl_get_context (OclPlatform *ocl) {