ViennaCL - The Vienna Computing Library  1.5.1
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-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 
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
36  #include "viennacl/backend/cuda.hpp"
37 #endif
38 
39 
40 namespace viennacl
41 {
42  namespace backend
43  {
44 
45 
46 // if a user compiles with CUDA, it is reasonable to expect that CUDA should be the default
47 #ifdef VIENNACL_WITH_CUDA
48  inline memory_types default_memory_type() { return CUDA_MEMORY; }
49 #elif defined(VIENNACL_WITH_OPENCL)
51 #else
53 #endif
54 
55 
62  class mem_handle
63  {
64  public:
67 
69  mem_handle() : active_handle_(MEMORY_NOT_INITIALIZED), size_in_bytes_(0) {}
70 
72  ram_handle_type & ram_handle() { return ram_handle_; }
74  ram_handle_type const & ram_handle() const { return ram_handle_; }
75 
76 #ifdef VIENNACL_WITH_OPENCL
77 
78  viennacl::ocl::handle<cl_mem> & opencl_handle() { return opencl_handle_; }
80  viennacl::ocl::handle<cl_mem> const & opencl_handle() const { return opencl_handle_; }
81 #endif
82 
83 #ifdef VIENNACL_WITH_CUDA
84 
85  cuda_handle_type & cuda_handle() { return cuda_handle_; }
87  cuda_handle_type const & cuda_handle() const { return cuda_handle_; }
88 #endif
89 
91  memory_types get_active_handle_id() const { return active_handle_; }
92 
95  {
96  if (new_id != active_handle_)
97  {
98  if (active_handle_ == MEMORY_NOT_INITIALIZED)
99  active_handle_ = new_id;
100  else if (active_handle_ == MAIN_MEMORY)
101  {
102  active_handle_ = new_id;
103  }
104  else if (active_handle_ == OPENCL_MEMORY)
105  {
106 #ifdef VIENNACL_WITH_OPENCL
107  active_handle_ = new_id;
108 #else
109  throw "compiled without OpenCL suppport!";
110 #endif
111  }
112  else if (active_handle_ == CUDA_MEMORY)
113  {
114 #ifdef VIENNACL_WITH_CUDA
115  active_handle_ = new_id;
116 #else
117  throw "compiled without CUDA suppport!";
118 #endif
119  }
120  else
121  throw "invalid new memory region!";
122  }
123  }
124 
126  bool operator==(mem_handle const & other) const
127  {
128  if (active_handle_ != other.active_handle_)
129  return false;
130 
131  switch (active_handle_)
132  {
133  case MAIN_MEMORY:
134  return ram_handle_.get() == other.ram_handle_.get();
135 #ifdef VIENNACL_WITH_OPENCL
136  case OPENCL_MEMORY:
137  return opencl_handle_.get() == other.opencl_handle_.get();
138 #endif
139 #ifdef VIENNACL_WITH_CUDA
140  case CUDA_MEMORY:
141  return cuda_handle_.get() == other.cuda_handle_.get();
142 #endif
143  default: break;
144  }
145 
146  return false;
147  }
148 
152  bool operator<(mem_handle const & other) const
153  {
154  if (active_handle_ != other.active_handle_)
155  return false;
156 
157  switch (active_handle_)
158  {
159  case MAIN_MEMORY:
160  return ram_handle_.get() < other.ram_handle_.get();
161 #ifdef VIENNACL_WITH_OPENCL
162  case OPENCL_MEMORY:
163  return opencl_handle_.get() < other.opencl_handle_.get();
164 #endif
165 #ifdef VIENNACL_WITH_CUDA
166  case CUDA_MEMORY:
167  return cuda_handle_.get() < other.cuda_handle_.get();
168 #endif
169  default: break;
170  }
171 
172  return false;
173  }
174 
175 
176  bool operator!=(mem_handle const & other) const { return !(*this == other); }
177 
179  void swap(mem_handle & other)
180  {
181  // swap handle type:
182  memory_types active_handle_tmp = other.active_handle_;
183  other.active_handle_ = active_handle_;
184  active_handle_ = active_handle_tmp;
185 
186  // swap ram handle:
187  ram_handle_type ram_handle_tmp = other.ram_handle_;
188  other.ram_handle_ = ram_handle_;
189  ram_handle_ = ram_handle_tmp;
190 
191  // swap OpenCL handle:
192 #ifdef VIENNACL_WITH_OPENCL
193  opencl_handle_.swap(other.opencl_handle_);
194 #endif
195 #ifdef VIENNACL_WITH_CUDA
196  cuda_handle_type cuda_handle_tmp = other.cuda_handle_;
197  other.cuda_handle_ = cuda_handle_;
198  cuda_handle_ = cuda_handle_tmp;
199 #endif
200  }
201 
203  vcl_size_t raw_size() const { return size_in_bytes_; }
204 
206  void raw_size(vcl_size_t new_size) { size_in_bytes_ = new_size; }
207 
208  private:
209  memory_types active_handle_;
210  ram_handle_type ram_handle_;
211 #ifdef VIENNACL_WITH_OPENCL
212  viennacl::ocl::handle<cl_mem> opencl_handle_;
213 #endif
214 #ifdef VIENNACL_WITH_CUDA
215  cuda_handle_type cuda_handle_;
216 #endif
217  vcl_size_t size_in_bytes_;
218  };
219 
220 
221  } //backend
222 
223 
224 } //viennacl
225 #endif
std::size_t vcl_size_t
Definition: forwards.h:58
Definition: forwards.h:478
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:72
viennacl::tools::shared_ptr< char > cuda_handle_type
Definition: mem_handle.hpp:66
T * get() const
Definition: shared_ptr.hpp:134
void raw_size(vcl_size_t new_size)
Sets the size of the currently active buffer. Use with care!
Definition: mem_handle.hpp:206
This file provides the forward declarations for the main types used within ViennaCL.
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:126
Definition: forwards.h:481
memory_types
Definition: forwards.h:476
void swap(shared_ptr< T > &other)
Definition: shared_ptr.hpp:116
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:91
Implementation of a shared pointer class (cf. std::shared_ptr, boost::shared_ptr). Will be used until C++11 is widely available.
bool operator!=(mem_handle const &other) const
Definition: mem_handle.hpp:176
Definition: forwards.h:480
Implementations for the OpenCL backend functionality.
viennacl::tools::shared_ptr< char > ram_handle_type
Definition: mem_handle.hpp:65
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:152
Implementations for the CUDA backend functionality.
mem_handle()
Default CTOR. No memory is allocated.
Definition: mem_handle.hpp:69
memory_types default_memory_type()
Definition: mem_handle.hpp:52
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:74
vcl_size_t raw_size() const
Returns the number of bytes of the currently active buffer.
Definition: mem_handle.hpp:203
Definition: forwards.h:479
void swap(mem_handle &other)
Implements a fast swapping method. No data is copied, only the handles are exchanged.
Definition: mem_handle.hpp:179
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:62
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:94
Implementations for the OpenCL backend functionality.