Skip to content

Commit 3a39304

Browse files
committed
seed rand with processor clock
1 parent 7d37fed commit 3a39304

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

libc/benchmarks/gpu/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ function(add_benchmark benchmark_name)
2222
${BENCHMARK_LINK_LIBRARIES}
2323
DEPENDS
2424
libc.src.stdio.printf
25+
libc.src.stdlib.srand
26+
libc.src.stdlib.rand
2527
${BENCHMARK_DEPENDS}
2628
${BENCHMARK_UNPARSED_ARGUMENTS}
2729
COMPILE_OPTIONS
@@ -62,6 +64,7 @@ add_unittest_framework_library(
6264
libc.src.__support.fixedvector
6365
libc.src.time.clock
6466
libc.src.stdlib.rand
67+
libc.src.stdlib.srand
6568
libc.benchmarks.gpu.timing.timing
6669
libc.src.stdio.printf
6770
)

libc/benchmarks/gpu/LibcGpuBenchmark.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "src/__support/fixedvector.h"
99
#include "src/__support/macros/config.h"
1010
#include "src/stdio/printf.h"
11+
#include "src/stdlib/srand.h"
1112
#include "src/time/gpu/time_utils.h"
1213

1314
namespace LIBC_NAMESPACE_DECL {
@@ -133,6 +134,29 @@ void print_header() {
133134
"--------------------------------\n");
134135
}
135136

137+
// We want our random values to be approximately
138+
// |real value| <= 2^(max_exponent) * (1 + (random 52 bits) * 2^-52) <
139+
// 2^(max_exponent + 1)
140+
// The largest integer that can be stored in a double is 2^53
141+
static constexpr int MAX_EXPONENT = 52;
142+
143+
static double get_rand() {
144+
using FPBits = LIBC_NAMESPACE::fputil::FPBits<double>;
145+
uint64_t bits = LIBC_NAMESPACE::rand();
146+
double scale = 0.5 + MAX_EXPONENT / 2048.0;
147+
FPBits fp(bits);
148+
fp.set_biased_exponent(
149+
static_cast<uint32_t>(fp.get_biased_exponent() * scale));
150+
return fp.get_val();
151+
}
152+
153+
static void init_random_input() {
154+
LIBC_NAMESPACE::srand(LIBC_NAMESPACE::gpu::processor_clock());
155+
for (int i = 0; i < RANDOM_INPUT_SIZE; i++) {
156+
random_input[i] = get_rand();
157+
}
158+
}
159+
136160
void Benchmark::run_benchmarks() {
137161
uint64_t id = gpu::get_thread_id();
138162

libc/benchmarks/gpu/LibcGpuBenchmark.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,30 +109,9 @@ class Benchmark {
109109
}
110110
};
111111

112-
// We want our random values to be approximately
113-
// |real value| <= 2^(max_exponent) * (1 + (random 52 bits) * 2^-52) <
114-
// 2^(max_exponent + 1)
115-
// The largest integer that can be stored in a double is 2^53
116-
static constexpr int MAX_EXPONENT = 52;
117112
static constexpr int RANDOM_INPUT_SIZE = 1024;
118113
static cpp::array<double, RANDOM_INPUT_SIZE> random_input;
119114

120-
static double get_rand() {
121-
using FPBits = LIBC_NAMESPACE::fputil::FPBits<double>;
122-
uint64_t bits = LIBC_NAMESPACE::rand();
123-
double scale = 0.5 + MAX_EXPONENT / 2048.0;
124-
FPBits fp(bits);
125-
fp.set_biased_exponent(
126-
static_cast<uint32_t>(fp.get_biased_exponent() * scale));
127-
return fp.get_val();
128-
}
129-
130-
static void init_random_input() {
131-
for (int i = 0; i < RANDOM_INPUT_SIZE; i++) {
132-
random_input[i] = get_rand();
133-
}
134-
}
135-
136115
template <typename T> class MathPerf {
137116
using FPBits = fputil::FPBits<T>;
138117
using StorageType = typename FPBits::StorageType;

libc/benchmarks/gpu/src/math/sin_benchmark.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ uint64_t get_bits(double x) {
1616
return LIBC_NAMESPACE::cpp::bit_cast<uint64_t>(x);
1717
}
1818

19-
// BENCHMARK() expects a function that with no parameters that returns a
19+
// BENCHMARK() expects a function with no parameters that returns a
2020
// uint64_t representing the latency. Defining each benchmark using macro that
21-
// expands to a lambda to allow us to switch the implementation of `sin()` to
22-
// easily register NVPTX benchmarks.
21+
// expands to a lambda to allow us to switch the implementation of `sin()` and
22+
// easily register vendor-specific benchmarks.
2323
#define BM_RANDOM_INPUT(Func) \
2424
[]() { \
2525
uint64_t total_time = 0; \

0 commit comments

Comments
 (0)