aligned_buffer.hpp
1 // Copyright (C) 2018-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #pragma once
6 
7 #include <cstddef>
8 
9 #include "ngraph/util.hpp"
10 
11 namespace ngraph
12 {
13  namespace runtime
14  {
15  /// \brief Allocates a block of memory on the specified alignment. The actual size of the
16  /// allocated memory is larger than the requested size by the alignment, so allocating 1
17  /// byte
18  /// on 64 byte alignment will allocate 65 bytes.
19  class NGRAPH_API AlignedBuffer
20  {
21  public:
22  // Allocator objects and the allocation interfaces are owned by the
23  // creators of AlignedBuffers. They need to ensure that the lifetime of
24  // allocator exceeds the lifetime of this AlignedBuffer.
25  AlignedBuffer(size_t byte_size, size_t alignment = 64);
26 
27  AlignedBuffer();
28  virtual ~AlignedBuffer();
29 
31  AlignedBuffer& operator=(AlignedBuffer&& other);
32 
33  size_t size() const { return m_byte_size; }
34  void* get_ptr(size_t offset) const { return m_aligned_buffer + offset; }
35  void* get_ptr() { return m_aligned_buffer; }
36  const void* get_ptr() const { return m_aligned_buffer; }
37  template <typename T>
38  T* get_ptr()
39  {
40  return reinterpret_cast<T*>(m_aligned_buffer);
41  }
42  template <typename T>
43  const T* get_ptr() const
44  {
45  return reinterpret_cast<const T*>(m_aligned_buffer);
46  }
47 
48  template <typename T>
49  explicit operator T*()
50  {
51  return get_ptr<T>();
52  }
53 
54  private:
55  AlignedBuffer(const AlignedBuffer&) = delete;
56  AlignedBuffer& operator=(const AlignedBuffer&) = delete;
57 
58  protected:
59  char* m_allocated_buffer;
60  char* m_aligned_buffer;
61  size_t m_byte_size;
62  };
63  } // namespace runtime
64  template <>
65  class NGRAPH_API AttributeAdapter<std::shared_ptr<runtime::AlignedBuffer>>
66  : public DirectValueAccessor<std::shared_ptr<runtime::AlignedBuffer>>
67  {
68  public:
69  AttributeAdapter(std::shared_ptr<runtime::AlignedBuffer>& value);
70 
71  static constexpr DiscreteTypeInfo type_info{
72  "AttributeAdapter<std::shared_ptr<runtime::AlignedBuffer>>", 0};
73  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
74  };
75 } // namespace ngraph
An AttributeAdapter "captures" an attribute as an AT& and makes it available as a ValueAccessor<VAT>.
Definition: attribute_adapter.hpp:161
Definition: attribute_adapter.hpp:67
Allocates a block of memory on the specified alignment. The actual size of the allocated memory is la...
Definition: aligned_buffer.hpp:20
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:16
Definition: type.hpp:27