This tutorial shows how ViennaCL can be used to wrap user-provided memory buffers allocated on the host. The benefit of such a wrapper is that the algorithms in ViennaCL can directly be run without pre- or postprocessing the data.
We start with including the required headers:
#include <iostream>
#include <cstdlib>
#include <string>
We setup two STL-vectors and add them up as a reference. Then the buffer get wrapped by ViennaCL and added up.
Part 1: Allocate some buffers on the host
std::vector<ScalarType> host_x(size, 1.0);
std::vector<ScalarType> host_y(size, 2.0);
std::cout << "Result on host: ";
for (std::size_t i=0; i<
size; ++i)
std::cout << host_x[i] + host_y[i] << " ";
std::cout << std::endl;
Part 2: Now do the same computations within ViennaCL
vcl_vec1 += vcl_vec2;
std::cout << "Result with ViennaCL: " << vcl_vec1 << std::endl;
std::cout << "Data in STL-vector: ";
for (std::size_t i=0; i<
size; ++i)
std::cout << host_x[i] << " ";
std::cout << std::endl;
That's it. Print success message and exit.
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}
Full Example Code
#include <iostream>
#include <cstdlib>
#include <string>
{
std::size_t size = 10;
std::vector<ScalarType> host_x(size, 1.0);
std::vector<ScalarType> host_y(size, 2.0);
std::cout << "Result on host: ";
for (std::size_t i=0; i<
size; ++i)
std::cout << host_x[i] + host_y[i] << " ";
std::cout << std::endl;
vcl_vec1 += vcl_vec2;
std::cout << "Result with ViennaCL: " << vcl_vec1 << std::endl;
std::cout << "Data in STL-vector: ";
for (std::size_t i=0; i<
size; ++i)
std::cout << host_x[i] << " ";
std::cout << std::endl;
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}