Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 97 additions & 7 deletions src/ocl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down
11 changes: 11 additions & 0 deletions src/ocl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include <CL/cl.h>
#include <stdio.h>

#include "AOCL_Utils.h"

using namespace aocl_utils;

typedef struct OclPlatform OclPlatform;

#define OCL_CHECK_ERROR(error) { \
Expand Down Expand Up @@ -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);
Expand Down