device_context_test.cu 6.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
QI JUN 已提交
2 3 4 5 6 7 8 9 10 11 12 13

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. */
14 15
#include <vector>

D
dzhwinter 已提交
16
#include "glog/logging.h"
17
#include "gtest/gtest.h"
W
Wilber 已提交
18
#include "paddle/fluid/memory/allocation/allocator_facade.h"
19
#include "paddle/fluid/platform/device_context.h"
20
#include "paddle/phi/core/dense_tensor.h"
Q
QI JUN 已提交
21

Q
qijun 已提交
22
TEST(Device, Init) {
D
dzhwinter 已提交
23
  using paddle::platform::CUDAPlace;
24
  using paddle::platform::DeviceContext;
L
Leo Chen 已提交
25
  using phi::GPUContext;
Y
Yi Wang 已提交
26

27
  int count = paddle::platform::GetGPUDeviceCount();
Q
QI JUN 已提交
28
  for (int i = 0; i < count; i++) {
L
Leo Chen 已提交
29
    phi::GPUContext* device_context = new phi::GPUContext(CUDAPlace(i));
W
Wilber 已提交
30 31 32 33 34 35 36 37 38 39 40 41
    device_context->SetAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
            .GetAllocator(CUDAPlace(i), device_context->stream())
            .get());
    device_context->SetHostAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
            .GetAllocator(paddle::platform::CPUPlace())
            .get());
    device_context->SetZeroAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
            .GetZeroAllocator(CUDAPlace(i))
            .get());
42 43 44 45
    device_context->SetHostZeroAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
            .GetZeroAllocator(paddle::platform::CPUPlace())
            .get());
W
wanghuancoder 已提交
46 47 48 49
    device_context->SetPinnedAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
            .GetAllocator(paddle::platform::CUDAPinnedPlace())
            .get());
W
Wilber 已提交
50 51
    device_context->PartialInitWithAllocator();

Q
QI JUN 已提交
52
    Eigen::GpuDevice* gpu_device = device_context->eigen_device();
Q
qijun 已提交
53
    ASSERT_NE(nullptr, gpu_device);
Q
qijun 已提交
54
    delete device_context;
Q
QI JUN 已提交
55
  }
Q
qijun 已提交
56 57
}

L
Leo Chen 已提交
58
TEST(Device, GPUContext) {
D
dzhwinter 已提交
59
  using paddle::platform::CUDAPlace;
L
Leo Chen 已提交
60
  using phi::GPUContext;
Y
Yi Wang 已提交
61

62
  int count = paddle::platform::GetGPUDeviceCount();
Q
qijun 已提交
63
  for (int i = 0; i < count; i++) {
L
Leo Chen 已提交
64
    phi::GPUContext* device_context = new phi::GPUContext(CUDAPlace(i));
W
Wilber 已提交
65 66 67 68 69 70 71 72 73 74 75 76
    device_context->SetAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
            .GetAllocator(CUDAPlace(i), device_context->stream())
            .get());
    device_context->SetHostAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
            .GetAllocator(paddle::platform::CPUPlace())
            .get());
    device_context->SetZeroAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
            .GetZeroAllocator(CUDAPlace(i))
            .get());
77 78 79 80
    device_context->SetHostZeroAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
            .GetZeroAllocator(paddle::platform::CPUPlace())
            .get());
W
wanghuancoder 已提交
81 82 83 84
    device_context->SetPinnedAllocator(
        paddle::memory::allocation::AllocatorFacade::Instance()
            .GetAllocator(paddle::platform::CUDAPinnedPlace())
            .get());
W
Wilber 已提交
85
    device_context->PartialInitWithAllocator();
Q
qijun 已提交
86 87
    Eigen::GpuDevice* gpu_device = device_context->eigen_device();
    ASSERT_NE(nullptr, gpu_device);
88 89 90
#ifdef PADDLE_WITH_HIP
    miopenHandle_t cudnn_handle = device_context->cudnn_handle();
#else
Q
qijun 已提交
91
    cudnnHandle_t cudnn_handle = device_context->cudnn_handle();
92
#endif
Q
qijun 已提交
93
    ASSERT_NE(nullptr, cudnn_handle);
94 95 96
#ifdef PADDLE_WITH_HIP
    rocblas_handle cublas_handle = device_context->cublas_handle();
#else
97
    cublasHandle_t cublas_handle = device_context->cublas_handle();
98
#endif
99
    ASSERT_NE(nullptr, cublas_handle);
Q
qijun 已提交
100 101 102
    delete device_context;
  }
}
D
dzhwinter 已提交
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
TEST(Device, HostZeroAllocator) {
  using paddle::platform::CUDAPlace;

  auto device_context = std::make_unique<phi::GPUContext>(CUDAPlace(0));
  device_context->SetAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(CUDAPlace(0), device_context->stream())
          .get());
  device_context->SetHostAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CPUPlace())
          .get());
  device_context->SetZeroAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetZeroAllocator(CUDAPlace(0))
          .get());
  device_context->SetHostZeroAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetZeroAllocator(paddle::platform::CPUPlace())
          .get());
  device_context->SetPinnedAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CUDAPinnedPlace())
          .get());
  device_context->PartialInitWithAllocator();

  phi::DenseTensor tensor;
  tensor.Resize({0});
  device_context->HostAlloc<float>(&tensor);
  ASSERT_EQ(tensor.place().GetType(), phi::AllocationType::CPU);
  ASSERT_EQ(tensor.numel(), 0);
  ASSERT_EQ(tensor.dtype(), phi::DataType::FLOAT32);

  phi::GPUContext gpu_context(CUDAPlace(0));
  gpu_context.SetHostZeroAllocator(&device_context->GetHostZeroAllocator());
  gpu_context.HostAlloc<float>(&tensor);
  ASSERT_EQ(tensor.place().GetType(), phi::AllocationType::CPU);
}

D
dzhwinter 已提交
143 144
TEST(Device, DeviceContextPool) {
  using paddle::platform::CPUPlace;
D
dzhwinter 已提交
145
  using paddle::platform::CUDAPlace;
146 147
  using paddle::platform::DeviceContextPool;
  using paddle::platform::Place;
L
Leo Chen 已提交
148
  using phi::GPUContext;
D
dzhwinter 已提交
149

Y
Yu Yang 已提交
150 151 152 153
  DeviceContextPool& pool = DeviceContextPool::Instance();
  auto cpu_dev_ctx1 = pool.Get(CPUPlace());
  auto cpu_dev_ctx2 = pool.Get(CPUPlace());
  ASSERT_EQ(cpu_dev_ctx2, cpu_dev_ctx1);
D
dzhwinter 已提交
154 155

  std::vector<Place> gpu_places;
156
  int count = paddle::platform::GetGPUDeviceCount();
D
dzhwinter 已提交
157
  for (int i = 0; i < count; ++i) {
Y
Yu Yang 已提交
158 159
    auto dev_ctx = pool.Get(CUDAPlace(i));
    ASSERT_NE(dev_ctx, nullptr);
D
dzhwinter 已提交
160 161
  }
}