Skip to content

Commit 9029a9d

Browse files
reyoungwangkuiyi
authored andcommitted
Fix constructor bug in mixed_vector (#8364)
* Fix constructor bug in mixed_vector * Fix warnings * Clean code * Extract for-loop init. Make nvcc happy
1 parent 274f4e9 commit 9029a9d

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

paddle/fluid/framework/mixed_vector.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ class Vector {
3737

3838
// Fill vector with value. The vector size is `count`.
3939
explicit Vector(size_t count, const T& value = T()) {
40-
if (count == 0) {
41-
InitEmpty();
42-
} else {
40+
InitEmpty();
41+
if (count != 0) {
4342
resize(count);
4443
T* ptr = begin();
4544
for (size_t i = 0; i < count; ++i) {
@@ -122,6 +121,10 @@ class Vector {
122121
const T* begin() const { return &this->operator[](0); }
123122
const T* end() const { return &this->operator[](size()); }
124123

124+
const T* cbegin() const { return begin(); }
125+
126+
const T* cend() const { return end(); }
127+
125128
const T& back() const {
126129
auto it = end();
127130
--it;
@@ -244,7 +247,9 @@ class Vector {
244247

245248
bool operator==(const Vector<T>& other) const {
246249
if (size() != other.size()) return false;
247-
for (auto it1 = begin(), it2 = other.begin(); it1 < end(); ++it1, ++it2) {
250+
auto it1 = cbegin();
251+
auto it2 = other.cbegin();
252+
for (; it1 < cend(); ++it1, ++it2) {
248253
if (*it1 != *it2) {
249254
return false;
250255
}

paddle/fluid/framework/mixed_vector_test.cu

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ TEST(mixed_vector, CPU_VECTOR) {
2626
for (int i = 0; i < 10; ++i) {
2727
tmp.push_back(i);
2828
}
29-
ASSERT_EQ(tmp.size(), 10);
29+
ASSERT_EQ(tmp.size(), 10UL);
3030
vec<int> tmp2;
3131
tmp2 = tmp;
32-
ASSERT_EQ(tmp2.size(), 10);
32+
ASSERT_EQ(tmp2.size(), 10UL);
3333
for (int i = 0; i < 10; ++i) {
3434
ASSERT_EQ(tmp2[i], i);
3535
ASSERT_EQ(tmp2[i], tmp[i]);
@@ -58,7 +58,7 @@ TEST(mixed_vector, GPU_VECTOR) {
5858
for (int i = 0; i < 10; ++i) {
5959
tmp.push_back(i);
6060
}
61-
ASSERT_EQ(tmp.size(), 10);
61+
ASSERT_EQ(tmp.size(), 10UL);
6262
paddle::platform::CUDAPlace gpu(0);
6363

6464
multiply_10<<<1, 1, 0, GetCUDAStream(gpu)>>>(tmp.MutableData(gpu));
@@ -79,7 +79,7 @@ TEST(mixed_vector, MultiGPU) {
7979
for (int i = 0; i < 10; ++i) {
8080
tmp.push_back(i);
8181
}
82-
ASSERT_EQ(tmp.size(), 10);
82+
ASSERT_EQ(tmp.size(), 10UL);
8383
paddle::platform::CUDAPlace gpu0(0);
8484
paddle::platform::SetDeviceId(0);
8585
multiply_10<<<1, 1, 0, GetCUDAStream(gpu0)>>>(tmp.MutableData(gpu0));
@@ -91,3 +91,10 @@ TEST(mixed_vector, MultiGPU) {
9191
ASSERT_EQ(tmp[i], i * 100);
9292
}
9393
}
94+
95+
TEST(mixed_vector, InitWithCount) {
96+
paddle::framework::Vector<int> vec(10, 10);
97+
for (int i = 0; i < 10; ++i) {
98+
ASSERT_EQ(vec[i], 10);
99+
}
100+
}

0 commit comments

Comments
 (0)