c - Taking File Inputs in OpenCL? -


i'm new opencl, , i'm curious how read in data input perform simple operations (e.g. cross/dot product) on.

for particular example, i've compiled , trying run simple sample code calculate 3d dot product vectors: https://github.com/mattscar/opencl_dot_product

however, i'm not sure how format input code. in following code snippet:

/* create program file */ program = clcreateprogramwithsource(ctx, 1,    (const char**)&program_buffer, &program_size, &err);  if(err < 0) {   perror("couldn't create program");   exit(1); } 

clcreateprogramwithsource appears operate on context ctx, don't know how assign context file on hard drive read test vector data from. going right way?

one approach follows:

#include <stdio.h> #include <stdlib.h> #ifdef __apple__ #include <opencl/opencl.h> #else #include <cl/cl.h> #endif  int main() {      cl_platform_id platform;     cl_device_id device;     cl_context context;     cl_program program;      file* programhandle;     size_t programsize, kernelsourcesize;     char *programbuffer, *kernelsource;      // first available platform , gpu , create context     clgetplatformids(1, &platform, null);     clgetdeviceids(platform, cl_device_type_gpu, 1, &device, null);     context = clcreatecontext(null, 1, &device, null, null, null);      // size of kernel source     programhandle = fopen("kernel.cl", "r");     fseek(programhandle, 0, seek_end);     programsize = ftell(programhandle);     rewind(programhandle);      // read kernel source buffer     programbuffer = (char*) malloc(programsize + 1);     programbuffer[programsize] = '\0';     fread(programbuffer, sizeof(char), programsize, programhandle);     fclose(programhandle);      // create program buffer     program = clcreateprogramwithsource(context, 1,             (const char**) &programbuffer, &programsize, null);     free(programbuffer);      // read kernel source in program check     clgetprograminfo(program, cl_program_source, 0, null, &kernelsourcesize);     kernelsource = (char*) malloc(kernelsourcesize);     clgetprograminfo(program, cl_program_source, kernelsourcesize, kernelsource, null);     printf("nkernel source:nn%sn", kernelsource);     free(kernelsource);      clreleasecontext(context);     return 0;  } 

courtesy of http://dhruba.name/2012/08/16/opencl-cookbook-creating-programs-and-reading-kernels-from-a-file/

specifically part :

 // size of kernel source         programhandle = fopen("kernel.cl", "r");         fseek(programhandle, 0, seek_end);         programsize = ftell(programhandle);         rewind(programhandle);          // read kernel source buffer         programbuffer = (char*) malloc(programsize + 1);         programbuffer[programsize] = '\0';         fread(programbuffer, sizeof(char), programsize, programhandle);         fclose(programhandle);          // create program buffer         program = clcreateprogramwithsource(context, 1,                 (const char**) &programbuffer, &programsize, null);         free(programbuffer); 

you need know size of you're reading, seek end

fseek(programhandle, 0, seek_end); 

then position

programsize = ftell(programhandle); 

which should size.

then rewind reset file position.

rewind(programhandle); 

it's important allocate enough memory store entire program + null terminator have add yourself.

read file allocated space, set it's last index null.

programbuffer = (char*) malloc(programsize + 1); programbuffer[programsize] = '\0'; 

read in code , close file.

fread(programbuffer, sizeof(char), programsize, programhandle); fclose(programhandle); 

now have 1 row of code, thats count, know size of 1 line, thats length. , null indicate it's null terminated. done.

program = clcreateprogramwithsource(context, 1, (const char**) &programbuffer, &programsize, null); free(programbuffer); 

don't forget release program buffer when youre done.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -