Skip to content
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

Fix HEXL benchmark performance degradation #416

Merged
merged 1 commit into from
Nov 12, 2021
Merged
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
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