Skip to content

Commit

Permalink
Merge pull request #416 from fboemer/fboemer/faster-benchmark-random
Browse files Browse the repository at this point in the history
Fix HEXL benchmark performance degradation
  • Loading branch information
Wei Dai authored Nov 12, 2021
2 parents 39b0986 + 982fd35 commit 66ed03d
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions native/bench/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ namespace sealbench
}
evaluator_ = std::make_shared<seal::Evaluator>(context_);

seal::prng_seed_type seed{};
context_.key_context_data()->parms().random_generator()->create()->generate(
seal::prng_seed_byte_count, reinterpret_cast<seal::seal_byte *>(seed.data()));
prng_ = seal::UniformRandomGeneratorFactory::DefaultFactory()->create(seed);

pt_.resize(std::size_t(2));
for (std::size_t i = 0; i < 2; i++)
{
Expand Down Expand Up @@ -120,11 +115,6 @@ namespace sealbench
return evaluator_;
}

SEAL_NODISCARD std::shared_ptr<seal::UniformRandomGenerator> prng()
{
return prng_;
}

SEAL_NODISCARD seal::SecretKey &sk()
{
return sk_;
Expand Down Expand Up @@ -204,16 +194,13 @@ namespace sealbench
*/
void randomize_array_mod(std::uint64_t *data, std::size_t count, const seal::Modulus &modulus)
{
constexpr std::uint64_t max_random = static_cast<std::uint64_t>(0xFFFFFFFFFFFFFFFFULL);
prng_->generate(count * sizeof(std::uint64_t), reinterpret_cast<seal::seal_byte *>(data));
std::uint64_t max_multiple = max_random - seal::util::barrett_reduce_64(max_random, modulus) - 1;
std::transform(data, data + count, data, [&](std::uint64_t rand) {
while (rand >= max_multiple)
{
prng_->generate(sizeof(std::uint64_t), reinterpret_cast<seal::seal_byte *>(&rand));
}
return seal::util::barrett_reduce_64(rand, modulus);
});
// Avoid using seal::UniformRandomGenerator, as it's slow enough to
// degrade performance with HEXL on some systems, due to AVX512 transitions.
// See https://travisdowns.github.io/blog/2020/01/17/avxfreq1.html#voltage-only-transitions.
std::random_device rd;
std::mt19937_64 generator(rd());
std::uniform_int_distribution<std::uint64_t> dist(0, modulus.value() - 1);
std::generate(data, data + count, [&]() { return dist(generator); });
}

/**
Expand Down Expand Up @@ -319,7 +306,6 @@ namespace sealbench
std::shared_ptr<seal::BatchEncoder> batch_encoder_{ nullptr };
std::shared_ptr<seal::CKKSEncoder> ckks_encoder_{ nullptr };
std::shared_ptr<seal::Evaluator> evaluator_{ nullptr };
std::shared_ptr<seal::UniformRandomGenerator> prng_{ nullptr };

/**
The following data members are created as input/output containers for benchmark cases.
Expand Down

0 comments on commit 66ed03d

Please sign in to comment.