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

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. */

Y
Yi Wang 已提交
15 16 17
#include "paddle/fluid/memory/memory.h"
#include "paddle/fluid/memory/detail/memory_block.h"
#include "paddle/fluid/memory/detail/meta_data.h"
18

Y
Yi Wang 已提交
19 20 21
#include "paddle/fluid/platform/cpu_info.h"
#include "paddle/fluid/platform/gpu_info.h"
#include "paddle/fluid/platform/place.h"
L
liaogang 已提交
22

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
TEST(BuddyAllocator, CPUAllocation) {
  void *p = nullptr;

  EXPECT_EQ(p, nullptr);

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

  EXPECT_NE(p, nullptr);

47 48 49
  paddle::platform::Place place = cpu;
  EXPECT_EQ(paddle::memory::Used(cpu), paddle::memory::memory_usage(place));

L
liaogang 已提交
50 51 52
  paddle::memory::Free(cpu, p);
}

L
liaogang 已提交
53 54 55
TEST(BuddyAllocator, CPUMultAlloc) {
  paddle::platform::CPUPlace cpu;

56 57 58 59
  std::unordered_map<void *, size_t> ps;

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

L
liaogang 已提交
61
  for (auto size :
Q
Qiao Longfei 已提交
62
       {0, 128, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304}) {
63 64 65 66 67 68 69 70
    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 已提交
71 72 73
  }

  for (auto p : ps) {
L
update  
liaogang 已提交
74
    EXPECT_EQ(is_aligned(p.first), true);
75 76 77 78 79 80 81 82
    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 已提交
83 84 85
  }
}

86
#ifdef PADDLE_WITH_CUDA
L
liaogang 已提交
87

D
dzhwinter 已提交
88
size_t align(size_t size, paddle::platform::CUDAPlace place) {
L
liaogang 已提交
89 90 91 92 93 94
  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 已提交
95 96 97 98 99
TEST(BuddyAllocator, GPUAllocation) {
  void *p = nullptr;

  EXPECT_EQ(p, nullptr);

D
dzhwinter 已提交
100
  paddle::platform::CUDAPlace gpu(0);
L
liaogang 已提交
101 102 103 104
  p = paddle::memory::Alloc(gpu, 4096);

  EXPECT_NE(p, nullptr);

105 106 107
  paddle::platform::Place place = gpu;
  EXPECT_EQ(paddle::memory::Used(gpu), paddle::memory::memory_usage(place));

L
liaogang 已提交
108 109 110
  paddle::memory::Free(gpu, p);
}

L
liaogang 已提交
111
TEST(BuddyAllocator, GPUMultAlloc) {
D
dzhwinter 已提交
112
  paddle::platform::CUDAPlace gpu;
L
liaogang 已提交
113

114 115 116 117
  std::unordered_map<void *, size_t> ps;

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

  for (auto size :
Q
Qiao Longfei 已提交
120
       {0, 128, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304}) {
121 122 123 124 125 126 127 128
    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 已提交
129 130 131
  }

  for (auto p : ps) {
L
update  
liaogang 已提交
132
    EXPECT_EQ(is_aligned(p.first), true);
133 134 135 136 137 138 139 140
    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 已提交
141 142 143
  }
}

C
chengduoZH 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
size_t align(size_t size, paddle::platform::CUDAPinnedPlace 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);
}

TEST(BuddyAllocator, CUDAPinnedAllocator) {
  void *p = nullptr;

  EXPECT_EQ(p, nullptr);

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

  EXPECT_NE(p, nullptr);

  paddle::platform::Place place = cpu;
  EXPECT_EQ(paddle::memory::Used(cpu), paddle::memory::memory_usage(place));

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

TEST(BuddyAllocator, CUDAPinnedMultAllocator) {
  paddle::platform::CUDAPinnedPlace cpu;

  std::unordered_map<void *, size_t> ps;

  size_t total_size = paddle::memory::Used(cpu);
  EXPECT_EQ(total_size, 0UL);

  for (auto size :
       {0, 128, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304}) {
    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));
  }

  for (auto p : ps) {
    EXPECT_EQ(is_aligned(p.first), true);
    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
Luo Tao 已提交
199
#endif