Skip to content

Commit 65e5aeb

Browse files
committed
fix mixed_vector_test
1 parent da035fc commit 65e5aeb

File tree

3 files changed

+76
-63
lines changed

3 files changed

+76
-63
lines changed

paddle/fluid/framework/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ endif()
2323
cc_test(eigen_test SRCS eigen_test.cc DEPS tensor)
2424

2525
if(WITH_GPU)
26-
nv_test(mixed_vector_test SRCS mixed_vector_test.cc DEPS place memory device_context tensor)
26+
nv_test(mixed_vector_test SRCS mixed_vector_test.cc mixed_vector_test.cu DEPS place memory device_context tensor)
2727
else()
2828
cc_test(mixed_vector_test SRCS mixed_vector_test.cc DEPS place memory device_context tensor)
2929
endif()

paddle/fluid/framework/mixed_vector_test.cc

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,11 @@
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#ifdef PADDLE_WITH_CUDA
16-
17-
#include <cuda_runtime.h>
18-
19-
#endif
20-
2115
#include <memory>
2216

2317
#include "glog/logging.h"
2418
#include "gtest/gtest.h"
2519
#include "paddle/fluid/framework/mixed_vector.h"
26-
#include "paddle/fluid/platform/gpu_info.h"
2720

2821
template <typename T>
2922
using vec = paddle::framework::Vector<T>;
@@ -77,58 +70,3 @@ TEST(mixed_vector, Resize) {
7770
vec.push_back(0);
7871
vec.push_back(0);
7972
}
80-
81-
#ifdef PADDLE_WITH_CUDA
82-
83-
static __global__ void multiply_10(int* ptr) {
84-
for (int i = 0; i < 10; ++i) {
85-
ptr[i] *= 10;
86-
}
87-
}
88-
89-
cudaStream_t GetCUDAStream(paddle::platform::CUDAPlace place) {
90-
return reinterpret_cast<const paddle::platform::CUDADeviceContext*>(
91-
paddle::platform::DeviceContextPool::Instance().Get(place))
92-
->stream();
93-
}
94-
95-
TEST(mixed_vector, GPU_VECTOR) {
96-
vec<int> tmp;
97-
for (int i = 0; i < 10; ++i) {
98-
tmp.push_back(i);
99-
}
100-
ASSERT_EQ(tmp.size(), 10UL);
101-
paddle::platform::CUDAPlace gpu(0);
102-
103-
multiply_10<<<1, 1, 0, GetCUDAStream(gpu)>>>(tmp.MutableData(gpu));
104-
105-
for (int i = 0; i < 10; ++i) {
106-
ASSERT_EQ(tmp[i], i * 10);
107-
}
108-
}
109-
110-
TEST(mixed_vector, MultiGPU) {
111-
if (paddle::platform::GetCUDADeviceCount() < 2) {
112-
LOG(WARNING) << "Skip mixed_vector.MultiGPU since there are not multiple "
113-
"GPUs in your machine.";
114-
return;
115-
}
116-
117-
vec<int> tmp;
118-
for (int i = 0; i < 10; ++i) {
119-
tmp.push_back(i);
120-
}
121-
ASSERT_EQ(tmp.size(), 10UL);
122-
paddle::platform::CUDAPlace gpu0(0);
123-
paddle::platform::SetDeviceId(0);
124-
multiply_10<<<1, 1, 0, GetCUDAStream(gpu0)>>>(tmp.MutableData(gpu0));
125-
paddle::platform::CUDAPlace gpu1(1);
126-
auto* gpu1_ptr = tmp.MutableData(gpu1);
127-
paddle::platform::SetDeviceId(1);
128-
multiply_10<<<1, 1, 0, GetCUDAStream(gpu1)>>>(gpu1_ptr);
129-
for (int i = 0; i < 10; ++i) {
130-
ASSERT_EQ(tmp[i], i * 100);
131-
}
132-
}
133-
134-
#endif
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include <cuda_runtime.h>
16+
#include <memory>
17+
18+
#include "glog/logging.h"
19+
#include "gtest/gtest.h"
20+
#include "paddle/fluid/framework/mixed_vector.h"
21+
#include "paddle/fluid/platform/gpu_info.h"
22+
23+
template <typename T>
24+
using vec = paddle::framework::Vector<T>;
25+
26+
static __global__ void multiply_10(int* ptr) {
27+
for (int i = 0; i < 10; ++i) {
28+
ptr[i] *= 10;
29+
}
30+
}
31+
32+
cudaStream_t GetCUDAStream(paddle::platform::CUDAPlace place) {
33+
return reinterpret_cast<const paddle::platform::CUDADeviceContext*>(
34+
paddle::platform::DeviceContextPool::Instance().Get(place))
35+
->stream();
36+
}
37+
38+
TEST(mixed_vector, GPU_VECTOR) {
39+
vec<int> tmp;
40+
for (int i = 0; i < 10; ++i) {
41+
tmp.push_back(i);
42+
}
43+
ASSERT_EQ(tmp.size(), 10UL);
44+
paddle::platform::CUDAPlace gpu(0);
45+
46+
multiply_10<<<1, 1, 0, GetCUDAStream(gpu)>>>(tmp.MutableData(gpu));
47+
48+
for (int i = 0; i < 10; ++i) {
49+
ASSERT_EQ(tmp[i], i * 10);
50+
}
51+
}
52+
53+
TEST(mixed_vector, MultiGPU) {
54+
if (paddle::platform::GetCUDADeviceCount() < 2) {
55+
LOG(WARNING) << "Skip mixed_vector.MultiGPU since there are not multiple "
56+
"GPUs in your machine.";
57+
return;
58+
}
59+
60+
vec<int> tmp;
61+
for (int i = 0; i < 10; ++i) {
62+
tmp.push_back(i);
63+
}
64+
ASSERT_EQ(tmp.size(), 10UL);
65+
paddle::platform::CUDAPlace gpu0(0);
66+
paddle::platform::SetDeviceId(0);
67+
multiply_10<<<1, 1, 0, GetCUDAStream(gpu0)>>>(tmp.MutableData(gpu0));
68+
paddle::platform::CUDAPlace gpu1(1);
69+
auto* gpu1_ptr = tmp.MutableData(gpu1);
70+
paddle::platform::SetDeviceId(1);
71+
multiply_10<<<1, 1, 0, GetCUDAStream(gpu1)>>>(gpu1_ptr);
72+
for (int i = 0; i < 10; ++i) {
73+
ASSERT_EQ(tmp[i], i * 100);
74+
}
75+
}

0 commit comments

Comments
 (0)