device_context_test.cu 3.9 KB
Newer Older
Q
QI JUN 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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. */

#include "gtest/gtest.h"
D
dzhwinter 已提交
16 17 18
#include "paddle/platform/device_context.h"

#include "glog/logging.h"
Q
QI JUN 已提交
19

Q
qijun 已提交
20
TEST(Device, Init) {
Y
Yi Wang 已提交
21 22 23 24
  using paddle::platform::DeviceContext;
  using paddle::platform::CUDADeviceContext;
  using paddle::platform::GPUPlace;

25
  int count = paddle::platform::GetCUDADeviceCount();
Q
QI JUN 已提交
26
  for (int i = 0; i < count; i++) {
Q
QI JUN 已提交
27 28
    CUDADeviceContext* device_context = new CUDADeviceContext(GPUPlace(i));
    Eigen::GpuDevice* gpu_device = device_context->eigen_device();
Q
qijun 已提交
29
    ASSERT_NE(nullptr, gpu_device);
Q
qijun 已提交
30
    delete device_context;
Q
QI JUN 已提交
31
  }
Q
qijun 已提交
32 33 34
}

TEST(Device, CUDADeviceContext) {
Y
Yi Wang 已提交
35 36 37
  using paddle::platform::CUDADeviceContext;
  using paddle::platform::GPUPlace;

38
  int count = paddle::platform::GetCUDADeviceCount();
Q
qijun 已提交
39
  for (int i = 0; i < count; i++) {
Y
Yi Wang 已提交
40
    CUDADeviceContext* device_context = new CUDADeviceContext(GPUPlace(i));
Q
qijun 已提交
41 42
    Eigen::GpuDevice* gpu_device = device_context->eigen_device();
    ASSERT_NE(nullptr, gpu_device);
Q
qijun 已提交
43 44 45 46
    cudnnHandle_t cudnn_handle = device_context->cudnn_handle();
    ASSERT_NE(nullptr, cudnn_handle);
    cublasHandle_t cublas_handle = device_context->cublas_handle();
    ASSERT_NE(nullptr, cublas_handle);
Q
qijun 已提交
47
    ASSERT_NE(nullptr, device_context->stream());
Q
qijun 已提交
48 49 50
    delete device_context;
  }
}
D
dzhwinter 已提交
51

52 53 54
TEST(Device, CUDNNDeviceContext) {
  using paddle::platform::CUDNNDeviceContext;
  using paddle::platform::CUDNNPlace;
D
dzhwinter 已提交
55 56 57
  if (paddle::platform::dynload::HasCUDNN()) {
    int count = paddle::platform::GetCUDADeviceCount();
    for (int i = 0; i < count; ++i) {
58 59
      CUDNNDeviceContext* device_context =
          new CUDNNDeviceContext(CUDNNPlace(i));
D
dzhwinter 已提交
60 61 62 63 64 65 66
      cudnnHandle_t cudnn_handle = device_context->cudnn_handle();
      ASSERT_NE(nullptr, cudnn_handle);
      ASSERT_NE(nullptr, device_context->stream());
      delete device_context;
    }
  }
}
D
dzhwinter 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

TEST(Device, DeviceContextPool) {
  using paddle::platform::DeviceContextPool;
  using paddle::platform::CUDADeviceContext;
  using paddle::platform::Place;
  using paddle::platform::CPUPlace;
  using paddle::platform::GPUPlace;

  DeviceContextPool& pool = DeviceContextPool::Get();
  auto cpu_dev_ctx1 = pool.Borrow(CPUPlace());
  auto cpu_dev_ctx2 = pool.Borrow(CPUPlace());
  EXPECT_TRUE(cpu_dev_ctx2 == cpu_dev_ctx1);

  std::vector<Place> gpu_places;
  int count = paddle::platform::GetCUDADeviceCount();
  for (int i = 0; i < count; ++i) {
    gpu_places.emplace_back(GPUPlace(i));
  }
  auto dev_ctxs = pool.Borrow(gpu_places);
  for (size_t i = 0; i < dev_ctxs.size(); ++i) {
    auto* dev_ctx = static_cast<const CUDADeviceContext*>(dev_ctxs[i]);

    // check same as GPUPlace(i)
    GPUPlace place = boost::get<GPUPlace>(dev_ctx->GetPlace());
    EXPECT_EQ(place.GetDeviceId(), static_cast<int>(i));
  }
}

int main(int argc, char** argv) {
  int dev_count = paddle::platform::GetCUDADeviceCount();
  if (dev_count <= 1) {
    LOG(WARNING) << "Cannot test multi-gpu DeviceContextPool, because the CUDA "
                    "device count is "
                 << dev_count;
    return 0;
  }

  std::vector<paddle::platform::Place> places;

  places.emplace_back(paddle::platform::CPUPlace());
  int count = paddle::platform::GetCUDADeviceCount();
  for (int i = 0; i < count; ++i) {
    places.emplace_back(paddle::platform::GPUPlace(i));
  }

  VLOG(0) << " DeviceCount " << count;
  paddle::platform::DeviceContextPool::Create(places);

  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}