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
29 #include "viennacl/linalg/inner_prod.hpp"
30 
31 //include the generic norm functions of ViennaCL
32 #include "viennacl/linalg/norm_1.hpp"
33 #include "viennacl/linalg/norm_2.hpp"
34 #include "viennacl/linalg/norm_inf.hpp"
35 
36 
37 #ifdef VIENNACL_WITH_CUDA
38 
39 
40 // IxAMAX
41 
ViennaCLCUDAiSamax(ViennaCLBackend,ViennaCLInt n,ViennaCLInt * index,float * x,ViennaCLInt offx,ViennaCLInt incx)42 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDAiSamax(ViennaCLBackend /*backend*/, ViennaCLInt n,
43                                                              ViennaCLInt *index,
44                                                              float *x, ViennaCLInt offx, ViennaCLInt incx)
45 {
46   viennacl::vector_base<float> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
47 
48   *index = static_cast<ViennaCLInt>(viennacl::linalg::index_norm_inf(v1));
49   return ViennaCLSuccess;
50 }
51 
ViennaCLCUDAiDamax(ViennaCLBackend,ViennaCLInt n,ViennaCLInt * index,double * x,ViennaCLInt offx,ViennaCLInt incx)52 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDAiDamax(ViennaCLBackend /*backend*/, ViennaCLInt n,
53                                                              ViennaCLInt *index,
54                                                              double *x, ViennaCLInt offx, ViennaCLInt incx)
55 {
56   viennacl::vector_base<double> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
57 
58   *index = static_cast<ViennaCLInt>(viennacl::linalg::index_norm_inf(v1));
59   return ViennaCLSuccess;
60 }
61 
62 
63 
64 // xASUM
65 
ViennaCLCUDASasum(ViennaCLBackend,ViennaCLInt n,float * alpha,float * x,ViennaCLInt offx,ViennaCLInt incx)66 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASasum(ViennaCLBackend /*backend*/, ViennaCLInt n,
67                                                             float *alpha,
68                                                             float *x, ViennaCLInt offx, ViennaCLInt incx)
69 {
70   viennacl::vector_base<float> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
71 
72   *alpha = viennacl::linalg::norm_1(v1);
73   return ViennaCLSuccess;
74 }
75 
ViennaCLCUDADasum(ViennaCLBackend,ViennaCLInt n,double * alpha,double * x,ViennaCLInt offx,ViennaCLInt incx)76 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADasum(ViennaCLBackend /*backend*/, ViennaCLInt n,
77                                                             double *alpha,
78                                                             double *x, ViennaCLInt offx, ViennaCLInt incx)
79 {
80   viennacl::vector_base<double> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
81 
82   *alpha = viennacl::linalg::norm_1(v1);
83   return ViennaCLSuccess;
84 }
85 
86 
87 // xAXPY
88 
ViennaCLCUDASaxpy(ViennaCLBackend,ViennaCLInt n,float alpha,float * x,ViennaCLInt offx,ViennaCLInt incx,float * y,ViennaCLInt offy,ViennaCLInt incy)89 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASaxpy(ViennaCLBackend /*backend*/, ViennaCLInt n,
90                                                             float alpha,
91                                                             float *x, ViennaCLInt offx, ViennaCLInt incx,
92                                                             float *y, ViennaCLInt offy, ViennaCLInt incy)
93 {
94   viennacl::vector_base<float> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
95   viennacl::vector_base<float> v2(y, viennacl::CUDA_MEMORY, n, offy, incy);
96 
97   v2 += alpha * v1;
98   return ViennaCLSuccess;
99 }
100 
ViennaCLCUDADaxpy(ViennaCLBackend,ViennaCLInt n,double alpha,double * x,ViennaCLInt offx,ViennaCLInt incx,double * y,ViennaCLInt offy,ViennaCLInt incy)101 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADaxpy(ViennaCLBackend /*backend*/, ViennaCLInt n,
102                                                             double alpha,
103                                                             double *x, ViennaCLInt offx, ViennaCLInt incx,
104                                                             double *y, ViennaCLInt offy, ViennaCLInt incy)
105 {
106   viennacl::vector_base<double> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
107   viennacl::vector_base<double> v2(y, viennacl::CUDA_MEMORY, n, offy, incy);
108 
109   v2 += alpha * v1;
110   return ViennaCLSuccess;
111 }
112 
113 
114 // xCOPY
115 
ViennaCLCUDAScopy(ViennaCLBackend,ViennaCLInt n,float * x,ViennaCLInt offx,ViennaCLInt incx,float * y,ViennaCLInt offy,ViennaCLInt incy)116 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDAScopy(ViennaCLBackend /*backend*/, ViennaCLInt n,
117                                                             float *x, ViennaCLInt offx, ViennaCLInt incx,
118                                                             float *y, ViennaCLInt offy, ViennaCLInt incy)
119 {
120   viennacl::vector_base<float> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
121   viennacl::vector_base<float> v2(y, viennacl::CUDA_MEMORY, n, offy, incy);
122 
123   v2 = v1;
124   return ViennaCLSuccess;
125 }
126 
ViennaCLCUDADcopy(ViennaCLBackend,ViennaCLInt n,double * x,ViennaCLInt offx,ViennaCLInt incx,double * y,ViennaCLInt offy,ViennaCLInt incy)127 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADcopy(ViennaCLBackend /*backend*/, ViennaCLInt n,
128                                                             double *x, ViennaCLInt offx, ViennaCLInt incx,
129                                                             double *y, ViennaCLInt offy, ViennaCLInt incy)
130 {
131   viennacl::vector_base<double> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
132   viennacl::vector_base<double> v2(y, viennacl::CUDA_MEMORY, n, offy, incy);
133 
134   v2 = v1;
135   return ViennaCLSuccess;
136 }
137 
138 // xDOT
139 
ViennaCLCUDASdot(ViennaCLBackend,ViennaCLInt n,float * alpha,float * x,ViennaCLInt offx,ViennaCLInt incx,float * y,ViennaCLInt offy,ViennaCLInt incy)140 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASdot(ViennaCLBackend /*backend*/, ViennaCLInt n,
141                                                            float *alpha,
142                                                            float *x, ViennaCLInt offx, ViennaCLInt incx,
143                                                            float *y, ViennaCLInt offy, ViennaCLInt incy)
144 {
145   viennacl::vector_base<float> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
146   viennacl::vector_base<float> v2(y, viennacl::CUDA_MEMORY, n, offy, incy);
147 
148   *alpha = viennacl::linalg::inner_prod(v1, v2);
149   return ViennaCLSuccess;
150 }
151 
ViennaCLCUDADdot(ViennaCLBackend,ViennaCLInt n,double * alpha,double * x,ViennaCLInt offx,ViennaCLInt incx,double * y,ViennaCLInt offy,ViennaCLInt incy)152 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADdot(ViennaCLBackend /*backend*/, ViennaCLInt n,
153                                                            double *alpha,
154                                                            double *x, ViennaCLInt offx, ViennaCLInt incx,
155                                                            double *y, ViennaCLInt offy, ViennaCLInt incy)
156 {
157   viennacl::vector_base<double> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
158   viennacl::vector_base<double> v2(y, viennacl::CUDA_MEMORY, n, offy, incy);
159 
160   *alpha = viennacl::linalg::inner_prod(v1, v2);
161   return ViennaCLSuccess;
162 }
163 
164 // xNRM2
165 
ViennaCLCUDASnrm2(ViennaCLBackend,ViennaCLInt n,float * alpha,float * x,ViennaCLInt offx,ViennaCLInt incx)166 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASnrm2(ViennaCLBackend /*backend*/, ViennaCLInt n,
167                                                             float *alpha,
168                                                             float *x, ViennaCLInt offx, ViennaCLInt incx)
169 {
170   viennacl::vector_base<float> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
171 
172   *alpha = viennacl::linalg::norm_2(v1);
173   return ViennaCLSuccess;
174 }
175 
ViennaCLCUDADnrm2(ViennaCLBackend,ViennaCLInt n,double * alpha,double * x,ViennaCLInt offx,ViennaCLInt incx)176 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADnrm2(ViennaCLBackend /*backend*/, ViennaCLInt n,
177                                                             double *alpha,
178                                                             double *x, ViennaCLInt offx, ViennaCLInt incx)
179 {
180   viennacl::vector_base<double> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
181 
182   *alpha = viennacl::linalg::norm_2(v1);
183   return ViennaCLSuccess;
184 }
185 
186 
187 
188 // xROT
189 
ViennaCLCUDASrot(ViennaCLBackend,ViennaCLInt n,float * x,ViennaCLInt offx,ViennaCLInt incx,float * y,ViennaCLInt offy,ViennaCLInt incy,float c,float s)190 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASrot(ViennaCLBackend /*backend*/, ViennaCLInt n,
191                                                            float *x, ViennaCLInt offx, ViennaCLInt incx,
192                                                            float *y, ViennaCLInt offy, ViennaCLInt incy,
193                                                            float c, float s)
194 {
195   viennacl::vector_base<float> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
196   viennacl::vector_base<float> v2(y, viennacl::CUDA_MEMORY, n, offy, incy);
197 
198   viennacl::linalg::plane_rotation(v1, v2, c, s);
199   return ViennaCLSuccess;
200 }
201 
ViennaCLCUDADrot(ViennaCLBackend,ViennaCLInt n,double * x,ViennaCLInt offx,ViennaCLInt incx,double * y,ViennaCLInt offy,ViennaCLInt incy,double c,double s)202 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADrot(ViennaCLBackend /*backend*/, ViennaCLInt n,
203                                                            double *x, ViennaCLInt offx, ViennaCLInt incx,
204                                                            double *y, ViennaCLInt offy, ViennaCLInt incy,
205                                                            double c, double s)
206 {
207   viennacl::vector_base<double> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
208   viennacl::vector_base<double> v2(y, viennacl::CUDA_MEMORY, n, offy, incy);
209 
210   viennacl::linalg::plane_rotation(v1, v2, c, s);
211   return ViennaCLSuccess;
212 }
213 
214 
215 
216 // xSCAL
217 
ViennaCLCUDASscal(ViennaCLBackend,ViennaCLInt n,float alpha,float * x,ViennaCLInt offx,ViennaCLInt incx)218 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASscal(ViennaCLBackend /*backend*/, ViennaCLInt n,
219                                                             float alpha,
220                                                             float *x, ViennaCLInt offx, ViennaCLInt incx)
221 {
222   viennacl::vector_base<float> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
223 
224   v1 *= alpha;
225   return ViennaCLSuccess;
226 }
227 
ViennaCLCUDADscal(ViennaCLBackend,ViennaCLInt n,double alpha,double * x,ViennaCLInt offx,ViennaCLInt incx)228 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADscal(ViennaCLBackend /*backend*/, ViennaCLInt n,
229                                                             double alpha,
230                                                             double *x, ViennaCLInt offx, ViennaCLInt incx)
231 {
232   viennacl::vector_base<double> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
233 
234   v1 *= alpha;
235   return ViennaCLSuccess;
236 }
237 
238 
239 // xSWAP
240 
ViennaCLCUDASswap(ViennaCLBackend,ViennaCLInt n,float * x,ViennaCLInt offx,ViennaCLInt incx,float * y,ViennaCLInt offy,ViennaCLInt incy)241 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASswap(ViennaCLBackend /*backend*/, ViennaCLInt n,
242                                                             float *x, ViennaCLInt offx, ViennaCLInt incx,
243                                                             float *y, ViennaCLInt offy, ViennaCLInt incy)
244 {
245   viennacl::vector_base<float> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
246   viennacl::vector_base<float> v2(y, viennacl::CUDA_MEMORY, n, offy, incy);
247 
248   viennacl::swap(v1, v2);
249   return ViennaCLSuccess;
250 }
251 
ViennaCLCUDADswap(ViennaCLBackend,ViennaCLInt n,double * x,ViennaCLInt offx,ViennaCLInt incx,double * y,ViennaCLInt offy,ViennaCLInt incy)252 VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADswap(ViennaCLBackend /*backend*/, ViennaCLInt n,
253                                                             double *x, ViennaCLInt offx, ViennaCLInt incx,
254                                                             double *y, ViennaCLInt offy, ViennaCLInt incy)
255 {
256   viennacl::vector_base<double> v1(x, viennacl::CUDA_MEMORY, n, offx, incx);
257   viennacl::vector_base<double> v2(y, viennacl::CUDA_MEMORY, n, offy, incy);
258 
259   viennacl::swap(v1, v2);
260   return ViennaCLSuccess;
261 }
262 #endif
263 
264 
265