There are two circular statement, how can l write it in opencl kernel?

76 Views Asked by At

There are two circular statement, for example:

for(int i=0;i<1000;i++)
 for (int j=0;j<1000;j++)
{
  for(int k=i*5;k<i*5+5;k++)
   for(int l=j*5;l<j*5+5;j++)
  {
   marrytemp=A[i]+B[j]+marry;
  }  
 marry[i,j]=marrytemp;
}

how can l write it in opencl kernel?

1

There are 1 best solutions below

1
On

Write the kernel to handle the inner two loops (k,l), then enqueue it as a 2D kernels with global size of i,j.

Edit to add outline of kernel:

The kernel would be something along the lines of:

__kernel void innerLoop(__global float* A, __global float* B, __global float* marry)
{
    int i = get_global_id(1);
    int j = get_global_id(0);
    int marraytemp = 0;
    for(int k=i*5;k<i*5+5;k++)
    {
        for(int l=j*5;l<j*5+5;j++)
        {
            marrytemp=A[i]+B[j]+marrytemp;
        }  
    }
    marry[i,j]=marrytemp;
}

And then it would be called something like:

clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&A);
clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&B);
clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&marray);

size_t global_item_size[] = {100, 100};
clEnqueueNDRangeKernel(command_queue, kernel, 2, NULL, &global_item_size, NULL, 0, NULL, NULL);

Both of these need additional support code (such as creating command_queue and kernel) and have not been compiled. They are just to give you the idea of how to split your four nested loops into an OpenCL kernel.