Quick start in solving problems on GPU + CPU [Tutorial]

Правка en2, от dmkz, 2023-06-10 03:24:27

Hello, codeforces!

Do you have modern GPU and CPU and want to calculate something with them, but you don't know how to do it? Dont be upset, because you can read this blog and run your first GPU program on your AMD/Intel/NVIDIA GPU. I will describe installation process and then we will dive into the C++ code. As an example, we will calculate a lot of mazes for Bug Game in parallel. For doing this in simple way, we will use library Boost.Compute, which is a C++ STL-style wrapper over OpenCL library. ...

Installation for Windows

We need to install GCC, Boost and OpenCL. Let's do it with cygwin. Just download it by abode link and run installation process. Select www.cygwin.mirror.constant.com as a mirror!

Cygwin installation

Select GCC

On the step of choosing packages, select View: Full, type Search: gcc and select next packages:

  • colorgcc
  • gcc-core
  • gcc-g++
  • libgcc1
Screenshot for choosing of GCC

Select Boost

Then type Search: boost and select all of the packages.

List of selected packages
Screenshots

Select OpenCL

Then type Search: OpenCL and select all of the packages: libOpenCL-devel and libOpenCL1.

Screenshot

Finish installation

After selecting all of the packages, click Next twice and wait while installation process is not finished.

First example: print list of devices and default device

We are ready to write out first example. Lets print list of available devices. Here is the C++ code:

#include <iostream>
#include <iomanip>
#include <boost/compute/core.hpp>

namespace compute = boost::compute;

std::ostream &operator<<(std::ostream &os, const compute::device & device)
{
    return os << device.name() << " (platform: " << device.platform().name() << ")";
}

int main()
{
    // cycle over all of the devices:
    std::cout << "List of available devices:\n";
    for (int i = 0; auto device : compute::system::devices())
        std::cout << std::setw(4) << i++ << ":\t" << device << std::endl;
    
    // get the default device
    compute::device device = compute::system::default_device();
    std::cout << "\nDefault device:\n\t" << device << std::endl;    

    return 0;
}

This code will print list of available devices for using Boost.Compute and default device. I saved this code in directory C:\Users\dkozyrev\Desktop\gpu-examples as file printDevices.cpp.

Open cygwin and complete these steps for compilation and running:

  1. cd "C:\Users\dkozyrev\Desktop\gpu-examples" — going into our directory;
  2. g++ -Ofast -std=c++17 printDevices.cpp -o printDevices -lOpenCL — compilation and linking;
  3. ./printDevices — running executable.

You will see the result and will not see your GPU in the list of available devices. Now we are going to install GPU drivers.

Screenshot

Fixing bug in Boost 1.66

Теги gpu, cpu, boost, multi-threading, compute, opencl

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en13 Английский dmkz 2023-06-10 18:53:36 430
en12 Английский dmkz 2023-06-10 18:47:07 898
en11 Английский dmkz 2023-06-10 18:35:21 543 Tiny change: ' Field`:\n~~~~~\ns' -> ' Field`:\n\n~~~~~\ns' (published)
en10 Английский dmkz 2023-06-10 18:19:49 1457
en9 Английский dmkz 2023-06-10 17:53:03 2565
en8 Английский dmkz 2023-06-10 17:25:31 10116
en7 Английский dmkz 2023-06-10 07:13:14 13671
en6 Английский dmkz 2023-06-10 03:55:54 734
en5 Английский dmkz 2023-06-10 03:33:01 393
en4 Английский dmkz 2023-06-10 03:27:07 428
en3 Английский dmkz 2023-06-10 03:25:34 73
en2 Английский dmkz 2023-06-10 03:24:27 3525
en1 Английский dmkz 2023-06-10 02:25:39 1210 Initial revision (saved to drafts)