This tutorial explains the use of vector ranges with simple BLAS level 1 and 2 operations. Vector slices are used similarly and not further considered in this tutorial.
We start with including the required headers:
#define VIENNACL_WITH_UBLAS
#include <iostream>
#include <string>
#include "boost/numeric/ublas/vector.hpp"
#include "boost/numeric/ublas/vector_proxy.hpp"
#include "boost/numeric/ublas/io.hpp"
In the main() routine a couple of vectors from both Boost.uBLAS and ViennaCL are set up. Then, subvectors are extracted and manipulated using the usual operator overloads.
int main (
int,
const char **)
{
typedef boost::numeric::ublas::vector<ScalarType> VectorType;
std::size_t dim_large = 7;
std::size_t dim_small = 3;
Setup ublas objects and fill with data:
VectorType ublas_v1(dim_large);
VectorType ublas_v2(dim_small);
for (std::size_t i=0; i<ublas_v1.size(); ++i)
for (std::size_t i=0; i<ublas_v2.size(); ++i)
Extract submatrices using the ranges in ublas
boost::numeric::ublas::vector_range<VectorType> ublas_v1_sub1(ublas_v1, ublas_r1);
boost::numeric::ublas::vector_range<VectorType> ublas_v1_sub2(ublas_v1, ublas_r2);
boost::numeric::ublas::vector_range<VectorType> ublas_v1_sub3(ublas_v1, ublas_r3);
Create ViennaCL objects and copy data over from uBLAS objects.
VCLVectorType vcl_v1(dim_large);
VCLVectorType vcl_v2(dim_small);
Extract submatrices using the range-functionality in ViennaCL. This works exactly the same way as for uBLAS.
Copy from ublas to submatrices and back:
ublas_v1_sub1 = ublas_v2;
Addition of subvectors:
ublas_v1_sub1 += ublas_v1_sub1;
vcl_v1_sub1 += vcl_v1_sub1;
ublas_v1_sub2 += ublas_v1_sub2;
vcl_v1_sub2 += vcl_v1_sub2;
ublas_v1_sub3 += ublas_v1_sub3;
vcl_v1_sub3 += vcl_v1_sub3;
Print full vectors. Notice that the various entries have changed according to the subvector manipulations above.
std::cout << "ublas: " << ublas_v1 << std::endl;
std::cout << "ViennaCL: " << vcl_v1 << std::endl;
That's it. Print success message and exit.
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}
Full Example Code
#define VIENNACL_WITH_UBLAS
#include <iostream>
#include <string>
#include "boost/numeric/ublas/vector.hpp"
#include "boost/numeric/ublas/vector_proxy.hpp"
#include "boost/numeric/ublas/io.hpp"
int main (
int,
const char **)
{
typedef boost::numeric::ublas::vector<ScalarType> VectorType;
std::size_t dim_large = 7;
std::size_t dim_small = 3;
VectorType ublas_v1(dim_large);
VectorType ublas_v2(dim_small);
for (std::size_t i=0; i<ublas_v1.size(); ++i)
for (std::size_t i=0; i<ublas_v2.size(); ++i)
boost::numeric::ublas::vector_range<VectorType> ublas_v1_sub1(ublas_v1, ublas_r1);
boost::numeric::ublas::vector_range<VectorType> ublas_v1_sub2(ublas_v1, ublas_r2);
boost::numeric::ublas::vector_range<VectorType> ublas_v1_sub3(ublas_v1, ublas_r3);
VCLVectorType vcl_v1(dim_large);
VCLVectorType vcl_v2(dim_small);
ublas_v1_sub1 = ublas_v2;
ublas_v1_sub1 += ublas_v1_sub1;
vcl_v1_sub1 += vcl_v1_sub1;
ublas_v1_sub2 += ublas_v1_sub2;
vcl_v1_sub2 += vcl_v1_sub2;
ublas_v1_sub3 += ublas_v1_sub3;
vcl_v1_sub3 += vcl_v1_sub3;
std::cout << "ublas: " << ublas_v1 << std::endl;
std::cout << "ViennaCL: " << vcl_v1 << std::endl;
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}