The hello world example gives a simple application that prints the name of the default compute device on the system.
The boost::compute::system
class provides access to the OpenCL platforms and devices present on the host system.
Compute devices are represented with the device
class.
#include <iostream> #include <boost/compute/core.hpp> namespace compute = boost::compute; int main() { compute::device device = compute::system::default_device(); std::cout << "hello from " << device.name() << std::endl; return 0; }
Before any computation occurs, data must be transferred from the host to the compute device. The generic copy()
function provides a simple interface for transfering data and the generic vector<T>
class provides a container for storing data on a compute device.
The following example shows how to transfer data from an array on the host to a vector<T>
on the device and then back to a separate std::vector<T>
on the host. At the end of the example both host_array
and host_vector
contain the same values which were copied through the memory on the compute device.
#include <vector> #include <boost/compute/algorithm/copy.hpp> #include <boost/compute/container/vector.hpp> namespace compute = boost::compute; int main() { compute::device device = compute::system::default_device(); compute::context context(device); compute::command_queue queue(context, device); int host_data[] = { 1, 3, 5, 7, 9 }; compute::vector<int> device_vector(5, context); compute::copy( host_data, host_data + 5, device_vector.begin(), queue ); std::vector<int> host_vector(5); compute::copy( device_vector.begin(), device_vector.end(), host_vector.begin(), queue ); return 0; }
The following example shows how to calculate the square-root of a vector of float
s on a compute device using the transform()
function.
#include <vector> #include <algorithm> #include <boost/compute/algorithm/transform.hpp> #include <boost/compute/container/vector.hpp> #include <boost/compute/functional/math.hpp> namespace compute = boost::compute; int main() { compute::device device = compute::system::default_device(); compute::context context(device); compute::command_queue queue(context, device); std::vector<float> host_vector(10000); std::generate(host_vector.begin(), host_vector.end(), rand); compute::vector<float> device_vector(host_vector.size(), context); compute::copy( host_vector.begin(), host_vector.end(), device_vector.begin(), queue ); compute::transform( device_vector.begin(), device_vector.end(), device_vector.begin(), compute::sqrt<float>(), queue ); compute::copy( device_vector.begin(), device_vector.end(), host_vector.begin(), queue ); return 0; }
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4