allocator_facade_frac_flags_test.cc 2.8 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
DECLARE_double(fraction_of_cuda_pinned_memory_to_use);
22 23
DECLARE_uint64(initial_gpu_memory_in_mb);
DECLARE_uint64(reallocate_gpu_memory_in_mb);
S
sneaxiy 已提交
24 25
DECLARE_int64(gpu_allocator_retry_time);
#endif
S
sneaxiy 已提交
26 27 28 29 30

namespace paddle {
namespace memory {
namespace allocation {

Z
zhhsplendid 已提交
31 32
//! Run allocate test cases for different places
void AllocateTestCases() {
S
sneaxiy 已提交
33
  auto &instance = AllocatorFacade::Instance();
S
sneaxiy 已提交
34 35
  platform::Place place;
  size_t size = 1024;
S
sneaxiy 已提交
36 37

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

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

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

S
sneaxiy 已提交
69 70 71 72 73 74 75 76 77 78 79
  {
    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 已提交
80 81
}

82
TEST(Allocator, Allocator) {
Z
zhhsplendid 已提交
83 84 85 86 87 88 89 90 91
#ifdef PADDLE_WITH_CUDA
  FLAGS_fraction_of_gpu_memory_to_use = 0.01;
  FLAGS_gpu_allocator_retry_time = 500;
  FLAGS_fraction_of_cuda_pinned_memory_to_use = 0.5;
#endif

  AllocateTestCases();
}

S
sneaxiy 已提交
92 93 94
}  // namespace allocation
}  // namespace memory
}  // namespace paddle