Skip to content

Commit

Permalink
Rename Generator to RandomGenerator.
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Jul 26, 2018
1 parent 7468a0b commit f297074
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
19 changes: 10 additions & 9 deletions src/gpu/kmeans/kmeans_init.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ template <typename T>
class KmeansRandomInit : public KmeansInitBase<T> {
private:
int seed_;
std::unique_ptr<GeneratorBase<T>> generator_impl_;
std::unique_ptr<RandomGeneratorBase<T>> generator_impl_;

public:
/*
* @param seed Random seed for generating centroids.
*/
KmeansRandomInit(size_t _seed) :
seed_(_seed), generator_impl_ (new UniformGenerator<T>) {}
seed_(_seed), generator_impl_ (new UniformRandomGenerator<T>) {}

/*
* @param gen Unique pointer to Random generator for generating centroids.
*/
KmeansRandomInit(std::unique_ptr<GeneratorBase<T>>& _gen) :
KmeansRandomInit(std::unique_ptr<RandomGeneratorBase<T>>& _gen) :
generator_impl_(std::move(_gen)) {}

virtual ~KmeansRandomInit() override {}
Expand Down Expand Up @@ -134,7 +134,7 @@ struct KmeansLlInit : public KmeansInitBase<T> {
// Suggested in original paper, 8 is usually enough.
constexpr static float MAX_ITER = 8;

std::unique_ptr<GeneratorBase<T>> generator_;
std::unique_ptr<RandomGeneratorBase<T>> generator_;

// Buffer like variables
// store the self dot product of each data point
Expand All @@ -156,7 +156,7 @@ struct KmeansLlInit : public KmeansInitBase<T> {
*/
KmeansLlInit () :
over_sample_ (1.5f), seed_ (-1), k_ (0),
generator_ (new UniformGenerator<T>) {}
generator_ (new UniformRandomGenerator<T>) {}

/*
* Initialize KmeansLlInit algorithm.
Expand All @@ -168,7 +168,7 @@ struct KmeansLlInit : public KmeansInitBase<T> {
*/
KmeansLlInit (T _over_sample) :
over_sample_ (_over_sample), seed_ (-1), k_ (0),
generator_ (new UniformGenerator<T>) {}
generator_ (new UniformRandomGenerator<T>) {}

/*
* Initialize KmeansLlInit algorithm.
Expand All @@ -181,7 +181,7 @@ struct KmeansLlInit : public KmeansInitBase<T> {
*/
KmeansLlInit (int _seed, T _over_sample) :
seed_(_seed), over_sample_(_over_sample), k_(0),
generator_ (new UniformGenerator<T>(seed_)) {}
generator_ (new UniformRandomGenerator<T>(seed_)) {}

/*
* Initialize KmeansLlInit algorithm.
Expand All @@ -193,8 +193,9 @@ struct KmeansLlInit : public KmeansInitBase<T> {
* Note that when \f$over_sample != 1\f$, the probability for each data
* point doesn't add to 1.
*/
KmeansLlInit (std::unique_ptr<GeneratorBase<T>>& _gen, T _over_sample) :
generator_(std::move(_gen)), over_sample_ (1.5f), seed_ (-1), k_(0) {}
KmeansLlInit (std::unique_ptr<RandomGeneratorBase<T>>& _gen, T _over_sample) :
generator_(std::move(_gen)), over_sample_ (_over_sample), seed_ (-1),
k_(0) {}

virtual ~KmeansLlInit () override {}

Expand Down
19 changes: 9 additions & 10 deletions src/gpu/matrix/KmMatrix/Generator.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
#include "KmMatrix.hpp"
#include "../../utils/utils.cuh"


namespace h2o4gpu {
namespace Matrix {

template <typename T>
struct UniformGenerator : public GeneratorBase<T> {
struct UniformRandomGenerator : public RandomGeneratorBase<T> {
private:
size_t size_;
// FIXME: Cache random_numbers_ in a safer way.
Expand All @@ -28,27 +27,27 @@ struct UniformGenerator : public GeneratorBase<T> {
}

public:
UniformGenerator() : size_ (0) {
UniformRandomGenerator() : size_ (0) {
std::random_device rd;
seed_ = rd();
}

UniformGenerator (size_t _size, int _seed) : seed_(_seed) {
UniformRandomGenerator (size_t _size, int _seed) : seed_(_seed) {
if (_size == 0) {
h2o4gpu_error("Zero size for generate is not allowed.");
}
initialize(_size);
}

UniformGenerator(int _seed) :
UniformRandomGenerator(int _seed) :
seed_(_seed), size_ (0) {}

~UniformGenerator () {}
~UniformRandomGenerator () {}

UniformGenerator(const UniformGenerator<T>& _rhs) = delete;
UniformGenerator(UniformGenerator<T>&& _rhs) = delete;
void operator=(const UniformGenerator<T>& _rhs) = delete;
void operator=(UniformGenerator<T>&& _rhs) = delete;
UniformRandomGenerator(const UniformRandomGenerator<T>& _rhs) = delete;
UniformRandomGenerator(UniformRandomGenerator<T>&& _rhs) = delete;
void operator=(const UniformRandomGenerator<T>& _rhs) = delete;
void operator=(UniformRandomGenerator<T>&& _rhs) = delete;

KmMatrix<T> generate() override {
thrust::device_ptr<T> rn_ptr (random_numbers_.dev_ptr());
Expand Down
2 changes: 1 addition & 1 deletion src/gpu/matrix/KmMatrix/Generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace h2o4gpu {
namespace Matrix {

template <typename T>
class GeneratorBase {
class RandomGeneratorBase {
public:
virtual KmMatrix<T> generate() = 0;
virtual KmMatrix<T> generate(size_t _size) = 0;
Expand Down
6 changes: 3 additions & 3 deletions tests/cpp/gpu/kmeans/test_kmeans_init.cu
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using namespace h2o4gpu::kMeans;
using namespace h2o4gpu::Matrix;

template <typename T>
struct GeneratorMock : GeneratorBase<T> {
struct GeneratorMock : RandomGeneratorBase<T> {
public:
KmMatrix<T> generate() override {
h2o4gpu_error("Not implemented");
Expand All @@ -46,7 +46,7 @@ TEST(KmeansRandom, Init) {
h_data[i] = i * 2;
}
KmMatrix<float> data (h_data, 4, 5);
std::unique_ptr<GeneratorBase<float>> gen (new GeneratorMock<float>());
std::unique_ptr<RandomGeneratorBase<float>> gen (new GeneratorMock<float>());
KmeansRandomInit<float> init (gen);

auto res = init(data, 2);
Expand Down Expand Up @@ -134,7 +134,7 @@ TEST(KmeansLL, GreedyRecluster) {

// r --gtest_filter=KmeansLL.KmeansLLInit
TEST(KmeansLL, KmeansLLInit) {
std::unique_ptr<GeneratorBase<double>> mock_ptr (new GeneratorMock<double>);
std::unique_ptr<RandomGeneratorBase<double>> mock_ptr (new GeneratorMock<double>);
KmeansLlInit<double> kmeans_ll_init (mock_ptr, 2.5);
thrust::host_vector<double> h_data (30);
// We split the points into two groups, but the result is statistic.
Expand Down

0 comments on commit f297074

Please sign in to comment.