Skip to content

Commit

Permalink
[rand] make portable simplification of uniform_int_distribution for #35
Browse files Browse the repository at this point in the history
  • Loading branch information
sbeamer committed Oct 6, 2023
1 parent 30197cf commit bd39534
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ template<typename GraphT_>
class SourcePicker {
public:
explicit SourcePicker(const GraphT_ &g, NodeID given_source = -1)
: given_source(given_source), rng(kRandSeed), udist(0, g.num_nodes()-1),
g_(g) {}
: given_source_(given_source), rng_(kRandSeed),
udist_(g.num_nodes()-1, rng_), g_(g) {}

NodeID PickNext() {
if (given_source != -1)
return given_source;
if (given_source_ != -1)
return given_source_;
NodeID source;
do {
source = udist(rng);
source = udist_();
} while (g_.out_degree(source) == 0);
return source;
}

private:
NodeID given_source;
std::mt19937 rng;
std::uniform_int_distribution<NodeID> udist;
NodeID given_source_;
std::mt19937 rng_;
UniDist udist_;
const GraphT_ &g_;
};

Expand Down
46 changes: 41 additions & 5 deletions src/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define GENERATOR_H_

#include <algorithm>
#include <cassert>
#include <cinttypes>
#include <random>

Expand All @@ -28,6 +29,41 @@ Given scale and degree, generates edgelist for synthetic graph
*/


// maps to range [0,max_value], tailored to std::mt19937
class UniDist {
public:
UniDist(uint32_t max_value, std::mt19937 &rng): rng_(rng) {
uint64_t rng_range = 1l << 32;
assert(rng_.min() == 0);
assert(rng_.max() == rng_range - 1);
no_mod_ = rng_.max() == max_value;
mod_ = max_value + 1;
uint64_t remainder = rng_range % mod_;
if (remainder == 0)
cutoff_ = 0;
else
cutoff_ = rng_.max() - remainder;
}

uint32_t operator()() {
uint32_t rand_num = rng_();
if (no_mod_)
return rand_num;
if (cutoff_ != 0) {
while (rand_num >= cutoff_)
rand_num = rng_();
}
return rand_num % mod_;
}

private:
std::mt19937 &rng_;
bool no_mod_;
uint32_t mod_;
uint32_t cutoff_;
};


template <typename NodeID_, typename DestID_ = NodeID_,
typename WeightT_ = NodeID_>
class Generator {
Expand Down Expand Up @@ -66,12 +102,12 @@ class Generator {
#pragma omp parallel
{
std::mt19937 rng;
std::uniform_int_distribution<NodeID_> udist(0, num_nodes_-1);
#pragma omp for
UniDist udist(num_nodes_-1, rng);
#pragma omp for
for (int64_t block=0; block < num_edges_; block+=block_size) {
rng.seed(kRandSeed + block/block_size);
for (int64_t e=block; e < std::min(block+block_size, num_edges_); e++) {
el[e] = Edge(udist(rng), udist(rng));
el[e] = Edge(udist(), udist());
}
}
}
Expand Down Expand Up @@ -133,13 +169,13 @@ class Generator {
#pragma omp parallel
{
std::mt19937 rng;
std::uniform_int_distribution<int> udist(1, 255);
UniDist udist(254, rng);
int64_t el_size = el.size();
#pragma omp for
for (int64_t block=0; block < el_size; block+=block_size) {
rng.seed(kRandSeed + block/block_size);
for (int64_t e=block; e < std::min(block+block_size, el_size); e++) {
el[e].v.w = static_cast<WeightT_>(udist(rng));
el[e].v.w = static_cast<WeightT_>(udist()+1);
}
}
}
Expand Down

0 comments on commit bd39534

Please sign in to comment.