system_allocator_test.cc 1.4 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>

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

Y
Yi Wang 已提交
22 23 24 25 26 27 28 29 30 31 32 33
template <typename Allocator>
void TestAllocator() {
  {
    auto d = Allocator::Alloc(sizeof(int));
    EXPECT_NE(d.Ptr(), nullptr);
    std::unique_ptr<int> p(static_cast<int*>(d.Ptr()), d);
  }
  {
    auto d = Allocator::Alloc(0);
    EXPECT_EQ(d.Ptr(), nullptr);
    std::unique_ptr<int> p(static_cast<int*>(d.Ptr()), d);
  }
34 35
}

Y
Yi Wang 已提交
36 37 38
TEST(CPUAllocator, NoLockMem) {
  TestAllocator<paddle::memory::detail::CPUAllocator<false>>();
}
39
TEST(CPUAllocator, LockMem) {
Y
Yi Wang 已提交
40
  TestAllocator<paddle::memory::detail::CPUAllocator<true>>();
41 42 43
}

#ifndef PADDLE_ONLY_CPU
Y
Yi Wang 已提交
44 45
TEST(GPUAllocator, NoStaging) {
  TestAllocator<paddle::memory::detail::GPUAllocator<false>>();
L
liaogang 已提交
46 47
}
TEST(GPUAllocator, Staging) {
Y
Yi Wang 已提交
48
  TestAllocator<paddle::memory::detail::GPUAllocator<true>>();
L
liaogang 已提交
49
}
50
#endif  // PADDLE_ONLY_CPU