Viskores  1.0
Buffer.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_viskores_cont_internal_Buffer_h
19 #define viskores_viskores_cont_internal_Buffer_h
20 
22 
24 #include <viskores/cont/Logging.h>
26 #include <viskores/cont/Token.h>
27 
29 
30 #include <memory>
31 #include <mutex>
32 
33 namespace viskores
34 {
35 
36 namespace internal
37 {
38 
49 VISKORES_CONT_EXPORT viskores::BufferSizeType NumberOfValuesToNumberOfBytes(viskores::Id numValues,
50  std::size_t typeSize);
51 
52 template <typename T>
53 viskores::BufferSizeType NumberOfValuesToNumberOfBytes(viskores::Id numValues)
54 {
55  return NumberOfValuesToNumberOfBytes(numValues, sizeof(T));
56 }
58 
59 } // namespace internal
60 
61 namespace cont
62 {
63 namespace internal
64 {
65 
66 namespace detail
67 {
68 
69 struct BufferHelper;
70 
71 using DeleterType = void(void*);
72 
73 template <typename T>
74 void BasicDeleter(void* mem)
75 {
76  T* obj = reinterpret_cast<T*>(mem);
77  delete obj;
78 }
79 
80 using CopierType = void*(const void*);
81 template <typename T>
82 void* BasicCopier(const void* mem)
83 {
84  return new T(*reinterpret_cast<const T*>(mem));
85 }
86 
87 } // namespace detail
88 
95 class VISKORES_CONT_EXPORT Buffer final
96 {
97  class InternalsStruct;
98  std::shared_ptr<InternalsStruct> Internals;
99 
100  friend struct viskores::cont::internal::detail::BufferHelper;
101 
102 public:
105  VISKORES_CONT Buffer();
106 
107  VISKORES_CONT Buffer(const Buffer& src);
108  VISKORES_CONT Buffer(Buffer&& src) noexcept;
109 
110  VISKORES_CONT ~Buffer();
111 
112  VISKORES_CONT Buffer& operator=(const Buffer& src);
113  VISKORES_CONT Buffer& operator=(Buffer&& src) noexcept;
114 
121  VISKORES_CONT viskores::BufferSizeType GetNumberOfBytes() const;
122 
132  VISKORES_CONT void SetNumberOfBytes(viskores::BufferSizeType numberOfBytes,
133  viskores::CopyFlag preserve,
134  viskores::cont::Token& token) const;
135 
136 private:
137  VISKORES_CONT bool MetaDataIsType(const std::string& type) const;
138  VISKORES_CONT void SetMetaData(void* data,
139  const std::string& type,
140  detail::DeleterType* deleter,
141  detail::CopierType copier) const;
142  VISKORES_CONT void* GetMetaData(const std::string& type) const;
143 
144 public:
147  VISKORES_CONT bool HasMetaData() const;
148 
151  template <typename MetaDataType>
152  VISKORES_CONT bool MetaDataIsType() const
153  {
154  return this->MetaDataIsType(viskores::cont::TypeToString<MetaDataType>());
155  }
156 
166  template <typename MetaDataType>
167  VISKORES_CONT void SetMetaData(const MetaDataType& metadata) const
168  {
169  MetaDataType* metadataCopy = new MetaDataType(metadata);
170  this->SetMetaData(metadataCopy,
172  detail::BasicDeleter<MetaDataType>,
173  detail::BasicCopier<MetaDataType>);
174  }
175 
186  template <typename MetaDataType>
187  VISKORES_CONT MetaDataType& GetMetaData() const
188  {
189  if (!this->HasMetaData())
190  {
191  this->SetMetaData(MetaDataType{});
192  }
193  return *reinterpret_cast<MetaDataType*>(
194  this->GetMetaData(viskores::cont::TypeToString<MetaDataType>()));
195  }
196 
199  VISKORES_CONT bool IsAllocatedOnHost() const;
200 
207  VISKORES_CONT bool IsAllocatedOnDevice(viskores::cont::DeviceAdapterId device) const;
208 
215  VISKORES_CONT const void* ReadPointerHost(viskores::cont::Token& token) const;
216 
226  VISKORES_CONT const void* ReadPointerDevice(viskores::cont::DeviceAdapterId device,
227  viskores::cont::Token& token) const;
228 
235  VISKORES_CONT void* WritePointerHost(viskores::cont::Token& token) const;
236 
246  VISKORES_CONT void* WritePointerDevice(viskores::cont::DeviceAdapterId device,
247  viskores::cont::Token& token) const;
248 
261  VISKORES_CONT void Enqueue(const viskores::cont::Token& token) const;
262 
270  VISKORES_CONT void DeepCopyFrom(const viskores::cont::internal::Buffer& source) const;
271  VISKORES_CONT void DeepCopyFrom(const viskores::cont::internal::Buffer& source,
272  viskores::cont::DeviceAdapterId device) const;
274 
284  VISKORES_CONT void Reset(const viskores::cont::internal::BufferInfo& buffer);
285 
296  VISKORES_CONT void ReleaseDeviceResources() const;
297 
300  VISKORES_CONT viskores::cont::internal::BufferInfo GetHostBufferInfo() const;
301 
307  VISKORES_CONT viskores::cont::internal::BufferInfo GetDeviceBufferInfo(
308  viskores::cont::DeviceAdapterId device) const;
309 
313  VISKORES_CONT viskores::cont::internal::TransferredBuffer TakeHostBufferOwnership() const;
314 
318  VISKORES_CONT viskores::cont::internal::TransferredBuffer TakeDeviceBufferOwnership(
319  viskores::cont::DeviceAdapterId device) const;
320 
330  VISKORES_CONT void Fill(const void* source,
331  viskores::BufferSizeType sourceSize,
334  viskores::cont::Token& token) const;
335 
336  VISKORES_CONT bool operator==(const viskores::cont::internal::Buffer& rhs) const
337  {
338  return (this->Internals == rhs.Internals);
339  }
340 
341  VISKORES_CONT bool operator!=(const viskores::cont::internal::Buffer& rhs) const
342  {
343  return (this->Internals != rhs.Internals);
344  }
345 };
346 
347 template <typename... ResetArgs>
348 VISKORES_CONT viskores::cont::internal::Buffer MakeBuffer(ResetArgs&&... resetArgs)
349 {
350  viskores::cont::internal::Buffer buffer;
351  buffer.Reset(viskores::cont::internal::BufferInfo(std::forward<ResetArgs>(resetArgs)...));
352  return buffer;
353 }
354 }
355 }
356 } // namespace viskores::cont::internal
357 
358 //=============================================================================
359 // Specializations of serialization related classes
361 namespace mangled_diy_namespace
362 {
363 
364 template <>
365 struct VISKORES_CONT_EXPORT Serialization<viskores::cont::internal::Buffer>
366 {
367  static VISKORES_CONT void save(BinaryBuffer& bb, const viskores::cont::internal::Buffer& obj);
368  static VISKORES_CONT void load(BinaryBuffer& bb, viskores::cont::internal::Buffer& obj);
369 };
370 
371 } // diy
373 
374 #endif //viskores_viskores_cont_internal_Buffer_h
viskores::exec::arg::load
T load(const U &u, viskores::Id v)
Definition: FetchTagArrayDirectIn.h:44
viskores::cont::operator==
bool operator==(const viskores::cont::Token &token, viskores::cont::Token::Reference ref)
Definition: Token.h:182
DeviceAdapterTag.h
mangled_diy_namespace
Definition: Particle.h:373
DeviceAdapterMemoryManager.h
viskores::cont::operator!=
bool operator!=(const viskores::cont::Token &token, viskores::cont::Token::Reference ref)
Definition: Token.h:187
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
Serialization.h
viskores::BufferSizeType
viskores::Int64 BufferSizeType
Definition: DeviceAdapterMemoryManager.h:35
viskores::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:66
Logging.h
Logging utilities.
viskores::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:25
Token.h
viskores::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:43
viskores::cont::TypeToString
std::string TypeToString(const std::type_info &t)
Use RTTI information to retrieve the name of the type T.
viskores_cont_export.h