From 6d13fc8de92949f05384b78b948d3b790461132b Mon Sep 17 00:00:00 2001 From: Yibo Cai Date: Thu, 29 Apr 2021 05:57:34 +0000 Subject: [PATCH 1/3] ARROW-12533: [C++] Add random real distribution function Clang with gnu libstdc++ produces code very slow in generating random real numbers on Arm64. This patch implements three random utilities based on clang libc++: - std::generate_canonical - std::random_real_distribution - std::bernoulli_distribution It brings ~100x speedup on Arm64 and ~8x on x86_64 in generating random reals when build arrow with clang + gnu libstdc++. No influence to gcc + libstdc++, or clang + libc++. --- cpp/src/arrow/testing/random.cc | 10 ++-- cpp/src/arrow/testing/random.h | 3 +- cpp/src/arrow/testing/random_test.cc | 72 +++++++++++++++++++++++++ cpp/src/arrow/testing/uniform_real.h | 78 ++++++++++++++++++++++++++++ cpp/src/arrow/testing/util.cc | 4 +- 5 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 cpp/src/arrow/testing/uniform_real.h diff --git a/cpp/src/arrow/testing/random.cc b/cpp/src/arrow/testing/random.cc index f8f5d6f10f0e..ab0d4f33245f 100644 --- a/cpp/src/arrow/testing/random.cc +++ b/cpp/src/arrow/testing/random.cc @@ -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::quiet_NaN(); // A static cast is required due to the int16 -> int8 handling. @@ -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)) { @@ -211,7 +211,8 @@ PRIMITIVE_RAND_INTEGER_IMPL(Float16, int16_t, HalfFloatType) std::shared_ptr RandomArrayGenerator::Float32(int64_t size, float min, float max, double null_probability, double nan_probability) { - using OptionType = GenerateOptions>; + using OptionType = + GenerateOptions>; OptionType options(seed(), min, max, null_probability, nan_probability); return GenerateNumericArray(size, options); } @@ -219,7 +220,8 @@ std::shared_ptr RandomArrayGenerator::Float32(int64_t size, float min, fl std::shared_ptr RandomArrayGenerator::Float64(int64_t size, double min, double max, double null_probability, double nan_probability) { - using OptionType = GenerateOptions>; + using OptionType = + GenerateOptions>; OptionType options(seed(), min, max, null_probability, nan_probability); return GenerateNumericArray(size, options); } diff --git a/cpp/src/arrow/testing/random.h b/cpp/src/arrow/testing/random.h index 11bf9017c560..9d7b48546796 100644 --- a/cpp/src/arrow/testing/random.h +++ b/cpp/src/arrow/testing/random.h @@ -25,6 +25,7 @@ #include #include +#include "arrow/testing/uniform_real.h" #include "arrow/testing/visibility.h" #include "arrow/type.h" @@ -455,7 +456,7 @@ template void random_real(int64_t n, uint32_t seed, T min_value, T max_value, std::vector* out) { std::default_random_engine gen(seed); - std::uniform_real_distribution d(min_value, max_value); + ::arrow::random::uniform_real_distribution d(min_value, max_value); out->resize(n, static_cast(0)); std::generate(out->begin(), out->end(), [&d, &gen] { return static_cast(d(gen)); }); } diff --git a/cpp/src/arrow/testing/random_test.cc b/cpp/src/arrow/testing/random_test.cc index c1e5a83a31a9..0cf1f99eab7a 100644 --- a/cpp/src/arrow/testing/random_test.cc +++ b/cpp/src/arrow/testing/random_test.cc @@ -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 { @@ -352,5 +353,76 @@ TEST(RandomList, Basics) { } } +template +class UniformRealTest : public ::testing::Test { + protected: + void VerifyDist(int seed, T a, T b) { + pcg32_fast rng(seed); + ::arrow::random::uniform_real_distribution dist(a, b); + + const int kCount = 5000; + T min = std::numeric_limits::max(); + T max = std::numeric_limits::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(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; +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, total * p * (1 - dev)); + const int max = std::min(total, 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 diff --git a/cpp/src/arrow/testing/uniform_real.h b/cpp/src/arrow/testing/uniform_real.h new file mode 100644 index 000000000000..5509c4676a2e --- /dev/null +++ b/cpp/src/arrow/testing/uniform_real.h @@ -0,0 +1,78 @@ +// 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. + +// Based on clang libc++: https://github.com/llvm/llvm-project/tree/main/libcxx + +#pragma once + +#include + +#include + +namespace arrow { +namespace random { + +namespace detail { + +// std::generate_canonical, simplified +// https://en.cppreference.com/w/cpp/numeric/random/generate_canonical +template +RealType generate_canonical(Rng& rng) { + const size_t b = std::numeric_limits::digits; + const size_t log2R = 63 - ::arrow::BitUtil::CountLeadingZeros( + static_cast(Rng::max() - Rng::min()) + 1); + const size_t k = b / log2R + (b % log2R != 0) + (b == 0); + const RealType r = static_cast(Rng::max() - Rng::min()) + 1; + RealType base = r; + RealType sp = 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 +struct uniform_real_distribution { + const RealType a, b; + + explicit uniform_real_distribution(RealType a = 0, RealType b = 1) : a(a), b(b) {} + + template + RealType operator()(Rng& rng) { + return (b - a) * detail::generate_canonical(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 + double operator()(Rng& rng) { + return detail::generate_canonical(rng) < p; + } +}; + +} // namespace random +} // namespace arrow diff --git a/cpp/src/arrow/testing/util.cc b/cpp/src/arrow/testing/util.cc index a7085e5772d4..9e3e27174790 100644 --- a/cpp/src/arrow/testing/util.cc +++ b/cpp/src/arrow/testing/util.cc @@ -55,7 +55,7 @@ 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 d(0.0, 1.0); + ::arrow::random::uniform_real_distribution d(0.0, 1.0); std::generate(null_bytes, null_bytes + n, [&d, &gen, &pct_null] { return d(gen) > pct_null; }); } @@ -63,7 +63,7 @@ void random_null_bytes(int64_t n, double pct_null, uint8_t* null_bytes) { void random_is_valid(int64_t n, double pct_null, std::vector* is_valid, int random_seed) { pcg32_fast gen(random_seed); - std::uniform_real_distribution d(0.0, 1.0); + ::arrow::random::uniform_real_distribution 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; }); From 25a83337817d64998ad52b32c0386cdb1126cc1d Mon Sep 17 00:00:00 2001 From: Yibo Cai Date: Mon, 10 May 2021 02:39:34 +0000 Subject: [PATCH 2/3] suppress msvc warning --- cpp/src/arrow/testing/random_test.cc | 4 ++-- cpp/src/arrow/testing/uniform_real.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cpp/src/arrow/testing/random_test.cc b/cpp/src/arrow/testing/random_test.cc index 0cf1f99eab7a..851088a11b9d 100644 --- a/cpp/src/arrow/testing/random_test.cc +++ b/cpp/src/arrow/testing/random_test.cc @@ -414,8 +414,8 @@ TEST(BernoulliTest, Basic) { // 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, total * p * (1 - dev)); - const int max = std::min(total, total * p * (1 + dev)); + const int min = std::max(0, static_cast(total * p * (1 - dev))); + const int max = std::min(total, static_cast(total * p * (1 + dev))); ASSERT_TRUE(cnt >= min && cnt <= max); }; diff --git a/cpp/src/arrow/testing/uniform_real.h b/cpp/src/arrow/testing/uniform_real.h index 5509c4676a2e..c7ffbabd39c8 100644 --- a/cpp/src/arrow/testing/uniform_real.h +++ b/cpp/src/arrow/testing/uniform_real.h @@ -38,7 +38,7 @@ RealType generate_canonical(Rng& rng) { const size_t k = b / log2R + (b % log2R != 0) + (b == 0); const RealType r = static_cast(Rng::max() - Rng::min()) + 1; RealType base = r; - RealType sp = rng() - Rng::min(); + RealType sp = static_cast(rng() - Rng::min()); for (size_t i = 1; i < k; ++i, base *= r) { sp += (rng() - Rng::min()) * base; } @@ -69,7 +69,7 @@ struct bernoulli_distribution { explicit bernoulli_distribution(double p = 0.5) : p(p) {} template - double operator()(Rng& rng) { + bool operator()(Rng& rng) { return detail::generate_canonical(rng) < p; } }; From 11abdc4a4fd227d196278d64af0a9012d90d4a35 Mon Sep 17 00:00:00 2001 From: Yibo Cai Date: Tue, 11 May 2021 02:39:53 +0000 Subject: [PATCH 3/3] explain motivation --- cpp/src/arrow/testing/uniform_real.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/testing/uniform_real.h b/cpp/src/arrow/testing/uniform_real.h index c7ffbabd39c8..155cb16b641a 100644 --- a/cpp/src/arrow/testing/uniform_real.h +++ b/cpp/src/arrow/testing/uniform_real.h @@ -15,7 +15,13 @@ // specific language governing permissions and limitations // under the License. -// Based on clang libc++: https://github.com/llvm/llvm-project/tree/main/libcxx +// 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