ViennaCL - The Vienna Computing Library: libviennacl/src/blas1.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
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
31 #include "viennacl/linalg/inner_prod.hpp"
32
33 //include the generic norm functions of ViennaCL
34 #include "viennacl/linalg/norm_1.hpp"
35 #include "viennacl/linalg/norm_2.hpp"
36 #include "viennacl/linalg/norm_inf.hpp"
37
38 // IxAMAX
39
40 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLiamax(ViennaCLInt *index, ViennaCLVector x)
41 {
42 viennacl::backend::mem_handle v1_handle;
43
44 if (init_vector(v1_handle, x) != ViennaCLSuccess)
45 return ViennaCLGenericFailure;
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:
68 return ViennaCLGenericFailure;
69 }
70 }
71
72
73
74
75 // xASUM
76
77 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLasum(ViennaCLHostScalar *alpha, ViennaCLVector x)
78 {
79 if ((*alpha)->precision != x->precision)
80 return ViennaCLGenericFailure;
81
82 viennacl::backend::mem_handle v1_handle;
83
84 if (init_vector(v1_handle, x) != ViennaCLSuccess)
85 return ViennaCLGenericFailure;
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
116 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLaxpy(ViennaCLHostScalar alpha, ViennaCLVector x, ViennaCLVector y)
117 {
118 if (alpha->precision != x->precision)
119 return ViennaCLGenericFailure;
120
121 if (x->precision != y->precision)
122 return ViennaCLGenericFailure;
123
124 viennacl::backend::mem_handle v1_handle;
125 viennacl::backend::mem_handle v2_handle;
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
163 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLcopy(ViennaCLVector x, ViennaCLVector y)
164 {
165 if (x->precision != y->precision)
166 return ViennaCLGenericFailure;
167
168 viennacl::backend::mem_handle v1_handle;
169 viennacl::backend::mem_handle v2_handle;
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
206 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLdot(ViennaCLHostScalar *alpha, ViennaCLVector x, ViennaCLVector y)
207 {
208 if ((*alpha)->precision != x->precision)
209 return ViennaCLGenericFailure;
210
211 if (x->precision != y->precision)
212 return ViennaCLGenericFailure;
213
214 viennacl::backend::mem_handle v1_handle;
215 viennacl::backend::mem_handle v2_handle;
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
252 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLnrm2(ViennaCLHostScalar *alpha, ViennaCLVector x)
253 {
254 if ((*alpha)->precision != x->precision)
255 return ViennaCLGenericFailure;
256
257 viennacl::backend::mem_handle v1_handle;
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
291 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLrot(ViennaCLVector x, ViennaCLVector y,
292 ViennaCLHostScalar c, ViennaCLHostScalar s)
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
303 viennacl::backend::mem_handle v1_handle;
304 viennacl::backend::mem_handle v2_handle;
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
320 viennacl::linalg::plane_rotation(v1, v2, c->value_float, s->value_float);
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
330 viennacl::linalg::plane_rotation(v1, v2, c->value_double, s->value_double);
331 return ViennaCLSuccess;
332 }
333
334 default:
335 return ViennaCLGenericFailure;
336 }
337 }
338
339 // xSCAL
340
341 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLscal(ViennaCLHostScalar alpha, ViennaCLVector x)
342 {
343 if (alpha->precision != x->precision)
344 return ViennaCLGenericFailure;
345
346 viennacl::backend::mem_handle v1_handle;
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
379 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLswap(ViennaCLVector x, ViennaCLVector y)
380 {
381 if (x->precision != y->precision)
382 return ViennaCLGenericFailure;
383
384 viennacl::backend::mem_handle v1_handle;
385 viennacl::backend::mem_handle v2_handle;
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
viennacl::linalg::norm_2
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:96
viennacl::linalg::index_norm_inf
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.
Definition: vector_operations.hpp:845
norm_2.hpp
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
ViennaCLaxpy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLaxpy(ViennaCLHostScalar alpha, ViennaCLVector x, ViennaCLVector y)
Definition: blas1.cpp:116
ViennaCLVector_impl::precision
ViennaCLPrecision precision
Definition: viennacl_private.hpp:97
ViennaCLHostScalar_impl::value_float
float value_float
Definition: viennacl_private.hpp:72
ViennaCLswap
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLswap(ViennaCLVector x, ViennaCLVector y)
Definition: blas1.cpp:379
ViennaCLnrm2
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLnrm2(ViennaCLHostScalar *alpha, ViennaCLVector x)
Definition: blas1.cpp:252
viennacl::linalg::plane_rotation
void plane_rotation(vector_base< T > &vec1, vector_base< T > &vec2, T alpha, T beta)
Computes a plane rotation of two vectors.
Definition: vector_operations.hpp:1152
ViennaCLscal
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLscal(ViennaCLHostScalar alpha, ViennaCLVector x)
Definition: blas1.cpp:341
viennacl::linalg::inner_prod
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
ViennaCLDouble
Definition: viennacl.hpp:93
ViennaCLStatus
ViennaCLStatus
Definition: viennacl.hpp:97
inner_prod.hpp
Generic interface for the computation of inner products. See viennacl/linalg/vector_operations.hpp for implementations.
ViennaCLVector_impl::size
ViennaCLInt size
Definition: viennacl_private.hpp:110
ViennaCLrot
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLrot(ViennaCLVector x, ViennaCLVector y, ViennaCLHostScalar c, ViennaCLHostScalar s)
Definition: blas1.cpp:291
norm_1.hpp
Generic interface for the l^1-norm. See viennacl/linalg/vector_operations.hpp for implementations...
viennacl::swap
void swap(vector_base< T > &vec1, vector_base< T > &vec2)
Swaps the contents of two vectors, data is copied.
Definition: vector.hpp:1648
VIENNACL_EXPORTED_FUNCTION
#define VIENNACL_EXPORTED_FUNCTION
Definition: viennacl.hpp:40
v1
viennacl::vector< float > v1
Definition: global_variables.cpp:60
ViennaCLVector_impl::inc
ViennaCLInt inc
Definition: viennacl_private.hpp:109
ViennaCLVector_impl
Definition: viennacl_private.hpp:94
viennacl_private.hpp
ViennaCLiamax
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLiamax(ViennaCLInt *index, ViennaCLVector x)
Definition: blas1.cpp:40
ViennaCLasum
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLasum(ViennaCLHostScalar *alpha, ViennaCLVector x)
Definition: blas1.cpp:77
viennacl::vector_base< float >
init_vector.hpp
ViennaCLInt
int ViennaCLInt
Definition: viennacl.hpp:48
ViennaCLdot
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLdot(ViennaCLHostScalar *alpha, ViennaCLVector x, ViennaCLVector y)
Definition: blas1.cpp:206
ViennaCLGenericFailure
Definition: viennacl.hpp:100
ViennaCLVector_impl::offset
ViennaCLInt offset
Definition: viennacl_private.hpp:108
ViennaCLHostScalar_impl
Definition: viennacl_private.hpp:67
v2
viennacl::vector< int > v2
Definition: global_variables.cpp:61
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::linalg::norm_1
T norm_1(std::vector< T, A > const &v1)
Definition: norm_1.hpp:61
viennacl::backend::mem_handle
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
ViennaCLcopy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLcopy(ViennaCLVector x, ViennaCLVector y)
Definition: blas1.cpp:163
scalar.hpp
Implementation of the ViennaCL scalar class.
viennacl.hpp
norm_inf.hpp
Generic interface for the l^infty-norm. See viennacl/linalg/vector_operations.hpp for implementations...
ViennaCLHostScalar_impl::value_double
double value_double
Definition: viennacl_private.hpp:73
ViennaCLFloat
Definition: viennacl.hpp:92
ViennaCLHostScalar_impl::precision
ViennaCLPrecision precision
Definition: viennacl_private.hpp:69
ViennaCLSuccess
Definition: viennacl.hpp:99
viennacl::vector_base::size_type
SizeT size_type
Definition: vector_def.hpp:112