storage.h 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

#include <cstddef>

#include "boost/intrusive_ptr.hpp"
#include "paddle/pten/core/utils/intrusive_ptr.h"
#include "paddle/pten/core/utils/intrusive_ref_counter.h"
#include "paddle/pten/core/utils/type_info.h"

24
#include "paddle/fluid/memory/memory.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "paddle/fluid/platform/place.h"
#include "paddle/pten/core/allocator.h"

namespace pten {

/// \brief The interface of contiguous storage used for the dense tensor.
/// It should be used in conjunction with the intrusive pointer. We prohibit
/// all default copy operations to ensure the integrity of the package.
class Storage : public intrusive_ref_counter<Storage> {
 public:
  using Place = paddle::platform::Place;
  Storage() = default;
  Storage(const Storage&) = delete;

39 40 41 42 43 44
  /* @jim19930609: Following interfaces will be modified/replaced/removed
                   as soon as the new Allocation - Allocator design get
     finalized.
    */

  /*   --------- shared_ptr<Allocation> -------- */
45 46 47
  // Initialize a Storage with unique Allocation
  explicit Storage(std::shared_ptr<paddle::memory::Allocation>&& data)
      : data_(std::move(data)) {}
48

49 50 51 52 53 54
  // Initialize a Storage shareing Allocation with another storage
  explicit Storage(const std::shared_ptr<paddle::memory::Allocation>& data)
      : data_(data) {}

  void* data() const {
    return data_ ? reinterpret_cast<void*>(
55
                       reinterpret_cast<uintptr_t>(data_->ptr()))
56 57 58 59 60 61
                 : nullptr;
  }

  const std::shared_ptr<paddle::memory::Allocation> data_shared() const {
    return data_;
  }
62

63 64 65 66 67 68 69 70 71
  void set_data_shared(
      const std::shared_ptr<paddle::memory::Allocation>& holder) {
    data_ = holder;
  }

  std::shared_ptr<paddle::memory::Allocation> move_data_shared() {
    return std::move(data_);
  }

72 73 74 75 76 77 78
  virtual void ReallocShared(size_t n) {
    PADDLE_THROW(paddle::platform::errors::Unimplemented(
        "ReallocShared has not been overrided by the current Storage"));
  }
  /* --------- shared_ptr<Allocation> -------- */

  virtual ~Storage() = default;
79

石晓伟 已提交
80 81
  virtual void Clear() = 0;

82 83 84 85 86 87
  virtual size_t size() const = 0;
  virtual const Place& place() const = 0;
  virtual bool OwnsMemory() const = 0;
  virtual void Realloc(size_t n) = 0;

 protected:
88
  std::shared_ptr<paddle::memory::Allocation> data_;
89 90 91 92 93 94 95
};

class TensorStorage : public Storage {
 public:
  using Place = paddle::platform::Place;

  explicit TensorStorage(const std::shared_ptr<Allocator>& a) : alloc_(a) {}
96

97
  TensorStorage(const std::shared_ptr<Allocator>& a, size_t size)
98 99 100 101 102 103 104 105 106 107
      : Storage(paddle::memory::AllocShared(a->place(), size)), alloc_(a) {
    size_ = data_->size();
  }

  void Clear() override {
    data_ = nullptr;
    size_ = 0;
  }

  void Realloc(size_t size) override;
108 109 110 111 112 113

  ~TensorStorage() = default;

  static const char* name() { return "TensorStorage"; }

  size_t size() const noexcept override { return size_; }
石晓伟 已提交
114

115 116 117 118 119 120 121 122 123 124
  const Place& place() const override {
    if (!data_ && !alloc_) {
      PADDLE_THROW(paddle::platform::errors::Unimplemented(
          "Unable to visit place: either data_ or alloc_ has to be initialized "
          "first."));
    }
    if (data_) {
      return data_->place();
    }
    return alloc_->place();
石晓伟 已提交
125 126
  }

127 128 129 130 131 132 133 134 135 136 137
  bool OwnsMemory() const noexcept override { return true; }
  const std::shared_ptr<Allocator>& allocator() const noexcept {
    return alloc_;
  }

 private:
  const std::shared_ptr<Allocator> alloc_;
  int64_t size_{0};
};

}  // namespace pten