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/allocation_with_underlying.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
void RetryAllocator::Free(Allocation* allocation) {
  // Delete underlying allocation first.
Y
Yu Yang 已提交
27
  reinterpret_cast<AllocationWithUnderlying*>(allocation)->allocation_.reset();
Y
Yu Yang 已提交
28 29 30 31 32
  {
    // 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 已提交
33
  delete allocation;
Y
Yu Yang 已提交
34 35
}

Y
Yu Yang 已提交
36
Allocation* RetryAllocator::AllocateImpl(size_t size, Allocator::Attr attr) {
S
sneaxiy 已提交
37
  auto alloc_func = [&, this]() {
Y
Yu Yang 已提交
38
    return new AllocationWithUnderlying(
Y
Yu Yang 已提交
39
        underlying_allocator_->Allocate(size, attr));
S
sneaxiy 已提交
40 41 42 43
  };
  // 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 已提交
44 45
    return alloc_func();
  } catch (BadAlloc& bad_alloc) {
S
sneaxiy 已提交
46 47 48 49 50 51
    {
      // 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 已提交
52 53 54 55 56
      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 已提交
57
        try {
Y
Yu Yang 已提交
58 59 60
          return alloc_func();
        } catch (BadAlloc& ex) {
          bad_alloc = ex;
S
sneaxiy 已提交
61
        } catch (...) {
Y
Yu Yang 已提交
62
          throw;
S
sneaxiy 已提交
63
        }
Y
Yu Yang 已提交
64
      }
S
sneaxiy 已提交
65

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

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