randen_benchmarks.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. #include "absl/random/internal/randen.h"
  16. #include <cstdint>
  17. #include <cstdio>
  18. #include <cstring>
  19. #include "absl/base/internal/raw_logging.h"
  20. #include "absl/random/internal/nanobenchmark.h"
  21. #include "absl/random/internal/platform.h"
  22. #include "absl/random/internal/randen_engine.h"
  23. #include "absl/random/internal/randen_hwaes.h"
  24. #include "absl/random/internal/randen_slow.h"
  25. #include "absl/strings/numbers.h"
  26. namespace {
  27. using absl::random_internal::Randen;
  28. using absl::random_internal::RandenHwAes;
  29. using absl::random_internal::RandenSlow;
  30. using absl::random_internal_nanobenchmark::FuncInput;
  31. using absl::random_internal_nanobenchmark::FuncOutput;
  32. using absl::random_internal_nanobenchmark::InvariantTicksPerSecond;
  33. using absl::random_internal_nanobenchmark::MeasureClosure;
  34. using absl::random_internal_nanobenchmark::Params;
  35. using absl::random_internal_nanobenchmark::PinThreadToCPU;
  36. using absl::random_internal_nanobenchmark::Result;
  37. // Local state parameters.
  38. static constexpr size_t kStateSizeT = Randen::kStateBytes / sizeof(uint64_t);
  39. static constexpr size_t kSeedSizeT = Randen::kSeedBytes / sizeof(uint32_t);
  40. // Randen implementation benchmarks.
  41. template <typename T>
  42. struct AbsorbFn : public T {
  43. mutable uint64_t state[kStateSizeT] = {};
  44. mutable uint32_t seed[kSeedSizeT] = {};
  45. static constexpr size_t bytes() { return sizeof(seed); }
  46. FuncOutput operator()(const FuncInput num_iters) const {
  47. for (size_t i = 0; i < num_iters; ++i) {
  48. this->Absorb(seed, state);
  49. }
  50. return state[0];
  51. }
  52. };
  53. template <typename T>
  54. struct GenerateFn : public T {
  55. mutable uint64_t state[kStateSizeT];
  56. GenerateFn() { std::memset(state, 0, sizeof(state)); }
  57. static constexpr size_t bytes() { return sizeof(state); }
  58. FuncOutput operator()(const FuncInput num_iters) const {
  59. const auto* keys = this->GetKeys();
  60. for (size_t i = 0; i < num_iters; ++i) {
  61. this->Generate(keys, state);
  62. }
  63. return state[0];
  64. }
  65. };
  66. template <typename UInt>
  67. struct Engine {
  68. mutable absl::random_internal::randen_engine<UInt> rng;
  69. static constexpr size_t bytes() { return sizeof(UInt); }
  70. FuncOutput operator()(const FuncInput num_iters) const {
  71. for (size_t i = 0; i < num_iters - 1; ++i) {
  72. rng();
  73. }
  74. return rng();
  75. }
  76. };
  77. template <size_t N>
  78. void Print(const char* name, const size_t n, const Result (&results)[N],
  79. const size_t bytes) {
  80. if (n == 0) {
  81. ABSL_RAW_LOG(
  82. WARNING,
  83. "WARNING: Measurement failed, should not happen when using "
  84. "PinThreadToCPU unless the region to measure takes > 1 second.\n");
  85. return;
  86. }
  87. static const double ns_per_tick = 1e9 / InvariantTicksPerSecond();
  88. static constexpr const double kNsPerS = 1e9; // ns/s
  89. static constexpr const double kMBPerByte = 1.0 / 1048576.0; // Mb / b
  90. static auto header = [] {
  91. return printf("%20s %8s: %12s ticks; %9s (%9s) %8s\n", "Name", "Count",
  92. "Total", "Variance", "Time", "bytes/s");
  93. }();
  94. (void)header;
  95. for (size_t i = 0; i < n; ++i) {
  96. const double ticks_per_call = results[i].ticks / results[i].input;
  97. const double ns_per_call = ns_per_tick * ticks_per_call;
  98. const double bytes_per_ns = bytes / ns_per_call;
  99. const double mb_per_s = bytes_per_ns * kNsPerS * kMBPerByte;
  100. // Output
  101. printf("%20s %8zu: %12.2f ticks; MAD=%4.2f%% (%6.1f ns) %8.1f Mb/s\n",
  102. name, results[i].input, results[i].ticks,
  103. results[i].variability * 100.0, ns_per_call, mb_per_s);
  104. }
  105. }
  106. // Fails here
  107. template <typename Op, size_t N>
  108. void Measure(const char* name, const FuncInput (&inputs)[N]) {
  109. Op op;
  110. Result results[N];
  111. Params params;
  112. params.verbose = false;
  113. params.max_evals = 6; // avoid test timeout
  114. const size_t num_results = MeasureClosure(op, inputs, N, results, params);
  115. Print(name, num_results, results, op.bytes());
  116. }
  117. // unpredictable == 1 but the compiler does not know that.
  118. void RunAll(const int argc, char* argv[]) {
  119. if (argc == 2) {
  120. int cpu = -1;
  121. if (!absl::SimpleAtoi(argv[1], &cpu)) {
  122. ABSL_RAW_LOG(FATAL, "The optional argument must be a CPU number >= 0.\n");
  123. }
  124. PinThreadToCPU(cpu);
  125. }
  126. // The compiler cannot reduce this to a constant.
  127. const FuncInput unpredictable = (argc != 999);
  128. static const FuncInput inputs[] = {unpredictable * 100, unpredictable * 1000};
  129. #if !defined(ABSL_INTERNAL_DISABLE_AES) && ABSL_HAVE_ACCELERATED_AES
  130. Measure<AbsorbFn<RandenHwAes>>("Absorb (HwAes)", inputs);
  131. #endif
  132. Measure<AbsorbFn<RandenSlow>>("Absorb (Slow)", inputs);
  133. #if !defined(ABSL_INTERNAL_DISABLE_AES) && ABSL_HAVE_ACCELERATED_AES
  134. Measure<GenerateFn<RandenHwAes>>("Generate (HwAes)", inputs);
  135. #endif
  136. Measure<GenerateFn<RandenSlow>>("Generate (Slow)", inputs);
  137. // Measure the production engine.
  138. static const FuncInput inputs1[] = {unpredictable * 1000,
  139. unpredictable * 10000};
  140. Measure<Engine<uint64_t>>("randen_engine<uint64_t>", inputs1);
  141. Measure<Engine<uint32_t>>("randen_engine<uint32_t>", inputs1);
  142. }
  143. } // namespace
  144. int main(int argc, char* argv[]) {
  145. RunAll(argc, argv);
  146. return 0;
  147. }