ViennaCL - The Vienna Computing Library: viennacl/traits/handle.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
handle.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_TRAITS_HANDLE_HPP_
2 #define VIENNACL_TRAITS_HANDLE_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 <string>
26 #include <fstream>
27 #include <sstream>
28 #include "viennacl/forwards.h"
29
30 #include "viennacl/backend/mem_handle.hpp"
31
32 namespace viennacl
33 {
34 namespace traits
35 {
36 //
37 // Generic memory handle
38 //
40 template<typename T>
41 viennacl::backend::mem_handle & handle(T & obj)
42 {
43 return obj.handle();
44 }
45
47 template<typename T>
48 viennacl::backend::mem_handle const & handle(T const & obj)
49 {
50 return obj.handle();
51 }
52
54 inline char handle(char val) { return val; } //for unification purposes when passing CPU-scalars to kernels
55 inline short handle(short val) { return val; } //for unification purposes when passing CPU-scalars to kernels
56 inline int handle(int val) { return val; } //for unification purposes when passing CPU-scalars to kernels
57 inline long handle(long val) { return val; } //for unification purposes when passing CPU-scalars to kernels
58 inline float handle(float val) { return val; } //for unification purposes when passing CPU-scalars to kernels
59 inline double handle(double val) { return val; } //for unification purposes when passing CPU-scalars to kernels
60
61 template<typename LHS, typename RHS, typename OP>
62 viennacl::backend::mem_handle & handle(viennacl::scalar_expression< const LHS, const RHS, OP> & obj)
63 {
64 return handle(obj.lhs());
65 }
66
67 template<typename LHS, typename RHS, typename OP>
68 viennacl::backend::mem_handle const & handle(viennacl::matrix_expression<LHS, RHS, OP> const & obj);
69
70 template<typename LHS, typename RHS, typename OP>
71 viennacl::backend::mem_handle const & handle(viennacl::vector_expression<LHS, RHS, OP> const & obj);
72
73 template<typename LHS, typename RHS, typename OP>
74 viennacl::backend::mem_handle const & handle(viennacl::scalar_expression< const LHS, const RHS, OP> const & obj)
75 {
76 return handle(obj.lhs());
77 }
78
79 // proxy objects require extra care (at the moment)
80 template<typename T>
81 viennacl::backend::mem_handle & handle(viennacl::vector_base<T> & obj)
82 {
83 return obj.handle();
84 }
85
86 template<typename T>
87 viennacl::backend::mem_handle const & handle(viennacl::vector_base<T> const & obj)
88 {
89 return obj.handle();
90 }
91
92
93
94 template<typename T>
95 viennacl::backend::mem_handle & handle(viennacl::matrix_range<T> & obj)
96 {
97 return obj.get().handle();
98 }
99
100 template<typename T>
101 viennacl::backend::mem_handle const & handle(viennacl::matrix_range<T> const & obj)
102 {
103 return obj.get().handle();
104 }
105
106
107 template<typename T>
108 viennacl::backend::mem_handle & handle(viennacl::matrix_slice<T> & obj)
109 {
110 return obj.get().handle();
111 }
112
113 template<typename T>
114 viennacl::backend::mem_handle const & handle(viennacl::matrix_slice<T> const & obj)
115 {
116 return obj.get().handle();
117 }
118
119 template<typename LHS, typename RHS, typename OP>
120 viennacl::backend::mem_handle const & handle(viennacl::vector_expression<LHS, RHS, OP> const & obj)
121 {
122 return handle(obj.lhs());
123 }
124
125 template<typename LHS, typename RHS, typename OP>
126 viennacl::backend::mem_handle const & handle(viennacl::matrix_expression<LHS, RHS, OP> const & obj)
127 {
128 return handle(obj.lhs());
129 }
130
133 //
134 // RAM handle extraction
135 //
137 template<typename T>
138 typename viennacl::backend::mem_handle::ram_handle_type & ram_handle(T & obj)
139 {
140 return viennacl::traits::handle(obj).ram_handle();
141 }
142
144 template<typename T>
145 typename viennacl::backend::mem_handle::ram_handle_type const & ram_handle(T const & obj)
146 {
147 return viennacl::traits::handle(obj).ram_handle();
148 }
149
151 inline viennacl::backend::mem_handle::ram_handle_type & ram_handle(viennacl::backend::mem_handle & h)
152 {
153 return h.ram_handle();
154 }
155
156 inline viennacl::backend::mem_handle::ram_handle_type const & ram_handle(viennacl::backend::mem_handle const & h)
157 {
158 return h.ram_handle();
159 }
162 //
163 // OpenCL handle extraction
164 //
165 #ifdef VIENNACL_WITH_OPENCL
166
167 template<typename T>
168 viennacl::ocl::handle<cl_mem> & opencl_handle(T & obj)
169 {
170 return viennacl::traits::handle(obj).opencl_handle();
171 }
172
174 template<typename T>
175 viennacl::ocl::handle<cl_mem> const & opencl_handle(T const & obj)
176 {
177 return viennacl::traits::handle(obj).opencl_handle();
178 }
179
180 inline cl_char opencl_handle(char val) { return val; } //for unification purposes when passing CPU-scalars to kernels
181 inline cl_short opencl_handle(short val) { return val; } //for unification purposes when passing CPU-scalars to kernels
182 inline cl_int opencl_handle(int val) { return val; } //for unification purposes when passing CPU-scalars to kernels
183 inline cl_long opencl_handle(long val) { return val; } //for unification purposes when passing CPU-scalars to kernels
184 inline cl_uchar opencl_handle(unsigned char val) { return val; } //for unification purposes when passing CPU-scalars to kernels
185 inline cl_ushort opencl_handle(unsigned short val) { return val; } //for unification purposes when passing CPU-scalars to kernels
186 inline cl_uint opencl_handle(unsigned int val) { return val; } //for unification purposes when passing CPU-scalars to kernels
187 inline cl_ulong opencl_handle(unsigned long val) { return val; } //for unification purposes when passing CPU-scalars to kernels
188 inline float opencl_handle(float val) { return val; } //for unification purposes when passing CPU-scalars to kernels
189 inline double opencl_handle(double val) { return val; } //for unification purposes when passing CPU-scalars to kernels
190
191
192 // for user-provided matrix-vector routines:
193 template<typename LHS, typename NumericT>
194 viennacl::ocl::handle<cl_mem> const & opencl_handle(viennacl::vector_expression<LHS, const vector_base<NumericT>, op_prod> const & obj)
195 {
196 return viennacl::traits::handle(obj.rhs()).opencl_handle();
197 }
198
199 template<typename T>
200 viennacl::ocl::context & opencl_context(T const & obj)
201 {
202 return const_cast<viennacl::ocl::context &>(opencl_handle(obj).context());
203 }
204 #endif
205
206 //
207 // OpenCL context extraction
208 //
209
210
211
212
213 //
214 // Active handle ID
215 //
217 template<typename T>
218 viennacl::memory_types active_handle_id(T const & obj)
219 {
220 return handle(obj).get_active_handle_id();
221 }
222
224 template<typename T>
225 viennacl::memory_types active_handle_id(circulant_matrix<T> const &) { return OPENCL_MEMORY; }
226
227 template<typename T>
228 viennacl::memory_types active_handle_id(hankel_matrix<T> const &) { return OPENCL_MEMORY; }
229
230 template<typename T>
231 viennacl::memory_types active_handle_id(toeplitz_matrix<T> const &) { return OPENCL_MEMORY; }
232
233 template<typename T>
234 viennacl::memory_types active_handle_id(vandermonde_matrix<T> const &) { return OPENCL_MEMORY; }
235
236 template<typename LHS, typename RHS, typename OP>
237 viennacl::memory_types active_handle_id(viennacl::vector_expression<LHS, RHS, OP> const &);
238
239 template<typename LHS, typename RHS, typename OP>
240 viennacl::memory_types active_handle_id(viennacl::scalar_expression<LHS, RHS, OP> const & obj)
241 {
242 return active_handle_id(obj.lhs());
243 }
244
245 template<typename LHS, typename RHS, typename OP>
246 viennacl::memory_types active_handle_id(viennacl::vector_expression<LHS, RHS, OP> const & obj)
247 {
248 return active_handle_id(obj.lhs());
249 }
250
251 template<typename LHS, typename RHS, typename OP>
252 viennacl::memory_types active_handle_id(viennacl::matrix_expression<LHS, RHS, OP> const & obj)
253 {
254 return active_handle_id(obj.lhs());
255 }
256
257 // for user-provided matrix-vector routines:
258 template<typename LHS, typename NumericT>
259 viennacl::memory_types active_handle_id(viennacl::vector_expression<LHS, const vector_base<NumericT>, op_prod> const & obj)
260 {
261 return active_handle_id(obj.rhs());
262 }
263
266 } //namespace traits
267 } //namespace viennacl
268
269
270 #endif
viennacl::matrix_slice
Class for representing strided submatrices of a bigger matrix A.
Definition: forwards.h:443
viennacl::ocl::context
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
viennacl::OPENCL_MEMORY
Definition: forwards.h:349
viennacl::matrix_expression
Expression template class for representing a tree of expressions which ultimately result in a matrix...
Definition: forwards.h:341
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::scalar_expression
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:230
viennacl::vector_expression::lhs
lhs_reference_type lhs() const
Get left hand side operand.
Definition: vector.hpp:74
viennacl::vector_expression
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:239
mem_handle.hpp
Implements the multi-memory-domain handle.
viennacl::vector_base
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
viennacl::tools::shared_ptr< char >
viennacl::matrix_base< MatrixType::cpu_value_type >::handle
handle_type & handle()
Returns the OpenCL handle, non-const-version.
Definition: matrix_def.hpp:244
viennacl::traits::active_handle_id
viennacl::memory_types active_handle_id(T const &obj)
Returns an ID for the currently active memory domain of an object.
Definition: handle.hpp:218
viennacl::matrix_expression::rhs
RHS & rhs() const
Get right hand side operand.
Definition: matrix.hpp:69
viennacl::scalar_expression::lhs
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:56
viennacl::backend::mem_handle
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
viennacl::circulant_matrix
A Circulant matrix class.
Definition: circulant_matrix.hpp:41
viennacl::matrix_range
Class for representing non-strided submatrices of a bigger matrix A.
Definition: forwards.h:440
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::memory_types
memory_types
Definition: forwards.h:345
viennacl::matrix_expression::lhs
LHS & lhs() const
Get left hand side operand.
Definition: matrix.hpp:66
viennacl::traits::ram_handle
viennacl::backend::mem_handle::ram_handle_type & ram_handle(T &obj)
Generic helper routine for extracting the RAM handle of a ViennaCL object. Non-const version...
Definition: handle.hpp:138
viennacl::vector_base::handle
const handle_type & handle() const
Returns the memory handle.
Definition: vector_def.hpp:128
viennacl::ocl::handle< cl_mem >
viennacl::backend::mem_handle::ram_handle
ram_handle_type & ram_handle()
Returns the handle to a buffer in CPU RAM. NULL is returned if no such buffer has been allocated...
Definition: mem_handle.hpp:99
viennacl::backend::mem_handle::get_active_handle_id
memory_types get_active_handle_id() const
Returns an ID for the currently active memory buffer. Other memory buffers might contain old or no da...
Definition: mem_handle.hpp:118