temporary_allocator.cc 5.1 KB
Newer Older
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/platform/temporary_allocator.h"
S
sneaxiy 已提交
16
#include <memory>
S
sneaxiy 已提交
17
#include <utility>
18 19
#include "paddle/fluid/memory/allocation/allocator_facade.h"

20 21 22 23 24 25 26 27 28
DEFINE_int64(limit_of_tmp_allocation, -1,
             "The up limit of temporary_allocation size.");
DEFINE_double(times_excess_than_required_tmp_allocation, 2,
              "times_excess_than_required_tmp_allocation indicates the "
              "max size the TemporaryAllocator can return. For example, "
              "if the required memory size is N, and "
              "times_excess_than_required_tmp_allocation is 2.0, "
              "the TemporaryAllocator will return the available allocation "
              "that the range of size is N ~ 2*N.");
29 30 31 32 33

namespace paddle {
namespace platform {
namespace alloc = memory::allocation;

Z
Zeng Jinle 已提交
34 35 36 37 38 39
TemporaryAllocation::TemporaryAllocation(
    alloc::AllocationPtr &&underlying_allocation)
    : Allocation(underlying_allocation->ptr(), underlying_allocation->size(),
                 underlying_allocation->place()),
      underlying_allocation_(std::move(underlying_allocation)) {}

40
TemporaryAllocator::TemporaryAllocator(platform::Place place) : place_(place) {
Z
Zeng Jinle 已提交
41
  temp_mem_map_.reset(new std::multimap<size_t, TemporaryAllocation *>());
42 43 44 45 46
}

bool TemporaryAllocator::IsAllocThreadSafe() const { return true; }

void TemporaryAllocator::Release(const std::function<void()> &callback) {
Z
Zeng Jinle 已提交
47
  std::unique_ptr<std::multimap<size_t, TemporaryAllocation *>> t_allocations;
48 49 50
  {
    std::unique_lock<std::mutex> lock(mtx_);
    callback();
51
    t_allocations.swap(temp_mem_map_);
Z
Zeng Jinle 已提交
52
    temp_mem_map_.reset(new std::multimap<size_t, TemporaryAllocation *>());
53 54
    wait_delete_mem_ = 0;
  }
55

56
  for (auto tmp : *t_allocations) {
57 58
    VLOG(10) << "Delete temporary allocation " << tmp.second->ptr()
             << " size: " << tmp.second->size();
Z
Zeng Jinle 已提交
59
    delete tmp.second;
60 61 62
  }
}

Z
Zeng Jinle 已提交
63 64 65
void TemporaryAllocator::Free(alloc::Allocation *allocation) {
  auto *temp_allocation = dynamic_cast<TemporaryAllocation *>(allocation);
  PADDLE_ENFORCE_NOT_NULL(temp_allocation);
66
  if (platform::is_gpu_place(temp_allocation->place())) {
67 68
    PADDLE_ENFORCE(platform::is_same_place(temp_allocation->place(), place_),
                   "The place should be the same.");
69 70 71
    size_t wait_delete_mem = 0;
    {
      std::unique_lock<std::mutex> lock(mtx_);
72
      temp_mem_map_->emplace(temp_allocation->size(), temp_allocation);
73 74 75 76
      wait_delete_mem_ += temp_allocation->size();
      wait_delete_mem = wait_delete_mem_;
      VLOG(10) << "Move temporary allocation: " << temp_allocation->ptr()
               << " to delete queue: " << temp_allocation->size() << "; "
77
               << "wait_delete_mem: " << wait_delete_mem;
78
    }
79 80 81 82

    if (FLAGS_limit_of_tmp_allocation > 0 &&
        wait_delete_mem > static_cast<size_t>(FLAGS_limit_of_tmp_allocation)) {
      PADDLE_ENFORCE(callback_ != nullptr, "The callback is non-initialized.");
83 84 85 86
      Release(callback_);
    }
    return;
  }
87 88
  VLOG(10) << "Delete temporary allocation " << temp_allocation->ptr()
           << " size: " << temp_allocation->size();
Z
Zeng Jinle 已提交
89
  delete temp_allocation;
90 91 92 93
}

size_t TemporaryAllocator::TemporaryAllocationQueueSize() {
  std::unique_lock<std::mutex> lock(mtx_);
94
  return temp_mem_map_ ? temp_mem_map_->size() : 0;
95 96 97 98 99 100 101 102
}

void TemporaryAllocator::SetCallback(const std::function<void()> &callback) {
  callback_ = callback;
}

alloc::Allocation *TemporaryAllocator::AllocateImpl(
    size_t size, alloc::Allocator::Attr attr) {
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  {
    // Find available allocation in temp_mem_map.
    std::unique_lock<std::mutex> lock(mtx_);
    if (temp_mem_map_->size()) {
      auto it = temp_mem_map_->lower_bound(size);
      // FIXME(zcd): Not sure the best value of excess fraction.
      if (it != temp_mem_map_->end() &&
          it->first <
              static_cast<size_t>(
                  size * FLAGS_times_excess_than_required_tmp_allocation)) {
        auto tmp_ptr = it->second;
        temp_mem_map_->erase(it);
        wait_delete_mem_ -= tmp_ptr->size();
        VLOG(10) << "Reuse temporary allocation: " << tmp_ptr->ptr() << ": "
                 << tmp_ptr->size();
        return tmp_ptr;
      }
    }
  }
  // If not find the the available allocation, get allocation from
  // AllocatorFacadeInstance.
Z
Zeng Jinle 已提交
124 125 126
  auto raw_allocation =
      alloc::AllocatorFacade::Instance().Alloc(place_, size, attr);
  auto temp_mem = new TemporaryAllocation(std::move(raw_allocation));
127
  VLOG(10) << "Alloc temporary allocation: " << temp_mem->ptr() << ": " << size;
Z
Zeng Jinle 已提交
128
  return temp_mem;
129 130 131 132
}

}  // namespace platform
}  // namespace paddle