Caffe2 - C++ API
A deep learning, cross platform ML framework
Storage.h
1 #pragma once
2 
3 #include <c10/core/StorageImpl.h>
4 
5 namespace c10 {
6 
7 struct C10_API Storage {
8  public:
9  Storage() {}
10  Storage(c10::intrusive_ptr<StorageImpl> ptr) : storage_impl_(std::move(ptr)) {}
11 
12  // Allocates memory buffer using given allocator and creates a storage with it
13  Storage(
14  caffe2::TypeMeta data_type,
15  size_t size,
16  Allocator* allocator,
17  bool resizable)
18  : storage_impl_(c10::make_intrusive<StorageImpl>(
19  data_type,
20  size,
21  allocator,
22  resizable)) {}
23 
24  // Creates storage with pre-allocated memory buffer. Allocator is given for
25  // potential future reallocations, however it can be nullptr if the storage
26  // is non-resizable
27  Storage(
28  caffe2::TypeMeta data_type,
29  int64_t numel,
30  at::DataPtr data_ptr,
31  at::Allocator* allocator,
32  bool resizable)
33  : storage_impl_(c10::make_intrusive<StorageImpl>(
34  data_type,
35  numel,
36  std::move(data_ptr),
37  allocator,
38  resizable)) {}
39 
40  // Legacy constructor for partially initialized (dtype or memory) storages
41  // that can be temporarily created with Caffe2 APIs. See the note on top of
42  // TensorImpl.h for details.
43  static Storage create_legacy(at::Device device, caffe2::TypeMeta data_type) {
44  return Storage(c10::make_intrusive<StorageImpl>(
45  data_type,
46  0,
47  at::DataPtr(nullptr, device),
48  GetAllocator(device.type()),
49  true));
50  }
51 
52  template <typename T>
53  inline bool IsType() const {
54  return storage_impl_->IsType<T>();
55  }
56 
57  template <typename T>
58  T* data() const { return storage_impl_->data<T>(); }
59 
60  template <typename T>
61  T* unsafe_data() const { return storage_impl_->unsafe_data<T>(); }
62 
63  size_t elementSize() const {
64  return storage_impl_->itemsize();
65  }
66 
67  inline size_t itemsize() const {
68  return storage_impl_->itemsize();
69  }
70 
71  ptrdiff_t size() const {
72  return storage_impl_->numel();
73  }
74 
75  int64_t numel() const {
76  return storage_impl_->numel();
77  }
78 
79  // TODO: remove later
80  void set_numel(int64_t numel) const {
81  storage_impl_.get()->set_numel(numel);
82  }
83 
84  bool resizable() const {
85  return storage_impl_->resizable();
86  }
87 
88  size_t capacity() const {
89  return storage_impl_->capacity();
90  }
91  // get() use here is to get const-correctness
92 
93  void* data() const {
94  return storage_impl_.get()->data();
95  }
96 
97  const caffe2::TypeMeta& dtype() const {
98  return storage_impl_->dtype();
99  }
100 
101  at::DataPtr& data_ptr() {
102  return storage_impl_->data_ptr();
103  }
104 
105  const at::DataPtr& data_ptr() const {
106  return storage_impl_->data_ptr();
107  }
108 
109  // Returns the previous data_ptr
110  at::DataPtr set_data_ptr(at::DataPtr&& data_ptr) const {
111  return storage_impl_.get()->set_data_ptr(std::move(data_ptr));
112  };
113 
114  void set_dtype(const caffe2::TypeMeta& data_type) const {
115  storage_impl_.get()->set_dtype(data_type);
116  }
117 
118  DeviceType device_type() const {
119  return storage_impl_->device_type();
120  }
121 
122  at::Allocator* allocator() const {
123  return storage_impl_.get()->allocator();
124  }
125 
126  at::Device device() const {
127  return storage_impl_->device();
128  }
129 
130  StorageImpl* unsafeReleaseStorageImpl() {
131  return storage_impl_.release();
132  }
133 
134  StorageImpl* unsafeGetStorageImpl() const noexcept {
135  return storage_impl_.get();
136  }
137 
138  operator bool() const {
139  return storage_impl_;
140  }
141 
142  size_t use_count() const {
143  return storage_impl_.use_count();
144  }
145 
146  inline bool unique() const {
147  return storage_impl_.unique();
148  }
149 
150  bool is_alias_of(const Storage& other) const {
151  return storage_impl_ == other.storage_impl_;
152  }
153 
154  void UniqueStorageShareExternalPointer(
155  void* src,
156  const caffe2::TypeMeta& data_type,
157  size_t capacity,
158  DeleterFnPtr d = nullptr) {
159  if (!storage_impl_.unique()) {
160  AT_ERROR(
161  "UniqueStorageShareExternalPointer can only be called when use_count == 1");
162  }
163  storage_impl_->UniqueStorageShareExternalPointer(
164  src, data_type, capacity, d);
165  }
166 
167  void UniqueStorageShareExternalPointer(
168  at::DataPtr&& data_ptr,
169  const caffe2::TypeMeta& data_type,
170  size_t capacity) {
171  if (!storage_impl_.unique()) {
172  AT_ERROR(
173  "UniqueStorageShareExternalPointer can only be called when use_count == 1");
174  }
175  storage_impl_->UniqueStorageShareExternalPointer(
176  std::move(data_ptr), data_type, capacity);
177  }
178 
179  protected:
180  c10::intrusive_ptr<StorageImpl> storage_impl_;
181 };
182 
183 } // namespace c10
C10_NODISCARD TensorOptions device(c10::optional< Device > device) const noexcept
Return a copy of TensorOptions with device set to the given one, or cleared if device is nullopt...
TensorOptions device(Device device)
Convenience function that returns a TensorOptions object with the device set to the given one...
Represents a a compute device on which a tensor is located.
Definition: Device.h:30
C10_NODISCARD TensorOptions dtype(c10::optional< caffe2::TypeMeta > dtype) const noexcept
Return a copy of TensorOptions with dtype set to the given one.
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
Definition: alias_info.h:7
TypeMeta is a thin class that allows us to store the type of a container such as a blob...
Definition: typeid.h:324
DeviceType type() const noexcept
Returns the type of device this is.
Definition: Device.h:65
TensorOptions dtype(caffe2::TypeMeta dtype)
Convenience function that returns a TensorOptions object with the dtype set to the given one...