retry_allocator.cc 3.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"
16

17 18
#include "glog/logging.h"

S
sneaxiy 已提交
19 20 21 22
namespace paddle {
namespace memory {
namespace allocation {

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
class WaitedAllocateSizeGuard {
 public:
  WaitedAllocateSizeGuard(std::atomic<size_t>* waited_size,
                          size_t requested_size)
      : waited_size_(waited_size), requested_size_(requested_size) {
    waited_size_->fetch_add(requested_size_,
                            std::memory_order::memory_order_relaxed);
  }

  ~WaitedAllocateSizeGuard() {
    waited_size_->fetch_sub(requested_size_,
                            std::memory_order::memory_order_relaxed);
  }

 private:
  std::atomic<size_t>* waited_size_;
  size_t requested_size_;
};

42
void RetryAllocator::FreeImpl(pten::Allocation* allocation) {
Y
Yu Yang 已提交
43
  // Delete underlying allocation first.
44
  size_t size = allocation->size();
Z
Zeng Jinle 已提交
45
  underlying_allocator_->Free(allocation);
46 47 48 49 50 51
  if (UNLIKELY(waited_allocate_size_)) {
    VLOG(10) << "Free " << size << " bytes and notify all waited threads, "
                                   "where waited_allocate_size_ = "
             << waited_allocate_size_;
    cv_.notify_all();
  }
Y
Yu Yang 已提交
52 53
}

54
pten::Allocation* RetryAllocator::AllocateImpl(size_t size) {
S
sneaxiy 已提交
55
  auto alloc_func = [&, this]() {
56
    return underlying_allocator_->Allocate(size).release();
S
sneaxiy 已提交
57 58 59 60
  };
  // 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 已提交
61
    return alloc_func();
62
  } catch (BadAlloc&) {
S
sneaxiy 已提交
63
    {
64 65 66
      WaitedAllocateSizeGuard guard(&waited_allocate_size_, size);
      VLOG(10) << "Allocation failed when allocating " << size
               << " bytes, waited_allocate_size_ = " << waited_allocate_size_;
S
sneaxiy 已提交
67
      // We can just write allocation retry inside the predicate function of
68 69
      // wait_until. But it needs to acquire the lock when executing predicate
      // function. For better performance, we use loop here
S
sneaxiy 已提交
70
      auto end_time = std::chrono::high_resolution_clock::now() + retry_time_;
Y
Yu Yang 已提交
71 72 73 74
      auto wait_until = [&, this] {
        std::unique_lock<std::mutex> lock(mutex_);
        return cv_.wait_until(lock, end_time);
      };
75 76

      size_t retry_time = 0;
Y
Yu Yang 已提交
77
      while (wait_until() != std::cv_status::timeout) {
S
sneaxiy 已提交
78
        try {
Y
Yu Yang 已提交
79
          return alloc_func();
80 81 82 83 84 85
        } catch (BadAlloc&) {
          // do nothing when it is not timeout
          ++retry_time;
          VLOG(10) << "Allocation failed when retrying " << retry_time
                   << " times when allocating " << size
                   << " bytes. Wait still.";
S
sneaxiy 已提交
86
        } catch (...) {
Y
Yu Yang 已提交
87
          throw;
S
sneaxiy 已提交
88
        }
Y
Yu Yang 已提交
89
      }
S
sneaxiy 已提交
90
    }
91 92 93
    VLOG(10) << "Allocation failed because of timeout when allocating " << size
             << " bytes.";
    return alloc_func();  // If timeout, try last allocation request.
S
sneaxiy 已提交
94
  } catch (...) {
Y
Yu Yang 已提交
95 96 97
    throw;
  }
}
S
sneaxiy 已提交
98 99 100 101

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