mixed_vector_test.cu 2.8 KB
Newer Older
Q
qiaolongfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.

   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. */

15
#ifdef PADDLE_WITH_CUDA
Q
qiaolongfei 已提交
16
#include <cuda_runtime.h>
17 18 19 20 21
#endif
#ifdef PADDLE_WITH_HIP
#include <hip/hip_runtime.h>
#endif

Q
qiaolongfei 已提交
22 23 24 25 26 27 28 29 30
#include <memory>

#include "glog/logging.h"
#include "gtest/gtest.h"
#include "paddle/fluid/framework/mixed_vector.h"
#include "paddle/fluid/platform/gpu_info.h"

template <typename T>
using vec = paddle::framework::Vector<T>;
31
using gpuStream_t = paddle::gpuStream_t;
Q
qiaolongfei 已提交
32 33 34 35 36 37 38

static __global__ void multiply_10(int* ptr) {
  for (int i = 0; i < 10; ++i) {
    ptr[i] *= 10;
  }
}

39
gpuStream_t GetCUDAStream(paddle::platform::CUDAPlace place) {
Q
qiaolongfei 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52
  return reinterpret_cast<const paddle::platform::CUDADeviceContext*>(
             paddle::platform::DeviceContextPool::Instance().Get(place))
      ->stream();
}

TEST(mixed_vector, GPU_VECTOR) {
  vec<int> tmp;
  for (int i = 0; i < 10; ++i) {
    tmp.push_back(i);
  }
  ASSERT_EQ(tmp.size(), 10UL);
  paddle::platform::CUDAPlace gpu(0);

53 54 55 56
#ifdef PADDLE_WITH_HIP
  hipLaunchKernelGGL(multiply_10, dim3(1), dim3(1), 0, GetCUDAStream(gpu),
                     tmp.MutableData(gpu));
#else
Q
qiaolongfei 已提交
57
  multiply_10<<<1, 1, 0, GetCUDAStream(gpu)>>>(tmp.MutableData(gpu));
58
#endif
Q
qiaolongfei 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

  for (int i = 0; i < 10; ++i) {
    ASSERT_EQ(tmp[i], i * 10);
  }
}

TEST(mixed_vector, MultiGPU) {
  if (paddle::platform::GetCUDADeviceCount() < 2) {
    LOG(WARNING) << "Skip mixed_vector.MultiGPU since there are not multiple "
                    "GPUs in your machine.";
    return;
  }

  vec<int> tmp;
  for (int i = 0; i < 10; ++i) {
    tmp.push_back(i);
  }
  ASSERT_EQ(tmp.size(), 10UL);
  paddle::platform::CUDAPlace gpu0(0);
  paddle::platform::SetDeviceId(0);
79 80 81 82 83

#ifdef PADDLE_WITH_HIP
  hipLaunchKernelGGL(multiply_10, dim3(1), dim3(1), 0, GetCUDAStream(gpu0),
                     tmp.MutableData(gpu0));
#else
Q
qiaolongfei 已提交
84
  multiply_10<<<1, 1, 0, GetCUDAStream(gpu0)>>>(tmp.MutableData(gpu0));
85
#endif
Q
qiaolongfei 已提交
86 87 88
  paddle::platform::CUDAPlace gpu1(1);
  auto* gpu1_ptr = tmp.MutableData(gpu1);
  paddle::platform::SetDeviceId(1);
89 90 91 92 93

#ifdef PADDLE_WITH_HIP
  hipLaunchKernelGGL(multiply_10, dim3(1), dim3(1), 0, GetCUDAStream(gpu1),
                     gpu1_ptr);
#else
Q
qiaolongfei 已提交
94
  multiply_10<<<1, 1, 0, GetCUDAStream(gpu1)>>>(gpu1_ptr);
95
#endif
Q
qiaolongfei 已提交
96 97 98 99
  for (int i = 0; i < 10; ++i) {
    ASSERT_EQ(tmp[i], i * 100);
  }
}