From 82c33c61d9e7584060916c604478b67f59fbdfc0 Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Sun, 11 Feb 2018 10:46:25 +0800 Subject: [PATCH 1/4] Fix constructor bug in mixed_vector --- paddle/fluid/framework/mixed_vector.h | 5 ++--- paddle/fluid/framework/mixed_vector_test.cu | 8 ++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/framework/mixed_vector.h b/paddle/fluid/framework/mixed_vector.h index 9756754260d465..902dedd48e1112 100644 --- a/paddle/fluid/framework/mixed_vector.h +++ b/paddle/fluid/framework/mixed_vector.h @@ -37,9 +37,8 @@ class Vector { // Fill vector with value. The vector size is `count`. explicit Vector(size_t count, const T& value = T()) { - if (count == 0) { - InitEmpty(); - } else { + InitEmpty(); + if (count != 0) { resize(count); T* ptr = begin(); for (size_t i = 0; i < count; ++i) { diff --git a/paddle/fluid/framework/mixed_vector_test.cu b/paddle/fluid/framework/mixed_vector_test.cu index a89064525661af..20b79d60c1a525 100644 --- a/paddle/fluid/framework/mixed_vector_test.cu +++ b/paddle/fluid/framework/mixed_vector_test.cu @@ -15,6 +15,7 @@ #include "glog/logging.h" #include "gtest/gtest.h" +#include "mixed_vector.h" #include "paddle/fluid/framework/mixed_vector.h" #include "paddle/fluid/platform/gpu_info.h" @@ -91,3 +92,10 @@ TEST(mixed_vector, MultiGPU) { ASSERT_EQ(tmp[i], i * 100); } } + +TEST(mixed_vector, InitWithCount) { + paddle::framework::Vector vec(10, 10); + for (int i = 0; i < 10; ++i) { + ASSERT_EQ(vec[i], 10); + } +} From 816fa8f32e951f2c6bef9c7b59bd2cb8dd4d9f96 Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Sun, 11 Feb 2018 10:59:56 +0800 Subject: [PATCH 2/4] Fix warnings --- paddle/fluid/framework/mixed_vector.h | 7 ++++++- paddle/fluid/framework/mixed_vector_test.cu | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/framework/mixed_vector.h b/paddle/fluid/framework/mixed_vector.h index 902dedd48e1112..26f160e509329a 100644 --- a/paddle/fluid/framework/mixed_vector.h +++ b/paddle/fluid/framework/mixed_vector.h @@ -121,6 +121,10 @@ class Vector { const T* begin() const { return &this->operator[](0); } const T* end() const { return &this->operator[](size()); } + const T* cbegin() const { return begin(); } + + const T* cend() const { return end(); } + const T& back() const { auto it = end(); --it; @@ -243,7 +247,8 @@ class Vector { bool operator==(const Vector& other) const { if (size() != other.size()) return false; - for (auto it1 = begin(), it2 = other.begin(); it1 < end(); ++it1, ++it2) { + for (const T *it1 = cbegin(), it2 = other.cbegin(); it1 < cend(); + ++it1, ++it2) { if (*it1 != *it2) { return false; } diff --git a/paddle/fluid/framework/mixed_vector_test.cu b/paddle/fluid/framework/mixed_vector_test.cu index 20b79d60c1a525..83694a590f861a 100644 --- a/paddle/fluid/framework/mixed_vector_test.cu +++ b/paddle/fluid/framework/mixed_vector_test.cu @@ -27,10 +27,10 @@ TEST(mixed_vector, CPU_VECTOR) { for (int i = 0; i < 10; ++i) { tmp.push_back(i); } - ASSERT_EQ(tmp.size(), 10); + ASSERT_EQ(tmp.size(), 10UL); vec tmp2; tmp2 = tmp; - ASSERT_EQ(tmp2.size(), 10); + ASSERT_EQ(tmp2.size(), 10UL); for (int i = 0; i < 10; ++i) { ASSERT_EQ(tmp2[i], i); ASSERT_EQ(tmp2[i], tmp[i]); @@ -59,7 +59,7 @@ TEST(mixed_vector, GPU_VECTOR) { for (int i = 0; i < 10; ++i) { tmp.push_back(i); } - ASSERT_EQ(tmp.size(), 10); + ASSERT_EQ(tmp.size(), 10UL); paddle::platform::CUDAPlace gpu(0); multiply_10<<<1, 1, 0, GetCUDAStream(gpu)>>>(tmp.MutableData(gpu)); @@ -80,7 +80,7 @@ TEST(mixed_vector, MultiGPU) { for (int i = 0; i < 10; ++i) { tmp.push_back(i); } - ASSERT_EQ(tmp.size(), 10); + ASSERT_EQ(tmp.size(), 10UL); paddle::platform::CUDAPlace gpu0(0); paddle::platform::SetDeviceId(0); multiply_10<<<1, 1, 0, GetCUDAStream(gpu0)>>>(tmp.MutableData(gpu0)); From ae2296e806fb3b70f2ffe326815ade868039715f Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Sun, 11 Feb 2018 11:01:11 +0800 Subject: [PATCH 3/4] Clean code --- paddle/fluid/framework/mixed_vector_test.cu | 1 - 1 file changed, 1 deletion(-) diff --git a/paddle/fluid/framework/mixed_vector_test.cu b/paddle/fluid/framework/mixed_vector_test.cu index 83694a590f861a..0d5a914eac7809 100644 --- a/paddle/fluid/framework/mixed_vector_test.cu +++ b/paddle/fluid/framework/mixed_vector_test.cu @@ -15,7 +15,6 @@ #include "glog/logging.h" #include "gtest/gtest.h" -#include "mixed_vector.h" #include "paddle/fluid/framework/mixed_vector.h" #include "paddle/fluid/platform/gpu_info.h" From 190119bb98eaca554bb183488f6828fd2b3e18c0 Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Sun, 11 Feb 2018 11:28:32 +0800 Subject: [PATCH 4/4] Extract for-loop init. Make nvcc happy --- paddle/fluid/framework/mixed_vector.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/framework/mixed_vector.h b/paddle/fluid/framework/mixed_vector.h index 26f160e509329a..4dc3de54deef71 100644 --- a/paddle/fluid/framework/mixed_vector.h +++ b/paddle/fluid/framework/mixed_vector.h @@ -247,8 +247,9 @@ class Vector { bool operator==(const Vector& other) const { if (size() != other.size()) return false; - for (const T *it1 = cbegin(), it2 = other.cbegin(); it1 < cend(); - ++it1, ++it2) { + auto it1 = cbegin(); + auto it2 = other.cbegin(); + for (; it1 < cend(); ++it1, ++it2) { if (*it1 != *it2) { return false; }