Skip to content

Introduction to OpenCL API during my Master. Simple exercises to learn.

Notifications You must be signed in to change notification settings

FandosA/Simple_Kernel_OpenCL-API_exercises

Repository files navigation

Simple Kernel OpenCL-API exercises

These are the first OpenCL exercises I did during my Master, in the subject Programming and Architecture of Computing Systems, with which I was introduced to this type of programming.

At the beginning, a very basic kernel was given, the kernel kernel_pow2.cl. This kernel creats an array and puts in there the squares of the numbers of an input array. Firstly, the kernel is loaded and converted to an array of characters to be processed by the OpenCL runtime. Then the kernel source is read into buffer and finally the program is created from this buffer. Once this is done, the source code is written in the executable file, kernel_pow2.c, for a given device (CPU, GPU…) using clBuildProgram. Now it is necessary to create and initialize all variables and arrays that are going to be used in the kernel and build the corresponding device objects (buffers) for each element that is going to be stored in the device memory. To do this, the OpenCL function clCreateBuffer is used, and then the elements of the input array at the host memory are copied to the corresponding input array at the device memory using clEnqueueWriteBuffer.

Before launching the kernel, all arguments that are going to be used in it have to be set. A pointer to the input memory at the device will be used, which should hold the source values on which the kernel operation will be performed, and another pointer to the output memory at the device, which should hold the result values after finishing the computations in the computation device. The rest of the variables that are going to be used do not have to be a pointer because they are not going to be modified.

Finally, the kernel is launched to perform the function to be executed at the computing device using clEnqueueNDRangeKernel. To finish the program, the results of the calculations from the output device memory object have to be read and copied to the host memory output array with clEnqueueReadBuffer, and all the OpenCL memory objects allocated along this program are released.

The kernels kernel_mirror.cl and kernel_rotate.cl flip and rotate an image, respectively. Images below show an example of each of the kernels.

              Original image                                      Flipped image

              Original image                                      Rotated image

About

Introduction to OpenCL API during my Master. Simple exercises to learn.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages