vSMC
vSMC: Scalable Monte Carlo
cl_buffer.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/opencl/cl_buffer.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013,2014, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_OPENCL_CL_BUFFER_HPP
33 #define VSMC_OPENCL_CL_BUFFER_HPP
34 
35 #include <vsmc/internal/common.hpp>
38 
39 namespace vsmc {
40 
48 template <typename T, typename ID = CLDefault>
49 class CLBuffer
50 {
51  public :
52 
53  typedef T value_type;
54  typedef std::size_t size_type;
56 
57  CLBuffer () :
58  size_(0), flag_(CL_MEM_READ_WRITE), host_ptr_(VSMC_NULLPTR) {}
59 
60  CLBuffer (size_type N, ::cl_mem_flags flag = CL_MEM_READ_WRITE,
61  void *host_ptr = VSMC_NULLPTR) :
62  size_(N), flag_(flag), host_ptr_(host_ptr),
63  data_(manager().template
64  create_buffer<value_type>(size_, flag_, host_ptr_)) {}
65 
66  CLBuffer (const CLBuffer<T, ID> &other) :
67  size_(other.size_), flag_(other.flag_), host_ptr_(other.host_ptr_),
68  data_(manager().template
69  create_buffer<value_type>(size_, flag_, host_ptr_))
70  {
71  if (size_ != 0
72 #if VSMC_OPENCL_VERSION >= 120
73  && (flag_ & CL_MEM_HOST_WRITE_ONLY) != 0
74  && (flag_ & CL_MEM_HOST_READ_ONLY) != 0
75 #endif
76  ) {
77  manager().template copy_buffer<value_type>(
78  other.data_, data_, size_);
79  }
80  }
81 
83  {
84  if (this != &other) {
85  resize(other.size_, other.flag_, other.host_ptr_);
86  if (size_ != 0
87 #if VSMC_OPENCL_VERSION >= 120
88  && (flag_ & CL_MEM_HOST_WRITE_ONLY) != 0
89  && (flag_ & CL_MEM_HOST_READ_ONLY) != 0
90 #endif
91  ) {
92  manager().template copy_buffer<value_type>(
93  other.data_, data_, size_);
94  }
95  }
96 
97  return *this;
98  }
99 
100 #if VSMC_HAS_CXX11_RVALUE_REFERENCES
102  size_(other.size_), flag_(other.flag_), host_ptr_(other.host_ptr_),
103  data_(cxx11::move(other.data_))
104  {
105  other.size_ = 0;
106  other.flag_ = CL_MEM_READ_WRITE;
107  other.host_ptr_ = VSMC_NULLPTR;
108  other.data_ = ::cl::Buffer();
109  }
110 
112  {
113  using std::swap;
114 
115  if (this != &other) {
116  swap(size_, other.size_);
117  swap(flag_, other.flag_);
118  swap(host_ptr_, other.host_ptr_);
119  swap(data_, other.data_);
120  }
121 
122  return *this;
123  }
124 #endif
125 
126  ~CLBuffer () {}
127 
128  static manager_type &manager () {return manager_type::instance();}
129 
130  size_type size () const {return size_;}
131 
132  ::cl_mem_flags flag () const {return flag_;}
133 
134  void *host_ptr () const {return host_ptr_;}
135 
141  const ::cl::Buffer &data () const {return data_;}
142 
143  void resize (size_type N)
144  {
145  if (N == size_)
146  return;
147 
148  size_ = N;
149  data_ = manager().template create_buffer<value_type>(
150  size_, flag_, host_ptr_);
151  }
152 
153  void resize (size_type N, ::cl_mem_flags flag)
154  {
155  if (N == size_ && flag == flag_)
156  return;
157 
158  size_ = N;
159  flag_ = flag;
160  data_ = manager().template create_buffer<value_type>(
161  size_, flag_, host_ptr_);
162  }
163 
164  void resize (size_type N, ::cl_mem_flags flag, void *host_ptr)
165  {
166  if (N == size_ && flag == flag_ && host_ptr == host_ptr_)
167  return;
168 
169  size_ = N;
170  flag_ = flag;
171  host_ptr_ = host_ptr;
172  data_ = manager().template create_buffer<value_type>(
173  size_, flag_, host_ptr_);
174  }
175 
176  private :
177 
178  size_type size_;
179  ::cl_mem_flags flag_;
180  void *host_ptr_;
181  ::cl::Buffer data_;
182 }; // class CLBuffer
183 
184 } // namespace vsmc
185 
186 #endif // VSMC_OPENCL_CL_BUFFER_HPP
static manager_type & manager()
Definition: cl_buffer.hpp:128
std::size_t size_type
Definition: cl_buffer.hpp:54
Definition: adapter.hpp:37
CLBuffer(size_type N,::cl_mem_flags flag=CL_MEM_READ_WRITE, void *host_ptr=nullptr)
Definition: cl_buffer.hpp:60
::cl_mem_flags flag() const
Definition: cl_buffer.hpp:132
const ::cl::Buffer & data() const
Read only access to the raw cl::Buffer object.
Definition: cl_buffer.hpp:141
void resize(size_type N)
Definition: cl_buffer.hpp:143
OpenCL buffer.
Definition: cl_buffer.hpp:49
void * host_ptr() const
Definition: cl_buffer.hpp:134
remove_reference< T >::type && move(T &&t) noexcept
#define VSMC_NULLPTR
nullptr
Definition: defines.hpp:79
CLBuffer< T, ID > & operator=(const CLBuffer< T, ID > &other)
Definition: cl_buffer.hpp:82
CLBuffer(CLBuffer< T, ID > &&other)
Definition: cl_buffer.hpp:101
void swap(Array< T, N > &ary1, Array< T, N > &ary2)
Array ADL of swap.
Definition: array.hpp:317
#define VSMC_OPENCL_VERSION
Definition: cl_wrapper.hpp:54
CLManager< ID > manager_type
Definition: cl_buffer.hpp:55
CLBuffer(const CLBuffer< T, ID > &other)
Definition: cl_buffer.hpp:66
size_type size() const
Definition: cl_buffer.hpp:130
OpenCL Manager.
Definition: cl_manager.hpp:129
void resize(size_type N,::cl_mem_flags flag, void *host_ptr)
Definition: cl_buffer.hpp:164
static CLManager< ID > & instance()
Get an instance of the manager singleton.
Definition: cl_manager.hpp:136
void resize(size_type N,::cl_mem_flags flag)
Definition: cl_buffer.hpp:153