how do we use freeocl.icd in opencl program

Hi
We compiled the free opencl source code “FreeOCL-0.3.6-Source” for x86 and got the opencl driver "freeocl.icd " . And now not able to use this icd driver in opencl program.
Please suggest the appropriate step to use opencl driver "freeocl.icd ".

Regards,
Manoj Kumar

I would like to clarify on my question, in case it is not clear. Below is my sample opencl program:
I have created two files: 1. main.c 2. kernel.cl
When I build and install Freeocl, the following libraries are built:
libFreeOCL.so libOpenCL.so libOpenCL.so.1 libOpenCL.so.1.2

Now, when I build my opencl program, I am able to make sure that my program links with this libOpenCL.so.
But I am not finding the way to make sure that my ‘read_imagef’ function used in my source file “kernel.cl”, links with libFreeOCL.so

Please let me know the steps as I need it urgently.

“main.c” source is as follows:

#include <stdio.h>
#include <stdlib.h>

#ifdef APPLE
#include <OpenCL/opencl.h>
#else
#include <OpenCL/cl.h>
#endif

#define MEM_SIZE (5)
#define MAX_SOURCE_SIZE (0x100000)

int main()
{
cl_device_id device_id = NULL;
cl_context context = NULL;
cl_command_queue command_queue = NULL;
cl_mem memobj = NULL;
cl_program program = NULL;
cl_kernel kernel = NULL;
cl_platform_id platform_id = NULL;
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
cl_int ret;
float buf[2700];
cl_int status;
char string[MEM_SIZE];

FILE *fp;
char fileName[] = “./kernel.cl”;
char *source_str;
size_t source_size;
int i;

/* Load the source code containing the kernel*/
fp = fopen(fileName, “r”);
if (!fp) {
fprintf(stderr, "Failed to load kernel.
");
exit(1);
}
source_str = (char*)malloc(MAX_SOURCE_SIZE);
source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
fclose(fp);

/* Get Platform and Device Info */
ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_CPU, 1, &device_id, &ret_num_devices);

/* Create OpenCL context */
context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret);

/Image object/
for (i=0; i<2700; i++)
buf[i] = 9460.5860 + (0.876453*i);

cl_image_desc desc;
desc.image_type = CL_MEM_OBJECT_IMAGE3D;
desc.image_width = 3;
desc.image_height = 3;
desc.image_depth = 3;
desc.image_array_size = 0;
desc.image_row_pitch = 34;
desc.image_slice_pitch = 3
4*3;
desc.num_mip_levels = 0;
desc.num_samples = 0;
desc.buffer =NULL;
cl_image_format format;
format.image_channel_order = CL_R;
format.image_channel_data_type = CL_FLOAT;
cl_mem d_inputImage = clCreateImage(context, CL_MEM_COPY_HOST_PTR, &format, &desc, (void *)buf, &status);

/* Create Command Queue */
command_queue = clCreateCommandQueue(context, device_id, 0, &ret);

/* Create Memory Buffer */
memobj = clCreateBuffer(context, CL_MEM_READ_WRITE,MEM_SIZE * sizeof(char), NULL, &ret);

/* Create Kernel Program from the source */
program = clCreateProgramWithSource(context, 1, (const char **)&source_str,
(const size_t *)&source_size, &ret);

/* Build Kernel Program */
ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);

/* Create OpenCL Kernel */
kernel = clCreateKernel(program, “hello”, &ret);

/* Set OpenCL Kernel Parameters */
ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), &d_inputImage);

/* Execute OpenCL Kernel */
ret = clEnqueueTask(command_queue, kernel, 0, NULL,NULL);

/* Finalization */
ret = clFlush(command_queue);
ret = clFinish(command_queue);
ret = clReleaseKernel(kernel);
ret = clReleaseProgram(program);
ret = clReleaseMemObject(memobj);
ret = clReleaseCommandQueue(command_queue);
ret = clReleaseContext(context);

free(source_str);

return 0;
}

