Skip to content

Add approximate counting C++ sample code #834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contents/approximate_counting/approximate_counting.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ As we do not have any objects to count, we will instead simulate the counting wi
{% method %}
{% sample lang="jl" %}
[import, lang:"julia"](code/julia/approximate_counting.jl)
{% sample lang="cpp" %}
[import, lang:"cpp"](code/c++/approximate_counting.cpp)
{% endmethod %}

### Bibliography
Expand Down
71 changes: 71 additions & 0 deletions contents/approximate_counting/code/c++/approximate_counting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <cmath>
#include <iostream>
#include <numeric>
#include <random>

// Returns a pseudo-random number generator
std::default_random_engine& rng() {
// Initialize static pseudo-random engine with non-deterministic random seed
static std::default_random_engine randEngine(std::random_device{}());
return randEngine;
}

// Returns a random double in [0, 1)
double drand() {
return std::uniform_real_distribution<double>(0.0, 1.0)(rng());
}

// This function takes
// - v: value in register
// - a: a scaling value for the logarithm based on Morris's paper
// It returns n(v,a), the approximate count
auto n(double v, double a) { return a * (pow((1 + 1 / a), v) - 1); }

// This function takes
// - v: value in register
// - a: a scaling value for the logarithm based on Morris's paper
// It returns a new value for v
auto increment(int v, double a) {
// delta is the probability of incrementing our counter
const auto delta = 1 / (n(v + 1, a) - n(v, a));
return (drand() <= delta) ? v + 1 : v;
}

// This simulates counting and takes
// - n_items: number of items to count and loop over
// - a: a scaling value for the logarithm based on Morris's paper
// It returns n(v,a), the approximate count
auto approximate_count(int n_items, double a) {
auto v = 0;
for (auto i = 0; i < n_items; ++i)
v = increment(v, a);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a warning for double to int conversion, because v is of type int and increment returns a double, which would round down the values.

I'm guessing it should be auto v = 0.0 instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to reproduce the issue, what compiler are you using, and compilation flags?


return n(v, a);
}

// This function takes
// - n_trials: the number of counting trials
// - n_items: the number of items to count to
// - a: a scaling value for the logarithm based on Morris's paper
// - threshold: the maximum percent error allowed
// It returns a "pass" / "fail" test value
auto test_approximate_count(
int n_trials, int n_items, double a, double threshold) {
auto sum = 0.0;
for (auto i = 0; i < n_trials; ++i)
sum += approximate_count(n_items, a);
const auto avg = sum / n_trials;
return std::abs((avg - n_items) / n_items) < threshold ? "pass" : "fail";
}

int main() {
std::cout << "Counting Tests, 100 trials\n";

std::cout << "testing 1,000, a = 30, 1% error "
<< test_approximate_count(100, 1000, 30, 0.1) << "\n";
std::cout << "testing 12,345, a = 10, 1% error "
<< test_approximate_count(100, 12345, 10, 0.1) << "\n";
// Note : with a lower a, we need more trials, so a higher % error here.
std::cout << "testing 222,222, a = 0.5, 10% error "
<< test_approximate_count(100, 222222, 0.5, 0.2) << "\n";
}