ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
blas1_opencl.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 basic scalar and vector types of ViennaCL
25 #include "viennacl/scalar.hpp"
26 #include "viennacl/vector.hpp"
27 
28 //include the generic inner product functions of ViennaCL
30 
31 //include the generic norm functions of ViennaCL
35 
36 #ifdef VIENNACL_WITH_OPENCL
37 
38 // IxAMAX
39 
41  ViennaCLInt *index,
42  cl_mem x, ViennaCLInt offx, ViennaCLInt incx)
43 {
45  typedef viennacl::vector_base<float>::size_type difference_type;
46  viennacl::vector_base<float> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
47 
48  *index = static_cast<ViennaCLInt>(viennacl::linalg::index_norm_inf(v1));
49  return ViennaCLSuccess;
50 }
51 
53  ViennaCLInt *index,
54  cl_mem x, ViennaCLInt offx, ViennaCLInt incx)
55 {
57  typedef viennacl::vector_base<double>::size_type difference_type;
58  viennacl::vector_base<double> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
59 
60  *index = static_cast<ViennaCLInt>(viennacl::linalg::index_norm_inf(v1));
61  return ViennaCLSuccess;
62 }
63 
64 
65 
66 
67 // xASUM
68 
70  float *alpha,
71  cl_mem x, ViennaCLInt offx, ViennaCLInt incx)
72 {
74  typedef viennacl::vector_base<float>::size_type difference_type;
75  viennacl::vector_base<float> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
76 
77  *alpha = viennacl::linalg::norm_1(v1);
78  return ViennaCLSuccess;
79 }
80 
82  double *alpha,
83  cl_mem x, ViennaCLInt offx, ViennaCLInt incx)
84 {
86  typedef viennacl::vector_base<double>::size_type difference_type;
87  viennacl::vector_base<double> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
88 
89  *alpha = viennacl::linalg::norm_1(v1);
90  return ViennaCLSuccess;
91 }
92 
93 
94 
95 // xAXPY
96 
98  float alpha,
99  cl_mem x, ViennaCLInt offx, ViennaCLInt incx,
100  cl_mem y, ViennaCLInt offy, ViennaCLInt incy)
101 {
102  typedef viennacl::vector_base<float>::size_type size_type;
103  typedef viennacl::vector_base<float>::size_type difference_type;
104  viennacl::vector_base<float> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
105  viennacl::vector_base<float> v2(y, size_type(n), size_type(offy), difference_type(incy), viennacl::ocl::get_context(backend->opencl_backend.context_id));
106 
107  v2 += alpha * v1;
108  return ViennaCLSuccess;
109 }
110 
112  double alpha,
113  cl_mem x, ViennaCLInt offx, ViennaCLInt incx,
114  cl_mem y, ViennaCLInt offy, ViennaCLInt incy)
115 {
117  typedef viennacl::vector_base<double>::size_type difference_type;
118  viennacl::vector_base<double> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
119  viennacl::vector_base<double> v2(y, size_type(n), size_type(offy), difference_type(incy), viennacl::ocl::get_context(backend->opencl_backend.context_id));
120 
121  v2 += alpha * v1;
122  return ViennaCLSuccess;
123 }
124 
125 
126 // xCOPY
127 
129  cl_mem x, ViennaCLInt offx, ViennaCLInt incx,
130  cl_mem y, ViennaCLInt offy, ViennaCLInt incy)
131 {
132  typedef viennacl::vector_base<float>::size_type size_type;
133  typedef viennacl::vector_base<float>::size_type difference_type;
134  viennacl::vector_base<float> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
135  viennacl::vector_base<float> v2(y, size_type(n), size_type(offy), difference_type(incy), viennacl::ocl::get_context(backend->opencl_backend.context_id));
136 
137  v2 = v1;
138  return ViennaCLSuccess;
139 }
140 
142  cl_mem x, ViennaCLInt offx, ViennaCLInt incx,
143  cl_mem y, ViennaCLInt offy, ViennaCLInt incy)
144 {
146  typedef viennacl::vector_base<double>::size_type difference_type;
147  viennacl::vector_base<double> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
148  viennacl::vector_base<double> v2(y, size_type(n), size_type(offy), difference_type(incy), viennacl::ocl::get_context(backend->opencl_backend.context_id));
149 
150  v2 = v1;
151  return ViennaCLSuccess;
152 }
153 
154 // xDOT
155 
157  float *alpha,
158  cl_mem x, ViennaCLInt offx, ViennaCLInt incx,
159  cl_mem y, ViennaCLInt offy, ViennaCLInt incy)
160 {
161  typedef viennacl::vector_base<float>::size_type size_type;
162  typedef viennacl::vector_base<float>::size_type difference_type;
163  viennacl::vector_base<float> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
164  viennacl::vector_base<float> v2(y, size_type(n), size_type(offy), difference_type(incy), viennacl::ocl::get_context(backend->opencl_backend.context_id));
165 
167  return ViennaCLSuccess;
168 }
169 
171  double *alpha,
172  cl_mem x, ViennaCLInt offx, ViennaCLInt incx,
173  cl_mem y, ViennaCLInt offy, ViennaCLInt incy)
174 {
176  typedef viennacl::vector_base<double>::size_type difference_type;
177  viennacl::vector_base<double> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
178  viennacl::vector_base<double> v2(y, size_type(n), size_type(offy), difference_type(incy), viennacl::ocl::get_context(backend->opencl_backend.context_id));
179 
181  return ViennaCLSuccess;
182 }
183 
184 
185 // xNRM2
186 
188  float *alpha,
189  cl_mem x, ViennaCLInt offx, ViennaCLInt incx)
190 {
191  typedef viennacl::vector_base<float>::size_type size_type;
192  typedef viennacl::vector_base<float>::size_type difference_type;
193  viennacl::vector_base<float> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
194 
195  *alpha = viennacl::linalg::norm_2(v1);
196  return ViennaCLSuccess;
197 }
198 
200  double *alpha,
201  cl_mem x, ViennaCLInt offx, ViennaCLInt incx)
202 {
204  typedef viennacl::vector_base<double>::size_type difference_type;
205  viennacl::vector_base<double> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
206 
207  *alpha = viennacl::linalg::norm_2(v1);
208  return ViennaCLSuccess;
209 }
210 
211 
212 // xROT
213 
215  cl_mem x, ViennaCLInt offx, ViennaCLInt incx,
216  cl_mem y, ViennaCLInt offy, ViennaCLInt incy,
217  float c, float s)
218 {
219  typedef viennacl::vector_base<float>::size_type size_type;
220  typedef viennacl::vector_base<float>::size_type difference_type;
221  viennacl::vector_base<float> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
222  viennacl::vector_base<float> v2(y, size_type(n), size_type(offy), difference_type(incy), viennacl::ocl::get_context(backend->opencl_backend.context_id));
223 
225  return ViennaCLSuccess;
226 }
227 
229  cl_mem x, ViennaCLInt offx, ViennaCLInt incx,
230  cl_mem y, ViennaCLInt offy, ViennaCLInt incy,
231  double c, double s)
232 {
234  typedef viennacl::vector_base<double>::size_type difference_type;
235  viennacl::vector_base<double> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
236  viennacl::vector_base<double> v2(y, size_type(n), size_type(offy), difference_type(incy), viennacl::ocl::get_context(backend->opencl_backend.context_id));
237 
239  return ViennaCLSuccess;
240 }
241 
242 
243 
244 // xSCAL
245 
247  float alpha,
248  cl_mem x, ViennaCLInt offx, ViennaCLInt incx)
249 {
250  typedef viennacl::vector_base<float>::size_type size_type;
251  typedef viennacl::vector_base<float>::size_type difference_type;
252  viennacl::vector_base<float> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
253 
254  v1 *= alpha;
255  return ViennaCLSuccess;
256 }
257 
259  double alpha,
260  cl_mem x, ViennaCLInt offx, ViennaCLInt incx)
261 {
263  typedef viennacl::vector_base<double>::size_type difference_type;
264  viennacl::vector_base<double> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
265 
266  v1 *= alpha;
267  return ViennaCLSuccess;
268 }
269 
270 // xSWAP
271 
273  cl_mem x, ViennaCLInt offx, ViennaCLInt incx,
274  cl_mem y, ViennaCLInt offy, ViennaCLInt incy)
275 {
276  typedef viennacl::vector_base<float>::size_type size_type;
277  typedef viennacl::vector_base<float>::size_type difference_type;
278  viennacl::vector_base<float> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
279  viennacl::vector_base<float> v2(y, size_type(n), size_type(offy), difference_type(incy), viennacl::ocl::get_context(backend->opencl_backend.context_id));
280 
281  viennacl::swap(v1, v2);
282  return ViennaCLSuccess;
283 }
284 
286  cl_mem x, ViennaCLInt offx, ViennaCLInt incx,
287  cl_mem y, ViennaCLInt offy, ViennaCLInt incy)
288 {
290  typedef viennacl::vector_base<double>::size_type difference_type;
291  viennacl::vector_base<double> v1(x, size_type(n), size_type(offx), difference_type(incx), viennacl::ocl::get_context(backend->opencl_backend.context_id));
292  viennacl::vector_base<double> v2(y, size_type(n), size_type(offy), difference_type(incy), viennacl::ocl::get_context(backend->opencl_backend.context_id));
293 
294  viennacl::swap(v1, v2);
295  return ViennaCLSuccess;
296 }
297 #endif
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...
Generic backend for CUDA, OpenCL, host-based stuff.
void plane_rotation(vector_base< T > &vec1, vector_base< T > &vec2, T alpha, T beta)
Computes a plane rotation of two vectors.
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.
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
ViennaCLBackend backend
ViennaCLOpenCLBackend_impl opencl_backend
int ViennaCLInt
Definition: viennacl.hpp:48
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
viennacl::ocl::context & get_context(long i)
Convenience function for returning the current context.
Definition: backend.hpp:225
Implementation of the ViennaCL scalar class.
Generic interface for the l^infty-norm. See viennacl/linalg/vector_operations.hpp for implementations...