delete blob_bottom_;
delete blob_top_;
}
+
// http://en.wikipedia.org/wiki/Hamming_distance
- int ReferenceHammingDistance(const int n, const Dtype* x, const Dtype* y);
+ int ReferenceHammingDistance(const int n, const Dtype* x, const Dtype* y) {
+ int dist = 0;
+ uint64_t val;
+ for (int i = 0; i < n; ++i) {
+ if (sizeof(Dtype) == 8) {
+ val = static_cast<uint64_t>(x[i]) ^ static_cast<uint64_t>(y[i]);
+ } else if (sizeof(Dtype) == 4) {
+ val = static_cast<uint32_t>(x[i]) ^ static_cast<uint32_t>(y[i]);
+ } else {
+ LOG(FATAL) << "Unrecognized Dtype size: " << sizeof(Dtype);
+ }
+ // Count the number of set bits
+ while (val) {
+ ++dist;
+ val &= val - 1;
+ }
+ }
+ return dist;
+ }
Blob<Dtype>* const blob_bottom_;
Blob<Dtype>* const blob_top_;
};
-#define REF_HAMMING_DIST(float_type, int_type) \
-template<> \
-int MathFunctionsTest<float_type>::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<int_type>(x[i]) ^ static_cast<int_type>(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<float, double> Dtypes;
TYPED_TEST_CASE(MathFunctionsTest, Dtypes);
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_cpu_hamming_distance<TypeParam>(n, x, y));
+ EXPECT_EQ(this->ReferenceHammingDistance(n, x, y),
+ caffe_cpu_hamming_distance<TypeParam>(n, x, y));
}
TYPED_TEST(MathFunctionsTest, TestHammingDistanceGPU) {
int n = this->blob_bottom_->count();
- const TypeParam* x = this->blob_bottom_->gpu_data();
- const TypeParam* y = this->blob_top_->gpu_data();
- const TypeParam* cpu_x = this->blob_bottom_->cpu_data();
- const TypeParam* cpu_y = this->blob_top_->cpu_data();
- CHECK_EQ(this->ReferenceHammingDistance(n, cpu_x, cpu_y),
- caffe_gpu_hamming_distance<TypeParam>(n, x, y));
+ const TypeParam* x = this->blob_bottom_->cpu_data();
+ const TypeParam* y = this->blob_top_->cpu_data();
+ int reference_distance = this->ReferenceHammingDistance(n, x, y);
+ x = this->blob_bottom_->gpu_data();
+ y = this->blob_top_->gpu_data();
+ int computed_distance = caffe_gpu_hamming_distance<TypeParam>(n, x, y);
+ EXPECT_EQ(reference_distance, computed_distance);
}
TYPED_TEST(MathFunctionsTest, TestAsumCPU) {
std_asum += std::fabs(x[i]);
}
TypeParam cpu_asum = caffe_cpu_asum<TypeParam>(n, x);
- CHECK_LT((cpu_asum - std_asum) / std_asum, 1e-2);
+ EXPECT_LT((cpu_asum - std_asum) / std_asum, 1e-2);
}
TYPED_TEST(MathFunctionsTest, TestAsumGPU) {
}
TypeParam gpu_asum;
caffe_gpu_asum<TypeParam>(n, this->blob_bottom_->gpu_data(), &gpu_asum);
- CHECK_LT((gpu_asum - std_asum) / std_asum, 1e-2);
+ EXPECT_LT((gpu_asum - std_asum) / std_asum, 1e-2);
}
TYPED_TEST(MathFunctionsTest, TestSignCPU) {
caffe_cpu_sign<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
const TypeParam* signs = this->blob_bottom_->cpu_diff();
for (int i = 0; i < n; ++i) {
- CHECK_EQ(signs[i], x[i] > 0 ? 1 : (x[i] < 0 ? -1 : 0));
+ EXPECT_EQ(signs[i], x[i] > 0 ? 1 : (x[i] < 0 ? -1 : 0));
}
}
const TypeParam* signs = this->blob_bottom_->cpu_diff();
const TypeParam* x = this->blob_bottom_->cpu_data();
for (int i = 0; i < n; ++i) {
- CHECK_EQ(signs[i], x[i] > 0 ? 1 : (x[i] < 0 ? -1 : 0));
+ EXPECT_EQ(signs[i], x[i] > 0 ? 1 : (x[i] < 0 ? -1 : 0));
}
}
caffe_cpu_sgnbit<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
const TypeParam* signbits = this->blob_bottom_->cpu_diff();
for (int i = 0; i < n; ++i) {
- CHECK_EQ(signbits[i], x[i] < 0 ? 1 : 0);
+ EXPECT_EQ(signbits[i], x[i] < 0 ? 1 : 0);
}
}
const TypeParam* signbits = this->blob_bottom_->cpu_diff();
const TypeParam* x = this->blob_bottom_->cpu_data();
for (int i = 0; i < n; ++i) {
- CHECK_EQ(signbits[i], x[i] < 0 ? 1 : 0);
+ EXPECT_EQ(signbits[i], x[i] < 0 ? 1 : 0);
}
}
caffe_cpu_fabs<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
const TypeParam* abs_val = this->blob_bottom_->cpu_diff();
for (int i = 0; i < n; ++i) {
- CHECK_EQ(abs_val[i], x[i] > 0 ? x[i] : -x[i]);
+ EXPECT_EQ(abs_val[i], x[i] > 0 ? x[i] : -x[i]);
}
}
const TypeParam* abs_val = this->blob_bottom_->cpu_diff();
const TypeParam* x = this->blob_bottom_->cpu_data();
for (int i = 0; i < n; ++i) {
- CHECK_EQ(abs_val[i], x[i] > 0 ? x[i] : -x[i]);
+ EXPECT_EQ(abs_val[i], x[i] > 0 ? x[i] : -x[i]);
}
}
const TypeParam* scaled = this->blob_bottom_->cpu_diff();
const TypeParam* x = this->blob_bottom_->cpu_data();
for (int i = 0; i < n; ++i) {
- CHECK_EQ(scaled[i], x[i] * alpha);
+ EXPECT_EQ(scaled[i], x[i] * alpha);
}
}
const TypeParam* scaled = this->blob_bottom_->cpu_diff();
const TypeParam* x = this->blob_bottom_->cpu_data();
for (int i = 0; i < n; ++i) {
- CHECK_EQ(scaled[i], x[i] * alpha);
+ EXPECT_EQ(scaled[i], x[i] * alpha);
}
}