retry_allocator.cc 2.5 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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/retry_allocator.h"
Y
Yu Yang 已提交
16
#include "paddle/fluid/memory/allocation/underlying_manual_allocation.h"
S
sneaxiy 已提交
17 18 19 20
namespace paddle {
namespace memory {
namespace allocation {

Y
Yu Yang 已提交
21 22
bool RetryAllocator::IsAllocThreadSafe() const {
  return underlying_allocator_->IsAllocThreadSafe();
S
sneaxiy 已提交
23 24
}

Y
Yu Yang 已提交
25 26 27 28
void RetryAllocator::Free(Allocation* allocation) {
  // Delete underlying allocation first.
  reinterpret_cast<UnderlyingManualAllocation*>(allocation)
      ->allocation_.reset();
Y
Yu Yang 已提交
29 30 31 32 33
  {
    // notify all waited allocators, they can try to allocate memory after free.
    std::lock_guard<std::mutex> lock(mutex_);
    cv_.notify_all();
  }
Y
Yu Yang 已提交
34
  delete allocation;
Y
Yu Yang 已提交
35 36
}

Y
Yu Yang 已提交
37
Allocation* RetryAllocator::AllocateImpl(size_t size, Allocator::Attr attr) {
S
sneaxiy 已提交
38
  auto alloc_func = [&, this]() {
Y
Yu Yang 已提交
39 40
    return new UnderlyingManualAllocation(
        underlying_allocator_->Allocate(size, attr));
S
sneaxiy 已提交
41 42 43 44
  };
  // In fact, we can unify the code of allocation success and failure
  // But it would add lock even when allocation success at the first time
  try {
Y
Yu Yang 已提交
45 46
    return alloc_func();
  } catch (BadAlloc& bad_alloc) {
S
sneaxiy 已提交
47 48 49 50 51 52
    {
      // We can just write allocation retry inside the predicate function of
      // wait_until
      // But it needs to acquire the lock when executing predicate function
      // For better performance, we use loop here
      auto end_time = std::chrono::high_resolution_clock::now() + retry_time_;
Y
Yu Yang 已提交
53 54 55 56 57
      auto wait_until = [&, this] {
        std::unique_lock<std::mutex> lock(mutex_);
        return cv_.wait_until(lock, end_time);
      };
      while (wait_until() != std::cv_status::timeout) {
S
sneaxiy 已提交
58
        try {
Y
Yu Yang 已提交
59 60 61
          return alloc_func();
        } catch (BadAlloc& ex) {
          bad_alloc = ex;
S
sneaxiy 已提交
62
        } catch (...) {
Y
Yu Yang 已提交
63
          throw;
S
sneaxiy 已提交
64
        }
Y
Yu Yang 已提交
65
      }
S
sneaxiy 已提交
66

Y
Yu Yang 已提交
67
      throw;  // rethrow the original exception or throw the internal bad_alloc
S
sneaxiy 已提交
68 69
    }
  } catch (...) {
Y
Yu Yang 已提交
70 71 72
    throw;
  }
}
S
sneaxiy 已提交
73 74 75 76

}  // namespace allocation
}  // namespace memory
}  // namespace paddle