Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
shelhamer committed Mar 22, 2014
1 parent 453fcf9 commit aaa2646
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
4 changes: 3 additions & 1 deletion include/caffe/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ class Caffe {

// boost RNG
typedef boost::mt19937 random_generator_t;
inline static random_generator_t &rng_stream() { return Get().random_generator_; }
inline static random_generator_t &rng_stream() {
return Get().random_generator_;
}

// Returns the mode: running on CPU or GPU.
inline static Brew mode() { return Get().mode_; }
Expand Down
3 changes: 1 addition & 2 deletions src/caffe/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ int64_t cluster_seedgen(void) {
Caffe::Caffe()
: mode_(Caffe::CPU), phase_(Caffe::TRAIN), cublas_handle_(NULL),
curand_generator_(NULL),
random_generator_()
{
random_generator_() {
// Try to create a cublas handler, and report an error if failed (but we will
// keep the program running as one might just want to run CPU code).
if (cublasCreate(&cublas_handle_) != CUBLAS_STATUS_SUCCESS) {
Expand Down
6 changes: 4 additions & 2 deletions src/caffe/test/test_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ TEST_F(CommonTest, TestRandSeedCPU) {
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],
Expand Down
32 changes: 19 additions & 13 deletions src/caffe/test/test_random_number_generator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// 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"
Expand All @@ -15,47 +20,46 @@ class RandomNumberGeneratorTest : public ::testing::Test {
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));
Caffe::set_random_seed(1701);
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);
}

Expand All @@ -67,12 +71,13 @@ TYPED_TEST(RandomNumberGeneratorTest, TestRngUniform) {
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);
}

Expand All @@ -82,7 +87,8 @@ TYPED_TEST(RandomNumberGeneratorTest, TestRngBernoulli) {
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);
Expand Down
5 changes: 3 additions & 2 deletions src/caffe/util/math_functions.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright 2013 Yangqing Jia
// Copyright 2014 kloudkl@github

#include <limits>
#include <boost/math/special_functions/next.hpp>
#include <boost/random.hpp>

#include <cublas_v2.h>

#include <limits>

#include "caffe/common.hpp"
#include "caffe/util/math_functions.hpp"

Expand Down

0 comments on commit aaa2646

Please sign in to comment.