memory_test.cc 4.1 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
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 62
  for (auto size :
       {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 120

  for (auto size :
       {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
  }
}

L
Luo Tao 已提交
144
#endif