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 
31 
32 namespace viennacl
33 {
34 namespace traits
35 {
36 //
37 // Generic memory handle
38 //
40 template<typename T>
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>
63 {
64  return handle(obj.lhs());
65 }
66 
67 template<typename LHS, typename RHS, typename OP>
69 
70 template<typename LHS, typename RHS, typename OP>
72 
73 template<typename LHS, typename RHS, typename OP>
75 {
76  return handle(obj.lhs());
77 }
78 
79 // proxy objects require extra care (at the moment)
80 template<typename T>
82 {
83  return obj.handle();
84 }
85 
86 template<typename T>
88 {
89  return obj.handle();
90 }
91 
92 
93 
94 template<typename T>
96 {
97  return obj.get().handle();
98 }
99 
100 template<typename T>
102 {
103  return obj.get().handle();
104 }
105 
106 
107 template<typename T>
109 {
110  return obj.get().handle();
111 }
112 
113 template<typename T>
115 {
116  return obj.get().handle();
117 }
118 
119 template<typename LHS, typename RHS, typename OP>
121 {
122  return handle(obj.lhs());
123 }
124 
125 template<typename LHS, typename RHS, typename OP>
127 {
128  return handle(obj.lhs());
129 }
130 
133 //
134 // RAM handle extraction
135 //
137 template<typename T>
139 {
140  return viennacl::traits::handle(obj).ram_handle();
141 }
142 
144 template<typename T>
146 {
147  return viennacl::traits::handle(obj).ram_handle();
148 }
149 
152 {
153  return h.ram_handle();
154 }
155 
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>
219 {
220  return handle(obj).get_active_handle_id();
221 }
222 
224 template<typename T>
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>
238 
239 template<typename LHS, typename RHS, typename OP>
241 {
242  return active_handle_id(obj.lhs());
243 }
244 
245 template<typename LHS, typename RHS, typename OP>
247 {
248  return active_handle_id(obj.lhs());
249 }
250 
251 template<typename LHS, typename RHS, typename OP>
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
Class for representing strided submatrices of a bigger matrix A.
Definition: forwards.h:443
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
Expression template class for representing a tree of expressions which ultimately result in a matrix...
Definition: forwards.h:341
This file provides the forward declarations for the main types used within ViennaCL.
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:230
lhs_reference_type lhs() const
Get left hand side operand.
Definition: vector.hpp:74
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:239
Implements the multi-memory-domain handle.
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
handle_type & handle()
Returns the OpenCL handle, non-const-version.
Definition: matrix_def.hpp:244
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
RHS & rhs() const
Get right hand side operand.
Definition: matrix.hpp:69
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:56
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
A Circulant matrix class.
Class for representing non-strided submatrices of a bigger matrix A.
Definition: forwards.h:440
viennacl::backend::mem_handle & handle(T &obj)
Returns the generic memory handle of an object. Non-const version.
Definition: handle.hpp:41
memory_types
Definition: forwards.h:345
LHS & lhs() const
Get left hand side operand.
Definition: matrix.hpp:66
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
const handle_type & handle() const
Returns the memory handle.
Definition: vector_def.hpp:128
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
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