allocator_facade_test.cc 2.6 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// 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/fluid/memory/allocation/allocator_facade.h"
#include <gflags/gflags.h>
#include <gtest/gtest.h>

S
sneaxiy 已提交
19
#ifdef PADDLE_WITH_CUDA
S
sneaxiy 已提交
20
DECLARE_double(fraction_of_gpu_memory_to_use);
S
sneaxiy 已提交
21 22 23
DECLARE_double(fraction_of_cuda_pinned_memory_to_use);
DECLARE_int64(gpu_allocator_retry_time);
#endif
S
sneaxiy 已提交
24 25 26 27 28 29

namespace paddle {
namespace memory {
namespace allocation {

TEST(allocator, allocator) {
S
sneaxiy 已提交
30
#ifdef PADDLE_WITH_CUDA
S
sneaxiy 已提交
31 32
  FLAGS_fraction_of_gpu_memory_to_use = 0.01;
  FLAGS_gpu_allocator_retry_time = 500;
S
sneaxiy 已提交
33 34
  FLAGS_fraction_of_cuda_pinned_memory_to_use = 0.5;
#endif
S
sneaxiy 已提交
35 36

  auto &instance = AllocatorFacade::Instance();
S
sneaxiy 已提交
37 38
  platform::Place place;
  size_t size = 1024;
S
sneaxiy 已提交
39 40

  {
S
sneaxiy 已提交
41 42 43
    place = platform::CPUPlace();
    size = 1024;
    auto cpu_allocation = instance.Alloc(place, size);
S
sneaxiy 已提交
44
    ASSERT_NE(cpu_allocation, nullptr);
S
sneaxiy 已提交
45 46 47
    ASSERT_NE(cpu_allocation->ptr(), nullptr);
    ASSERT_EQ(cpu_allocation->place(), place);
    ASSERT_EQ(cpu_allocation->size(), size);
S
sneaxiy 已提交
48 49
  }

S
sneaxiy 已提交
50
#ifdef PADDLE_WITH_CUDA
S
sneaxiy 已提交
51
  {
S
sneaxiy 已提交
52 53 54
    place = platform::CUDAPlace(0);
    size = 1024;
    auto gpu_allocation = instance.Alloc(place, size);
S
sneaxiy 已提交
55
    ASSERT_NE(gpu_allocation, nullptr);
S
sneaxiy 已提交
56 57 58
    ASSERT_NE(gpu_allocation->ptr(), nullptr);
    ASSERT_EQ(gpu_allocation->place(), place);
    ASSERT_GE(gpu_allocation->size(), size);
S
sneaxiy 已提交
59 60 61 62
  }

  {
    // Allocate 2GB gpu memory
S
sneaxiy 已提交
63 64 65
    place = platform::CUDAPlace(0);
    size = 2 * static_cast<size_t>(1 << 30);
    auto gpu_allocation = instance.Alloc(place, size);
S
sneaxiy 已提交
66
    ASSERT_NE(gpu_allocation, nullptr);
S
sneaxiy 已提交
67 68 69
    ASSERT_NE(gpu_allocation->ptr(), nullptr);
    ASSERT_EQ(gpu_allocation->place(), place);
    ASSERT_GE(gpu_allocation->size(), size);
S
sneaxiy 已提交
70 71
  }

S
sneaxiy 已提交
72 73 74 75 76 77 78 79 80 81 82
  {
    place = platform::CUDAPinnedPlace();
    size = (1 << 20);
    auto cuda_pinned_allocation =
        instance.Alloc(platform::CUDAPinnedPlace(), 1 << 20);
    ASSERT_NE(cuda_pinned_allocation, nullptr);
    ASSERT_NE(cuda_pinned_allocation->ptr(), nullptr);
    ASSERT_EQ(cuda_pinned_allocation->place(), place);
    ASSERT_GE(cuda_pinned_allocation->size(), size);
  }
#endif
S
sneaxiy 已提交
83 84 85 86 87
}

}  // namespace allocation
}  // namespace memory
}  // namespace paddle