Add benchmark

This commit is contained in:
Jeehoon Kang
2020-07-01 14:23:41 +09:00
parent c3237276dc
commit 114f38cbb6
7 changed files with 190 additions and 1 deletions

3
bench/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/.depend
/bench
*.o

28
bench/Makefile Normal file
View File

@@ -0,0 +1,28 @@
CC=riscv64-linux-gnu-gcc-10
CXX=riscv64-linux-gnu-g++-10
KECC=../target/release/kecc
CFLAGS=-O
RM=rm -f
SRCS=$(shell find . -name "*.c")
OBJS=$(subst .c,.s,$(SRCS))
all: bench
bench: $(OBJS) driver.o
$(CXX) -o bench $(OBJS) driver.o
run: bench
qemu-riscv64-static -L /usr/riscv64-linux-gnu ./bench
driver.o: driver.cpp
$(CXX) $(CFLAGS) -o driver.o -c -I. driver.cpp
.c.s:
($(KECC) -O $< >$@) || (rm $@ -rf; exit 1)
$(KECC):
cargo build --manifest-path=../Cargo.toml --release --bin kecc
clean:
$(RM) $(OBJS) driver.o bench

75
bench/driver.cpp Normal file
View File

@@ -0,0 +1,75 @@
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <optional>
#include <vector>
#include <functional>
namespace model {
#include <exotic_arguments.c>
#include <fibonacci.c>
#include <two_dimension_array.c>
}
extern "C" {
int exotic_arguments_struct_small(model::small, int);
long exotic_arguments_struct_large(model::large, int);
float exotic_arguments_float(float, int);
double exotic_arguments_double(double, int);
int fibonacci_recursive(int, int);
int fibonacci_loop(int, int);
int two_dimension_array(int, int);
}
namespace {
inline unsigned long read_cycles()
{
unsigned long cycles;
asm volatile ("rdcycle %0" : "=r" (cycles));
return cycles;
}
template<typename I, typename O>
inline unsigned long evaluate(const char *name, I input, O (*solution)(I, int), O (*model)(I, int)) {
std::cout << "[" << name << "] ";
int nonce = 1 + (std::rand() % 100);
auto start = read_cycles();
auto output = solution(input, nonce);
auto end = read_cycles();
auto expected = model(input, nonce);
if (output != expected) {
std::cout << "mismatched result (expected: " << expected << ", actual: " << output << ")" << std::endl;
std::exit(1);
}
auto cycles = end - start;
std::cout << cycles << std::endl;
return cycles;
}
}
int main() {
std::srand(static_cast<unsigned>(time(NULL)));
std::vector<unsigned long> cycles;
cycles.push_back(evaluate("exotic_arguments_struct_small", model::small { .a = 3, .b = 4 }, exotic_arguments_struct_small, model::exotic_arguments_struct_small));
cycles.push_back(evaluate("exotic_arguments_struct_large", model::large { .a = 5, .b = 6, .c = 7, .d = 8, .e = 9, .f = 10, .g = 11, .h = 12 }, exotic_arguments_struct_large, model::exotic_arguments_struct_large));
cycles.push_back(evaluate("exotic_arguments_float", 0.42f, exotic_arguments_float, model::exotic_arguments_float));
cycles.push_back(evaluate("exotic_arguments_double", 0.42, exotic_arguments_double, model::exotic_arguments_double));
cycles.push_back(evaluate("fibonacci_recursive", 30, fibonacci_recursive, model::fibonacci_recursive));
cycles.push_back(evaluate("fibonacci_loop", 30, fibonacci_loop, model::fibonacci_loop));
cycles.push_back(evaluate("two_dimension_array", 100, two_dimension_array, model::two_dimension_array));
double average = 1.0;
for (auto cycle: cycles) {
average *= static_cast<double>(cycle);
}
average = std::pow(average, 1 / static_cast<double>(cycles.size()));
std::cout << "[AVERAGE] " << average << std::endl;
return 0;
}

31
bench/exotic_arguments.c Normal file
View File

@@ -0,0 +1,31 @@
typedef struct {
int a;
int b;
} small;
typedef struct {
long a;
long b;
long c;
long d;
long e;
long f;
long g;
long h;
} large;
int exotic_arguments_struct_small(small a, int nonce) {
return a.a + a.b + nonce;
}
long exotic_arguments_struct_large(large a, int nonce) {
return a.a + a.b + a.c + a.d + a.e + a.f + a.g + a.h + nonce;
}
float exotic_arguments_float(float a, int nonce) {
return a + (float) nonce;
}
double exotic_arguments_double(double a, int nonce) {
return a + (double) nonce;
}

20
bench/fibonacci.c Normal file
View File

@@ -0,0 +1,20 @@
int fibonacci_loop(int n, int nonce) {
int x = nonce;
int y = nonce;
for (int i = 1; i < n; ++i) {
int newy = x + y;
x = y;
y = newy;
}
return y;
}
int fibonacci_recursive(int n, int nonce) {
if (n < 2) {
return nonce;
}
return fibonacci_recursive(n - 1, nonce) + fibonacci_recursive(n - 2, nonce);
}

View File

@@ -0,0 +1,24 @@
int two_dimension_array_arr[100];
int two_dimension_array(int n, int nonce) {
if (!(n <= 100)) {
return nonce;
}
for (int i = 0; i < n; ++i) {
two_dimension_array_arr[i] = i + nonce;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; i < n; ++i) {
two_dimension_array_arr[i] += two_dimension_array_arr[j];
}
}
int result = 0;
for (int i = 0; i < n; ++i) {
result += two_dimension_array_arr[i];
}
return result;
}