VTK-m  2.2
Buffer.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_vtkm_cont_internal_Buffer_h
11 #define vtk_m_vtkm_cont_internal_Buffer_h
12 
14 
16 #include <vtkm/cont/Logging.h>
18 #include <vtkm/cont/Token.h>
19 
21 
22 #include <memory>
23 #include <mutex>
24 
25 namespace vtkm
26 {
27 
28 namespace internal
29 {
30 
41 VTKM_CONT_EXPORT vtkm::BufferSizeType NumberOfValuesToNumberOfBytes(vtkm::Id numValues,
42  std::size_t typeSize);
43 
44 template <typename T>
45 vtkm::BufferSizeType NumberOfValuesToNumberOfBytes(vtkm::Id numValues)
46 {
47  return NumberOfValuesToNumberOfBytes(numValues, sizeof(T));
48 }
50 
51 } // namespace internal
52 
53 namespace cont
54 {
55 namespace internal
56 {
57 
58 namespace detail
59 {
60 
61 struct BufferHelper;
62 
63 using DeleterType = void(void*);
64 
65 template <typename T>
66 void BasicDeleter(void* mem)
67 {
68  T* obj = reinterpret_cast<T*>(mem);
69  delete obj;
70 }
71 
72 using CopierType = void*(const void*);
73 template <typename T>
74 void* BasicCopier(const void* mem)
75 {
76  return new T(*reinterpret_cast<const T*>(mem));
77 }
78 
79 } // namespace detail
80 
87 class VTKM_CONT_EXPORT Buffer final
88 {
89  class InternalsStruct;
90  std::shared_ptr<InternalsStruct> Internals;
91 
92  friend struct vtkm::cont::internal::detail::BufferHelper;
93 
94 public:
97  VTKM_CONT Buffer();
98 
99  VTKM_CONT Buffer(const Buffer& src);
100  VTKM_CONT Buffer(Buffer&& src) noexcept;
101 
102  VTKM_CONT ~Buffer();
103 
104  VTKM_CONT Buffer& operator=(const Buffer& src);
105  VTKM_CONT Buffer& operator=(Buffer&& src) noexcept;
106 
113  VTKM_CONT vtkm::BufferSizeType GetNumberOfBytes() const;
114 
124  VTKM_CONT void SetNumberOfBytes(vtkm::BufferSizeType numberOfBytes,
125  vtkm::CopyFlag preserve,
126  vtkm::cont::Token& token) const;
127 
128 private:
129  VTKM_CONT bool MetaDataIsType(const std::string& type) const;
130  VTKM_CONT void SetMetaData(void* data,
131  const std::string& type,
132  detail::DeleterType* deleter,
133  detail::CopierType copier) const;
134  VTKM_CONT void* GetMetaData(const std::string& type) const;
135 
136 public:
139  VTKM_CONT bool HasMetaData() const;
140 
143  template <typename MetaDataType>
144  VTKM_CONT bool MetaDataIsType() const
145  {
146  return this->MetaDataIsType(vtkm::cont::TypeToString<MetaDataType>());
147  }
148 
158  template <typename MetaDataType>
159  VTKM_CONT void SetMetaData(const MetaDataType& metadata) const
160  {
161  MetaDataType* metadataCopy = new MetaDataType(metadata);
162  this->SetMetaData(metadataCopy,
163  vtkm::cont::TypeToString(metadata),
164  detail::BasicDeleter<MetaDataType>,
165  detail::BasicCopier<MetaDataType>);
166  }
167 
178  template <typename MetaDataType>
179  VTKM_CONT MetaDataType& GetMetaData() const
180  {
181  if (!this->HasMetaData())
182  {
183  this->SetMetaData(MetaDataType{});
184  }
185  return *reinterpret_cast<MetaDataType*>(
186  this->GetMetaData(vtkm::cont::TypeToString<MetaDataType>()));
187  }
188 
191  VTKM_CONT bool IsAllocatedOnHost() const;
192 
199  VTKM_CONT bool IsAllocatedOnDevice(vtkm::cont::DeviceAdapterId device) const;
200 
207  VTKM_CONT const void* ReadPointerHost(vtkm::cont::Token& token) const;
208 
218  VTKM_CONT const void* ReadPointerDevice(vtkm::cont::DeviceAdapterId device,
219  vtkm::cont::Token& token) const;
220 
227  VTKM_CONT void* WritePointerHost(vtkm::cont::Token& token) const;
228 
238  VTKM_CONT void* WritePointerDevice(vtkm::cont::DeviceAdapterId device,
239  vtkm::cont::Token& token) const;
240 
253  VTKM_CONT void Enqueue(const vtkm::cont::Token& token) const;
254 
262  VTKM_CONT void DeepCopyFrom(const vtkm::cont::internal::Buffer& source) const;
263  VTKM_CONT void DeepCopyFrom(const vtkm::cont::internal::Buffer& source,
264  vtkm::cont::DeviceAdapterId device) const;
266 
276  VTKM_CONT void Reset(const vtkm::cont::internal::BufferInfo& buffer);
277 
288  VTKM_CONT void ReleaseDeviceResources() const;
289 
292  VTKM_CONT vtkm::cont::internal::BufferInfo GetHostBufferInfo() const;
293 
299  VTKM_CONT vtkm::cont::internal::BufferInfo GetDeviceBufferInfo(
300  vtkm::cont::DeviceAdapterId device) const;
301 
305  VTKM_CONT vtkm::cont::internal::TransferredBuffer TakeHostBufferOwnership() const;
306 
310  VTKM_CONT vtkm::cont::internal::TransferredBuffer TakeDeviceBufferOwnership(
311  vtkm::cont::DeviceAdapterId device) const;
312 
322  VTKM_CONT void Fill(const void* source,
323  vtkm::BufferSizeType sourceSize,
324  vtkm::BufferSizeType start,
326  vtkm::cont::Token& token) const;
327 
328  VTKM_CONT bool operator==(const vtkm::cont::internal::Buffer& rhs) const
329  {
330  return (this->Internals == rhs.Internals);
331  }
332 
333  VTKM_CONT bool operator!=(const vtkm::cont::internal::Buffer& rhs) const
334  {
335  return (this->Internals != rhs.Internals);
336  }
337 };
338 
339 template <typename... ResetArgs>
340 VTKM_CONT vtkm::cont::internal::Buffer MakeBuffer(ResetArgs&&... resetArgs)
341 {
342  vtkm::cont::internal::Buffer buffer;
343  buffer.Reset(vtkm::cont::internal::BufferInfo(std::forward<ResetArgs>(resetArgs)...));
344  return buffer;
345 }
346 }
347 }
348 } // namespace vtkm::cont::internal
349 
350 //=============================================================================
351 // Specializations of serialization related classes
353 namespace mangled_diy_namespace
354 {
355 
356 template <>
357 struct VTKM_CONT_EXPORT Serialization<vtkm::cont::internal::Buffer>
358 {
359  static VTKM_CONT void save(BinaryBuffer& bb, const vtkm::cont::internal::Buffer& obj);
360  static VTKM_CONT void load(BinaryBuffer& bb, vtkm::cont::internal::Buffer& obj);
361 };
362 
363 } // diy
365 
366 #endif //vtk_m_vtkm_cont_internal_Buffer_h
vtkm::cont::TypeToString
std::string TypeToString(const std::type_info &t)
Use RTTI information to retrieve the name of the type T.
vtkm::exec::arg::load
T load(const U &u, vtkm::Id v)
Definition: FetchTagArrayDirectIn.h:36
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::operator!=
bool operator!=(const vtkm::cont::Token &token, vtkm::cont::Token::Reference ref)
Definition: Token.h:177
DeviceAdapterTag.h
vtkm::BufferSizeType
vtkm::Int64 BufferSizeType
Definition: DeviceAdapterMemoryManager.h:27
mangled_diy_namespace
Definition: Particle.h:351
DeviceAdapterMemoryManager.h
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
VTKM_CONT_EXPORT
#define VTKM_CONT_EXPORT
Definition: vtkm_cont_export.h:44
Serialization.h
vtkm_cont_export.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
vtkm::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:17
vtkm::cont::operator==
bool operator==(const vtkm::cont::Token &token, vtkm::cont::Token::Reference ref)
Definition: Token.h:173
Logging.h
Logging utilities.
Token.h