device_context_test.cu 2.7 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. */
Y
Yi Wang 已提交
14
#include "paddle/fluid/platform/device_context.h"
D
dzhwinter 已提交
15

D
dzhwinter 已提交
16
#include <iostream>
17 18
#include <vector>

D
dzhwinter 已提交
19
#include "glog/logging.h"
20
#include "gtest/gtest.h"
Q
QI JUN 已提交
21

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

D
dzhwinter 已提交
27
  VLOG(3) << "before Init";
28
  int count = paddle::platform::GetCUDADeviceCount();
Q
QI JUN 已提交
29
  for (int i = 0; i < count; i++) {
D
dzhwinter 已提交
30
    CUDADeviceContext* device_context = new CUDADeviceContext(CUDAPlace(i));
Q
QI JUN 已提交
31
    Eigen::GpuDevice* gpu_device = device_context->eigen_device();
Q
qijun 已提交
32
    ASSERT_NE(nullptr, gpu_device);
Q
qijun 已提交
33
    delete device_context;
Q
QI JUN 已提交
34
  }
D
dzhwinter 已提交
35
  VLOG(3) << "eigen pass";
Q
qijun 已提交
36 37 38
}

TEST(Device, CUDADeviceContext) {
Y
Yi Wang 已提交
39
  using paddle::platform::CUDADeviceContext;
D
dzhwinter 已提交
40
  using paddle::platform::CUDAPlace;
Y
Yi Wang 已提交
41

D
dzhwinter 已提交
42
  VLOG(3) << "cudnn start";
43
  int count = paddle::platform::GetCUDADeviceCount();
Q
qijun 已提交
44
  for (int i = 0; i < count; i++) {
D
dzhwinter 已提交
45
    CUDADeviceContext* device_context = new CUDADeviceContext(CUDAPlace(i));
D
dzhwinter 已提交
46
    VLOG(3) << "device context start";
Q
qijun 已提交
47 48
    Eigen::GpuDevice* gpu_device = device_context->eigen_device();
    ASSERT_NE(nullptr, gpu_device);
Q
qijun 已提交
49
    cudnnHandle_t cudnn_handle = device_context->cudnn_handle();
D
dzhwinter 已提交
50
    VLOG(3) << "cudnn pass";
Q
qijun 已提交
51 52
    ASSERT_NE(nullptr, cudnn_handle);
    cublasHandle_t cublas_handle = device_context->cublas_handle();
D
dzhwinter 已提交
53
    VLOG(3) << "cublas pass";
Q
qijun 已提交
54
    ASSERT_NE(nullptr, cublas_handle);
Q
qijun 已提交
55
    ASSERT_NE(nullptr, device_context->stream());
Q
qijun 已提交
56 57 58
    delete device_context;
  }
}
D
dzhwinter 已提交
59

D
dzhwinter 已提交
60 61 62 63 64
TEST(Device, DeviceContextPool) {
  using paddle::platform::DeviceContextPool;
  using paddle::platform::CUDADeviceContext;
  using paddle::platform::Place;
  using paddle::platform::CPUPlace;
D
dzhwinter 已提交
65
  using paddle::platform::CUDAPlace;
D
dzhwinter 已提交
66

D
dzhwinter 已提交
67
  VLOG(3) << "before instance";
Y
Yu Yang 已提交
68
  DeviceContextPool& pool = DeviceContextPool::Instance();
D
dzhwinter 已提交
69
  VLOG(3) << "after instance";
Y
Yu Yang 已提交
70 71 72
  auto cpu_dev_ctx1 = pool.Get(CPUPlace());
  auto cpu_dev_ctx2 = pool.Get(CPUPlace());
  ASSERT_EQ(cpu_dev_ctx2, cpu_dev_ctx1);
D
dzhwinter 已提交
73 74 75 76

  std::vector<Place> gpu_places;
  int count = paddle::platform::GetCUDADeviceCount();
  for (int i = 0; i < count; ++i) {
Y
Yu Yang 已提交
77 78
    auto dev_ctx = pool.Get(CUDAPlace(i));
    ASSERT_NE(dev_ctx, nullptr);
D
dzhwinter 已提交
79 80
  }
}