echuraev / HSE-OpenCL-Webinar

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

General-purpose computing on GPU with OpenCL

Practice from webinar in HSE Master's Programme: Master of Computer Vision.

Table of Contents

Requirements

  • compiler with C++14 features
  • cmake
  • OpenCL
  • OpenCV (if you'd like to build color2gray program)

Building

$ mkdir build && cd build
$ cmake ..
$ make -j4

You can also build only one of two examples by using cmake options:

  • -DONLY_VECTOR=ON - only vector_add will be built.
  • -DONLY_Image=ON - only color2gray will be built.

For example:

$ mkdir build && cd build
$ cmake -DONLY_VECTOR=ON ..
$ make -j4

Project structure

  • common - contains definitions and implementations of several common used functions.
  • images - contains input and output image for color2gray.
  • implementations - contains implementations of host programs.
  • kernels - contains implementations of OpenCL kernels.

Description of the programs

vector_add

This is the easies example of using OpenCL for general-purpose computing on GPU. This program calculates sum of two input vectors and save the result to the third vector. Vectors contain 1 million integer elements.

color2gray

This program converts color image to grayscale and write the output image to file out.png in the directory from where color2gray was executed.

As input we take color logo of HSE with resolution 9917x9917 pixels: color logo

After program execution we will get grayscale image: grayscale logo

Run the programs

Both programs can take the following arguments:

Available arguments:
        cpu     Run program on CPU
        gpu     Run program on GPU (default)
        h, help Show this help message

By default program will be executed on the first available GPU. In case when no GPUs available, then the program will be executed on CPU.

Example of execution program with command line arguments:

./vector_add cpu

Performance

I measured performance of the programs on Ubuntu 20.04 with the following hardware:

  • CPU: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
  • GPU: Nvidia GeForce GTX 660

The mechanism of OpenCL events was used to measure the time spent on executing program on the OpenCL device. More you can read here.

The measured numbers are presented in the table below:

Program CPU (ms) GPU (ms)
vector_add 0.45 0.12
color2gray 173.96 14.05

About


Languages

Language:C++ 93.0%Language:CMake 4.2%Language:C 2.8%