This tutorial explains the use of matrix ranges with simple BLAS level 1 and 2 operations.
We start with including the necessary headers:
#define VIENNACL_WITH_UBLAS
#include <iostream>
#include <string>
#include "boost/numeric/ublas/vector.hpp"
#include "boost/numeric/ublas/matrix.hpp"
#include "boost/numeric/ublas/matrix_proxy.hpp"
#include "boost/numeric/ublas/io.hpp"
In the main() routine we set up Boost.uBLAS as well as ViennaCL objects. A few standard operations on submatrices are performed by using the matrix_range<> view available in both libraries.
int main (
int,
const char **)
{
typedef boost::numeric::ublas::matrix<ScalarType> MatrixType;
Setup ublas objects and fill with data:
std::size_t dim_large = 5;
std::size_t dim_small = 3;
MatrixType ublas_A(dim_large, dim_large);
MatrixType ublas_B(dim_small, dim_small);
MatrixType ublas_C(dim_large, dim_small);
MatrixType ublas_D(dim_small, dim_large);
for (std::size_t i=0; i<ublas_A.size1(); ++i)
for (std::size_t j=0; j<ublas_A.size2(); ++j)
ublas_A(i,j) = static_cast<ScalarType>((i+1) + (j+1)*(i+1));
for (std::size_t i=0; i<ublas_B.size1(); ++i)
for (std::size_t j=0; j<ublas_B.size2(); ++j)
ublas_B(i,j) = static_cast<ScalarType>((i+1) + (j+1)*(i+1));
for (std::size_t i=0; i<ublas_C.size1(); ++i)
for (std::size_t j=0; j<ublas_C.size2(); ++j)
ublas_C(i,j) = static_cast<ScalarType>((j+2) + (j+1)*(i+1));
for (std::size_t i=0; i<ublas_D.size1(); ++i)
for (std::size_t j=0; j<ublas_D.size2(); ++j)
ublas_D(i,j) = static_cast<ScalarType>((j+2) + (j+1)*(i+1));
Extract submatrices using the ranges in ublas
boost::numeric::ublas::matrix_range<MatrixType> ublas_A_sub1(ublas_A, ublas_r1, ublas_r1);
boost::numeric::ublas::matrix_range<MatrixType> ublas_A_sub2(ublas_A, ublas_r2, ublas_r2);
boost::numeric::ublas::matrix_range<MatrixType> ublas_C_sub(ublas_C, ublas_r1, ublas_r1);
boost::numeric::ublas::matrix_range<MatrixType> ublas_D_sub(ublas_D, ublas_r1, ublas_r1);
Setup ViennaCL objects and copy data from uBLAS objects
VCLMatrixType vcl_A(dim_large, dim_large);
VCLMatrixType vcl_B(dim_small, dim_small);
VCLMatrixType vcl_C(dim_large, dim_small);
VCLMatrixType vcl_D(dim_small, dim_large);
Extract submatrices using the ranges in ViennaCL. Similar to the code above for uBLAS.
First use case: Copy from ublas to submatrices and back:
Second use case: Addition of matrices.
ublas_A_sub2 += ublas_A_sub2;
vcl_A_sub2 += vcl_A_sub2;
ublas_B += ublas_A_sub2;
vcl_B += vcl_A_sub2;
Third use case: Matrix range with matrix-matrix product:
ublas_A_sub1 +=
prod(ublas_C_sub, ublas_D_sub);
Print result matrices:
std::cout << "Result ublas: " << ublas_A << std::endl;
std::cout << "Result ViennaCL: " << vcl_A << std::endl;
That's it. Print a success message:
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/matrix.hpp"
#include "boost/numeric/ublas/matrix_proxy.hpp"
#include "boost/numeric/ublas/io.hpp"
int main (
int,
const char **)
{
typedef boost::numeric::ublas::matrix<ScalarType> MatrixType;
std::size_t dim_large = 5;
std::size_t dim_small = 3;
MatrixType ublas_A(dim_large, dim_large);
MatrixType ublas_B(dim_small, dim_small);
MatrixType ublas_C(dim_large, dim_small);
MatrixType ublas_D(dim_small, dim_large);
for (std::size_t i=0; i<ublas_A.size1(); ++i)
for (std::size_t j=0; j<ublas_A.size2(); ++j)
ublas_A(i,j) = static_cast<ScalarType>((i+1) + (j+1)*(i+1));
for (std::size_t i=0; i<ublas_B.size1(); ++i)
for (std::size_t j=0; j<ublas_B.size2(); ++j)
ublas_B(i,j) = static_cast<ScalarType>((i+1) + (j+1)*(i+1));
for (std::size_t i=0; i<ublas_C.size1(); ++i)
for (std::size_t j=0; j<ublas_C.size2(); ++j)
ublas_C(i,j) = static_cast<ScalarType>((j+2) + (j+1)*(i+1));
for (std::size_t i=0; i<ublas_D.size1(); ++i)
for (std::size_t j=0; j<ublas_D.size2(); ++j)
ublas_D(i,j) = static_cast<ScalarType>((j+2) + (j+1)*(i+1));
boost::numeric::ublas::matrix_range<MatrixType> ublas_A_sub1(ublas_A, ublas_r1, ublas_r1);
boost::numeric::ublas::matrix_range<MatrixType> ublas_A_sub2(ublas_A, ublas_r2, ublas_r2);
boost::numeric::ublas::matrix_range<MatrixType> ublas_C_sub(ublas_C, ublas_r1, ublas_r1);
boost::numeric::ublas::matrix_range<MatrixType> ublas_D_sub(ublas_D, ublas_r1, ublas_r1);
VCLMatrixType vcl_A(dim_large, dim_large);
VCLMatrixType vcl_B(dim_small, dim_small);
VCLMatrixType vcl_C(dim_large, dim_small);
VCLMatrixType vcl_D(dim_small, dim_large);
ublas_A_sub1 = ublas_B;
ublas_A_sub2 += ublas_A_sub2;
vcl_A_sub2 += vcl_A_sub2;
ublas_B += ublas_A_sub2;
vcl_B += vcl_A_sub2;
ublas_A_sub1 +=
prod(ublas_C_sub, ublas_D_sub);
std::cout << "Result ublas: " << ublas_A << std::endl;
std::cout << "Result ViennaCL: " << vcl_A << std::endl;
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}