ViennaCL - The Vienna Computing Library: viennacl/linalg/cuda/common.hpp 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
common.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_CUDA_COMMON_HPP_
2 #define VIENNACL_LINALG_CUDA_COMMON_HPP_
3
4 /* =========================================================================
5 Copyright (c) 2010-2016, Institute for Microelectronics,
6 Institute for Analysis and Scientific Computing,
7 TU Wien.
8 Portions of this software are copyright by UChicago Argonne, LLC.
9
10 -----------------
11 ViennaCL - The Vienna Computing Library
12 -----------------
13
14 Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15
16 (A list of authors and contributors can be found in the manual)
17
18 License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20
25 #include <sstream>
26 #include <cuda_runtime.h>
27 #include "viennacl/backend/cuda.hpp"
28 #include "viennacl/traits/handle.hpp"
29
30 #define VIENNACL_CUDA_LAST_ERROR_CHECK(message) detail::cuda_last_error_check (message, __FILE__, __LINE__)
31
32 namespace viennacl
33 {
34
36
38 template<typename NumericT>
39 NumericT * cuda_arg(scalar<NumericT> & obj)
40 {
41 return reinterpret_cast<NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
42 }
43
45 template<typename NumericT>
46 const NumericT * cuda_arg(scalar<NumericT> const & obj)
47 {
48 return reinterpret_cast<const NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
49 }
50
51
53
55 template<typename NumericT>
56 NumericT * cuda_arg(vector_base<NumericT> & obj)
57 {
58 return reinterpret_cast<NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
59 }
60
62 template<typename NumericT>
63 const NumericT * cuda_arg(vector_base<NumericT> const & obj)
64 {
65 return reinterpret_cast<const NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
66 }
67
69 template<typename ReturnT, typename NumericT>
70 ReturnT * cuda_arg(vector_base<NumericT> & obj)
71 {
72 return reinterpret_cast<ReturnT *>(viennacl::traits::handle(obj).cuda_handle().get());
73 }
74
76 template<typename ReturnT, typename NumericT>
77 const ReturnT * cuda_arg(vector_base<NumericT> const & obj)
78 {
79 return reinterpret_cast<const ReturnT *>(viennacl::traits::handle(obj).cuda_handle().get());
80 }
81
82
84
86 template<typename NumericT>
87 NumericT * cuda_arg(matrix_base<NumericT> & obj)
88 {
89 return reinterpret_cast<NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
90 }
91
93 template<typename NumericT>
94 const NumericT * cuda_arg(matrix_base<NumericT> const & obj)
95 {
96 return reinterpret_cast<const NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
97 }
98
99
100
102
103
105 template<typename ReturnT>
106 ReturnT * cuda_arg(viennacl::backend::mem_handle & h)
107 {
108 return reinterpret_cast<ReturnT *>(h.cuda_handle().get());
109 }
110
112 template<typename ReturnT>
113 ReturnT const * cuda_arg(viennacl::backend::mem_handle const & h)
114 {
115 return reinterpret_cast<const ReturnT *>(h.cuda_handle().get());
116 }
117
119 template<typename ReturnT>
120 ReturnT * cuda_arg(viennacl::backend::mem_handle::cuda_handle_type & h)
121 {
122 return reinterpret_cast<ReturnT *>(h.get());
123 }
124
125 template<typename ReturnT>
126 ReturnT const * cuda_arg(viennacl::backend::mem_handle::cuda_handle_type const & h)
127 {
128 return reinterpret_cast<const ReturnT *>(h.get());
129 }
130
131 inline unsigned int cuda_arg(unsigned int val) { return val; }
132
133 template<typename NumericT> char cuda_arg(char val) { return val; }
134 template<typename NumericT> unsigned char cuda_arg(unsigned char val) { return val; }
135
136 template<typename NumericT> short cuda_arg(short val) { return val; }
137 template<typename NumericT> unsigned short cuda_arg(unsigned short val) { return val; }
138
139 template<typename NumericT> int cuda_arg(int val) { return val; }
140 template<typename NumericT> unsigned int cuda_arg(unsigned int val) { return val; }
141
142 template<typename NumericT> long cuda_arg(long val) { return val; }
143 template<typename NumericT> unsigned long cuda_arg(unsigned long val) { return val; }
144
145 template<typename NumericT> float cuda_arg(float val) { return val; }
146 template<typename NumericT> double cuda_arg(double val) { return val; }
147
151 namespace linalg
152 {
153 namespace cuda
154 {
155
156
157 namespace detail
158 {
159
160 inline unsigned int make_options(vcl_size_t length, bool reciprocal, bool flip_sign)
161 {
162 return static_cast<unsigned int>( ((length > 1) ? (static_cast<unsigned int>(length) << 2) : 0) + (reciprocal ? 2 : 0) + (flip_sign ? 1 : 0) );
163 }
164
165 inline void cuda_last_error_check(const char * message, const char * file, const int line )
166 {
167 cudaError_t error_code = cudaGetLastError();
168
169 if (cudaSuccess != error_code)
170 {
171 std::stringstream ss;
172 ss << file << "(" << line << "): " << ": getLastCudaError() CUDA error " << error_code << ": " << cudaGetErrorString( error_code ) << " @ " << message << std::endl;
173 throw viennacl::backend::cuda::cuda_exception(ss.str(), error_code);
174 }
175 }
176
177 template<typename NumericT>
178 struct type_to_type2;
179
180 template<>
181 struct type_to_type2<float> { typedef float2 type; };
182
183 template<>
184 struct type_to_type2<double> { typedef double2 type; };
185
186
187 template<typename NumericT, typename OtherT>
188 typename viennacl::backend::mem_handle::cuda_handle_type & arg_reference(viennacl::scalar<NumericT> & s, OtherT) { return s.handle().cuda_handle(); }
189
190 template<typename NumericT, typename OtherT>
191 typename viennacl::backend::mem_handle::cuda_handle_type const & arg_reference(viennacl::scalar<NumericT> const & s, OtherT) { return s.handle().cuda_handle(); }
192
193 // all other cases where T is not a ViennaCL scalar
194 template<typename ArgT>
195 typename viennacl::enable_if< viennacl::is_cpu_scalar<ArgT>::value,
196 char const &>::type
197 arg_reference(ArgT, char const & val) { return val; }
198
199 template<typename ArgT>
200 typename viennacl::enable_if< viennacl::is_cpu_scalar<ArgT>::value,
201 unsigned char const &>::type
202 arg_reference(ArgT, unsigned char const & val) { return val; }
203
204 template<typename ArgT>
205 typename viennacl::enable_if< viennacl::is_cpu_scalar<ArgT>::value,
206 short const &>::type
207 arg_reference(ArgT, short const & val) { return val; }
208
209 template<typename ArgT>
210 typename viennacl::enable_if< viennacl::is_cpu_scalar<ArgT>::value,
211 unsigned short const &>::type
212 arg_reference(ArgT, unsigned short const & val) { return val; }
213
214 template<typename ArgT>
215 typename viennacl::enable_if< viennacl::is_cpu_scalar<ArgT>::value,
216 int const &>::type
217 arg_reference(ArgT, int const & val) { return val; }
218
219 template<typename ArgT>
220 typename viennacl::enable_if< viennacl::is_cpu_scalar<ArgT>::value,
221 unsigned int const &>::type
222 arg_reference(ArgT, unsigned int const & val) { return val; }
223
224 template<typename ArgT>
225 typename viennacl::enable_if< viennacl::is_cpu_scalar<ArgT>::value,
226 long const &>::type
227 arg_reference(ArgT, long const & val) { return val; }
228
229 template<typename ArgT>
230 typename viennacl::enable_if< viennacl::is_cpu_scalar<ArgT>::value,
231 unsigned long const &>::type
232 arg_reference(ArgT, unsigned long const & val) { return val; }
233
234 template<typename ArgT>
235 typename viennacl::enable_if< viennacl::is_cpu_scalar<ArgT>::value,
236 float const &>::type
237 arg_reference(ArgT, float const & val) { return val; }
238
239 template<typename ArgT>
240 typename viennacl::enable_if< viennacl::is_cpu_scalar<ArgT>::value,
241 double const &>::type
242 arg_reference(ArgT, double const & val) { return val; }
243
244 } //namespace detail
245 } //namespace cuda
246 } //namespace linalg
247 } //namespace viennacl
248
249
250 #endif
viennacl::enable_if
Simple enable-if variant that uses the SFINAE pattern.
Definition: enable_if.hpp:30
viennacl::linalg::cuda::detail::make_options
unsigned int make_options(vcl_size_t length, bool reciprocal, bool flip_sign)
Definition: common.hpp:160
viennacl::scalar
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
viennacl::linalg::cuda::detail::type_to_type2< float >::type
float2 type
Definition: common.hpp:181
viennacl::scalar::handle
handle_type & handle()
Returns the memory handle, non-const version.
Definition: scalar.hpp:833
viennacl::matrix_base< NumericT >
viennacl::linalg::cuda::detail::cuda_last_error_check
void cuda_last_error_check(const char *message, const char *file, const int line)
Definition: common.hpp:165
viennacl::linalg::cuda::detail::type_to_type2< double >::type
double2 type
Definition: common.hpp:184
NumericT
float NumericT
Definition: bisect.cpp:40
viennacl::linalg::host_based::detail::flip_sign
NumericT flip_sign(NumericT val)
Definition: vector_operations.hpp:57
viennacl::vector_base< NumericT >
viennacl::tools::shared_ptr< char >
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
cuda.hpp
Implementations for the CUDA backend functionality.
viennacl::tools::shared_ptr::get
T * get() const
Definition: shared_ptr.hpp:101
viennacl::backend::mem_handle
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
viennacl::backend::cuda::cuda_exception
Definition: cuda.hpp:59
viennacl::cuda_arg
NumericT * cuda_arg(scalar< NumericT > &obj)
Convenience helper function for extracting the CUDA handle from a ViennaCL scalar. Non-const version.
Definition: common.hpp:39
handle.hpp
Extracts the underlying OpenCL handle from a vector, a matrix, an expression etc. ...
viennacl::traits::handle
viennacl::backend::mem_handle & handle(T &obj)
Returns the generic memory handle of an object. Non-const version.
Definition: handle.hpp:41
viennacl::linalg::cuda::detail::type_to_type2
Definition: common.hpp:178
viennacl::linalg::cuda::detail::arg_reference
viennacl::backend::mem_handle::cuda_handle_type & arg_reference(viennacl::scalar< NumericT > &s, OtherT)
Definition: common.hpp:188