2
votes

I am a newbie in OpenCL, followed some basic tutorial of matrices operation in OpenCL, but I hardly understand on how to implement some loop operations in C++ into OpenCL.

For a 2D array, the direct multiplication of matrix A x matrix A will be implemented as:

__kernel void sum(
    __global const float *a, __global float *g) {
        const int size = 4;
        int i = get_global_id(1);
        int j = get_global_id(0);

        float z  = a_g[i];
        g [i+size*j] = a[i+size*j] *a[i+size*j]  ;
        }

but what if I only want to multiply the elements in certain rows rather than the whole matrix?

for (int j=1; j < 4; j++){
    for (int i=0; i < 4; i++){
         P[0][j] += Z[i][0]*Z[i][j];
    }
}

Says P is a 1D-array, where each element of P is the sum of multiplication of first element each row with all elements in the row of matrix Z. How can I address the first element in each row as denoted by Z[i][0] and multiply with Z[i][j] in OpenCL?

Thanks

1

1 Answers

2
votes

Firstly, the kernel in your example is not computing a standard matrix product. It appears to be doing a Hadamard product. However, a direct translation of the for loop code in CL may look something like this:

__kernel void product(__global const float *Z, __global float *P)
{
        const int size = get_global_size(0);
        int j = get_global_id(0);
        P[j] = 0;
        for (int i = 0; i < size; ++i)
        {
             P[j] += a[size*i] *a[j + size*i];
        }
}

With a global size of {4}. This is not an optimal solution but it is the simplest.