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 26
#include <gtest/gtest.h>
#include <unordered_map>

inline bool is_aligned(void const *p, const size_t n) {
L
liaogang 已提交
27 28
  return 0 == (reinterpret_cast<uintptr_t>(p) % n);
}
L
liaogang 已提交
29

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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);
}

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);
}

void update_size(size_t &total_size, const size_t size) {}

L
liaogang 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
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 已提交
59 60 61
TEST(BuddyAllocator, CPUMultAlloc) {
  paddle::platform::CPUPlace cpu;

62 63 64 65
  std::unordered_map<void *, size_t> ps;

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

L
liaogang 已提交
67 68
  for (auto size :
       {128, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304}) {
69 70 71 72 73 74 75 76
    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 已提交
77 78 79
  }

  for (auto p : ps) {
80 81 82 83 84 85 86 87 88
    EXPECT_EQ(is_aligned(p.first, 32), 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
liaogang 已提交
89 90 91
  }
}

L
liaogang 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#ifndef PADDLE_ONLY_CPU

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 已提交
107 108 109
TEST(BuddyAllocator, GPUMultAlloc) {
  paddle::platform::GPUPlace gpu;

110 111 112 113
  std::unordered_map<void *, size_t> ps;

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

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

  for (auto p : ps) {
128 129 130 131 132 133 134 135 136
    EXPECT_EQ(is_aligned(p.first, 32), true);
    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 已提交
137 138 139
  }
}

L
liaogang 已提交
140
#endif  // PADDLE_ONLY_CPU