garbage_collector.h 2.0 KB
Newer Older
W
wanghuancoder 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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 <queue>
17

18
#include "paddle/fluid/framework/new_executor/instruction/instruction_base.h"
19
#include "paddle/fluid/framework/new_executor/new_executor_defs.h"
W
wanghuancoder 已提交
20 21
#include "paddle/fluid/memory/allocation/spin_lock.h"
#include "paddle/fluid/platform/device_event.h"
22 23
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/errors.h"
W
wanghuancoder 已提交
24 25 26 27

namespace paddle {
namespace framework {

28 29 30
using Garbage = std::shared_ptr<memory::Allocation>;
using GarbageQueue = std::deque<Garbage>;

W
wanghuancoder 已提交
31 32 33
class InterpreterCoreGarbageCollector {
 public:
  InterpreterCoreGarbageCollector();
34
  virtual ~InterpreterCoreGarbageCollector() {}
35 36 37

  virtual void Add(Variable* var, const Instruction& instruction) = 0;

38 39
  virtual void Add(Variable* var, const InstructionBase* instruction) = 0;

W
wanghuancoder 已提交
40 41
  DISABLE_COPY_AND_ASSIGN(InterpreterCoreGarbageCollector);

42
 protected:
W
wanghuancoder 已提交
43
  std::unique_ptr<GarbageQueue> garbages_;
44 45 46
  int64_t max_memory_size_;
  int64_t cur_memory_size_;
  memory::SpinLock spinlock_;
W
wanghuancoder 已提交
47
};
48

49 50 51 52 53 54 55
bool IsInterpretercoreFastGCEnabled();

std::unique_ptr<InterpreterCoreGarbageCollector>
CreateInterpreterCoreGarbageCollector(
    const platform::Place& place,
    const std::vector<Instruction>& vec_instruction);

56 57 58 59 60
std::unique_ptr<InterpreterCoreGarbageCollector>
CreateInterpreterCoreGarbageCollector(
    const platform::Place& place,
    const std::vector<std::unique_ptr<InstructionBase>>& vec_instruction);

W
wanghuancoder 已提交
61 62
}  // namespace framework
}  // namespace paddle