Skip to content

Commit

Permalink
Merge pull request #25 from alanvgreen/cleanup
Browse files Browse the repository at this point in the history
Cleanup: Cleanups in common C code and nmv2_first gateware
  • Loading branch information
tcal-x committed Mar 14, 2021
2 parents fb2e943 + e510e2f commit f730246
Show file tree
Hide file tree
Showing 32 changed files with 816 additions and 896 deletions.
59 changes: 27 additions & 32 deletions common/src/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,44 @@
* limitations under the License.
*/

#include "base.h"

#include <console.h>
#include <generated/csr.h>
#include <hw/common.h>
#include <console.h>
#include <irq.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <irq.h>
#include <uart.h>

#include "base.h"

void isr(void)
{
__attribute__((unused)) unsigned int irqs;
void isr(void) {
__attribute__((unused)) unsigned int irqs;

irqs = irq_pending() & irq_getmask();
irqs = irq_pending() & irq_getmask();

if (irqs & (1 << UART_INTERRUPT))
{
uart_isr();
}
if (irqs & (1 << UART_INTERRUPT)) {
uart_isr();
}
}

void init_runtime()
{
irq_setmask(0);
irq_setie(1);
uart_init();
void init_runtime() {
irq_setmask(0);
irq_setie(1);
uart_init();
}

uint32_t read_val(const char *prompt)
{
printf("%s > ", prompt);
char buf[81];
char c = readchar();
int i = 0;
while (i < 80 && c != '\r' && c != '\n')
{
buf[i++] = c;
putchar(c);
c = readchar();
}
buf[i] = '\0';
putchar('\n');
return strtol(buf, NULL, 0);
uint32_t read_val(const char* prompt) {
printf("%s > ", prompt);
char buf[81];
char c = readchar();
int i = 0;
while (i < 80 && c != '\r' && c != '\n') {
buf[i++] = c;
putchar(c);
c = readchar();
}
buf[i] = '\0';
putchar('\n');
return strtol(buf, NULL, 0);
}
7 changes: 4 additions & 3 deletions common/src/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
void init_runtime();

// Read value from console
// Uses strtol. Can accept values in hex or decimal. Also allows negative values.
uint32_t read_val(const char *prompt);
// Uses strtol. Can accept values in hex or decimal. Also allows negative
// values.
uint32_t read_val(const char* prompt);

#endif // !BASE_H
#endif // !BASE_H
188 changes: 91 additions & 97 deletions common/src/benchmarks.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,131 +14,125 @@
* limitations under the License.
*/

#include "benchmarks.h"

#include <stdio.h>

#include "assert.h"
#include "menu.h"
#include "perf.h"
#include "assert.h"
#include "benchmarks.h"

#define BUF_SIZE (128*1024) // must be at least 1024

#define BUF_SIZE (128 * 1024) // must be at least 1024

// Each project should make their own proj_menu.c, which will replace this one.

static void __attribute__ ((noinline)) do_loads_cached(void)
{
puts("Hello, Cached Load!\n");
assert(BUF_SIZE >= 1024);
int buf[BUF_SIZE];
int acc=0;
for (int j=0; j<1024; ++j) { // warmup
acc += buf[j];
static void __attribute__((noinline)) do_loads_cached(void) {
puts("Hello, Cached Load!\n");
assert(BUF_SIZE >= 1024);
int buf[BUF_SIZE];
int acc = 0;
for (int j = 0; j < 1024; ++j) { // warmup
acc += buf[j];
}
int start_time = perf_get_mcycle();
for (int i = 0; i < 1024; ++i) {
for (int j = 0; j < 1024; ++j) {
acc += buf[j];
}
int start_time = perf_get_mcycle();
for (int i=0; i<1024; ++i) {
for (int j=0; j<1024; ++j) {
acc += buf[j];
}
buf[i] = acc; // inhibit optimization
}
int end_time = perf_get_mcycle();
int total_iters = 1024*1024;
printf("Val:%d Cycles: %d Cycles/load: %d\n\n\n",
acc, end_time-start_time, (end_time-start_time) / total_iters);
buf[i] = acc; // inhibit optimization
}
int end_time = perf_get_mcycle();
int total_iters = 1024 * 1024;
printf("Val:%d Cycles: %d Cycles/load: %d\n\n\n", acc,
end_time - start_time, (end_time - start_time) / total_iters);
}


static void __attribute__ ((noinline)) do_loads_strided(void)
{
puts("Hello, Strided Load!\n");
int buf[BUF_SIZE];
int acc=0;
int start_time = perf_get_mcycle();
for (int i=0; i<64; ++i) {
// 32 bytes per cache line, so stride by 8 4-byte ints
for (int j=0; j<BUF_SIZE; j+=8) {
acc += buf[j];
}
buf[i] = acc; // inhibit optimization
static void __attribute__((noinline)) do_loads_strided(void) {
puts("Hello, Strided Load!\n");
int buf[BUF_SIZE];
int acc = 0;
int start_time = perf_get_mcycle();
for (int i = 0; i < 64; ++i) {
// 32 bytes per cache line, so stride by 8 4-byte ints
for (int j = 0; j < BUF_SIZE; j += 8) {
acc += buf[j];
}
int end_time = perf_get_mcycle();
int total_iters = 64*BUF_SIZE/8;
printf("Val:%d Cycles: %d Cycles/load: %d\n\n\n",
acc, end_time-start_time, (end_time-start_time) / total_iters);
buf[i] = acc; // inhibit optimization
}
int end_time = perf_get_mcycle();
int total_iters = 64 * BUF_SIZE / 8;
printf("Val:%d Cycles: %d Cycles/load: %d\n\n\n", acc,
end_time - start_time, (end_time - start_time) / total_iters);
}


static void __attribute__ ((noinline)) do_loads(void)
{
puts("Hello, Load!\n");
int buf[BUF_SIZE];
int acc=0;
int start_time = perf_get_mcycle();
for (int i=0; i<8; ++i) {
for (int j=0; j<BUF_SIZE; ++j) {
acc += buf[j];
}
buf[i] = acc; // inhibit optimization
static void __attribute__((noinline)) do_loads(void) {
puts("Hello, Load!\n");
int buf[BUF_SIZE];
int acc = 0;
int start_time = perf_get_mcycle();
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < BUF_SIZE; ++j) {
acc += buf[j];
}
int end_time = perf_get_mcycle();
int total_iters = 8*BUF_SIZE;
printf("Val:%d Cycles: %d Cycles/load: %d\n\n\n",
acc, end_time-start_time, (end_time-start_time) / total_iters);
buf[i] = acc; // inhibit optimization
}
int end_time = perf_get_mcycle();
int total_iters = 8 * BUF_SIZE;
printf("Val:%d Cycles: %d Cycles/load: %d\n\n\n", acc,
end_time - start_time, (end_time - start_time) / total_iters);
}


static void __attribute__ ((noinline)) do_stores(void)
{
puts("Hello, Store!\n");
int buf[BUF_SIZE];
int acc=0;
int start_time = perf_get_mcycle();
for (int i=0; i<8; ++i) {
for (int j=0; j<BUF_SIZE; ++j) {
buf[j] = i;
}
acc += buf[i]; // inhibit optimization
static void __attribute__((noinline)) do_stores(void) {
puts("Hello, Store!\n");
int buf[BUF_SIZE];
int acc = 0;
int start_time = perf_get_mcycle();
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < BUF_SIZE; ++j) {
buf[j] = i;
}
int end_time = perf_get_mcycle();
int total_iters = 8*BUF_SIZE;
printf("Val:%d Cycles: %d Cycles/store: %d\n\n\n",
acc, end_time-start_time, (end_time-start_time) / (total_iters));
acc += buf[i]; // inhibit optimization
}
int end_time = perf_get_mcycle();
int total_iters = 8 * BUF_SIZE;
printf("Val:%d Cycles: %d Cycles/store: %d\n\n\n", acc,
end_time - start_time, (end_time - start_time) / (total_iters));
}


static void __attribute__ ((noinline)) do_increment_mem(void)
{
puts("Hello, Increment!\n");
int buf[BUF_SIZE];
int acc=0;
int start_time = perf_get_mcycle();
for (int i=0; i<8; ++i) {
for (int j=0; j<BUF_SIZE; ++j) {
buf[j] += i;
}
acc += buf[i]; // inhibit optimization
static void __attribute__((noinline)) do_increment_mem(void) {
puts("Hello, Increment!\n");
int buf[BUF_SIZE];
int acc = 0;
int start_time = perf_get_mcycle();
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < BUF_SIZE; ++j) {
buf[j] += i;
}
int end_time = perf_get_mcycle();
int total_iters = 8*BUF_SIZE;
printf("Val:%d Cycles: %d Cycles/(load-add-store): %d\n\n\n",
acc, end_time-start_time, (end_time-start_time) / total_iters);
acc += buf[i]; // inhibit optimization
}
int end_time = perf_get_mcycle();
int total_iters = 8 * BUF_SIZE;
printf("Val:%d Cycles: %d Cycles/(load-add-store): %d\n\n\n", acc,
end_time - start_time, (end_time - start_time) / total_iters);
}


static struct Menu MENU = {
"Benchmarks Menu",
"benchmark",
{
MENU_ITEM('l', "sequential loads benchmark (expect one miss per cache line, one every eight accesses)", do_loads),
MENU_ITEM('c', "cached loads benchmark (expect all hits)", do_loads_cached),
MENU_ITEM('8', "strided loads benchmark (expect all misses)", do_loads_strided),
MENU_ITEM('l',
"sequential loads benchmark (expect one miss per cache line, "
"one every eight accesses)",
do_loads),
MENU_ITEM('c', "cached loads benchmark (expect all hits)",
do_loads_cached),
MENU_ITEM('8', "strided loads benchmark (expect all misses)",
do_loads_strided),
MENU_ITEM('s', "sequential stores benchmark", do_stores),
MENU_ITEM('i', "load-increment-store benchmark (expect misses)", do_increment_mem),
MENU_ITEM('i', "load-increment-store benchmark (expect misses)",
do_increment_mem),
MENU_END,
},
};

void do_benchmarks_menu()
{
menu_run(&MENU);
}
void do_benchmarks_menu() { menu_run(&MENU); }
2 changes: 1 addition & 1 deletion common/src/benchmarks.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
// micro-benchmark menu
void do_benchmarks_menu();

#endif // !BENCHMARKS_H
#endif // !BENCHMARKS_H

0 comments on commit f730246

Please sign in to comment.