From: Kai Li Date: Tue, 25 Feb 2014 10:23:04 +0000 (+0800) Subject: Add __builtin_popcount* based fast Hamming distance math function X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4de8280c1e97bef319b4b65e708137a5d54fdb88;p=platform%2Fupstream%2Fcaffe.git Add __builtin_popcount* based fast Hamming distance math function --- diff --git a/include/caffe/util/math_functions.hpp b/include/caffe/util/math_functions.hpp index e9e2db8..26abb2d 100644 --- a/include/caffe/util/math_functions.hpp +++ b/include/caffe/util/math_functions.hpp @@ -1,4 +1,5 @@ // Copyright 2013 Yangqing Jia +// Copyright 2014 kloudkl@github #ifndef CAFFE_UTIL_MATH_FUNCTIONS_H_ #define CAFFE_UTIL_MATH_FUNCTIONS_H_ @@ -100,6 +101,9 @@ Dtype caffe_cpu_dot(const int n, const Dtype* x, const Dtype* y); template void caffe_gpu_dot(const int n, const Dtype* x, const Dtype* y, Dtype* out); +template +int caffe_hamming_distance(const int n, const Dtype* x, const Dtype* y); + } // namespace caffe diff --git a/src/caffe/test/test_math_functions.cpp b/src/caffe/test/test_math_functions.cpp new file mode 100644 index 0000000..0e313ee --- /dev/null +++ b/src/caffe/test/test_math_functions.cpp @@ -0,0 +1,77 @@ +// Copyright 2014 kloudkl@github + +#include // for uint32_t & uint64_t + +#include "gtest/gtest.h" +#include "caffe/blob.hpp" +#include "caffe/common.hpp" +#include "caffe/filler.hpp" +#include "caffe/util/math_functions.hpp" + +#include "caffe/test/test_caffe_main.hpp" + +namespace caffe { + +template +class MathFunctionsTest : public ::testing::Test { + protected: + MathFunctionsTest() + : blob_bottom_(new Blob()), + blob_top_(new Blob()) { + } + + virtual void SetUp() { + Caffe::set_random_seed(1701); + this->blob_bottom_->Reshape(100, 70, 50, 30); + this->blob_top_->Reshape(100, 70, 50, 30); + // fill the values + FillerParameter filler_param; + GaussianFiller filler(filler_param); + filler.Fill(this->blob_bottom_); + filler.Fill(this->blob_top_); + } + + virtual ~MathFunctionsTest() { + delete blob_bottom_; + delete blob_top_; + } + // http://en.wikipedia.org/wiki/Hamming_distance + int ReferenceHammingDistance(const int n, const Dtype* x, const Dtype* y); + + Blob* const blob_bottom_; + Blob* const blob_top_; +}; + +#define REF_HAMMING_DIST(float_type, int_type) \ +template<> \ +int MathFunctionsTest::ReferenceHammingDistance(const int n, \ + const float_type* x, \ + const float_type* y) { \ + int dist = 0; \ + int_type val; \ + for (int i = 0; i < n; ++i) { \ + val = static_cast(x[i]) ^ static_cast(y[i]); \ + /* Count the number of set bits */ \ + while (val) { \ + ++dist; \ + val &= val - 1; \ + } \ + } \ + return dist; \ +} + +REF_HAMMING_DIST(float, uint32_t); +REF_HAMMING_DIST(double, uint64_t); + +typedef ::testing::Types Dtypes; +TYPED_TEST_CASE(MathFunctionsTest, Dtypes); + +TYPED_TEST(MathFunctionsTest, TestHammingDistance){ + int n = this->blob_bottom_->count(); + const TypeParam* x = this->blob_bottom_->cpu_data(); + const TypeParam* y = this->blob_top_->cpu_data(); + CHECK_EQ(this->ReferenceHammingDistance(n, x, y), + caffe_hamming_distance(n, x, y)); +} + +} diff --git a/src/caffe/util/math_functions.cpp b/src/caffe/util/math_functions.cpp index 60656b8..790f00e 100644 --- a/src/caffe/util/math_functions.cpp +++ b/src/caffe/util/math_functions.cpp @@ -1,4 +1,5 @@ // Copyright 2013 Yangqing Jia +// Copyright 2014 kloudkl@github #include #include @@ -293,4 +294,26 @@ void caffe_gpu_dot(const int n, const double* x, const double* y, CUBLAS_CHECK(cublasDdot(Caffe::cublas_handle(), n, x, 1, y, 1, out)); } +template <> +int caffe_hamming_distance(const int n, const float* x, + const float* y) { + int dist = 0; + for (int i = 0; i < n; ++i) { + dist += __builtin_popcount(static_cast(x[i]) ^ + static_cast(y[i])); + } + return dist; +} + +template <> +int caffe_hamming_distance(const int n, const double* x, + const double* y) { + int dist = 0; + for (int i = 0; i < n; ++i) { + dist += __builtin_popcountl(static_cast(x[i]) ^ + static_cast(y[i])); + } + return dist; +} + } // namespace caffe