malloc_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
Update  
Yi Wang 已提交
15
#include "paddle/fluid/memory/malloc.h"
Y
Yi Wang 已提交
16 17 18 19

#include <unordered_map>

#include "gtest/gtest.h"
Y
Yi Wang 已提交
20 21 22 23
#include "paddle/fluid/memory/detail/memory_block.h"
#include "paddle/fluid/platform/cpu_info.h"
#include "paddle/fluid/platform/gpu_info.h"
#include "paddle/fluid/platform/place.h"
L
liaogang 已提交
24

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

29
size_t align(size_t size, paddle::platform::CPUPlace place) {
Y
Update  
Yi Wang 已提交
30
  size += sizeof(paddle::memory::detail::MemoryBlock::Desc);
31 32 33 34 35
  size_t alignment = paddle::platform::CpuMinChunkSize();
  size_t remaining = size % alignment;
  return remaining == 0 ? size : size + (alignment - remaining);
}

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

  EXPECT_EQ(p, nullptr);

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

  EXPECT_NE(p, nullptr);

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

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

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

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

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

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

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

85
#ifdef PADDLE_WITH_CUDA
L
liaogang 已提交
86

D
dzhwinter 已提交
87
size_t align(size_t size, paddle::platform::CUDAPlace place) {
Y
Update  
Yi Wang 已提交
88
  size += sizeof(paddle::memory::detail::MemoryBlock::Desc);
L
liaogang 已提交
89 90 91 92 93
  size_t alignment = paddle::platform::GpuMinChunkSize();
  size_t remaining = size % alignment;
  return remaining == 0 ? size : size + (alignment - remaining);
}

L
liaogang 已提交
94 95 96 97 98
TEST(BuddyAllocator, GPUAllocation) {
  void *p = nullptr;

  EXPECT_EQ(p, nullptr);

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

  EXPECT_NE(p, nullptr);

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

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

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

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

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

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

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

C
chengduoZH 已提交
143
size_t align(size_t size, paddle::platform::CUDAPinnedPlace place) {
Y
Update  
Yi Wang 已提交
144
  size += sizeof(paddle::memory::detail::MemoryBlock::Desc);
145
  size_t alignment = paddle::platform::CUDAPinnedMinChunkSize();
C
chengduoZH 已提交
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
  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 已提交
198
#endif