garbage_collector.h 3.7 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright (c) 2018 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 <deque>
#include <functional>
#include <memory>
#include <mutex>  // NOLINT
S
sneaxiy 已提交
21
#include <utility>
S
sneaxiy 已提交
22 23 24 25 26 27 28
#include "paddle/fluid/platform/device_context.h"

namespace paddle {
namespace framework {

class GarbageCollector {
 public:
S
sneaxiy 已提交
29
  using GarbageQueue = std::deque<std::shared_ptr<memory::Allocation>>;
S
sneaxiy 已提交
30

S
sneaxiy 已提交
31
  GarbageCollector(const platform::Place &place, size_t max_memory_size);
S
sneaxiy 已提交
32

S
sneaxiy 已提交
33
  virtual ~GarbageCollector() = default;
S
fix bug  
sneaxiy 已提交
34

S
sneaxiy 已提交
35
  virtual void Wait() const {}
S
sneaxiy 已提交
36 37

  template <typename Container>
S
sneaxiy 已提交
38
  void Add(Container &&objs);
S
sneaxiy 已提交
39 40

  template <typename Container, typename Callback>
S
sneaxiy 已提交
41
  void Add(Container &&objs, Callback &&callback);
S
sneaxiy 已提交
42 43 44 45 46

 protected:
  virtual void ClearCallback(const std::function<void()> &callback) = 0;

  platform::DeviceContext *dev_ctx_;
S
sneaxiy 已提交
47
  std::unique_ptr<GarbageQueue> garbages_;
S
sneaxiy 已提交
48 49
  mutable std::mutex mutex_;
  const size_t max_memory_size_;
S
sneaxiy 已提交
50
  size_t cur_memory_size_{0};
S
sneaxiy 已提交
51 52
};

S
sneaxiy 已提交
53
class CPUGarbageCollector : public GarbageCollector {
S
sneaxiy 已提交
54
 public:
S
sneaxiy 已提交
55
  CPUGarbageCollector(const platform::CPUPlace &place, size_t max_memory_size);
S
sneaxiy 已提交
56 57

 protected:
S
sneaxiy 已提交
58
  void ClearCallback(const std::function<void()> &callback) override;
S
sneaxiy 已提交
59 60 61
};

#ifdef PADDLE_WITH_CUDA
S
sneaxiy 已提交
62
class UnsafeFastGPUGarbageCollector : public GarbageCollector {
S
fix bug  
sneaxiy 已提交
63 64
 public:
  UnsafeFastGPUGarbageCollector(const platform::CUDAPlace &place,
S
sneaxiy 已提交
65
                                size_t max_memory_size);
S
fix bug  
sneaxiy 已提交
66 67

 protected:
S
sneaxiy 已提交
68
  void ClearCallback(const std::function<void()> &callback) override;
S
fix bug  
sneaxiy 已提交
69 70
};

S
sneaxiy 已提交
71
class DefaultStreamGarbageCollector : public GarbageCollector {
S
sneaxiy 已提交
72 73
 public:
  DefaultStreamGarbageCollector(const platform::CUDAPlace &place,
S
sneaxiy 已提交
74
                                size_t max_memory_size);
S
sneaxiy 已提交
75

S
sneaxiy 已提交
76
  void Wait() const override;
S
sneaxiy 已提交
77 78

 protected:
S
sneaxiy 已提交
79
  void ClearCallback(const std::function<void()> &callback) override;
S
sneaxiy 已提交
80 81
};

S
sneaxiy 已提交
82
class StreamGarbageCollector : public GarbageCollector {
S
sneaxiy 已提交
83 84
 public:
  StreamGarbageCollector(const platform::CUDAPlace &place,
S
sneaxiy 已提交
85
                         size_t max_memory_size);
S
sneaxiy 已提交
86

S
sneaxiy 已提交
87
  ~StreamGarbageCollector();
S
sneaxiy 已提交
88

S
sneaxiy 已提交
89
  void Wait() const override;
S
sneaxiy 已提交
90

S
sneaxiy 已提交
91
  cudaStream_t stream() const;
S
sneaxiy 已提交
92 93

 protected:
S
sneaxiy 已提交
94
  void ClearCallback(const std::function<void()> &callback) override;
S
sneaxiy 已提交
95 96 97 98 99 100 101

 private:
  cudaStream_t stream_;
  std::unique_ptr<platform::StreamCallbackManager> callback_manager_;
};
#endif

S
sneaxiy 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
template <typename Container>
void GarbageCollector::Add(Container &&objs) {
  Add(std::forward<Container>(objs), []() {});
}

template <typename Container, typename Callback>
void GarbageCollector::Add(Container &&objs, Callback &&callback) {
  GarbageQueue *garbage_queue = nullptr;
  {
    std::lock_guard<std::mutex> guard(mutex_);
    for (auto &obj : objs) {
      if (!obj) continue;
      cur_memory_size_ += obj->size();
      garbages_->push_back(std::move(obj));
    }
    if (cur_memory_size_ >= max_memory_size_) {
      cur_memory_size_ = 0;
      garbage_queue = garbages_.release();
      garbages_.reset(new GarbageQueue());
    }
  }

  if (garbage_queue) {
    callback();
    ClearCallback([garbage_queue]() { delete garbage_queue; });
  }
}

S
sneaxiy 已提交
130 131 132 133 134
int64_t GetEagerDeletionThreshold();
bool IsFastEagerDeletionModeEnabled();

extern void UseGarbageCollectorGFlags();

S
sneaxiy 已提交
135 136
}  // namespace framework
}  // namespace paddle