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 above 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
Select Boost
Then type Search: boost
and select all of the packages.
Select OpenCL
Then type Search: OpenCL
and select all of the packages: libOpenCL-devel
and libOpenCL1
.
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 launcher (not installer!) and complete these steps for compilation and running:
cd "C:\Users\dkozyrev\Desktop\gpu-examples"
— going into our directory;g++ -Ofast -std=c++17 printDevices.cpp -o printDevices -lOpenCL
— compilation and linking;./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.
Install GPU Drivers
Intel GPU
Google intel opencl sdk
and you will see this website. Download and install Intel® SDK for OpenCL™ Applications
.
When installation will be finished, you will see the file C:\Windows\System32\OpenCL.dll
. Open cygwin launcher (not installer!) and complete next:
cp "/cygdrive/c/Windows/System32/OpenCl.dll" "/cygdrive/c/cygwin64/bin/"
cp "/cygdrive/c/Windows/System32/OpenCl.dll" "/cygdrive/c/cygwin64/bin/cygOpenCL-1.dll"
echo "cygOpenCL-1.dll" > "/etc/OpenCL/vendors/gpu.icd"
./printDevices
— rerun first example (printing all of the available devices)
You will see, that GPU is available now! I see my Intel(R) Iris(R) Xe Graphics
in the list of all devices.