I'm having trouble creating a kernel for this serial code:
void distlag(double *x, double *y,double *data, double *ncoord, double *res)
{
// x: x-coordinate
// y: y-coordinate
// data: value associated with x-y coordinate
// ncoord: number of coordinates
int i=0, j=0;
double u=0.0, v=0.0, lags=0.0;
for(i=0;i<(ncoord[0]-1);i++)
for(j=(i+1); j<ncoord[0];j++)
{
// Pairwise distances
lags=hypot(x[i]-x[j],y[i]-y[j]);
u=data[i];
v=data[j];
*res+= u+v+lags;
}
}
I think the main problem is that in hypot
, distances depend on values in the same vector. Or, it may be that the j
value depends on i
. How can I parallel this code using OpenCL?
(More Info:)
Following this post, I started by trying to get upper triangular distance matrix values first. This is the kernel code:
__kernel void totexp(__global float *x, __global float *xauto, int n)
{
int num_wrk_items = get_local_size(0);
int local_id = get_local_id(0);
int group_id = get_group_id(0);
int j, gid = get_global_id(0);
float sum = 0.0;
for (j = 0; j < n; j++)
{
if ( ((gid+j)!= j) && ((gid+j) < n))
{
sum = fabs(x[j]-x[gid+j]);
}
else
continue;
}
xauto[gid] = sum;
}
int n
is the length
of x
(x is equal to (1,2,3,4,5,6,7,8)). I should then have n(n-1)/2
values (28 in total for the example). I set the global and local work sizes as n
(8). The size of cl_mem
buffer objects for x
and xauto
are n
and n(n-1)/2
(28) respectively. But when I read the results back I get 28 values but not the expected ones. I get: (0,7,12,15,16,15,12,7,0,0,0...,0). Any help?