ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
armadillo-with-viennacl.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2015, Institute for Microelectronics,
3  Institute for Analysis and Scientific Computing,
4  TU Wien.
5  Portions of this software are copyright by UChicago Argonne, LLC.
6 
7  -----------------
8  ViennaCL - The Vienna Computing Library
9  -----------------
10 
11  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
12 
13  (A list of authors and contributors can be found in the PDF manual)
14 
15  License: MIT (X11), see file LICENSE in the base directory
16 ============================================================================= */
17 
25 // System headers
26 #include <iostream>
27 
28 // Armadillo headers (disable BLAS and LAPACK to avoid linking issues)
29 #define ARMA_DONT_USE_BLAS
30 #define ARMA_DONT_USE_LAPACK
31 #include <armadillo>
32 
33 // IMPORTANT: Must be set prior to any ViennaCL includes if you want to use ViennaCL algorithms on Armadillo objects
34 #define VIENNACL_WITH_ARMADILLO 1
35 
36 
37 // ViennaCL includes
38 #include "viennacl/vector.hpp"
39 #include "viennacl/matrix.hpp"
41 #include "viennacl/linalg/prod.hpp"
42 
43 
44 // Helper functions for this tutorial:
45 #include "vector-io.hpp"
46 
47 
58 template<typename NumericT>
59 void run_tutorial()
60 {
61  typedef arma::SpMat<NumericT> ArmaSparseMatrix;
62  typedef arma::Mat<NumericT> ArmaMatrix;
63  typedef arma::Col<NumericT> ArmaVector;
64 
68  ArmaMatrix arma_densemat(6, 5);
69  ArmaMatrix arma_densemat2(6, 5);
70  arma_densemat(0,0) = 2.0; arma_densemat(0,1) = -1.0;
71  arma_densemat(1,0) = -1.0; arma_densemat(1,1) = 2.0; arma_densemat(1,2) = -1.0;
72  arma_densemat(2,1) = -1.0; arma_densemat(2,2) = -1.0; arma_densemat(2,3) = -1.0;
73  arma_densemat(3,2) = -1.0; arma_densemat(3,3) = 2.0; arma_densemat(3,4) = -1.0;
74  arma_densemat(5,4) = -1.0; arma_densemat(4,4) = -1.0;
75 
79  ArmaSparseMatrix arma_sparsemat(6, 5);
80  ArmaSparseMatrix arma_sparsemat2(6, 5);
81  arma_sparsemat(0,0) = 2.0; arma_sparsemat(0,1) = -1.0;
82  arma_sparsemat(1,1) = 2.0; arma_sparsemat(1,2) = -1.0;
83  arma_sparsemat(2,2) = -1.0; arma_sparsemat(2,3) = -1.0;
84  arma_sparsemat(3,3) = 2.0; arma_sparsemat(3,4) = -1.0;
85  arma_sparsemat(5,4) = -1.0;
86 
90  ArmaVector arma_rhs(5);
91  ArmaVector arma_result(6);
92  ArmaVector arma_temp(6);
93 
94  arma_rhs(0) = 10.0;
95  arma_rhs(1) = 11.0;
96  arma_rhs(2) = 12.0;
97  arma_rhs(3) = 13.0;
98  arma_rhs(4) = 14.0;
99 
100 
104  viennacl::vector<NumericT> vcl_rhs(5);
105  viennacl::vector<NumericT> vcl_result(6);
106  viennacl::matrix<NumericT> vcl_densemat(6, 5);
107  viennacl::compressed_matrix<NumericT> vcl_sparsemat(6, 5);
108 
109 
113  viennacl::copy(arma_rhs.memptr(), arma_rhs.memptr() + arma_rhs.n_elem, vcl_rhs.begin()); //method 1: via iterator interface (cf. std::copy())
114  viennacl::copy(arma_rhs, vcl_rhs); //method 2: via built-in wrappers (convenience layer)
115 
116  viennacl::copy(arma_densemat, vcl_densemat);
117  viennacl::copy(arma_sparsemat, vcl_sparsemat);
118  std::cout << "VCL sparsematrix dimensions: " << vcl_sparsemat.size1() << ", " << vcl_sparsemat.size2() << std::endl;
119 
120  // For completeness: Copy matrices from ViennaCL back to Eigen:
121  viennacl::copy(vcl_densemat, arma_densemat2);
122  viennacl::copy(vcl_sparsemat, arma_sparsemat2);
123 
124 
128  arma_result = arma_densemat * arma_rhs;
129  vcl_result = viennacl::linalg::prod(vcl_densemat, vcl_rhs);
130  viennacl::copy(vcl_result, arma_temp);
131  std::cout << "Difference for dense matrix-vector product: " << norm(arma_result - arma_temp) << std::endl;
132  std::cout << "Difference for dense matrix-vector product (Armadillo -> ViennaCL -> Armadillo): "
133  << norm(arma_densemat2 * arma_rhs - arma_temp) << std::endl;
134 
138  arma_result = arma_sparsemat * arma_rhs;
139  vcl_result = viennacl::linalg::prod(vcl_sparsemat, vcl_rhs);
140  viennacl::copy(vcl_result, arma_temp);
141  std::cout << "Difference for sparse matrix-vector product: " << norm(arma_result - arma_temp) << std::endl;
142  std::cout << "Difference for sparse matrix-vector product (Armadillo -> ViennaCL -> Armadillo): "
143  << norm(arma_sparsemat2 * arma_rhs - arma_temp) << std::endl;
144 }
145 
146 
150 int main(int, char *[])
151 {
152  std::cout << "----------------------------------------------" << std::endl;
153  std::cout << "## Single precision" << std::endl;
154  std::cout << "----------------------------------------------" << std::endl;
155  run_tutorial<float>();
156 
157 #ifdef VIENNACL_HAVE_OPENCL
159 #endif
160  {
161  std::cout << "----------------------------------------------" << std::endl;
162  std::cout << "## Double precision" << std::endl;
163  std::cout << "----------------------------------------------" << std::endl;
164  run_tutorial<double>();
165  }
166 
170  std::cout << std::endl;
171  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
172  std::cout << std::endl;
173 
174 }
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
Implementation of the dense matrix class.
int main()
Definition: bisect.cpp:91
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
Definition: backend.hpp:351
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
Implementation of the compressed_matrix class.
bool double_support() const
ViennaCL convenience function: Returns true if the device supports double precision.
Definition: device.hpp:956
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
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) ...