PDA

View Full Version : Problem with matrix



lesiupoland
03-13-2011, 09:04 AM
Hi

I have big, but I think simple, problem. I have to do kernel that calculates value of function. I have points in one matrix:

args = [x1,x2,x3,y1,y2,y3,z1,z2,z3]

I have to calculate:

f(x1,y1,z1), f(x2,y2,z2), f(x3,y3,z3)

In what ways I can write kernel to do this function?

I don't have problems with adding vectors in kernel, but this is too much for me.
Big thanks for all of replies and sorry for my English :).

david.garcia
03-13-2011, 10:36 AM
Hi lesiupoland,

I'm not sure I understand the problem. Could you explain how to solve the problem in C? Then we can then help you with OpenCL.

lesiupoland
03-13-2011, 11:47 AM
I have many points P(x,y,z) written in convention of 1D array:

D = [x1,x2,x3....,xn,y1,y2,y3.....,yn,z1,z2,z3.....,zn]

Where n is number of all points.

I have ecuation in example:

F = x+y+z

I have to write kernel that takes all of needed variables from a 1D array and gives me back value F.

in C:
F1 = D[0]+D[n]+D[2*n]
F2 = D[1]+D[n+1]+D[2*n+1]
etc.

I know how to write kernel which use all of elements from array, or many arrays of the same size. Example is adding vectors.
I don't know how to write kernel which choose specified elements from array and do operations on them. I searched many tutorials, and I didn't find any solution of my problem.

david.garcia
03-13-2011, 01:53 PM
Okay, this is roughly what your kernel will look like:




float my_function(float x, float y, float z)
{
// For example
return x + y + z;
}

__kernel void myKernel(__global float* input, __global float* output)
{
size_t gid = get_global_id(0);

// Notice that '3' is the number of input arguments (x,y,z)
float x = input[3*gid];
float y = input[3*gid+1];
float z = input[3*gid+2];

float result = my_function(x,y,z);

output[gid] = result;
}


Let me know if you have any questions.

lesiupoland
03-13-2011, 02:00 PM
Thanks for quick reply. I'll check this suggestion tomorrow!

lesiupoland
03-20-2011, 11:04 AM
__kernel void Fitness(__global float* functions_args, __global float* functions_values, __global int* N)
{
size_t idx = get_global_id(0);
functions_values[idx] = pow(functions_args[N*idx],2)+pow(functions_args[N*idx+1],2);",
}
Can I pass N as argument to kernel? Above kernel don't work, but this works good:

__kernel void Fitness(__global float* functions_args, __global float* functions_values, __global int* N)
{
size_t idx = get_global_id(0);
functions_values[idx] = pow(functions_args[2*idx],2)+pow(functions_args[2*idx+1],2);",
}

Value of passed argument is 2.
What I'm doing wrong?
Explicit "2" in 2nd kernel generates good program, but N from first kernel don't work -> it generates error when compiler tries to compile kernel.

lesiupoland
03-20-2011, 11:14 AM
I found solution!
I had a mistake in passing args to kernel. Sorry :).