ViennaCL - The Vienna Computing Library  1.5.1
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-2014, 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 PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
26 
27 #define VIENNACL_CUDA_LAST_ERROR_CHECK(message) detail::cuda_last_error_check (message, __FILE__, __LINE__)
28 
29 namespace viennacl
30 {
31  namespace linalg
32  {
33  namespace cuda
34  {
35  namespace detail
36  {
37  inline unsigned int make_options(vcl_size_t length, bool reciprocal, bool flip_sign)
38  {
39  return static_cast<unsigned int>( ((length > 1) ? (static_cast<unsigned int>(length) << 2) : 0) + (reciprocal ? 2 : 0) + (flip_sign ? 1 : 0) );
40  }
41 
42  inline void cuda_last_error_check(const char * message, const char * file, const int line )
43  {
44  cudaError_t error_code = cudaGetLastError();
45 
46  if(cudaSuccess != error_code)
47  {
48  std::cerr << file << "(" << line << "): " << ": getLastCudaError() CUDA error " << error_code << ": " << cudaGetErrorString( error_code ) << " @ " << message << std::endl;
49  throw "CUDA error";
50  }
51  }
52 
53  template <typename T, typename U>
55  {
56  return reinterpret_cast<T *>(viennacl::traits::handle(obj).cuda_handle().get());
57  }
58 
59  template <typename T, typename U>
60  const T * cuda_arg(vector_base<U> const & obj)
61  {
62  return reinterpret_cast<const T *>(viennacl::traits::handle(obj).cuda_handle().get());
63  }
64 
65  template <typename NumericT, typename F>
67  {
68  return reinterpret_cast<NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
69  }
70 
71  template <typename NumericT, typename F>
72  const NumericT * cuda_arg(matrix_base<NumericT, F> const & obj)
73  {
74  return reinterpret_cast<const NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
75  }
76 
77 
78  template <typename ScalarType, typename T>
80  ScalarType *>::type
81  cuda_arg(T & obj)
82  {
83  return reinterpret_cast<ScalarType *>(viennacl::traits::handle(obj).cuda_handle().get());
84  }
85 
86  template <typename ScalarType, typename T>
88  const ScalarType *>::type
89  cuda_arg(T const & obj)
90  {
91  return reinterpret_cast<const ScalarType *>(viennacl::traits::handle(obj).cuda_handle().get());
92  }
93 
94  template <typename ScalarType>
96  {
97  return reinterpret_cast<ScalarType *>(h.get());
98  }
99 
100  template <typename ScalarType>
102  {
103  return reinterpret_cast<const ScalarType *>(h.get());
104  }
105 
106  //template <typename ScalarType>
107  //ScalarType cuda_arg(ScalarType const & val) { return val; }
108 
109  inline unsigned int cuda_arg(unsigned int val) { return val; }
110 
111  template <typename T> char cuda_arg(char val) { return val; }
112  template <typename T> unsigned char cuda_arg(unsigned char val) { return val; }
113 
114  template <typename T> short cuda_arg(short val) { return val; }
115  template <typename T> unsigned short cuda_arg(unsigned short val) { return val; }
116 
117  template <typename T> int cuda_arg(int val) { return val; }
118  template <typename T> unsigned int cuda_arg(unsigned int val) { return val; }
119 
120  template <typename T> long cuda_arg(long val) { return val; }
121  template <typename T> unsigned long cuda_arg(unsigned long val) { return val; }
122 
123  template <typename T> float cuda_arg(float val) { return val; }
124  template <typename T> double cuda_arg(double val) { return val; }
125 
126  template <typename T, typename U>
128 
129  template <typename T, typename U>
130  typename viennacl::backend::mem_handle::cuda_handle_type const & arg_reference(viennacl::scalar<T> const & s, U) { return s.handle().cuda_handle(); }
131 
132  // all other cases where T is not a ViennaCL scalar
133  template <typename T>
135  char const &>::type
136  arg_reference(T, char const & val) { return val; }
137 
138  template <typename T>
140  unsigned char const &>::type
141  arg_reference(T, unsigned char const & val) { return val; }
142 
143  template <typename T>
145  short const &>::type
146  arg_reference(T, short const & val) { return val; }
147 
148  template <typename T>
150  unsigned short const &>::type
151  arg_reference(T, unsigned short const & val) { return val; }
152 
153  template <typename T>
155  int const &>::type
156  arg_reference(T, int const & val) { return val; }
157 
158  template <typename T>
160  unsigned int const &>::type
161  arg_reference(T, unsigned int const & val) { return val; }
162 
163  template <typename T>
165  long const &>::type
166  arg_reference(T, long const & val) { return val; }
167 
168  template <typename T>
170  unsigned long const &>::type
171  arg_reference(T, unsigned long const & val) { return val; }
172 
173  template <typename T>
175  float const &>::type
176  arg_reference(T, float const & val) { return val; }
177 
178  template <typename T>
180  double const &>::type
181  arg_reference(T, double const & val) { return val; }
182  } //namespace detail
183 
184  } //namespace cuda
185  } //namespace linalg
186 } //namespace viennacl
187 
188 
189 #endif
Simple enable-if variant that uses the SFINAE pattern.
Definition: enable_if.hpp:29
std::size_t vcl_size_t
Definition: forwards.h:58
unsigned int make_options(vcl_size_t length, bool reciprocal, bool flip_sign)
Definition: common.hpp:37
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:172
viennacl::backend::mem_handle::cuda_handle_type & arg_reference(viennacl::scalar< T > &s, U)
Definition: common.hpp:127
T * get() const
Definition: shared_ptr.hpp:134
A dense matrix class.
Definition: forwards.h:290
void cuda_last_error_check(const char *message, const char *file, const int line)
Definition: common.hpp:42
handle_type & handle()
Returns the memory handle, non-const version.
Definition: scalar.hpp:704
NumericT flip_sign(NumericT val)
Definition: vector_operations.hpp:54
Common base class for dense vectors, vector ranges, and vector slices.
Definition: forwards.h:205
T * cuda_arg(vector_base< U > &obj)
Definition: common.hpp:54
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