diff --git a/src/common/bench/bench.c b/src/common/bench/bench.c index 524032d..13e426c 100644 --- a/src/common/bench/bench.c +++ b/src/common/bench/bench.c @@ -67,3 +67,14 @@ uint32_t checksum(void *start, void *end) { hash += hash << 5; return hash; } + +static uint32_t seed = 1; + +void bench_srand(uint32_t _seed) { + seed = _seed & 0x7fff; +} + +uint32_t bench_rand() { + seed = (seed * (uint32_t)214013L + (uint32_t)2531011L); + return (seed >> 16) & 0x7fff; +} diff --git a/src/common/bench/include/bench.h b/src/common/bench/include/bench.h index b96a8eb..9028a60 100644 --- a/src/common/bench/include/bench.h +++ b/src/common/bench/include/bench.h @@ -11,6 +11,8 @@ extern "C" { uint64_t uptime(); char *format_time(uint64_t us); uint32_t checksum(void *start, void *end); +void bench_srand(uint32_t _seed); +uint32_t bench_rand(); typedef struct { void *sub_config; diff --git a/src/gemm/configs/ref-config.c b/src/gemm/configs/ref-config.c index 14987ff..fe17f25 100644 --- a/src/gemm/configs/ref-config.c +++ b/src/gemm/configs/ref-config.c @@ -1,3 +1,4 @@ #include -bench_gemm_config config = {110, 110, 110}; +bench_gemm_config config = {.m = 110, .n = 110, .k = 110, .checksum = 0x503d0fe9}; + diff --git a/src/gemm/configs/test-config.c b/src/gemm/configs/test-config.c index e1d725c..cbcf50b 100644 --- a/src/gemm/configs/test-config.c +++ b/src/gemm/configs/test-config.c @@ -1,3 +1,3 @@ #include -bench_gemm_config config = {50, 50, 50}; +bench_gemm_config config = {.m = 50, .n = 50, .k = 50, .checksum = 0x53c1fb28}; diff --git a/src/gemm/configs/train-config.c b/src/gemm/configs/train-config.c index b20fa67..17b3714 100644 --- a/src/gemm/configs/train-config.c +++ b/src/gemm/configs/train-config.c @@ -1,3 +1,3 @@ #include -bench_gemm_config config = {40, 40, 40}; +bench_gemm_config config = {.m = 40, .n = 40, .k = 40, .checksum = 0x1efebe93}; diff --git a/src/gemm/gemm.c b/src/gemm/gemm.c index d7c6a4d..a8432c4 100644 --- a/src/gemm/gemm.c +++ b/src/gemm/gemm.c @@ -15,7 +15,7 @@ void serial_init(int m, int n, double *a, int lda) { void random_init(int m, int n, double *a, int lda) { for (int j = 0; j < n; j++) { for (int i = 0; i < m; i++) - A(i, j) = 2.0 * rand() - 1.0; + A(i, j) = 2.0 * bench_rand() - 1.0; } } @@ -41,8 +41,9 @@ int main() { memset(C, 0, m * n * sizeof(double)); uint64_t start_time, end_time; - srand(1556); + bench_srand(1556); + //Because we init A and B randomly, the checksum of C will be different. random_init(m, k, A, m); random_init(k, n, B, k); @@ -50,10 +51,12 @@ int main() { matmul(m, n, k, A, m, B, k, C, m); end_time = uptime(); + bench_free(A); bench_free(B); bench_free(C); + BENCH_LOG(INFO, "OpenPerf time: %s", format_time(end_time - start_time)); return 0; } diff --git a/src/gemm/include/gemm.h b/src/gemm/include/gemm.h index 6ace54b..795b34f 100644 --- a/src/gemm/include/gemm.h +++ b/src/gemm/include/gemm.h @@ -10,6 +10,7 @@ typedef struct { uint32_t m; uint32_t n; uint32_t k; + uint64_t checksum; } bench_gemm_config; void AddDot4x4(int, double *, int, double *, int, double *, int); diff --git a/src/tcc/configs/ref-config.c b/src/tcc/configs/ref-config.c index e69de29..e8277d8 100644 --- a/src/tcc/configs/ref-config.c +++ b/src/tcc/configs/ref-config.c @@ -0,0 +1,18 @@ +#include "../config.h" +#include + +Finfo file_table[] = { + {"/share/trm.c", 269, 0, NULL, NULL}, + {"/share/trap.h", 110, 269, NULL, NULL}, + {"/share/ref.c", 1844, 379, NULL, NULL}, + {"/share/ref", 21332, 2223, NULL, NULL} +}; + +int tcc_argc1 = 10; +char *tcc_argv1[] = { + "./tcc", "/share/trm.c", "/share/ref.c", "-ffreestanding", + "-nostdlib", "-o", "/share/ref", "-Wl,-Ttext=0x80000000", + "-O2", "-static" +}; + +bench_tcc_config config = {.file_count = 4}; diff --git a/src/tcc/input/ref b/src/tcc/input/ref new file mode 100755 index 0000000..f8cc33e Binary files /dev/null and b/src/tcc/input/ref differ diff --git a/src/tcc/input/ref.c b/src/tcc/input/ref.c new file mode 100644 index 0000000..6613270 --- /dev/null +++ b/src/tcc/input/ref.c @@ -0,0 +1,87 @@ +#include "trap.h" + +typedef unsigned int uint32_t; +typedef unsigned char uint8_t; +typedef uint32_t size_t; +#define NULL 0 +#define LENGTH(x) sizeof(x) / sizeof(x[0]) + +void *memset(void *s, int c, size_t n) { + char *p = NULL; + if (s == NULL) + return s; + + p = (char *)s; + while (n > 0) { + *(p++) = c; + n--; + } + return s; +} + +int add(int a, int b) { return a + b; } +int sub(int a, int b) { return a - b; } +int mul(int a, int b) { return a * b; } +int div_safe(int a, int b) { return b != 0 ? a / b : 0; } + +int test_data[] = {0, 1, 2, 3, 0x7fffffff, 0x80000000, 0x80000001, 0xffffffff}; +int ans_data[1000] = {0}; + +#define NR_DATA 1000 + +int add_main() { + int i, j; + for (i = 0; i < NR_DATA; i++) { + for (j = 0; j < NR_DATA; j++) { + int expected = add(test_data[i % 8], test_data[j % 8]); + check(add(test_data[i % 8], test_data[j % 8]) == expected); + } + } + return 0; +} + +int sub_main() { + int i, j; + for (i = 0; i < NR_DATA; i++) { + for (j = 0; j < NR_DATA; j++) { + int expected = sub(test_data[i % 8], test_data[j % 8]); + check(sub(test_data[i % 8], test_data[j % 8]) == expected); + } + } + return 0; +} + +int mul_main() { + int i, j; + for (i = 0; i < NR_DATA; i++) { + for (j = 0; j < NR_DATA; j++) { + int expected = mul(test_data[i % 8], test_data[j % 8]); + check(mul(test_data[i % 8], test_data[j % 8]) == expected); + } + } + return 0; +} + +int div_main() { + int i, j; + for (i = 0; i < NR_DATA; i++) { + for (j = 0; j < NR_DATA; j++) { + int expected = div_safe(test_data[i % 8], test_data[(j % 7) + 1]); + check(div_safe(test_data[i % 8], test_data[(j % 7) + 1]) == expected); + } + } + return 0; +} + +int main() { + memset(ans_data, 0, sizeof(ans_data)); + + for (int k = 0; k < 10; k++) { + add_main(); + sub_main(); + mul_main(); + div_main(); + } + return 0; +} + diff --git a/src/tcc/resources/resources-ref.S b/src/tcc/resources/resources-ref.S index e69de29..53c68f1 100644 --- a/src/tcc/resources/resources-ref.S +++ b/src/tcc/resources/resources-ref.S @@ -0,0 +1,8 @@ +.section .data +.global ramdisk_start, ramdisk_end +ramdisk_start: +.incbin "input/trm.c" +.incbin "input/trap.h" +.incbin "input/ref.c" +.incbin "input/ref" +ramdisk_end: