This tutorial demonstrates the use of sparse matrices. The primary operation for sparse matrices in ViennaCL is the sparse matrix-vector product.
We start with including the respective headers:
#include <iostream>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/operation.hpp>
#include <boost/numeric/ublas/operation_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>
#define VIENNACL_WITH_UBLAS 1
using namespace boost::numeric;
We setup a sparse matrix in uBLAS and populate it with values. Then, the respective ViennaCL sparse matrix is created and initialized with data from the uBLAS matrix. After a direct manipulation of the ViennaCL matrix, matrix-vector products are computed with both matrices.
Set up some ublas objects
ublas::vector<ScalarType> rhs = ublas::scalar_vector<ScalarType>(
size,
ScalarType(size));
ublas::compressed_matrix<ScalarType> ublas_matrix(size, size);
ublas_matrix(0,0) = 2.0f; ublas_matrix(0,1) = -1.0f;
ublas_matrix(1,0) = -1.0f; ublas_matrix(1,1) = 2.0f; ublas_matrix(1,2) = -1.0f;
ublas_matrix(2,1) = -1.0f; ublas_matrix(2,2) = 2.0f; ublas_matrix(2,3) = -1.0f;
ublas_matrix(3,2) = -1.0f; ublas_matrix(3,3) = 2.0f; ublas_matrix(3,4) = -1.0f;
ublas_matrix(4,3) = -1.0f; ublas_matrix(4,4) = 2.0f;
std::cout << "ublas matrix: " << ublas_matrix << std::endl;
Set up some ViennaCL objects and initialize with data from uBLAS objects
ublas::compressed_matrix<ScalarType> temp(size, size);
std::cout << "ViennaCL: " << temp << std::endl;
std::cout << "Modifying vcl_compressed_matrix a bit: " << std::endl;
vcl_compressed_matrix(0, 0) = 3.0f;
vcl_compressed_matrix(2, 3) = -3.0f;
vcl_compressed_matrix(4, 2) = -3.0f;
vcl_compressed_matrix(4, 3) = -3.0f;
std::cout << "ViennaCL matrix copied to uBLAS matrix: " << temp << std::endl;
Compute matrix-vector products and output the results (should match):
std::cout <<
"ublas: " <<
ublas::prod(temp, rhs) << std::endl;
That's it. Print a success message and exit.
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}
Full Example Code
#include <iostream>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/operation.hpp>
#include <boost/numeric/ublas/operation_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>
#define VIENNACL_WITH_UBLAS 1
using namespace boost::numeric;
{
ublas::vector<ScalarType> rhs = ublas::scalar_vector<ScalarType>(
size,
ScalarType(size));
ublas::compressed_matrix<ScalarType> ublas_matrix(size, size);
ublas_matrix(0,0) = 2.0f; ublas_matrix(0,1) = -1.0f;
ublas_matrix(1,0) = -1.0f; ublas_matrix(1,1) = 2.0f; ublas_matrix(1,2) = -1.0f;
ublas_matrix(2,1) = -1.0f; ublas_matrix(2,2) = 2.0f; ublas_matrix(2,3) = -1.0f;
ublas_matrix(3,2) = -1.0f; ublas_matrix(3,3) = 2.0f; ublas_matrix(3,4) = -1.0f;
ublas_matrix(4,3) = -1.0f; ublas_matrix(4,4) = 2.0f;
std::cout << "ublas matrix: " << ublas_matrix << std::endl;
ublas::compressed_matrix<ScalarType> temp(size, size);
std::cout << "ViennaCL: " << temp << std::endl;
std::cout << "Modifying vcl_compressed_matrix a bit: " << std::endl;
vcl_compressed_matrix(0, 0) = 3.0f;
vcl_compressed_matrix(2, 3) = -3.0f;
vcl_compressed_matrix(4, 2) = -3.0f;
vcl_compressed_matrix(4, 3) = -3.0f;
std::cout << "ViennaCL matrix copied to uBLAS matrix: " << temp << std::endl;
std::cout <<
"ublas: " <<
ublas::prod(temp, rhs) << std::endl;
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}