ViennaCL - The Vienna Computing Library: examples/tutorial/vector-range.cpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vector-range.cpp
Go to the documentation of this file.
1 /* =========================================================================
2 Copyright (c) 2010-2016, 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
26 // Activate ublas support in ViennaCL
27 #define VIENNACL_WITH_UBLAS
28
29 // System headers
30 #include <iostream>
31 #include <string>
32
33 // ViennaCL headers
34 #include "viennacl/scalar.hpp"
35 #include "viennacl/matrix.hpp"
36 #include "viennacl/linalg/prod.hpp"
37 #include "viennacl/vector_proxy.hpp"
38
39 // Boost headers
40 #include "boost/numeric/ublas/vector.hpp"
41 #include "boost/numeric/ublas/vector_proxy.hpp"
42 #include "boost/numeric/ublas/io.hpp"
43
48 int main (int, const char **)
49 {
50 //feel free to change the floating point type to 'double' if supported by your hardware
51 typedef float ScalarType;
52
53 typedef boost::numeric::ublas::vector<ScalarType> VectorType;
54 typedef viennacl::vector<ScalarType> VCLVectorType;
55
56 std::size_t dim_large = 7;
57 std::size_t dim_small = 3;
58
62 VectorType ublas_v1(dim_large);
63 VectorType ublas_v2(dim_small);
64
65 for (std::size_t i=0; i<ublas_v1.size(); ++i)
66 ublas_v1(i) = ScalarType(i+1);
67
68 for (std::size_t i=0; i<ublas_v2.size(); ++i)
69 ublas_v2(i) = ScalarType(dim_large + i);
70
71
75 boost::numeric::ublas::range ublas_r1(0, dim_small); //the first 'dim_small' entries
76 boost::numeric::ublas::range ublas_r2(dim_small - 1, 2*dim_small - 1); // 'dim_small' entries somewhere from the middle
77 boost::numeric::ublas::range ublas_r3(dim_large - dim_small, dim_large); // the last 'dim_small' entries
78 boost::numeric::ublas::vector_range<VectorType> ublas_v1_sub1(ublas_v1, ublas_r1); // front part of vector v_1
79 boost::numeric::ublas::vector_range<VectorType> ublas_v1_sub2(ublas_v1, ublas_r2); // center part of vector v_1
80 boost::numeric::ublas::vector_range<VectorType> ublas_v1_sub3(ublas_v1, ublas_r3); // tail of vector v_1
81
82
86 VCLVectorType vcl_v1(dim_large);
87 VCLVectorType vcl_v2(dim_small);
88
89 viennacl::copy(ublas_v1, vcl_v1);
90 viennacl::copy(ublas_v2, vcl_v2);
91
92
96 viennacl::range vcl_r1(0, dim_small); //the first 'dim_small' entries
97 viennacl::range vcl_r2(dim_small - 1, 2*dim_small - 1); // 'dim_small' entries somewhere from the middle
98 viennacl::range vcl_r3(dim_large - dim_small, dim_large); // the last 'dim_small' entries
99 viennacl::vector_range<VCLVectorType> vcl_v1_sub1(vcl_v1, vcl_r1); // front part of vector v_1
100 viennacl::vector_range<VCLVectorType> vcl_v1_sub2(vcl_v1, vcl_r2); // center part of vector v_1
101 viennacl::vector_range<VCLVectorType> vcl_v1_sub3(vcl_v1, vcl_r3); // tail of vector v_1
102
103
107 ublas_v1_sub1 = ublas_v2;
108 viennacl::copy(ublas_v2, vcl_v1_sub1);
109 viennacl::copy(vcl_v1_sub1, ublas_v2);
110
115 ublas_v1_sub1 += ublas_v1_sub1;
116 vcl_v1_sub1 += vcl_v1_sub1;
117
118 ublas_v1_sub2 += ublas_v1_sub2;
119 vcl_v1_sub2 += vcl_v1_sub2;
120
121 ublas_v1_sub3 += ublas_v1_sub3;
122 vcl_v1_sub3 += vcl_v1_sub3;
123
127 std::cout << "ublas: " << ublas_v1 << std::endl;
128 std::cout << "ViennaCL: " << vcl_v1 << std::endl;
129
133 std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
134
135 return EXIT_SUCCESS;
136 }
137
prod.hpp
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
matrix.hpp
Implementation of the dense matrix class.
main
int main()
Definition: bisect.cpp:91
viennacl::range
basic_range range
Definition: forwards.h:424
viennacl::vector_range
Class for representing non-strided subvectors of a bigger vector x.
Definition: forwards.h:434
viennacl::vector< ScalarType >
vector_proxy.hpp
Proxy classes for vectors.
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
viennacl::basic_range
A range class that refers to an interval [start, stop), where 'start' is included, and 'stop' is excluded.
Definition: forwards.h:424
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
scalar.hpp
Implementation of the ViennaCL scalar class.