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
  using paddle::platform::DeviceContext;
  using paddle::platform::CUDADeviceContext;
D
dzhwinter 已提交
23
  using paddle::platform::CUDAPlace;
Y
Yi Wang 已提交
24

25
  int count = paddle::platform::GetCUDADeviceCount();
Q
QI JUN 已提交
26
  for (int i = 0; i < count; i++) {
D
dzhwinter 已提交
27
    CUDADeviceContext* device_context = new CUDADeviceContext(CUDAPlace(i));
Q
QI JUN 已提交
28
    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
  using paddle::platform::CUDADeviceContext;
D
dzhwinter 已提交
36
  using paddle::platform::CUDAPlace;
Y
Yi Wang 已提交
37

38
  int count = paddle::platform::GetCUDADeviceCount();
Q
qijun 已提交
39
  for (int i = 0; i < count; i++) {
D
dzhwinter 已提交
40
    CUDADeviceContext* device_context = new CUDADeviceContext(CUDAPlace(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
TEST(Device, CUDNNDeviceContext) {
  using paddle::platform::CUDNNDeviceContext;
Q
QI JUN 已提交
54
  using paddle::platform::CUDAPlace;
D
dzhwinter 已提交
55 56 57
  if (paddle::platform::dynload::HasCUDNN()) {
    int count = paddle::platform::GetCUDADeviceCount();
    for (int i = 0; i < count; ++i) {
Q
QI JUN 已提交
58
      CUDNNDeviceContext* device_context = new CUDNNDeviceContext(CUDAPlace(i));
D
dzhwinter 已提交
59 60 61 62 63 64 65
      cudnnHandle_t cudnn_handle = device_context->cudnn_handle();
      ASSERT_NE(nullptr, cudnn_handle);
      ASSERT_NE(nullptr, device_context->stream());
      delete device_context;
    }
  }
}
D
dzhwinter 已提交
66 67 68 69 70 71

TEST(Device, DeviceContextPool) {
  using paddle::platform::DeviceContextPool;
  using paddle::platform::CUDADeviceContext;
  using paddle::platform::Place;
  using paddle::platform::CPUPlace;
D
dzhwinter 已提交
72
  using paddle::platform::CUDAPlace;
D
dzhwinter 已提交
73 74 75 76 77 78 79 80 81

  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) {
D
dzhwinter 已提交
82
    gpu_places.emplace_back(CUDAPlace(i));
D
dzhwinter 已提交
83 84 85 86 87
  }
  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]);

D
dzhwinter 已提交
88 89
    // check same as CUDAPlace(i)
    CUDAPlace place = boost::get<CUDAPlace>(dev_ctx->GetPlace());
D
dzhwinter 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    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) {
D
dzhwinter 已提交
108
    places.emplace_back(paddle::platform::CUDAPlace(i));
D
dzhwinter 已提交
109 110 111 112 113 114 115 116
  }

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

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