All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sparse.cpp
Go to the documentation of this file.
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the PDF manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
// system headers
#include <iostream>
// ublas headers
#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>
// Must be set if you want to use ViennaCL algorithms on ublas objects
#define VIENNACL_WITH_UBLAS 1
// ViennaCL includes
#include "viennacl/scalar.hpp"
#include "viennacl/vector.hpp"
#include "viennacl/compressed_matrix.hpp"
#include "viennacl/linalg/prod.hpp"
// Additional helper functions for this tutorial:
#include "vector-io.hpp"
// Shortcut for writing 'ublas::' instead of 'boost::numeric::ublas::'
using namespace boost::numeric;
int main()
{
typedef float ScalarType;
std::size_t size = 5;
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;
viennacl::vector<ScalarType> vcl_rhs(size);
viennacl::compressed_matrix<ScalarType> vcl_compressed_matrix(size, size);
viennacl::copy(rhs, vcl_rhs);
viennacl::copy(ublas_matrix, vcl_compressed_matrix);
// just get the data directly from the GPU and print it:
ublas::compressed_matrix<ScalarType> temp(size, size);
viennacl::copy(vcl_compressed_matrix, temp);
std::cout << "ViennaCL: " << temp << std::endl;
// now modify GPU data directly:
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; //this is a new nonzero entry
vcl_compressed_matrix(4, 3) = -3.0f;
// and print it again:
viennacl::copy(vcl_compressed_matrix, temp);
std::cout << "ViennaCL matrix copied to uBLAS matrix: " << temp << std::endl;
std::cout << "ublas: " << ublas::prod(temp, rhs) << std::endl;
std::cout << "ViennaCL: " << viennacl::linalg::prod(vcl_compressed_matrix, vcl_rhs) << std::endl;
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}
prod.hpp
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
main
int main()
Definition: sparse.cpp:1076
vector-io.hpp
viennacl::linalg::prod
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
viennacl::traits::size
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:239
compressed_matrix.hpp
Implementation of the compressed_matrix class.
viennacl::vector< ScalarType >
prod
void prod(std::vector< std::map< IndexT, NumericT > > const &stl_A, std::vector< std::map< IndexT, NumericT > > const &stl_B, std::vector< std::map< IndexT, NumericT > > &stl_C)
Definition: sparse_prod.cpp:114
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::copy
void copy(std::vector< NumericT > &cpu_vec, circulant_matrix< NumericT, AlignmentV > &gpu_mat)
Copies a circulant matrix from the std::vector to the OpenCL device (either GPU or multi-core CPU) ...
Definition: circulant_matrix.hpp:150
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
viennacl::compressed_matrix
A sparse square matrix in compressed sparse rows format.
Definition: compressed_matrix.hpp:559
scalar.hpp
Implementation of the ViennaCL scalar class.