-1

I'd like to run this simple C code in GPU in an OpenCl-Kernel. Is it possible?

#include <stdio.h>        
int main()
{
  int a[15]={7,8,0,4,13,1,14,5,10,2,3,11,12,6,9};
  int b[15];
  printf(input datas:  ");
  for (i=0;i<15;i++) printf("%3d",a[i]);
  printf("\n");
  for (i=0;i<15;i++) b[a[i]]=i;
  for (i=0;i<15;i++) printf("%3d",b[i]);
  printf("\n");
  return 0;
}

My input and output data should be:

Input:  7 8 0 4 13 1 14 5 10 2 3 11 12 6 9
Output: 2 5 9 10 3 7 13 0 1 14 8 11 12 4 6
Snel23
  • 1,381
  • 1
  • 9
  • 19
  • // input datas : 7 8 0 4 13 1 14 5 10 2 3 11 12 6 9 // output datas: 2 5 9 10 3 7 13 0 1 14 8 11 12 4 6 – Christa Jul 28 '19 at 23:23

1 Answers1

0

It is possible, although it will be really inefficient because of those random memory accesses. Simplifying it a lot, GPUs work better when work-items (instances of an OpenCL kernel) access memory sequentially.

Having said this, to do this in C and OpenCL you need to perform the following steps (again I'm simplifying a bit):

  • Include OpenCL headers.
  • Write the OpenCL kernel itself, and either put it in a string in your main() save it to a .cl file and read it into a string from your main().
  • Get desired GPU device and create a context.
  • Create an OpenCL command queue.
  • Create the input and output device buffers.
  • Write the desired information to the input device buffer (via the command queue).
  • Create an OpenCL program (from the kernel source string), build it, get the kernel object and set its parameters.
  • Run the kernel (via the command queue), which will perform the desired operation, reading from the input buffer and writing to the output buffer.
  • Read back the data from the output device buffer (via the command queue) and show it on screen.
  • Release all the created OpenCL objects.

See this link on how to get started with OpenCL and GPU computing. It gives a good ideia of how something like this is done. You will notice that doing this in pure C is very verbose, so either use a wrapper library such as cf4ocl, use C++, or use some other language with higher-level bindings (e.g. Python).

faken
  • 6,572
  • 4
  • 27
  • 28