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>
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>
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>
57 {
58  return reinterpret_cast<NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
59 }
60 
62 template<typename NumericT>
64 {
65  return reinterpret_cast<const NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
66 }
67 
69 template<typename ReturnT, typename NumericT>
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>
88 {
89  return reinterpret_cast<NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
90 }
91 
93 template<typename NumericT>
95 {
96  return reinterpret_cast<const NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
97 }
98 
99 
100 
102 
103 
105 template<typename ReturnT>
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>
121 {
122  return reinterpret_cast<ReturnT *>(h.get());
123 }
124 
125 template<typename ReturnT>
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>
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>
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>
196  char const &>::type
197 arg_reference(ArgT, char const & val) { return val; }
198 
199 template<typename ArgT>
201  unsigned char const &>::type
202 arg_reference(ArgT, unsigned char const & val) { return val; }
203 
204 template<typename ArgT>
206  short const &>::type
207 arg_reference(ArgT, short const & val) { return val; }
208 
209 template<typename ArgT>
211  unsigned short const &>::type
212 arg_reference(ArgT, unsigned short const & val) { return val; }
213 
214 template<typename ArgT>
216  int const &>::type
217 arg_reference(ArgT, int const & val) { return val; }
218 
219 template<typename ArgT>
221  unsigned int const &>::type
222 arg_reference(ArgT, unsigned int const & val) { return val; }
223 
224 template<typename ArgT>
226  long const &>::type
227 arg_reference(ArgT, long const & val) { return val; }
228 
229 template<typename ArgT>
231  unsigned long const &>::type
232 arg_reference(ArgT, unsigned long const & val) { return val; }
233 
234 template<typename ArgT>
236  float const &>::type
237 arg_reference(ArgT, float const & val) { return val; }
238 
239 template<typename ArgT>
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
Simple enable-if variant that uses the SFINAE pattern.
Definition: enable_if.hpp:30
unsigned int make_options(vcl_size_t length, bool reciprocal, bool flip_sign)
Definition: common.hpp:160
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
handle_type & handle()
Returns the memory handle, non-const version.
Definition: scalar.hpp:833
void cuda_last_error_check(const char *message, const char *file, const int line)
Definition: common.hpp:165
float NumericT
Definition: bisect.cpp:40
std::size_t vcl_size_t
Definition: forwards.h:75
Implementations for the CUDA backend functionality.
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
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
Extracts the underlying OpenCL handle from a vector, a matrix, an expression etc. ...
viennacl::backend::mem_handle & handle(T &obj)
Returns the generic memory handle of an object. Non-const version.
Definition: handle.hpp:41
viennacl::backend::mem_handle::cuda_handle_type & arg_reference(viennacl::scalar< NumericT > &s, OtherT)
Definition: common.hpp:188