From 58f74e2ca17fd6b417355e2686ce00f3ff898fbd Mon Sep 17 00:00:00 2001 From: liaogang Date: Sat, 10 Sep 2016 09:05:57 +0800 Subject: [PATCH] Add main entry for unit test files and replace memalign by posix_memalign --- paddle/math/Allocator.h | 4 ---- paddle/math/tests/test_SIMDFunctions.cpp | 6 ++++-- paddle/math/tests/test_perturbation.cpp | 5 +++++ paddle/parameter/tests/test_common.cpp | 10 ++++++---- paddle/utils/tests/test_StringUtils.cpp | 5 +++++ 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/paddle/math/Allocator.h b/paddle/math/Allocator.h index 7d277b1c10d..ca8eadbc1aa 100644 --- a/paddle/math/Allocator.h +++ b/paddle/math/Allocator.h @@ -48,14 +48,10 @@ public: * @return Pointer to the allocated memory */ virtual void* alloc(size_t size) { - #if defined(__APPLE__) || defined(__OSX__) - return malloc(size); - #else void* ptr; posix_memalign(&ptr, 32ul, size); CHECK(ptr) << "Fail to allocate CPU memory: size=" << size; return ptr; - #endif } /** diff --git a/paddle/math/tests/test_SIMDFunctions.cpp b/paddle/math/tests/test_SIMDFunctions.cpp index 631d0516cf4..bae5d8c684d 100644 --- a/paddle/math/tests/test_SIMDFunctions.cpp +++ b/paddle/math/tests/test_SIMDFunctions.cpp @@ -24,7 +24,7 @@ limitations under the License. */ #include #include -#include +#include #include static constexpr size_t VECTOR_LEN = 3072; @@ -37,7 +37,9 @@ static std::mt19937 RandomEngine(time(0)); inline static std::unique_ptr NewVector(size_t len = VECTOR_LEN, size_t align = ALIGN) { - return std::unique_ptr((float*)memalign(align, len * sizeof(float))); + float* ptr; + posix_memalign((void**)&ptr, align, len * sizeof(float)); + return std::unique_ptr(ptr); } inline static std::unique_ptr NewRandomVector(size_t len = VECTOR_LEN, diff --git a/paddle/math/tests/test_perturbation.cpp b/paddle/math/tests/test_perturbation.cpp index 4fa9bc72013..51e346fef91 100644 --- a/paddle/math/tests/test_perturbation.cpp +++ b/paddle/math/tests/test_perturbation.cpp @@ -249,4 +249,9 @@ TEST_F(PerturbationTest, scale_test) { } } +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} + #endif diff --git a/paddle/parameter/tests/test_common.cpp b/paddle/parameter/tests/test_common.cpp index 3db96ccf941..4f92aec1d96 100644 --- a/paddle/parameter/tests/test_common.cpp +++ b/paddle/parameter/tests/test_common.cpp @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include +#include #include #include @@ -124,9 +124,11 @@ void CommonTest::test_sgdUpadate(real* gradientBuffer, real* valueBuffer, TEST_F(CommonTest, sgdUpdate) { const size_t alignHeader[] = {0, 2, 3, 5, 7, 8}; for (auto& size : sizeVec_) { - real* gradientBuffer = (real*)memalign(32, sizeof(real) * size); - real* valueBuffer = (real*)memalign(32, sizeof(real) * size); - real* momentumBuffer = (real*)memalign(32, sizeof(real) * size); + real *gradientBuffer, *valueBuffer, *momentumBuffer; + posix_memalign((void**)&gradientBuffer, 32, sizeof(real) * size); + posix_memalign((void**)&valueBuffer, 32, sizeof(real) * size); + posix_memalign((void**)&momentumBuffer, 32, sizeof(real) * size); + for (size_t i = 0; i < size; i++) { gradientBuffer[i] = 1.0; valueBuffer[i] = 2.0; diff --git a/paddle/utils/tests/test_StringUtils.cpp b/paddle/utils/tests/test_StringUtils.cpp index b8636709e9b..95290005ae9 100644 --- a/paddle/utils/tests/test_StringUtils.cpp +++ b/paddle/utils/tests/test_StringUtils.cpp @@ -22,3 +22,8 @@ TEST(StringUtil, to) { ASSERT_DEATH(paddle::str::to("12.45x23"), ".*"); ASSERT_DEATH(paddle::str::to(""), ".*"); } + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} -- GitLab