device_context_test.cu 3.4 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

Y
Yang Yu 已提交
74 75 76 77
  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 已提交
78 79 80 81

  std::vector<Place> gpu_places;
  int count = paddle::platform::GetCUDADeviceCount();
  for (int i = 0; i < count; ++i) {
Y
Yang Yu 已提交
82 83
    auto dev_ctx = pool.Get(CUDAPlace(i));
    ASSERT_NE(dev_ctx, nullptr);
D
dzhwinter 已提交
84 85 86 87 88 89 90 91 92
  }
}

int main(int argc, char** argv) {
  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 已提交
93
    places.emplace_back(paddle::platform::CUDAPlace(i));
D
dzhwinter 已提交
94 95 96
  }

  VLOG(0) << " DeviceCount " << count;
Y
Yang Yu 已提交
97
  paddle::platform::DeviceContextPool::Init(places);
D
dzhwinter 已提交
98 99 100 101

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