Commit 953e3065 authored by Caleb C. Sander's avatar Caleb C. Sander
Browse files

Fix #2

parent b3903296
No related merge requests found
Showing with 67 additions and 56 deletions
+67 -56
bin/* bin/*
out/* out/*
.debug
# #
# Makefile for the malloc lab driver # Makefile for the malloc lab driver
# #
CC = clang
CFLAGS = -Werror -Wall -Wextra -O3 -g
all: bin/mdriver-implicit bin/mdriver-explicit CLEAN_COMMAND = rm -f out/* bin/*
ifdef DEBUG
CC = clang-with-asan
CFLAGS = -Werror -Wall -Wextra
ifeq ($(wildcard .debug),)
$(shell $(CLEAN_COMMAND))
$(shell touch .debug)
endif
else
CC = clang
CFLAGS = -Werror -Wall -Wextra -O3 -g
ifneq ($(wildcard .debug),)
$(shell $(CLEAN_COMMAND) .debug)
endif
endif
bin/mdriver-implicit: out/mdriver-implicit.o out/mm-implicit.o out/memlib.o out/fsecs.o out/fcyc.o out/clock.o out/ftimer.o all: bin/mdriver-implicit bin/mdriver-explicit
$(CC) $(CFLAGS) $^ -o $@
bin/mdriver-explicit: out/mdriver-explicit.o out/mm-explicit.o out/memlib.o out/fsecs.o out/fcyc.o out/clock.o out/ftimer.o bin/mdriver-%: out/mdriver-%.o out/mm-%.o out/memlib.o out/fsecs.o out/fcyc.o out/clock.o
$(CC) $(CFLAGS) $^ -o $@ $(CC) $(CFLAGS) $^ -o $@
out/mdriver-implicit.o: driver/mdriver.c out/mdriver-implicit.o: driver/mdriver.c
...@@ -25,4 +36,6 @@ out/%.o: driver/%.c ...@@ -25,4 +36,6 @@ out/%.o: driver/%.c
$(CC) $(CFLAGS) -c $^ -o $@ $(CC) $(CFLAGS) -c $^ -o $@
clean: clean:
rm -f out/* bin/* $(CLEAN_COMMAND)
.PRECIOUS: bin/mdriver-% out/mdriver-implicit.o out/mdriver-explicit.o out/%.o
...@@ -43,11 +43,11 @@ static double *samples = NULL; ...@@ -43,11 +43,11 @@ static double *samples = NULL;
static void init_sampler() static void init_sampler()
{ {
if (values) if (values)
free(values); free(values);
values = calloc(kbest, sizeof(double)); values = calloc(kbest, sizeof(double));
#if KEEP_SAMPLES #if KEEP_SAMPLES
if (samples) if (samples)
free(samples); free(samples);
/* Allocate extra for wraparound analysis */ /* Allocate extra for wraparound analysis */
samples = calloc(maxsamples+kbest, sizeof(double)); samples = calloc(maxsamples+kbest, sizeof(double));
#endif #endif
...@@ -61,11 +61,11 @@ static void add_sample(double val) ...@@ -61,11 +61,11 @@ static void add_sample(double val)
{ {
int pos = 0; int pos = 0;
if (samplecount < kbest) { if (samplecount < kbest) {
pos = samplecount; pos = samplecount;
values[pos] = val; values[pos] = val;
} else if (val < values[kbest-1]) { } else if (val < values[kbest-1]) {
pos = kbest-1; pos = kbest-1;
values[pos] = val; values[pos] = val;
} }
#if KEEP_SAMPLES #if KEEP_SAMPLES
samples[samplecount] = val; samples[samplecount] = val;
...@@ -73,10 +73,10 @@ static void add_sample(double val) ...@@ -73,10 +73,10 @@ static void add_sample(double val)
samplecount++; samplecount++;
/* Insertion sort */ /* Insertion sort */
while (pos > 0 && values[pos-1] > values[pos]) { while (pos > 0 && values[pos-1] > values[pos]) {
double temp = values[pos-1]; double temp = values[pos-1];
values[pos-1] = values[pos]; values[pos-1] = values[pos];
values[pos] = temp; values[pos] = temp;
pos--; pos--;
} }
} }
...@@ -86,34 +86,31 @@ static void add_sample(double val) ...@@ -86,34 +86,31 @@ static void add_sample(double val)
static int has_converged() static int has_converged()
{ {
return return
(samplecount >= kbest) && (samplecount >= kbest) &&
((1 + epsilon)*values[0] >= values[kbest-1]); ((1 + epsilon)*values[0] >= values[kbest-1]);
} }
/* /*
* clear - Code to clear cache * clear - Code to clear cache
*/ */
static volatile int sink = 0;
static void clear() static void clear()
{ {
int x = sink;
int *cptr, *cend; int *cptr, *cend;
int incr = cache_block/sizeof(int); int incr = cache_block/sizeof(int);
if (!cache_buf) { if (!cache_buf) {
cache_buf = malloc(cache_bytes); cache_buf = malloc(cache_bytes);
if (!cache_buf) { if (!cache_buf) {
fprintf(stderr, "Fatal error. Malloc returned null when trying to clear cache\n"); fprintf(stderr, "Fatal error. Malloc returned null when trying to clear cache\n");
exit(1); exit(1);
} }
} }
cptr = (int *) cache_buf; cptr = (int *) cache_buf;
cend = cptr + cache_bytes/sizeof(int); cend = cptr + cache_bytes/sizeof(int);
while (cptr < cend) { while (cptr < cend) {
x += *cptr; *(volatile int *) cptr;
cptr += incr; cptr += incr;
} }
sink = x;
} }
/* /*
...@@ -136,32 +133,32 @@ double fcyc(test_funct f, void *argp) ...@@ -136,32 +133,32 @@ double fcyc(test_funct f, void *argp)
double result; double result;
init_sampler(); init_sampler();
if (compensate) { if (compensate) {
do { do {
double cyc; double cyc;
if (clear_cache) if (clear_cache)
clear(); clear();
start_comp_counter(); start_comp_counter();
f(argp); f(argp);
cyc = get_comp_counter(); cyc = get_comp_counter();
add_sample(cyc); add_sample(cyc);
} while (!has_converged() && samplecount < maxsamples); } while (!has_converged() && samplecount < maxsamples);
} else { } else {
do { do {
double cyc; double cyc;
if (clear_cache) if (clear_cache)
clear(); clear();
start_counter(); start_counter();
f(argp); f(argp);
cyc = get_counter(); cyc = get_counter();
add_sample(cyc); add_sample(cyc);
} while (!has_converged() && samplecount < maxsamples); } while (!has_converged() && samplecount < maxsamples);
} }
#ifdef DEBUG #ifdef DEBUG
{ {
int i; int i;
printf(" %d smallest values: [", kbest); printf(" %d smallest values: [", kbest);
for (i = 0; i < kbest; i++) for (i = 0; i < kbest; i++)
printf("%.0f%s", values[i], i==kbest-1 ? "]\n" : ", "); printf("%.0f%s", values[i], i==kbest-1 ? "]\n" : ", ");
} }
#endif #endif
result = values[0]; result = values[0];
...@@ -194,11 +191,11 @@ void set_fcyc_clear_cache(int clear) ...@@ -194,11 +191,11 @@ void set_fcyc_clear_cache(int clear)
void set_fcyc_cache_size(int bytes) void set_fcyc_cache_size(int bytes)
{ {
if (bytes != cache_bytes) { if (bytes != cache_bytes) {
cache_bytes = bytes; cache_bytes = bytes;
if (cache_buf) { if (cache_buf) {
free(cache_buf); free(cache_buf);
cache_buf = NULL; cache_buf = NULL;
} }
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment