retry_allocator.cc 3.4 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

S
sneaxiy 已提交
17 18 19 20
namespace paddle {
namespace memory {
namespace allocation {

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
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_;
};

Z
Zeng Jinle 已提交
40
void RetryAllocator::FreeImpl(Allocation* allocation) {
Y
Yu Yang 已提交
41
  // Delete underlying allocation first.
42
  size_t size = allocation->size();
Z
Zeng Jinle 已提交
43
  underlying_allocator_->Free(allocation);
44 45 46 47 48 49
  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 已提交
50 51
}

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

      size_t retry_time = 0;
Y
Yu Yang 已提交
75
      while (wait_until() != std::cv_status::timeout) {
S
sneaxiy 已提交
76
        try {
Y
Yu Yang 已提交
77
          return alloc_func();
78 79 80 81 82 83
        } 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 已提交
84
        } catch (...) {
Y
Yu Yang 已提交
85
          throw;
S
sneaxiy 已提交
86
        }
Y
Yu Yang 已提交
87
      }
S
sneaxiy 已提交
88
    }
89 90 91
    VLOG(10) << "Allocation failed because of timeout when allocating " << size
             << " bytes.";
    return alloc_func();  // If timeout, try last allocation request.
S
sneaxiy 已提交
92
  } catch (...) {
Y
Yu Yang 已提交
93 94 95
    throw;
  }
}
S
sneaxiy 已提交
96 97 98 99

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