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

16 17
#include <vector>

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

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

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

TEST(Device, CUDADeviceContext) {
Y
Yi Wang 已提交
36
  using paddle::platform::CUDADeviceContext;
D
dzhwinter 已提交
37
  using paddle::platform::CUDAPlace;
Y
Yi Wang 已提交
38

39
  int count = paddle::platform::GetCUDADeviceCount();
Q
qijun 已提交
40
  for (int i = 0; i < count; i++) {
D
dzhwinter 已提交
41
    CUDADeviceContext* device_context = new CUDADeviceContext(CUDAPlace(i));
Q
qijun 已提交
42 43
    Eigen::GpuDevice* gpu_device = device_context->eigen_device();
    ASSERT_NE(nullptr, gpu_device);
Q
qijun 已提交
44 45 46 47
    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 已提交
48
    ASSERT_NE(nullptr, device_context->stream());
Q
qijun 已提交
49 50 51
    delete device_context;
  }
}
D
dzhwinter 已提交
52

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

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

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