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
mem_handle.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_BACKEND_MEM_HANDLE_HPP
2 #define VIENNACL_BACKEND_MEM_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 <vector>
26 #include <cassert>
27 #include "viennacl/forwards.h"
30 
31 #ifdef VIENNACL_WITH_OPENCL
33 #endif
34 
35 #ifdef VIENNACL_WITH_CUDA
37 #endif
38 
39 
40 namespace viennacl
41 {
42 namespace backend
43 {
44 
45 namespace detail
46 {
52  {
53  // if a user compiles with CUDA, it is reasonable to expect that CUDA should be the default
54 #ifdef VIENNACL_WITH_CUDA
55  static memory_types mem_type = CUDA_MEMORY;
56 #elif defined(VIENNACL_WITH_OPENCL)
57  static memory_types mem_type = OPENCL_MEMORY;
58 #else
59  static memory_types mem_type = MAIN_MEMORY;
60 #endif
61 
62  if (new_mem_type)
63  mem_type = *new_mem_type;
64 
65  return mem_type;
66  }
67 }
68 
74 
80 inline memory_types default_memory_type(memory_types new_memory_type) { return detail::get_set_default_memory_type(&new_memory_type); }
81 
82 
90 {
91 public:
94 
96  mem_handle() : active_handle_(MEMORY_NOT_INITIALIZED), size_in_bytes_(0) {}
97 
99  ram_handle_type & ram_handle() { return ram_handle_; }
101  ram_handle_type const & ram_handle() const { return ram_handle_; }
102 
103 #ifdef VIENNACL_WITH_OPENCL
104 
105  viennacl::ocl::handle<cl_mem> & opencl_handle() { return opencl_handle_; }
107  viennacl::ocl::handle<cl_mem> const & opencl_handle() const { return opencl_handle_; }
108 #endif
109 
110 #ifdef VIENNACL_WITH_CUDA
111 
112  cuda_handle_type & cuda_handle() { return cuda_handle_; }
114  cuda_handle_type const & cuda_handle() const { return cuda_handle_; }
115 #endif
116 
118  memory_types get_active_handle_id() const { return active_handle_; }
119 
122  {
123  if (new_id != active_handle_)
124  {
125  if (active_handle_ == MEMORY_NOT_INITIALIZED)
126  active_handle_ = new_id;
127  else if (active_handle_ == MAIN_MEMORY)
128  {
129  active_handle_ = new_id;
130  }
131  else if (active_handle_ == OPENCL_MEMORY)
132  {
133 #ifdef VIENNACL_WITH_OPENCL
134  active_handle_ = new_id;
135 #else
136  throw memory_exception("compiled without OpenCL suppport!");
137 #endif
138  }
139  else if (active_handle_ == CUDA_MEMORY)
140  {
141 #ifdef VIENNACL_WITH_CUDA
142  active_handle_ = new_id;
143 #else
144  throw memory_exception("compiled without CUDA suppport!");
145 #endif
146  }
147  else
148  throw memory_exception("invalid new memory region!");
149  }
150  }
151 
153  bool operator==(mem_handle const & other) const
154  {
155  if (active_handle_ != other.active_handle_)
156  return false;
157 
158  switch (active_handle_)
159  {
160  case MAIN_MEMORY:
161  return ram_handle_.get() == other.ram_handle_.get();
162 #ifdef VIENNACL_WITH_OPENCL
163  case OPENCL_MEMORY:
164  return opencl_handle_.get() == other.opencl_handle_.get();
165 #endif
166 #ifdef VIENNACL_WITH_CUDA
167  case CUDA_MEMORY:
168  return cuda_handle_.get() == other.cuda_handle_.get();
169 #endif
170  default: break;
171  }
172 
173  return false;
174  }
175 
179  bool operator<(mem_handle const & other) const
180  {
181  if (active_handle_ != other.active_handle_)
182  return false;
183 
184  switch (active_handle_)
185  {
186  case MAIN_MEMORY:
187  return ram_handle_.get() < other.ram_handle_.get();
188 #ifdef VIENNACL_WITH_OPENCL
189  case OPENCL_MEMORY:
190  return opencl_handle_.get() < other.opencl_handle_.get();
191 #endif
192 #ifdef VIENNACL_WITH_CUDA
193  case CUDA_MEMORY:
194  return cuda_handle_.get() < other.cuda_handle_.get();
195 #endif
196  default: break;
197  }
198 
199  return false;
200  }
201 
202 
203  bool operator!=(mem_handle const & other) const { return !(*this == other); }
204 
206  void swap(mem_handle & other)
207  {
208  // swap handle type:
209  memory_types active_handle_tmp = other.active_handle_;
210  other.active_handle_ = active_handle_;
211  active_handle_ = active_handle_tmp;
212 
213  // swap ram handle:
214  ram_handle_type ram_handle_tmp = other.ram_handle_;
215  other.ram_handle_ = ram_handle_;
216  ram_handle_ = ram_handle_tmp;
217 
218  // swap OpenCL handle:
219 #ifdef VIENNACL_WITH_OPENCL
220  opencl_handle_.swap(other.opencl_handle_);
221 #endif
222 #ifdef VIENNACL_WITH_CUDA
223  cuda_handle_type cuda_handle_tmp = other.cuda_handle_;
224  other.cuda_handle_ = cuda_handle_;
225  cuda_handle_ = cuda_handle_tmp;
226 #endif
227  }
228 
230  vcl_size_t raw_size() const { return size_in_bytes_; }
231 
233  void raw_size(vcl_size_t new_size) { size_in_bytes_ = new_size; }
234 
235 private:
236  memory_types active_handle_;
237  ram_handle_type ram_handle_;
238 #ifdef VIENNACL_WITH_OPENCL
239  viennacl::ocl::handle<cl_mem> opencl_handle_;
240 #endif
241 #ifdef VIENNACL_WITH_CUDA
242  cuda_handle_type cuda_handle_;
243 #endif
244  vcl_size_t size_in_bytes_;
245 };
246 
247 
248 } //backend
249 } //viennacl
250 #endif
Exception class in case of memory errors.
Definition: forwards.h:572
bool operator<(mem_handle const &other) const
Compares the two handles and returns true if the active memory handles in the two mem_handles point a...
Definition: mem_handle.hpp:179
viennacl::tools::shared_ptr< char > ram_handle_type
Definition: mem_handle.hpp:92
ram_handle_type const & ram_handle() const
Returns the handle to a buffer in CPU RAM. NULL is returned if no such buffer has been allocated...
Definition: mem_handle.hpp:101
void swap(mem_handle &other)
Implements a fast swapping method. No data is copied, only the handles are exchanged.
Definition: mem_handle.hpp:206
This file provides the forward declarations for the main types used within ViennaCL.
mem_handle()
Default CTOR. No memory is allocated.
Definition: mem_handle.hpp:96
void swap(shared_ptr< T > &other)
Definition: shared_ptr.hpp:133
Implementation of a shared pointer class (cf. std::shared_ptr, boost::shared_ptr). Will be used until C++11 is widely available.
void raw_size(vcl_size_t new_size)
Sets the size of the currently active buffer. Use with care!
Definition: mem_handle.hpp:233
Implementations for the OpenCL backend functionality.
viennacl::tools::shared_ptr< char > cuda_handle_type
Definition: mem_handle.hpp:93
bool operator==(mem_handle const &other) const
Compares the two handles and returns true if the active memory handles in the two mem_handles point t...
Definition: mem_handle.hpp:153
std::size_t vcl_size_t
Definition: forwards.h:75
Implementations for the CUDA backend functionality.
memory_types default_memory_type()
Returns the default memory type for the given configuration.
Definition: mem_handle.hpp:73
void switch_active_handle_id(memory_types new_id)
Switches the currently active handle. If no support for that backend is provided, an exception is thr...
Definition: mem_handle.hpp:121
bool operator!=(mem_handle const &other) const
Definition: mem_handle.hpp:203
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
vcl_size_t raw_size() const
Returns the number of bytes of the currently active buffer.
Definition: mem_handle.hpp:230
Implementations for the OpenCL backend functionality.
memory_types
Definition: forwards.h:345
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_set_default_memory_type(memory_types *new_mem_type)
Singleton for managing the default memory type.
Definition: mem_handle.hpp:51
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