memory_test.cc 3.9 KB
Newer Older
L
liaogang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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/memory/memory.h"
16 17 18 19 20
#include "paddle/memory/detail/memory_block.h"
#include "paddle/memory/detail/meta_data.h"

#include "paddle/platform/cpu_info.h"
#include "paddle/platform/gpu_info.h"
L
liaogang 已提交
21 22
#include "paddle/platform/place.h"

23 24 25
#include <gtest/gtest.h>
#include <unordered_map>

L
update  
liaogang 已提交
26
inline bool is_aligned(void const *p) {
L
liaogang 已提交
27
  return 0 == (reinterpret_cast<uintptr_t>(p) & 0x3);
L
liaogang 已提交
28
}
L
liaogang 已提交
29

30 31 32 33 34 35 36
size_t align(size_t size, paddle::platform::CPUPlace place) {
  size += sizeof(paddle::memory::detail::Metadata);
  size_t alignment = paddle::platform::CpuMinChunkSize();
  size_t remaining = size % alignment;
  return remaining == 0 ? size : size + (alignment - remaining);
}

L
liaogang 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49
TEST(BuddyAllocator, CPUAllocation) {
  void *p = nullptr;

  EXPECT_EQ(p, nullptr);

  paddle::platform::CPUPlace cpu;
  p = paddle::memory::Alloc(cpu, 4096);

  EXPECT_NE(p, nullptr);

  paddle::memory::Free(cpu, p);
}

L
liaogang 已提交
50 51 52
TEST(BuddyAllocator, CPUMultAlloc) {
  paddle::platform::CPUPlace cpu;

53 54 55 56
  std::unordered_map<void *, size_t> ps;

  size_t total_size = paddle::memory::Used(cpu);
  EXPECT_EQ(total_size, 0UL);
L
liaogang 已提交
57

L
liaogang 已提交
58 59
  for (auto size :
       {128, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304}) {
60 61 62 63 64 65 66 67
    ps[paddle::memory::Alloc(cpu, size)] = size;

    // Buddy Allocator doesn't manage too large memory chunk
    if (paddle::memory::Used(cpu) == total_size) continue;

    size_t aligned_size = align(size, cpu);
    total_size += aligned_size;
    EXPECT_EQ(total_size, paddle::memory::Used(cpu));
L
liaogang 已提交
68 69 70
  }

  for (auto p : ps) {
L
update  
liaogang 已提交
71
    EXPECT_EQ(is_aligned(p.first), true);
72 73 74 75 76 77 78 79
    paddle::memory::Free(cpu, p.first);

    // Buddy Allocator doesn't manage too large memory chunk
    if (paddle::memory::Used(cpu) == total_size) continue;

    size_t aligned_size = align(p.second, cpu);
    total_size -= aligned_size;
    EXPECT_EQ(total_size, paddle::memory::Used(cpu));
L
liaogang 已提交
80 81 82
  }
}

83
#ifdef PADDLE_WITH_GPU
L
liaogang 已提交
84

L
liaogang 已提交
85 86 87 88 89 90 91
size_t align(size_t size, paddle::platform::GPUPlace place) {
  size += sizeof(paddle::memory::detail::Metadata);
  size_t alignment = paddle::platform::GpuMinChunkSize();
  size_t remaining = size % alignment;
  return remaining == 0 ? size : size + (alignment - remaining);
}

L
liaogang 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104
TEST(BuddyAllocator, GPUAllocation) {
  void *p = nullptr;

  EXPECT_EQ(p, nullptr);

  paddle::platform::GPUPlace gpu(0);
  p = paddle::memory::Alloc(gpu, 4096);

  EXPECT_NE(p, nullptr);

  paddle::memory::Free(gpu, p);
}

L
liaogang 已提交
105 106 107
TEST(BuddyAllocator, GPUMultAlloc) {
  paddle::platform::GPUPlace gpu;

108 109 110 111
  std::unordered_map<void *, size_t> ps;

  size_t total_size = paddle::memory::Used(gpu);
  EXPECT_EQ(total_size, 0UL);
L
liaogang 已提交
112 113 114

  for (auto size :
       {128, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304}) {
115 116 117 118 119 120 121 122
    ps[paddle::memory::Alloc(gpu, size)] = size;

    // Buddy Allocator doesn't manage too large memory chunk
    if (paddle::memory::Used(gpu) == total_size) continue;

    size_t aligned_size = align(size, gpu);
    total_size += aligned_size;
    EXPECT_EQ(total_size, paddle::memory::Used(gpu));
L
liaogang 已提交
123 124 125
  }

  for (auto p : ps) {
L
update  
liaogang 已提交
126
    EXPECT_EQ(is_aligned(p.first), true);
127 128 129 130 131 132 133 134
    paddle::memory::Free(gpu, p.first);

    // Buddy Allocator doesn't manage too large memory chunk
    if (paddle::memory::Used(gpu) == total_size) continue;

    size_t aligned_size = align(p.second, gpu);
    total_size -= aligned_size;
    EXPECT_EQ(total_size, paddle::memory::Used(gpu));
L
liaogang 已提交
135 136 137
  }
}

L
liaogang 已提交
138
#endif  // PADDLE_ONLY_CPU