Skip to content
Closed
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
10 changes: 6 additions & 4 deletions cpp/src/arrow/testing/random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct GenerateOptions {
}
pcg32_fast rng(seed_++);
DistributionType dist(min_, max_);
std::bernoulli_distribution nan_dist(nan_probability_);
::arrow::random::bernoulli_distribution nan_dist(nan_probability_);
const ValueType nan_value = std::numeric_limits<ValueType>::quiet_NaN();

// A static cast is required due to the int16 -> int8 handling.
Expand All @@ -102,7 +102,7 @@ struct GenerateOptions {
void GenerateBitmap(uint8_t* buffer, size_t n, int64_t* null_count) {
int64_t count = 0;
pcg32_fast rng(seed_++);
std::bernoulli_distribution dist(1.0 - probability_);
::arrow::random::bernoulli_distribution dist(1.0 - probability_);

for (size_t i = 0; i < n; i++) {
if (dist(rng)) {
Expand Down Expand Up @@ -211,15 +211,17 @@ PRIMITIVE_RAND_INTEGER_IMPL(Float16, int16_t, HalfFloatType)
std::shared_ptr<Array> RandomArrayGenerator::Float32(int64_t size, float min, float max,
double null_probability,
double nan_probability) {
using OptionType = GenerateOptions<float, std::uniform_real_distribution<float>>;
using OptionType =
GenerateOptions<float, ::arrow::random::uniform_real_distribution<float>>;
OptionType options(seed(), min, max, null_probability, nan_probability);
return GenerateNumericArray<FloatType, OptionType>(size, options);
}

std::shared_ptr<Array> RandomArrayGenerator::Float64(int64_t size, double min, double max,
double null_probability,
double nan_probability) {
using OptionType = GenerateOptions<double, std::uniform_real_distribution<double>>;
using OptionType =
GenerateOptions<double, ::arrow::random::uniform_real_distribution<double>>;
OptionType options(seed(), min, max, null_probability, nan_probability);
return GenerateNumericArray<DoubleType, OptionType>(size, options);
}
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/arrow/testing/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <random>
#include <vector>

#include "arrow/testing/uniform_real.h"
#include "arrow/testing/visibility.h"
#include "arrow/type.h"

Expand Down Expand Up @@ -455,7 +456,7 @@ template <typename T, typename U>
void random_real(int64_t n, uint32_t seed, T min_value, T max_value,
std::vector<U>* out) {
std::default_random_engine gen(seed);
std::uniform_real_distribution<T> d(min_value, max_value);
::arrow::random::uniform_real_distribution<T> d(min_value, max_value);
out->resize(n, static_cast<T>(0));
std::generate(out->begin(), out->end(), [&d, &gen] { return static_cast<U>(d(gen)); });
}
Expand Down
72 changes: 72 additions & 0 deletions cpp/src/arrow/testing/random_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "arrow/type.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/key_value_metadata.h"
#include "arrow/util/pcg_random.h"

namespace arrow {

Expand Down Expand Up @@ -352,5 +353,76 @@ TEST(RandomList, Basics) {
}
}

template <typename T>
class UniformRealTest : public ::testing::Test {
protected:
void VerifyDist(int seed, T a, T b) {
pcg32_fast rng(seed);
::arrow::random::uniform_real_distribution<T> dist(a, b);

const int kCount = 5000;
T min = std::numeric_limits<T>::max();
T max = std::numeric_limits<T>::lowest();
double sum = 0;
double square_sum = 0;
for (int i = 0; i < kCount; ++i) {
const T v = dist(rng);
min = std::min(min, v);
max = std::max(max, v);
sum += v;
square_sum += static_cast<double>(v) * v;
}

ASSERT_GE(min, a);
ASSERT_LT(max, b);

// verify E(X), E(X^2) is near theory
const double E_X = (a + b) / 2.0;
const double E_X2 = 1.0 / 12 * (a - b) * (a - b) + E_X * E_X;
ASSERT_NEAR(sum / kCount, E_X, std::abs(E_X) * 0.02);
ASSERT_NEAR(square_sum / kCount, E_X2, E_X2 * 0.02);
}
};

using RealCTypes = ::testing::Types<float, double>;
TYPED_TEST_SUITE(UniformRealTest, RealCTypes);

TYPED_TEST(UniformRealTest, Basic) {
int seed = 42;
this->VerifyDist(seed++, 0, 1);
this->VerifyDist(seed++, -3, 1);
this->VerifyDist(seed++, -123456, 654321);
}

TEST(BernoulliTest, Basic) {
int seed = 42;

// count #trues (values less than p), p = 0 ~ 1
auto count = [&seed](double p, int total) {
pcg32_fast rng(seed++);
::arrow::random::bernoulli_distribution dist(p);
int cnt = 0;
for (int i = 0; i < total; ++i) {
cnt += dist(rng);
}
return cnt;
};

ASSERT_EQ(count(0, 1000), 0);
ASSERT_EQ(count(1, 1000), 1000);

// verify #trues is near p*total
auto verify = [&count](double p, int total, double dev) {
const int cnt = count(p, total);
const int min = std::max(0, static_cast<int>(total * p * (1 - dev)));
const int max = std::min(total, static_cast<int>(total * p * (1 + dev)));
ASSERT_TRUE(cnt >= min && cnt <= max);
};

for (double p = 0.1; p < 0.95; p += 0.1) {
verify(p, 5000, 0.1);
}
}

} // namespace random
} // namespace arrow
84 changes: 84 additions & 0 deletions cpp/src/arrow/testing/uniform_real.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// Random real generation is very slow on Arm if built with clang + libstdc++
// due to software emulated long double arithmetic.
// This file ports some random real libs from llvm libc++ library, which are
// free from long double calculation.
// It improves performance significantly on both Arm (~100x) and x86 (~8x) in
// generating random reals when built with clang + gnu libstdc++.
// Based on: https://github.com/llvm/llvm-project/tree/main/libcxx

#pragma once

#include <limits>

#include <arrow/util/bit_util.h>

namespace arrow {
namespace random {

namespace detail {

// std::generate_canonical, simplified
// https://en.cppreference.com/w/cpp/numeric/random/generate_canonical
template <typename RealType, typename Rng>
RealType generate_canonical(Rng& rng) {
const size_t b = std::numeric_limits<RealType>::digits;
const size_t log2R = 63 - ::arrow::BitUtil::CountLeadingZeros(
static_cast<uint64_t>(Rng::max() - Rng::min()) + 1);
const size_t k = b / log2R + (b % log2R != 0) + (b == 0);
const RealType r = static_cast<RealType>(Rng::max() - Rng::min()) + 1;
RealType base = r;
RealType sp = static_cast<RealType>(rng() - Rng::min());
for (size_t i = 1; i < k; ++i, base *= r) {
sp += (rng() - Rng::min()) * base;
}
return sp / base;
}

} // namespace detail

// std::uniform_real_distribution, simplified
// https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution
template <typename RealType = double>
struct uniform_real_distribution {
const RealType a, b;

explicit uniform_real_distribution(RealType a = 0, RealType b = 1) : a(a), b(b) {}

template <typename Rng>
RealType operator()(Rng& rng) {
return (b - a) * detail::generate_canonical<RealType>(rng) + a;
}
};

// std::bernoulli_distribution, simplified
// https://en.cppreference.com/w/cpp/numeric/random/bernoulli_distribution
struct bernoulli_distribution {
const double p;

explicit bernoulli_distribution(double p = 0.5) : p(p) {}

template <class Rng>
bool operator()(Rng& rng) {
return detail::generate_canonical<double>(rng) < p;
}
};

} // namespace random
} // namespace arrow
4 changes: 2 additions & 2 deletions cpp/src/arrow/testing/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ uint64_t random_seed() {
void random_null_bytes(int64_t n, double pct_null, uint8_t* null_bytes) {
const int random_seed = 0;
pcg32_fast gen(random_seed);
std::uniform_real_distribution<double> d(0.0, 1.0);
::arrow::random::uniform_real_distribution<double> d(0.0, 1.0);
std::generate(null_bytes, null_bytes + n,
[&d, &gen, &pct_null] { return d(gen) > pct_null; });
}

void random_is_valid(int64_t n, double pct_null, std::vector<bool>* is_valid,
int random_seed) {
pcg32_fast gen(random_seed);
std::uniform_real_distribution<double> d(0.0, 1.0);
::arrow::random::uniform_real_distribution<double> d(0.0, 1.0);
is_valid->resize(n, false);
std::generate(is_valid->begin(), is_valid->end(),
[&d, &gen, &pct_null] { return d(gen) > pct_null; });
Expand Down