“kernel.cl” source is as follows:
__constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_LINEAR;
__kernel void hello(__read_only image3d_t img)
{
float4 tmp4;
float rit,xmd,ymd;
rit = 0.5;
xmd = 0.000000;
ymd = 0.000000;
printf("tmp4 = ");
tmp4 = read_imagef(img,sampler,(float4)(rit,xmd+0.5f,ymd+0.5f,0.f));
printf("tmp4 = %f
",tmp4.x);
}

Thank you!

Have you tried to link with libFreeOCL.so directly?

Thank you very much for your reply and help.

Now I was successfully able to compile my sample program with both the OpenCL platforms (FreeOCL as well as with intelocl).

But I observed different results from read_imagef function from both the platforms.

Request you to please let me know if this different results are as expected (which I doubt).

The output from FreeOCL is as follows:

tmp4 = 9466.283203

tmp4 = 9471.979492

tmp4 = 9483.374023

The output from IntelOCL is as follows:

tmp4 = 9460.585938

tmp4 = 9466.283203

tmp4 = 9477.676758

My sample program is as follows:

main.c:


#include <stdio.h>

#include <stdlib.h>

#ifdef APPLE

#include <OpenCL/opencl.h>

#else

#include <OpenCL/cl.h>

#endif

#define MEM_SIZE (5)

#define MAX_SOURCE_SIZE (0x100000)

int main()

{

cl_device_id *device_id = NULL;

cl_context context = NULL;

cl_command_queue command_queue = NULL;

cl_mem memobj = NULL;

cl_program program = NULL;

cl_kernel kernel = NULL;

cl_platform_id *platform_id;

cl_platform_id platform = NULL;

cl_uint ret_num_devices;

cl_uint ret_num_platforms;

cl_int ret;

float buf[2700];

cl_int status;

char string[MEM_SIZE];

FILE *fp;

char fileName[] = “./kernel.cl”;

char *source_str;

size_t source_size;

int i;

/* Load the source code containing the kernel*/

fp = fopen(fileName, “r”);

if (!fp) {

fprintf(stderr, "Failed to load kernel.
");

exit(1);

}

source_str = (char*)malloc(MAX_SOURCE_SIZE);

source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);

fclose(fp);

/* Get Platform Info */

ret = clGetPlatformIDs(0, NULL, &ret_num_platforms);

platform_id = (cl_platform_id*) malloc(ret_num_platforms * sizeof(cl_platform_id));

ret = clGetPlatformIDs(ret_num_platforms, platform_id, NULL);

/* select one platform of them */

platform = platform_id[1];

/* Get Device Info */

ret = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 0, NULL, &ret_num_devices);

device_id = (cl_device_id*) malloc(ret_num_devices * sizeof(cl_device_id));

ret = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, ret_num_devices, device_id, NULL);

/* Create OpenCL context */

context = clCreateContext(NULL, 1, device_id, NULL, NULL, &ret);

/Image object/

for (i=0; i<2700; i++)

buf[i] = 9460.5860 + (0.876453*i);

cl_image_desc desc;

desc.image_type = CL_MEM_OBJECT_IMAGE3D;

desc.image_width = 3;

desc.image_height = 3;

desc.image_depth = 3;

desc.image_array_size = 0;

desc.image_row_pitch = 3*4;

desc.image_slice_pitch = 343;

desc.num_mip_levels = 0;

desc.num_samples = 0;

desc.buffer =NULL;

cl_image_format format;

format.image_channel_order = CL_R;

format.image_channel_data_type = CL_FLOAT;

cl_mem d_inputImage = clCreateImage(context, CL_MEM_COPY_HOST_PTR, &format, &desc, (void *)buf, &status);

/* Create Command Queue */

command_queue = clCreateCommandQueue(context, device_id[0], 0, &ret);

/* Create Memory Buffer */

memobj = clCreateBuffer(context, CL_MEM_READ_WRITE,MEM_SIZE * sizeof(char), NULL, &ret);

/* Create Kernel Program from the source */

program = clCreateProgramWithSource(context, 1, (const char **)&source_str,

(const size_t *)&source_size, &ret);

/* Build Kernel Program */

ret = clBuildProgram(program, 1, device_id, NULL, NULL, NULL);

/* Create OpenCL Kernel */

kernel = clCreateKernel(program, “hello”, &ret);

/* Set OpenCL Kernel Parameters */

ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), &d_inputImage);

/* Execute OpenCL Kernel */

ret = clEnqueueTask(command_queue, kernel, 0, NULL,NULL);

/* Finalization */

ret = clFlush(command_queue);

ret = clFinish(command_queue);

ret = clReleaseKernel(kernel);

ret = clReleaseProgram(program);

ret = clReleaseMemObject(memobj);

ret = clReleaseCommandQueue(command_queue);

ret = clReleaseContext(context);

free(source_str);

return 0;

}


kernel.cl:

__constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_LINEAR;

__kernel void hello(__read_only image3d_t img)

{

float4 tmp4;

tmp4 = read_imagef(img,sampler,(float4)(.5f, .5f, .5f, .5f));

printf("tmp4 = %f
",tmp4.x);

tmp4 = read_imagef(img,sampler,(float4)(1,1,1,1));

printf("tmp4 = %f
",tmp4.x);

tmp4 = read_imagef(img,sampler,(float4)(2,2,2,2));

printf("tmp4 = %f
",tmp4.x);

}

Please let us know if I am making any incorrect things.

Thanks & Best Regards,

Can you play with different sampler parameters? Does discrepancy present with normalized coordinates, or/and when you use CLK_FILTER_NEAREST?
Also, try this code with this tool: https://github.com/jrprice/Oclgrind
And on AMD CPU driver, if you need to be sure:
http://developer.amd.com/tools-and-sdks/opencl-zone/amd-accelerated-parallel-processing-app-sdk/

Thank you very much Salabar for your tips. It helped me.
I tried with different sampler parameters and found that the results from standard drivers and freeocl matches when CLK_FILTER_NEAREST is used.
I am not sure if it is bug in freeocl. For now I would be implementing the linear interpolation my self as per opencl specifications and would check the results.

Any further comments (if you have) would be useful for me as I am kind of beginner in OPENCL.

Thanks again!

I am not sure if it is bug in freeocl

I’m pretty sure it is. You should probably report it, although freeocl repo appears to be dead now.