buffered_allocator.cc 2.4 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 21 22 23
// 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.

#include "paddle/fluid/memory/allocation/buffered_allocator.h"
#include <algorithm>
#include <limits>
#include <utility>

namespace paddle {
namespace memory {
namespace allocation {

Z
Zeng Jinle 已提交
24
BufferedAllocator::BufferedAllocator(std::shared_ptr<Allocator> allocator)
Y
Yu Yang 已提交
25
    : underlying_allocator_(std::move(allocator)) {
S
sneaxiy 已提交
26 27
  PADDLE_ENFORCE_NOT_NULL(
      underlying_allocator_,
Z
Zeng Jinle 已提交
28
      "Underlying allocator of BufferedAllocator must not be null");
S
sneaxiy 已提交
29 30 31 32 33
  if (underlying_allocator_->IsAllocThreadSafe()) {
    mtx_.reset(new std::mutex());
  }
}

Y
Yu Yang 已提交
34
BufferedAllocator::~BufferedAllocator() { FreeCache(-1UL); }
S
sneaxiy 已提交
35

Y
Yu Yang 已提交
36 37
void BufferedAllocator::FreeCache(size_t size) {
  platform::LockGuardPtr<std::mutex> guard(mtx_);
S
sneaxiy 已提交
38 39
  if (UNLIKELY(size == 0)) return;
  size_t cur = 0;
Y
Yu Yang 已提交
40 41 42
  while (!allocations_.empty()) {  // free the largest
    auto it = --allocations_.end();
    cur += it->second->size();
Z
Zeng Jinle 已提交
43
    underlying_allocator_->Free(it->second.release());
Y
Yu Yang 已提交
44 45
    allocations_.erase(it);
    if (cur >= size) return;
S
sneaxiy 已提交
46 47 48
  }
}

Z
Zeng Jinle 已提交
49 50 51
bool BufferedAllocator::IsAllocThreadSafe() const { return mtx_ != nullptr; }

void BufferedAllocator::FreeImpl(Allocation *allocation) {
Y
Yu Yang 已提交
52
  platform::LockGuardPtr<std::mutex> guard(mtx_);
Y
Yu Yang 已提交
53
  allocations_.emplace(allocation->size(), AllocationPtr(allocation));
S
sneaxiy 已提交
54
}
Z
Zeng Jinle 已提交
55

Y
Yu Yang 已提交
56
Allocation *BufferedAllocator::AllocateImpl(size_t size, Allocator::Attr attr) {
Y
Yu Yang 已提交
57 58 59 60
  {
    platform::LockGuardPtr<std::mutex> guard(mtx_);
    auto it = allocations_.lower_bound(size);
    if (it != allocations_.end() && it->first < size * 2) {
Y
Yu Yang 已提交
61
      AllocationPtr result(std::move(it->second));
Y
Yu Yang 已提交
62
      allocations_.erase(it);
Z
Zeng Jinle 已提交
63
      return result.release();
Y
Yu Yang 已提交
64 65
    }
  }
S
sneaxiy 已提交
66

Y
Yu Yang 已提交
67
  try {
Z
Zeng Jinle 已提交
68
    return underlying_allocator_->Allocate(size, attr).release();
Y
Yu Yang 已提交
69 70
  } catch (BadAlloc &) {
    FreeCache(size);
Z
Zeng Jinle 已提交
71
    return underlying_allocator_->Allocate(size, attr).release();
Y
Yu Yang 已提交
72
  }
73 74
}

S
sneaxiy 已提交
75 76 77
}  // namespace allocation
}  // namespace memory
}  // namespace paddle