Skip to content

Use cpp (c++) boost::compute on fedora-lxqt

boost::compute
boost::compute

Install fedora-lxqt desktop

Download and install fedora-lxqt desktop.

Install boost libraries

dnf install boost-devel

Install boost::compute backend: OpenCL

dnf install intel-opencl clinfo opencl-headers ocl-icd-devel

Lookup Your GPU support with clinfo

Run Commands:

clinfo
clinfo | grep -i platforms
clinfo | grep -i devices
clinfo | grep -i units
clinfo | grep -i vendor
clinfo | grep -i version

Test Your boost::compute with cpp (c++) code

#include <vector>
#include <iostream>
#include <boost/compute.hpp>

namespace compute = boost::compute;

using i32 = std::int32_t; // Notice: c++ std::int32_t is different from int32_t

int main() try {
        // compute::device device = ???
        auto device = compute::system::default_device();
        std::cout << "device.name() => " << device.name() << '\n'
                << "device.vendor() => " << device.vendor() << '\n'
                << "device.version() => " << device.version() << '\n';
        auto context = compute::context{device};
        auto queue = compute::command_queue{context, device};
        // Declare a vector on the GPU.
        compute::vector<i32> device_vector(20, context);
        // compute::iota algorithm.
        compute::iota(device_vector.begin(), device_vector.end(), 0, queue);
        // Declare a vector on the Host Memory.
        std::vector<i32> host_vector;
        // Copy the vector from GPU to Host Memory.
        compute::copy(
                device_vector.begin(),
                device_vector.end(),
                std::back_inserter(host_vector),
                queue
        );
        // Print the host vector to "std::cout".
        std::copy(
                host_vector.begin(),
                host_vector.end(),
                std::ostream_iterator<i32>(std::cout, " ")
        );
        std::cout << std::endl;
        // Print the GPU vector directly.
        compute::copy(
                device_vector.begin(),
                device_vector.end(),
                std::ostream_iterator<i32>(std::cout, " "),
                queue
        );
        std::cout << std::endl;
} catch (std::exception & exc) {
        std::cerr << "[boost::compute exception] " << exc.what() << std::endl;
}

Compile and Run the Code:

$ g++ compute.cpp -std=c++23 `pkgconf --cflags --libs OpenCL` -o compute
$ ./compute 
device.name() => ***
device.vendor() => Intel(R) Corporation
device.version() => ***
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

 

 

 

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Form options

Submitted comments will be subject to moderation before being displayed.

@cppfx.xyz