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
blas1.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 
27 // include necessary system headers
28 #include <iostream>
29 
30 //include basic scalar and vector types of ViennaCL
31 #include "viennacl/scalar.hpp"
32 #include "viennacl/vector.hpp"
33 
34 //include the generic inner product functions of ViennaCL
36 
37 //include the generic norm functions of ViennaCL
42 
43 
47 int main()
48 {
49  //Change this type definition to double if your gpu supports that
50  typedef float ScalarType;
51 
53 
63  ScalarType s1 = ScalarType(3.1415926); //note: writing ScalarType s1 = 3.1415926; leads to warnings with some compilers if ScalarType is 'float'.
64  ScalarType s2 = ScalarType(2.71763);
65  ScalarType s3 = ScalarType(42.0);
66 
70 
74  std::cout << "Copying a few scalars..." << std::endl;
75  vcl_s1 = s1;
76  s2 = vcl_s2;
77  vcl_s3 = s3;
78 
84  std::cout << "Manipulating a few scalars..." << std::endl;
85  std::cout << "operator +=" << std::endl;
86  s1 += s2;
87  vcl_s1 += vcl_s2;
88 
89  std::cout << "operator *=" << std::endl;
90  s1 *= s2;
91  vcl_s1 *= vcl_s2;
92 
93  std::cout << "operator -=" << std::endl;
94  s1 -= s2;
95  vcl_s1 -= vcl_s2;
96 
97  std::cout << "operator /=" << std::endl;
98  s1 /= s2;
99  vcl_s1 /= vcl_s2;
100 
101  std::cout << "operator +" << std::endl;
102  s1 = s2 + s3;
103  vcl_s1 = vcl_s2 + vcl_s3;
104 
105  std::cout << "multiple operators" << std::endl;
106  s1 = s2 + s3 * s2 - s3 / s1;
107  vcl_s1 = vcl_s2 + vcl_s3 * vcl_s2 - vcl_s3 / vcl_s1;
108 
109 
113  std::cout << "mixed operations" << std::endl;
114  vcl_s1 = s1 * vcl_s2 + s3 - vcl_s3;
115 
116 
121  std::cout << "CPU scalar s3: " << s3 << std::endl;
122  std::cout << "GPU scalar vcl_s3: " << vcl_s3 << std::endl;
123 
124 
130  std::vector<ScalarType> std_vec1(10);
131  std::vector<ScalarType> std_vec2(10);
132  ScalarType plain_vec3[10]; //plain C array
133 
134  viennacl::vector<ScalarType> vcl_vec1(10);
135  viennacl::vector<ScalarType> vcl_vec2(10);
136  viennacl::vector<ScalarType> vcl_vec3(10);
137 
142  for (unsigned int i = 0; i < 10; ++i)
143  {
144  std_vec1[i] = randomNumber();
145  vcl_vec2(i) = randomNumber(); //also works for GPU vectors, but is MUCH slower (approx. factor 10.000) than the CPU analogue
146  plain_vec3[i] = randomNumber();
147  }
148 
152  viennacl::copy(std_vec1.begin(), std_vec1.end(), vcl_vec1.begin()); //either the STL way
153  viennacl::copy(vcl_vec2.begin(), vcl_vec2.end(), std_vec2.begin()); //either the STL way
154  viennacl::copy(vcl_vec2, std_vec2); //using the short hand notation for objects that provide .begin() and .end() members
155  viennacl::copy(vcl_vec2.begin(), vcl_vec2.end(), plain_vec3); //copy to plain C vector
156 
160  viennacl::copy(std_vec1.begin() + 4, std_vec1.begin() + 8, vcl_vec1.begin() + 4); //cpu to gpu
161  viennacl::copy(vcl_vec1.begin() + 4, vcl_vec1.begin() + 8, vcl_vec2.begin() + 1); //gpu to gpu
162  viennacl::copy(vcl_vec1.begin() + 4, vcl_vec1.begin() + 8, std_vec1.begin() + 1); //gpu to cpu
163 
167  vcl_s1 = viennacl::linalg::inner_prod(vcl_vec1, vcl_vec2);
168  s1 = viennacl::linalg::inner_prod(vcl_vec1, vcl_vec2);
169  s2 = viennacl::linalg::inner_prod(std_vec1, std_vec2); //inner prod can also be used with std::vector (computations are carried out on CPU then)
170 
174  s1 = viennacl::linalg::norm_1(vcl_vec1);
175  vcl_s2 = viennacl::linalg::norm_2(vcl_vec2);
176  s3 = viennacl::linalg::norm_inf(vcl_vec3);
177 
178 
182  viennacl::linalg::plane_rotation(vcl_vec1, vcl_vec2, 1.1f, 2.3f);
183 
188  //simple expression:
189  vcl_vec1 = vcl_s1 * vcl_vec2 / vcl_s3;
190 
191  //more complicated expression:
192  vcl_vec1 = vcl_vec2 / vcl_s3 + vcl_s2 * (vcl_vec1 - vcl_s2 * vcl_vec2);
193 
194 
198  viennacl::swap(vcl_vec1, vcl_vec2); //swaps all entries in memory
199  viennacl::fast_swap(vcl_vec1, vcl_vec2); //swaps OpenCL memory handles only
200 
204  vcl_vec1.clear();
205  vcl_vec2.clear();
206 
210  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
211 
212  return EXIT_SUCCESS;
213 }
214 
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:96
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
vector< NumericT, AlignmentV > & fast_swap(vector< NumericT, AlignmentV > &v1, vector< NumericT, AlignmentV > &v2)
Swaps the content of two vectors by swapping OpenCL handles only, NO data is copied.
Definition: vector.hpp:1659
void plane_rotation(vector_base< T > &vec1, vector_base< T > &vec2, T alpha, T beta)
Computes a plane rotation of two vectors.
int main()
Definition: bisect.cpp:91
viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< VectorT1 >::type >::value, typename VectorT1::value_type >::type inner_prod(VectorT1 const &v1, VectorT2 const &v2)
Definition: inner_prod.hpp:100
viennacl::scalar< int > s2
viennacl::scalar< float > s1
Generic interface for the computation of inner products. See viennacl/linalg/vector_operations.hpp for implementations.
Generic interface for the l^1-norm. See viennacl/linalg/vector_operations.hpp for implementations...
void swap(vector_base< T > &vec1, vector_base< T > &vec2)
Swaps the contents of two vectors, data is copied.
Definition: vector.hpp:1648
Random number generator for returning uniformly distributed values in the closed interval [0...
Definition: random.hpp:44
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
T norm_inf(std::vector< T, A > const &v1)
Definition: norm_inf.hpp:60
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) ...
A small collection of sequential random number generators.
T norm_1(std::vector< T, A > const &v1)
Definition: norm_1.hpp:61
float ScalarType
Definition: fft_1d.cpp:42
Implementation of the ViennaCL scalar class.
Generic interface for the l^infty-norm. See viennacl/linalg/vector_operations.hpp for implementations...