SyncedMemory data_a(10 * sizeof(int));
SyncedMemory data_b(10 * sizeof(int));
Caffe::set_random_seed(1701);
- caffe_vRngBernoulli(10, reinterpret_cast<int*>(data_a.mutable_cpu_data()), 0.5);
+ caffe_vRngBernoulli(10,
+ reinterpret_cast<int*>(data_a.mutable_cpu_data()), 0.5);
Caffe::set_random_seed(1701);
- caffe_vRngBernoulli(10, reinterpret_cast<int*>(data_b.mutable_cpu_data()), 0.5);
+ caffe_vRngBernoulli(10,
+ reinterpret_cast<int*>(data_b.mutable_cpu_data()), 0.5);
for (int i = 0; i < 10; ++i) {
EXPECT_EQ(((const int*)(data_a.cpu_data()))[i],
+// Copyright 2014 kloudkl@github
+// Copyright 2014 Jeff Donahue
+// Copyright 2014 Alejandro Dubrovsky
+// Copyright 2014 Evan Shelhamer
+
+#include <cuda_runtime.h>
#include <cmath>
#include <cstring>
-#include <cuda_runtime.h>
#include "gtest/gtest.h"
#include "caffe/common.hpp"
public:
virtual ~RandomNumberGeneratorTest() {}
- Dtype sample_mean(const Dtype* const seqs, const size_t sample_size)
- {
+ Dtype sample_mean(const Dtype* const seqs, const size_t sample_size) {
double sum = 0;
for (int i = 0; i < sample_size; ++i) {
sum += seqs[i];
return sum / sample_size;
}
- Dtype sample_mean(const int* const seqs, const size_t sample_size)
- {
+ Dtype sample_mean(const int* const seqs, const size_t sample_size) {
Dtype sum = 0;
for (int i = 0; i < sample_size; ++i) {
sum += Dtype(seqs[i]);
return sum / sample_size;
}
- Dtype mean_bound(const Dtype std, const size_t sample_size)
- {
- return std/sqrt((double)sample_size);
+ Dtype mean_bound(const Dtype std, const size_t sample_size) {
+ return std/sqrt(static_cast<double>(sample_size));
}
};
typedef ::testing::Types<float, double> Dtypes;
TYPED_TEST_CASE(RandomNumberGeneratorTest, Dtypes);
+
TYPED_TEST(RandomNumberGeneratorTest, TestRngGaussian) {
size_t sample_size = 10000;
SyncedMemory data_a(sample_size * sizeof(TypeParam));
TypeParam mu = 0;
TypeParam sigma = 1;
caffe_vRngGaussian(sample_size,
- (TypeParam*)data_a.mutable_cpu_data(), mu, sigma);
+ reinterpret_cast<TypeParam*>(data_a.mutable_cpu_data()), mu, sigma);
TypeParam true_mean = mu;
TypeParam true_std = sigma;
TypeParam bound = this->mean_bound(true_std, sample_size);
TypeParam empirical_mean =
- this->sample_mean((TypeParam*)data_a.cpu_data(), sample_size);
+ this->sample_mean(reinterpret_cast<const TypeParam*>(data_a.cpu_data()),
+ sample_size);
EXPECT_NEAR(empirical_mean, true_mean, bound);
}
TypeParam lower = 0;
TypeParam upper = 1;
caffe_vRngUniform(sample_size,
- (TypeParam*)data_a.mutable_cpu_data(), lower, upper);
+ reinterpret_cast<TypeParam*>(data_a.mutable_cpu_data()), lower, upper);
TypeParam true_mean = (lower + upper) / 2;
TypeParam true_std = (upper - lower) / sqrt(12);
TypeParam bound = this->mean_bound(true_std, sample_size);
TypeParam empirical_mean =
- this->sample_mean((TypeParam*)data_a.cpu_data(), sample_size);
+ this->sample_mean(reinterpret_cast<const TypeParam*>(data_a.cpu_data()),
+ sample_size);
EXPECT_NEAR(empirical_mean, true_mean, bound);
}
SyncedMemory data_a(sample_size * sizeof(int));
Caffe::set_random_seed(1701);
double p = 0.3;
- caffe_vRngBernoulli(sample_size, (int*)data_a.mutable_cpu_data(), p);
+ caffe_vRngBernoulli(sample_size,
+ static_cast<int*>(data_a.mutable_cpu_data()), p);
TypeParam true_mean = p;
TypeParam true_std = sqrt(p * (1 - p));
TypeParam bound = this->mean_bound(true_std, sample_size);