system_allocator_test.cc 1.8 KB
Newer Older
L
liaogang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "paddle/memory/detail/system_allocator.h"
Y
Yi Wang 已提交
16 17 18 19

#include <memory>
#include <vector>

20
#include "gflags/gflags.h"
L
liaogang 已提交
21 22
#include "gtest/gtest.h"

23
DECLARE_bool(use_pinned_memory);
24

25
void TestAllocator(paddle::memory::detail::SystemAllocator& a, size_t size) {
26 27
  bool freed = false;
  {
28
    void* p = a.Alloc(size);
29 30 31 32 33
    if (size > 0) {
      EXPECT_NE(p, nullptr);
    } else {
      EXPECT_EQ(p, nullptr);
    }
34

35
    int* i = static_cast<int*>(p);
36
    std::shared_ptr<int> ptr(i, [&](void* p) {
37
      freed = true;
38
      a.Free(p, size);
39 40 41
    });
  }
  EXPECT_TRUE(freed);
42 43
}

Y
Yi Wang 已提交
44
TEST(CPUAllocator, NoLockMem) {
45 46
  FLAGS_use_pinned_memory = false;
  paddle::memory::detail::CPUAllocator a;
47 48
  TestAllocator(a, 2048);
  TestAllocator(a, 0);
Y
Yi Wang 已提交
49
}
50

51
TEST(CPUAllocator, LockMem) {
52 53
  FLAGS_use_pinned_memory = true;
  paddle::memory::detail::CPUAllocator a;
54 55
  TestAllocator(a, 2048);
  TestAllocator(a, 0);
56 57 58
}

#ifndef PADDLE_ONLY_CPU
Y
Yi Wang 已提交
59
TEST(GPUAllocator, NoStaging) {
60 61
  FLAGS_use_pinned_memory = false;
  paddle::memory::detail::GPUAllocator a;
62 63
  TestAllocator(a, 2048);
  TestAllocator(a, 0);
L
liaogang 已提交
64 65
}
TEST(GPUAllocator, Staging) {
66 67
  FLAGS_use_pinned_memory = true;
  paddle::memory::detail::GPUAllocator a;
68 69
  TestAllocator(a, 2048);
  TestAllocator(a, 0);
L
liaogang 已提交
70
}
71
#endif  // PADDLE_ONLY_CPU