PDA

View Full Version : Call OpenCL functions from another file



youli13
09-16-2012, 12:57 PM
I have a main C source where there is the main loop for doing iterative computation. Here is this main loop :


while(!convergence)
{
step = step + 1;
t = t + dt;

Explicit(source, source_size, x0, x, size_x, size_y, diagx, diagy,
weightx, weighty, &error, k0) ;

result = sqrt(error);

if ((result<epsilon) || (step>maxStep)) break;
}

As you can see in this loop, I call `Explicit` function which contains all the required OpenCL functions for computing the next values of x0 at each iteration. Here's a part of this another source :



#include <stdio.h>
#include <CL/cl.h>

void Explicit( const char* source, size_t source_size, double** x0, double** x,
int size_x, int size_y, double diagx, double diagy, double weightx, double
weighty, double* error, double k0)
{
/* local variables */
int i,j,iter;
double rk, eps, time, result;
double* r;

// Allocate cumulative error array
r = malloc((size_x+2)*(size_y+2)*sizeof(double));

// Get platform and device information

cl_platform_id platform_id = NULL;
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
cl_int ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
ret = clGetDeviceIDs( platform_id, CL_DEVICE_TYPE_CPU, 1,
&device_id, &ret_num_devices);

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

// Create a command queue
cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &ret);

// Create memory buffers on the device for each vector
cl_mem x0_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY,
(size_x+2) * (size_y+2) * sizeof(cl_double), NULL, &ret);

...
...

// Execute the OpenCL kernel on the list
size_t global_item_size[2] = {size_y,size_x};
// Process the entire lists
ret = clEnqueueNDRangeKernel(command_queue, kernel, 2, NULL,
global_item_size, NULL, 0, NULL, NULL);

// Read the buffer back to the array
readBuffer2D(command_queue, x_mem_obj, x, size_x, size_y);
ret = clEnqueueReadBuffer(command_queue, r_mem_obj, CL_TRUE, 0,
(size_x+2) * (size_y+2) * sizeof(double), r, 0, NULL, NULL);

...

The problem is that if I put this `Explicit` function in another C source, the code compiles fine but doesn't give good results ( x0 array values all to zero ).


If I include this function into the main file, i.e into only one source, the code works fine. Is this specific to OpenCL parameters passing ?

Anyone could help me please.

notzed
09-16-2012, 04:48 PM
There's nothing special about the C conventions used in the host code.

You probably just have a mismatch of the function prototypes, or some logic problem (like returning local variable addresses), but there isn't enough code shown to tell. Try compiling with -Wall to see what's going on.