Add allocate_memory and free_memory
This commit is contained in:
@@ -7,8 +7,8 @@
|
||||
|
||||
#include <benchmark/benchmark.h>
|
||||
|
||||
void init_x(std::vector<float> &x) {
|
||||
for (size_t i = 0; i < x.size(); ++i) {
|
||||
void init_x(float *x, size_t n) {
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
x[i] = (i % 360) * 0.0174533f; // degrees to radians
|
||||
}
|
||||
}
|
||||
@@ -16,24 +16,31 @@ void init_x(std::vector<float> &x) {
|
||||
template <typename Backend>
|
||||
static void benchmark_sinf(benchmark::State &state) {
|
||||
const size_t N = static_cast<size_t>(state.range(0));
|
||||
std::vector<float> x(N), s(N);
|
||||
init_x(x);
|
||||
|
||||
Backend backend;
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
backend.init(N);
|
||||
float *x =
|
||||
reinterpret_cast<float *>(backend.allocate_memory(N * sizeof(float)));
|
||||
float *s =
|
||||
reinterpret_cast<float *>(backend.allocate_memory(N * sizeof(float)));
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
state.counters["init_ms"] =
|
||||
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
|
||||
.count() /
|
||||
1.e3;
|
||||
|
||||
init_x(x, N);
|
||||
|
||||
for (auto _ : state) {
|
||||
backend.compute_sinf(N, x.data(), s.data());
|
||||
backend.compute_sinf(N, x, s);
|
||||
benchmark::DoNotOptimize(s);
|
||||
}
|
||||
|
||||
backend.free_memory(x);
|
||||
backend.free_memory(s);
|
||||
|
||||
state.SetItemsProcessed(static_cast<int64_t>(state.iterations()) *
|
||||
static_cast<int64_t>(N));
|
||||
}
|
||||
@@ -41,24 +48,31 @@ static void benchmark_sinf(benchmark::State &state) {
|
||||
template <typename Backend>
|
||||
static void benchmark_cosf(benchmark::State &state) {
|
||||
const size_t N = static_cast<size_t>(state.range(0));
|
||||
std::vector<float> x(N), c(N);
|
||||
init_x(x);
|
||||
|
||||
Backend backend;
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
backend.init(N);
|
||||
float *x =
|
||||
reinterpret_cast<float *>(backend.allocate_memory(N * sizeof(float)));
|
||||
float *c =
|
||||
reinterpret_cast<float *>(backend.allocate_memory(N * sizeof(float)));
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
state.counters["init_ms"] =
|
||||
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
|
||||
.count() /
|
||||
1.e3;
|
||||
|
||||
init_x(x, N);
|
||||
|
||||
for (auto _ : state) {
|
||||
backend.compute_cosf(N, x.data(), c.data());
|
||||
backend.compute_cosf(N, x, c);
|
||||
benchmark::DoNotOptimize(c);
|
||||
}
|
||||
|
||||
backend.free_memory(x);
|
||||
backend.free_memory(c);
|
||||
|
||||
state.SetItemsProcessed(static_cast<int64_t>(state.iterations()) *
|
||||
static_cast<int64_t>(N));
|
||||
}
|
||||
@@ -66,25 +80,35 @@ static void benchmark_cosf(benchmark::State &state) {
|
||||
template <typename Backend>
|
||||
static void benchmark_sincosf(benchmark::State &state) {
|
||||
const size_t N = static_cast<size_t>(state.range(0));
|
||||
std::vector<float> x(N), s(N), c(N);
|
||||
init_x(x);
|
||||
|
||||
Backend backend;
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
backend.init(N);
|
||||
float *x =
|
||||
reinterpret_cast<float *>(backend.allocate_memory(N * sizeof(float)));
|
||||
float *s =
|
||||
reinterpret_cast<float *>(backend.allocate_memory(N * sizeof(float)));
|
||||
float *c =
|
||||
reinterpret_cast<float *>(backend.allocate_memory(N * sizeof(float)));
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
state.counters["init_ms"] =
|
||||
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
|
||||
.count() /
|
||||
1.e3;
|
||||
|
||||
init_x(x, N);
|
||||
|
||||
for (auto _ : state) {
|
||||
backend.compute_sincosf(N, x.data(), s.data(), c.data());
|
||||
backend.compute_sincosf(N, x, s, c);
|
||||
benchmark::DoNotOptimize(s);
|
||||
benchmark::DoNotOptimize(c);
|
||||
}
|
||||
|
||||
backend.free_memory(x);
|
||||
backend.free_memory(s);
|
||||
backend.free_memory(c);
|
||||
|
||||
state.SetItemsProcessed(static_cast<int64_t>(state.iterations()) *
|
||||
static_cast<int64_t>(N));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user