From 65e5aebd437f5352e1e937ca6de83f8baa318c8e Mon Sep 17 00:00:00 2001 From: qiaolongfei Date: Wed, 25 Jul 2018 12:59:40 +0800 Subject: [PATCH] fix mixed_vector_test --- paddle/fluid/framework/CMakeLists.txt | 2 +- paddle/fluid/framework/mixed_vector_test.cc | 62 ----------------- paddle/fluid/framework/mixed_vector_test.cu | 75 +++++++++++++++++++++ 3 files changed, 76 insertions(+), 63 deletions(-) create mode 100644 paddle/fluid/framework/mixed_vector_test.cu diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index a526c47aa91..93ec047c801 100644 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -23,7 +23,7 @@ endif() cc_test(eigen_test SRCS eigen_test.cc DEPS tensor) if(WITH_GPU) - nv_test(mixed_vector_test SRCS mixed_vector_test.cc DEPS place memory device_context tensor) + nv_test(mixed_vector_test SRCS mixed_vector_test.cc mixed_vector_test.cu DEPS place memory device_context tensor) else() cc_test(mixed_vector_test SRCS mixed_vector_test.cc DEPS place memory device_context tensor) endif() diff --git a/paddle/fluid/framework/mixed_vector_test.cc b/paddle/fluid/framework/mixed_vector_test.cc index 90d9a7c8281..0599c8d3846 100644 --- a/paddle/fluid/framework/mixed_vector_test.cc +++ b/paddle/fluid/framework/mixed_vector_test.cc @@ -12,18 +12,11 @@ See the License for the specific language governing permissions and limitations under the License. */ -#ifdef PADDLE_WITH_CUDA - -#include - -#endif - #include #include "glog/logging.h" #include "gtest/gtest.h" #include "paddle/fluid/framework/mixed_vector.h" -#include "paddle/fluid/platform/gpu_info.h" template using vec = paddle::framework::Vector; @@ -77,58 +70,3 @@ TEST(mixed_vector, Resize) { vec.push_back(0); vec.push_back(0); } - -#ifdef PADDLE_WITH_CUDA - -static __global__ void multiply_10(int* ptr) { - for (int i = 0; i < 10; ++i) { - ptr[i] *= 10; - } -} - -cudaStream_t GetCUDAStream(paddle::platform::CUDAPlace place) { - return reinterpret_cast( - paddle::platform::DeviceContextPool::Instance().Get(place)) - ->stream(); -} - -TEST(mixed_vector, GPU_VECTOR) { - vec tmp; - for (int i = 0; i < 10; ++i) { - tmp.push_back(i); - } - ASSERT_EQ(tmp.size(), 10UL); - paddle::platform::CUDAPlace gpu(0); - - multiply_10<<<1, 1, 0, GetCUDAStream(gpu)>>>(tmp.MutableData(gpu)); - - 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 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); - multiply_10<<<1, 1, 0, GetCUDAStream(gpu0)>>>(tmp.MutableData(gpu0)); - paddle::platform::CUDAPlace gpu1(1); - auto* gpu1_ptr = tmp.MutableData(gpu1); - paddle::platform::SetDeviceId(1); - multiply_10<<<1, 1, 0, GetCUDAStream(gpu1)>>>(gpu1_ptr); - for (int i = 0; i < 10; ++i) { - ASSERT_EQ(tmp[i], i * 100); - } -} - -#endif diff --git a/paddle/fluid/framework/mixed_vector_test.cu b/paddle/fluid/framework/mixed_vector_test.cu new file mode 100644 index 00000000000..4b0caa8d350 --- /dev/null +++ b/paddle/fluid/framework/mixed_vector_test.cu @@ -0,0 +1,75 @@ +/* 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. */ + +#include +#include + +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "paddle/fluid/framework/mixed_vector.h" +#include "paddle/fluid/platform/gpu_info.h" + +template +using vec = paddle::framework::Vector; + +static __global__ void multiply_10(int* ptr) { + for (int i = 0; i < 10; ++i) { + ptr[i] *= 10; + } +} + +cudaStream_t GetCUDAStream(paddle::platform::CUDAPlace place) { + return reinterpret_cast( + paddle::platform::DeviceContextPool::Instance().Get(place)) + ->stream(); +} + +TEST(mixed_vector, GPU_VECTOR) { + vec tmp; + for (int i = 0; i < 10; ++i) { + tmp.push_back(i); + } + ASSERT_EQ(tmp.size(), 10UL); + paddle::platform::CUDAPlace gpu(0); + + multiply_10<<<1, 1, 0, GetCUDAStream(gpu)>>>(tmp.MutableData(gpu)); + + 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 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); + multiply_10<<<1, 1, 0, GetCUDAStream(gpu0)>>>(tmp.MutableData(gpu0)); + paddle::platform::CUDAPlace gpu1(1); + auto* gpu1_ptr = tmp.MutableData(gpu1); + paddle::platform::SetDeviceId(1); + multiply_10<<<1, 1, 0, GetCUDAStream(gpu1)>>>(gpu1_ptr); + for (int i = 0; i < 10; ++i) { + ASSERT_EQ(tmp[i], i * 100); + } +} -- GitLab