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-2014, 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 
18 // include necessary system headers
19 #include <iostream>
20 
21 #include "viennacl.hpp"
22 #include "viennacl_private.hpp"
23 
24 #include "init_vector.hpp"
25 
26 //include basic scalar and vector types of ViennaCL
27 #include "viennacl/scalar.hpp"
28 #include "viennacl/vector.hpp"
29 
30 //include the generic inner product functions of ViennaCL
32 
33 //include the generic norm functions of ViennaCL
37 
38 // IxAMAX
39 
41 {
43 
44  if (init_vector(v1_handle, x) != ViennaCLSuccess)
46 
47  switch (x->precision)
48  {
49  case ViennaCLFloat:
50  {
51  typedef viennacl::vector_base<float>::size_type difference_type;
52  viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
53 
54  *index = static_cast<ViennaCLInt>(viennacl::linalg::index_norm_inf(v1));
55  return ViennaCLSuccess;
56  }
57 
58  case ViennaCLDouble:
59  {
60  typedef viennacl::vector_base<double>::size_type difference_type;
61  viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
62 
63  *index = static_cast<ViennaCLInt>(viennacl::linalg::index_norm_inf(v1));
64  return ViennaCLSuccess;
65  }
66 
67  default:
69  }
70 }
71 
72 
73 
74 
75 // xASUM
76 
78 {
79  if ((*alpha)->precision != x->precision)
81 
83 
84  if (init_vector(v1_handle, x) != ViennaCLSuccess)
86 
87  switch (x->precision)
88  {
89  case ViennaCLFloat:
90  {
91  typedef viennacl::vector_base<float>::size_type difference_type;
92  viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
93 
94  (*alpha)->value_float = viennacl::linalg::norm_1(v1);
95  return ViennaCLSuccess;
96  }
97 
98  case ViennaCLDouble:
99  {
100  typedef viennacl::vector_base<double>::size_type difference_type;
101  viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
102 
103  (*alpha)->value_double = viennacl::linalg::norm_1(v1);
104  return ViennaCLSuccess;
105  }
106 
107  default:
108  return ViennaCLGenericFailure;
109  }
110 }
111 
112 
113 
114 // xAXPY
115 
117 {
118  if (alpha->precision != x->precision)
119  return ViennaCLGenericFailure;
120 
121  if (x->precision != y->precision)
122  return ViennaCLGenericFailure;
123 
126 
127  if (init_vector(v1_handle, x) != ViennaCLSuccess)
128  return ViennaCLGenericFailure;
129 
130  if (init_vector(v2_handle, y) != ViennaCLSuccess)
131  return ViennaCLGenericFailure;
132 
133  switch (x->precision)
134  {
135  case ViennaCLFloat:
136  {
137  typedef viennacl::vector_base<float>::size_type difference_type;
138  viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
139  viennacl::vector_base<float> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
140 
141  v2 += alpha->value_float * v1;
142  return ViennaCLSuccess;
143  }
144 
145  case ViennaCLDouble:
146  {
147  typedef viennacl::vector_base<double>::size_type difference_type;
148  viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
149  viennacl::vector_base<double> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
150 
151  v2 += alpha->value_double * v1;
152  return ViennaCLSuccess;
153  }
154 
155  default:
156  return ViennaCLGenericFailure;
157  }
158 }
159 
160 
161 // xCOPY
162 
164 {
165  if (x->precision != y->precision)
166  return ViennaCLGenericFailure;
167 
170 
171  if (init_vector(v1_handle, x) != ViennaCLSuccess)
172  return ViennaCLGenericFailure;
173 
174  if (init_vector(v2_handle, y) != ViennaCLSuccess)
175  return ViennaCLGenericFailure;
176 
177  switch (x->precision)
178  {
179  case ViennaCLFloat:
180  {
181  typedef viennacl::vector_base<float>::size_type difference_type;
182  viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
183  viennacl::vector_base<float> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
184 
185  v2 = v1;
186  return ViennaCLSuccess;
187  }
188 
189  case ViennaCLDouble:
190  {
191  typedef viennacl::vector_base<double>::size_type difference_type;
192  viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
193  viennacl::vector_base<double> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
194 
195  v2 = v1;
196  return ViennaCLSuccess;
197  }
198 
199  default:
200  return ViennaCLGenericFailure;
201  }
202 }
203 
204 // xDOT
205 
207 {
208  if ((*alpha)->precision != x->precision)
209  return ViennaCLGenericFailure;
210 
211  if (x->precision != y->precision)
212  return ViennaCLGenericFailure;
213 
216 
217  if (init_vector(v1_handle, x) != ViennaCLSuccess)
218  return ViennaCLGenericFailure;
219 
220  if (init_vector(v2_handle, y) != ViennaCLSuccess)
221  return ViennaCLGenericFailure;
222 
223  switch (x->precision)
224  {
225  case ViennaCLFloat:
226  {
227  typedef viennacl::vector_base<float>::size_type difference_type;
228  viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
229  viennacl::vector_base<float> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
230 
231  (*alpha)->value_float = viennacl::linalg::inner_prod(v1, v2);
232  return ViennaCLSuccess;
233  }
234 
235  case ViennaCLDouble:
236  {
237  typedef viennacl::vector_base<double>::size_type difference_type;
238  viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
239  viennacl::vector_base<double> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
240 
241  (*alpha)->value_double = viennacl::linalg::inner_prod(v1, v2);
242  return ViennaCLSuccess;
243  }
244 
245  default:
246  return ViennaCLGenericFailure;
247  }
248 }
249 
250 // xNRM2
251 
253 {
254  if ((*alpha)->precision != x->precision)
255  return ViennaCLGenericFailure;
256 
258 
259  if (init_vector(v1_handle, x) != ViennaCLSuccess)
260  return ViennaCLGenericFailure;
261 
262  switch (x->precision)
263  {
264  case ViennaCLFloat:
265  {
266  typedef viennacl::vector_base<float>::size_type difference_type;
267  viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
268 
269  (*alpha)->value_float = viennacl::linalg::norm_2(v1);
270  return ViennaCLSuccess;
271  }
272 
273  case ViennaCLDouble:
274  {
275  typedef viennacl::vector_base<double>::size_type difference_type;
276  viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
277 
278  (*alpha)->value_double = viennacl::linalg::norm_2(v1);
279  return ViennaCLSuccess;
280  }
281 
282  default:
283  return ViennaCLGenericFailure;
284  }
285 }
286 
287 
288 
289 // xROT
290 
293 {
294  if (c->precision != x->precision)
295  return ViennaCLGenericFailure;
296 
297  if (s->precision != x->precision)
298  return ViennaCLGenericFailure;
299 
300  if (x->precision != y->precision)
301  return ViennaCLGenericFailure;
302 
305 
306  if (init_vector(v1_handle, x) != ViennaCLSuccess)
307  return ViennaCLGenericFailure;
308 
309  if (init_vector(v2_handle, y) != ViennaCLSuccess)
310  return ViennaCLGenericFailure;
311 
312  switch (x->precision)
313  {
314  case ViennaCLFloat:
315  {
316  typedef viennacl::vector_base<float>::size_type difference_type;
317  viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
318  viennacl::vector_base<float> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
319 
321  return ViennaCLSuccess;
322  }
323 
324  case ViennaCLDouble:
325  {
326  typedef viennacl::vector_base<double>::size_type difference_type;
327  viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
328  viennacl::vector_base<double> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
329 
331  return ViennaCLSuccess;
332  }
333 
334  default:
335  return ViennaCLGenericFailure;
336  }
337 }
338 
339 // xSCAL
340 
342 {
343  if (alpha->precision != x->precision)
344  return ViennaCLGenericFailure;
345 
347 
348  if (init_vector(v1_handle, x) != ViennaCLSuccess)
349  return ViennaCLGenericFailure;
350 
351  switch (x->precision)
352  {
353  case ViennaCLFloat:
354  {
355  typedef viennacl::vector_base<float>::size_type difference_type;
356  viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
357 
358  v1 *= alpha->value_float;
359  return ViennaCLSuccess;
360  }
361 
362  case ViennaCLDouble:
363  {
364  typedef viennacl::vector_base<double>::size_type difference_type;
365  viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
366 
367  v1 *= alpha->value_double;
368  return ViennaCLSuccess;
369  }
370 
371  default:
372  return ViennaCLGenericFailure;
373  }
374 }
375 
376 
377 // xSWAP
378 
380 {
381  if (x->precision != y->precision)
382  return ViennaCLGenericFailure;
383 
386 
387  if (init_vector(v1_handle, x) != ViennaCLSuccess)
388  return ViennaCLGenericFailure;
389 
390  if (init_vector(v2_handle, y) != ViennaCLSuccess)
391  return ViennaCLGenericFailure;
392 
393  switch (x->precision)
394  {
395  case ViennaCLFloat:
396  {
397  typedef viennacl::vector_base<float>::size_type difference_type;
398  viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
399  viennacl::vector_base<float> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
400 
401  viennacl::swap(v1, v2);
402  return ViennaCLSuccess;
403  }
404 
405  case ViennaCLDouble:
406  {
407  typedef viennacl::vector_base<double>::size_type difference_type;
408  viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
409  viennacl::vector_base<double> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
410 
411  viennacl::swap(v1, v2);
412  return ViennaCLSuccess;
413  }
414 
415  default:
416  return ViennaCLGenericFailure;
417  }
418 }
419 
420 
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:96
vcl_size_t index_norm_inf(vector_base< T > const &vec)
Computes the index of the first entry that is equal to the supremum-norm in modulus.
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLaxpy(ViennaCLHostScalar alpha, ViennaCLVector x, ViennaCLVector y)
Definition: blas1.cpp:116
ViennaCLPrecision precision
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLswap(ViennaCLVector x, ViennaCLVector y)
Definition: blas1.cpp:379
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLnrm2(ViennaCLHostScalar *alpha, ViennaCLVector x)
Definition: blas1.cpp:252
void plane_rotation(vector_base< T > &vec1, vector_base< T > &vec2, T alpha, T beta)
Computes a plane rotation of two vectors.
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLscal(ViennaCLHostScalar alpha, ViennaCLVector x)
Definition: blas1.cpp:341
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
ViennaCLStatus
Definition: viennacl.hpp:97
Generic interface for the computation of inner products. See viennacl/linalg/vector_operations.hpp for implementations.
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLrot(ViennaCLVector x, ViennaCLVector y, ViennaCLHostScalar c, ViennaCLHostScalar s)
Definition: blas1.cpp:291
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
#define VIENNACL_EXPORTED_FUNCTION
Definition: viennacl.hpp:40
viennacl::vector< float > v1
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLiamax(ViennaCLInt *index, ViennaCLVector x)
Definition: blas1.cpp:40
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLasum(ViennaCLHostScalar *alpha, ViennaCLVector x)
Definition: blas1.cpp:77
int ViennaCLInt
Definition: viennacl.hpp:48
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLdot(ViennaCLHostScalar *alpha, ViennaCLVector x, ViennaCLVector y)
Definition: blas1.cpp:206
viennacl::vector< int > v2
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
T norm_1(std::vector< T, A > const &v1)
Definition: norm_1.hpp:61
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLcopy(ViennaCLVector x, ViennaCLVector y)
Definition: blas1.cpp:163
Implementation of the ViennaCL scalar class.
Generic interface for the l^infty-norm. See viennacl/linalg/vector_operations.hpp for implementations...
ViennaCLPrecision precision