ViennaCL - The Vienna Computing Library  1.6.2
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-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 
38 inline unsigned int make_options(vcl_size_t length, bool reciprocal, bool flip_sign)
39 {
40  return static_cast<unsigned int>( ((length > 1) ? (static_cast<unsigned int>(length) << 2) : 0) + (reciprocal ? 2 : 0) + (flip_sign ? 1 : 0) );
41 }
42 
43 inline void cuda_last_error_check(const char * message, const char * file, const int line )
44 {
45  cudaError_t error_code = cudaGetLastError();
46 
47  if (cudaSuccess != error_code)
48  {
49  std::cerr << file << "(" << line << "): " << ": getLastCudaError() CUDA error " << error_code << ": " << cudaGetErrorString( error_code ) << " @ " << message << std::endl;
50  throw "CUDA error";
51  }
52 }
53 
54 template<typename ReturnT, typename NumericT>
56 {
57  return reinterpret_cast<ReturnT *>(viennacl::traits::handle(obj).cuda_handle().get());
58 }
59 
60 template<typename ReturnT, typename NumericT>
61 const ReturnT * cuda_arg(vector_base<NumericT> const & obj)
62 {
63  return reinterpret_cast<const ReturnT *>(viennacl::traits::handle(obj).cuda_handle().get());
64 }
65 
66 template<typename NumericT>
68 {
69  return reinterpret_cast<NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
70 }
71 
72 template<typename NumericT>
73 const NumericT * cuda_arg(matrix_base<NumericT> const & obj)
74 {
75  return reinterpret_cast<const NumericT *>(viennacl::traits::handle(obj).cuda_handle().get());
76 }
77 
78 
79 template<typename ReturnT, typename ArgT>
81  ReturnT *>::type
82 cuda_arg(ArgT & obj)
83 {
84  return reinterpret_cast<ReturnT *>(viennacl::traits::handle(obj).cuda_handle().get());
85 }
86 
87 template<typename ReturnT, typename ArgT>
89  const ReturnT *>::type
90 cuda_arg(ArgT const & obj)
91 {
92  return reinterpret_cast<const ReturnT *>(viennacl::traits::handle(obj).cuda_handle().get());
93 }
94 
95 template<typename ReturnT>
97 {
98  return reinterpret_cast<ReturnT *>(h.get());
99 }
100 
101 template<typename ReturnT>
103 {
104  return reinterpret_cast<const ReturnT *>(h.get());
105 }
106 
107 template<typename NumericT>
109 
110 template<>
111 struct type_to_type2<float> { typedef float2 type; };
112 
113 template<>
114 struct type_to_type2<double> { typedef double2 type; };
115 
116 //template<typename ScalarType>
117 //ScalarType cuda_arg(ScalarType const & val) { return val; }
118 
119 inline unsigned int cuda_arg(unsigned int val) { return val; }
120 
121 template<typename NumericT> char cuda_arg(char val) { return val; }
122 template<typename NumericT> unsigned char cuda_arg(unsigned char val) { return val; }
123 
124 template<typename NumericT> short cuda_arg(short val) { return val; }
125 template<typename NumericT> unsigned short cuda_arg(unsigned short val) { return val; }
126 
127 template<typename NumericT> int cuda_arg(int val) { return val; }
128 template<typename NumericT> unsigned int cuda_arg(unsigned int val) { return val; }
129 
130 template<typename NumericT> long cuda_arg(long val) { return val; }
131 template<typename NumericT> unsigned long cuda_arg(unsigned long val) { return val; }
132 
133 template<typename NumericT> float cuda_arg(float val) { return val; }
134 template<typename NumericT> double cuda_arg(double val) { return val; }
135 
136 template<typename NumericT, typename OtherT>
138 
139 template<typename NumericT, typename OtherT>
140 typename viennacl::backend::mem_handle::cuda_handle_type const & arg_reference(viennacl::scalar<NumericT> const & s, OtherT) { return s.handle().cuda_handle(); }
141 
142 // all other cases where T is not a ViennaCL scalar
143 template<typename ArgT>
145  char const &>::type
146 arg_reference(ArgT, char const & val) { return val; }
147 
148 template<typename ArgT>
150  unsigned char const &>::type
151 arg_reference(ArgT, unsigned char const & val) { return val; }
152 
153 template<typename ArgT>
155  short const &>::type
156 arg_reference(ArgT, short const & val) { return val; }
157 
158 template<typename ArgT>
160  unsigned short const &>::type
161 arg_reference(ArgT, unsigned short const & val) { return val; }
162 
163 template<typename ArgT>
165  int const &>::type
166 arg_reference(ArgT, int const & val) { return val; }
167 
168 template<typename ArgT>
170  unsigned int const &>::type
171 arg_reference(ArgT, unsigned int const & val) { return val; }
172 
173 template<typename ArgT>
175  long const &>::type
176 arg_reference(ArgT, long const & val) { return val; }
177 
178 template<typename ArgT>
180  unsigned long const &>::type
181 arg_reference(ArgT, unsigned long const & val) { return val; }
182 
183 template<typename ArgT>
185  float const &>::type
186 arg_reference(ArgT, float const & val) { return val; }
187 
188 template<typename ArgT>
190  double const &>::type
191 arg_reference(ArgT, double const & val) { return val; }
192 
193 } //namespace detail
194 } //namespace cuda
195 } //namespace linalg
196 } //namespace viennacl
197 
198 
199 #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:38
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:226
handle_type & handle()
Returns the memory handle, non-const version.
Definition: scalar.hpp:792
void cuda_last_error_check(const char *message, const char *file, const int line)
Definition: common.hpp:43
A shared pointer class similar to boost::shared_ptr. Reimplemented in order to avoid a Boost-dependen...
Definition: shared_ptr.hpp:83
std::size_t vcl_size_t
Definition: forwards.h:74
ReturnT * cuda_arg(vector_base< NumericT > &obj)
Definition: common.hpp:55
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:137