system_allocator_test.cc 1.7 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 "glog/logging.h"
L
liaogang 已提交
21 22
#include "gtest/gtest.h"

Y
Yi Wang 已提交
23
template <typename Allocator>
24 25 26 27 28 29 30
void TestAllocator(void* p) {
  p = Allocator::Alloc(1024);

  int* i = static_cast<int*>(p);
  std::shared_ptr<int> ptr(i, [](int* p) { Allocator::Free(p, 1024); });

  EXPECT_NE(p, nullptr);
31 32
}

Y
Yi Wang 已提交
33
TEST(CPUAllocator, NoLockMem) {
34 35 36 37
  void* p = nullptr;
  FLAGS_uses_pinned_memory = false;
  TestAllocator<paddle::memory::detail::CPUAllocator>(p);
  EXPECT_EQ(p, nullptr);
Y
Yi Wang 已提交
38
}
39

40
TEST(CPUAllocator, LockMem) {
41 42 43 44
  void* p = nullptr;
  FLAGS_uses_pinned_memory = true;
  TestAllocator<paddle::memory::detail::CPUAllocator>(p);
  EXPECT_EQ(p, nullptr);
45 46 47
}

#ifndef PADDLE_ONLY_CPU
Y
Yi Wang 已提交
48
TEST(GPUAllocator, NoStaging) {
49 50 51 52
  void* p = nullptr;
  FLAGS_uses_pinned_memory = false;
  TestAllocator<paddle::memory::detail::GPUAllocator>(p);
  EXPECT_EQ(p, nullptr);
L
liaogang 已提交
53 54
}
TEST(GPUAllocator, Staging) {
55 56 57 58
  void* p = nullptr;
  FLAGS_uses_pinned_memory = true;
  TestAllocator<paddle::memory::detail::GPUAllocator>(p);
  EXPECT_EQ(p, nullptr);
L
liaogang 已提交
59
}
60
#endif  // PADDLE_ONLY_CPU