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

D
dzhwinter 已提交
52 53 54 55 56
TEST(Device, DeviceContextPool) {
  using paddle::platform::DeviceContextPool;
  using paddle::platform::CUDADeviceContext;
  using paddle::platform::Place;
  using paddle::platform::CPUPlace;
D
dzhwinter 已提交
57
  using paddle::platform::CUDAPlace;
D
dzhwinter 已提交
58

Y
Yu Yang 已提交
59 60 61 62
  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 已提交
63 64 65 66

  std::vector<Place> gpu_places;
  int count = paddle::platform::GetCUDADeviceCount();
  for (int i = 0; i < count; ++i) {
Y
Yu Yang 已提交
67 68
    auto dev_ctx = pool.Get(CUDAPlace(i));
    ASSERT_NE(dev_ctx, nullptr);
D
dzhwinter 已提交
69 70 71 72 73 74 75 76 77
  }
}

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 已提交
78
    places.emplace_back(paddle::platform::CUDAPlace(i));
D
dzhwinter 已提交
79 80 81
  }

  VLOG(0) << " DeviceCount " << count;
Y
Yu Yang 已提交
82
  paddle::platform::DeviceContextPool::Init(places);
D
dzhwinter 已提交
83 84 85 86

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