buffered_allocator_test.cc 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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/buffered_allocator.h"
#include <gtest/gtest.h>
Z
Zeng Jinle 已提交
17
#include <memory>
S
sneaxiy 已提交
18
#include <utility>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include "paddle/fluid/memory/allocation/best_fit_allocator.h"
#include "paddle/fluid/memory/allocation/cpu_allocator.h"
#include "paddle/fluid/memory/allocation/locked_allocator.h"

namespace paddle {
namespace memory {
namespace allocation {

inline std::unique_ptr<BufferedAllocator> GetBufferedAllocator(
    Allocation *allocation, bool thread_safe) {
  std::unique_ptr<Allocator> allocator(new BestFitAllocator(allocation));
  if (thread_safe) {
    allocator.reset(new LockedAllocator(std::move(allocator)));
  }

  return std::unique_ptr<BufferedAllocator>(
      new BufferedAllocator(std::move(allocator)));
}

TEST(buffered_allocator, thread_safety) {
  std::unique_ptr<CPUAllocator> allocator(new CPUAllocator());
Y
Yu Yang 已提交
40
  auto chunk = allocator->Allocate(1 << 20, allocator->kDefault);
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  {
    auto buf_allocator = GetBufferedAllocator(chunk.get(), true);
    ASSERT_EQ(buf_allocator->IsAllocThreadSafe(), true);
  }

  {
    auto buf_allocator = GetBufferedAllocator(chunk.get(), false);
    ASSERT_EQ(buf_allocator->IsAllocThreadSafe(), false);
  }
}

class StubAllocation : public Allocation {
 public:
  using Allocation::Allocation;
};

Y
Yu Yang 已提交
57
class StubAllocator : public Allocator {
58 59 60 61 62 63 64 65 66 67
 public:
  void ResetCounter() {
    construct_count_ = 0;
    destruct_count_ = 0;
  }

  size_t GetAllocCount() const { return construct_count_; }

  size_t GetFreeCount() const { return destruct_count_; }

Y
Yu Yang 已提交
68
 protected:
Z
Zeng Jinle 已提交
69
  void Free(Allocation *allocation) override {
Y
Yu Yang 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    auto *alloc = dynamic_cast<StubAllocation *>(allocation);
    PADDLE_ENFORCE_NOT_NULL(alloc);
    if (alloc->ptr()) delete[] static_cast<uint8_t *>(alloc->ptr());
    ++destruct_count_;
    delete allocation;
  }
  Allocation *AllocateImpl(size_t size, Allocator::Attr attr) override {
    ++construct_count_;
    if (size == 0) {
      return new StubAllocation(nullptr, 0, platform::CPUPlace());
    } else {
      return new StubAllocation(new uint8_t[size], size, platform::CPUPlace());
    }
  }

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
 private:
  size_t construct_count_ = 0;
  size_t destruct_count_ = 0;
};

constexpr size_t kZero = 0;
constexpr size_t kOne = 1;
constexpr size_t kTwo = 2;

TEST(buffered_allocator, lazy_free) {
  std::unique_ptr<StubAllocator> stub_allocator(new StubAllocator());
  auto *underlying_allocator = stub_allocator.get();
  std::unique_ptr<BufferedAllocator> allocator(
      new BufferedAllocator(std::move(stub_allocator)));

  {
    underlying_allocator->ResetCounter();
Y
Yu Yang 已提交
102
    auto x = allocator->Allocate(1025, allocator->kDefault);
103 104
    ASSERT_EQ(underlying_allocator->GetAllocCount(), kOne);
    ASSERT_EQ(underlying_allocator->GetFreeCount(), kZero);
Y
Yu Yang 已提交
105
    x = nullptr;
106 107 108 109 110
    ASSERT_EQ(underlying_allocator->GetFreeCount(), kZero);
  }

  {
    underlying_allocator->ResetCounter();
Y
Yu Yang 已提交
111
    auto x = allocator->Allocate(900, allocator->kDefault);
112 113
    ASSERT_EQ(underlying_allocator->GetAllocCount(), kZero);
    ASSERT_EQ(underlying_allocator->GetFreeCount(), kZero);
Y
Yu Yang 已提交
114
    auto y = allocator->Allocate(2048, allocator->kDefault);
115 116
    ASSERT_EQ(underlying_allocator->GetAllocCount(), kOne);
    ASSERT_EQ(underlying_allocator->GetFreeCount(), kZero);
Y
Yu Yang 已提交
117
    x = nullptr;
118
    ASSERT_EQ(underlying_allocator->GetFreeCount(), kZero);
Y
Yu Yang 已提交
119
    y = nullptr;
120 121 122 123 124
    ASSERT_EQ(underlying_allocator->GetFreeCount(), kZero);
  }

  {
    underlying_allocator->ResetCounter();
Y
Yu Yang 已提交
125
    allocator->ClearCache();
126 127 128 129 130 131 132
    ASSERT_EQ(underlying_allocator->GetAllocCount(), kZero);
    ASSERT_EQ(underlying_allocator->GetFreeCount(), kTwo);
  }
}

TEST(buffered_allocator, garbage_collection) {
  std::unique_ptr<CPUAllocator> cpu_allocator(new CPUAllocator());
Y
Yu Yang 已提交
133
  auto chunk = cpu_allocator->Allocate(2048, cpu_allocator->kDefault);
134
  auto allocator = GetBufferedAllocator(chunk.get(), false);
Y
Yu Yang 已提交
135 136 137 138 139
  auto x1 = allocator->Allocate(1600, allocator->kDefault);
  auto x2 = allocator->Allocate(400, allocator->kDefault);
  x1 = nullptr;
  x2 = nullptr;
  auto x3 = allocator->Allocate(1600, allocator->kDefault);
140 141 142 143 144 145 146
  ASSERT_NE(x3, nullptr);
  ASSERT_NE(x3->ptr(), nullptr);
}

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