buffered_allocator.cc 2.5 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.

#include "paddle/fluid/memory/allocation/buffered_allocator.h"

namespace paddle {
namespace memory {
namespace allocation {

Z
Zeng Jinle 已提交
21
BufferedAllocator::BufferedAllocator(std::shared_ptr<Allocator> allocator)
Y
Yu Yang 已提交
22
    : underlying_allocator_(std::move(allocator)) {
S
sneaxiy 已提交
23 24
  PADDLE_ENFORCE_NOT_NULL(
      underlying_allocator_,
25 26
      platform::errors::InvalidArgument(
          "Underlying allocator of BufferedAllocator is NULL"));
S
sneaxiy 已提交
27
  if (underlying_allocator_->IsAllocThreadSafe()) {
28
    mtx_ = std::make_unique<std::mutex>();
S
sneaxiy 已提交
29 30 31
  }
}

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

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

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

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

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

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

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