# Problem with matrix

• 03-13-2011, 09:04 AM
lesiupoland
Problem with matrix
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 :).
• 03-13-2011, 10:36 AM
david.garcia
Re: Problem with matrix
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.
• 03-13-2011, 11:47 AM
lesiupoland
Re: Problem with matrix
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.
• 03-13-2011, 01:53 PM
david.garcia
Re: Problem with matrix
Okay, this is roughly what your kernel will look like:

Code :

```  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.
• 03-13-2011, 02:00 PM
lesiupoland
Re: Problem with matrix
Thanks for quick reply. I'll check this suggestion tomorrow!
• 03-20-2011, 11:04 AM
lesiupoland
Re: Problem with matrix
Code :

```__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:
Code :

```__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.
• 03-20-2011, 11:14 AM
lesiupoland
Re: Problem with matrix
I found solution!
I had a mistake in passing args to kernel. Sorry :).