aligned_buffer.hpp
1 //*****************************************************************************
2 // Copyright 2017-2021 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16 
17 #pragma once
18 
19 #include <cstddef>
20 
21 #include "ngraph/util.hpp"
22 
23 namespace ngraph
24 {
25  namespace runtime
26  {
27  /// \brief Allocates a block of memory on the specified alignment. The actual size of the
28  /// allocated memory is larger than the requested size by the alignment, so allocating 1
29  /// byte
30  /// on 64 byte alignment will allocate 65 bytes.
31  class NGRAPH_API AlignedBuffer
32  {
33  public:
34  // Allocator objects and the allocation interfaces are owned by the
35  // creators of AlignedBuffers. They need to ensure that the lifetime of
36  // allocator exceeds the lifetime of this AlignedBuffer.
37  AlignedBuffer(size_t byte_size, size_t alignment = 64);
38 
39  AlignedBuffer();
40  ~AlignedBuffer();
41 
43  AlignedBuffer& operator=(AlignedBuffer&& other);
44 
45  size_t size() const { return m_byte_size; }
46  void* get_ptr(size_t offset) const { return m_aligned_buffer + offset; }
47  void* get_ptr() { return m_aligned_buffer; }
48  const void* get_ptr() const { return m_aligned_buffer; }
49  template <typename T>
50  T* get_ptr()
51  {
52  return reinterpret_cast<T*>(m_aligned_buffer);
53  }
54  template <typename T>
55  const T* get_ptr() const
56  {
57  return reinterpret_cast<const T*>(m_aligned_buffer);
58  }
59 
60  template <typename T>
61  explicit operator T*()
62  {
63  return get_ptr<T>();
64  }
65 
66  private:
67  AlignedBuffer(const AlignedBuffer&) = delete;
68  AlignedBuffer& operator=(const AlignedBuffer&) = delete;
69 
70  protected:
71  char* m_allocated_buffer;
72  char* m_aligned_buffer;
73  size_t m_byte_size;
74  };
75  }
76  template <>
77  class NGRAPH_API AttributeAdapter<std::shared_ptr<runtime::AlignedBuffer>>
78  : public DirectValueAccessor<std::shared_ptr<runtime::AlignedBuffer>>
79  {
80  public:
81  AttributeAdapter(std::shared_ptr<runtime::AlignedBuffer>& value);
82 
83  static constexpr DiscreteTypeInfo type_info{
84  "AttributeAdapter<std::shared_ptr<runtime::AlignedBuffer>>", 0};
85  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
86  };
87 }
An AttributeAdapter "captures" an attribute as an AT& and makes it available as a ValueAccessor<VAT>.
Definition: attribute_adapter.hpp:171
Definition: attribute_adapter.hpp:79
Allocates a block of memory on the specified alignment. The actual size of the allocated memory is la...
Definition: aligned_buffer.hpp:32
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
Definition: type.hpp:39