Skip to content

Commit

Permalink
add and use helper library for easier handcompilation
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-singer committed Oct 27, 2023
1 parent 137dcf9 commit 3ce8b6c
Show file tree
Hide file tree
Showing 15 changed files with 1,010 additions and 257 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Exes
handcomp_test/*
!handcomp_test/*.*
!handcomp_test/include

# Emacs backups
*~
Expand Down
4 changes: 2 additions & 2 deletions handcomp_test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ OBJS = $(patsubst %.c,%.o,$(SRCS))
DEFINES = $(ABI_DEF)

TESTS = cilksort fib mm_dac nqueens
OPTIONS = $(OPT) $(ARCH) $(DBG) -Wall $(DEFINES) -fno-omit-frame-pointer
OPTIONS = $(OPT) $(ARCH) $(DBG) -Wall $(DEFINES) -fno-omit-frame-pointer -Iinclude -I.. -I../include
# dynamic linking
# RTS_DLIBS = -L../runtime -Wl,-rpath -Wl,../runtime -lopencilk
# RTS_LIBS = ../runtime/$(RTS_LIB).so
Expand All @@ -20,7 +20,7 @@ TIMING_COUNT := 1

all: $(TESTS)

$(TESTS): %: %.o ktiming.o getoptions.o ZERO.o
$(TESTS): %: %.o ktiming.o getoptions.o ZERO.o c2cilk.o
$(CC) $^ -o $@ $(RTS_LIBS) -lrt -lpthread -lm

%.o: %.c
Expand Down
3 changes: 3 additions & 0 deletions handcomp_test/c2cilk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <stdlib.h>

size_t C2CILK_ZERO = 0;
158 changes: 55 additions & 103 deletions handcomp_test/cilksort.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@
#include <stdlib.h>
#include <string.h>

#include "../runtime/cilk2c.h"
#include "../runtime/cilk2c_inlined.c"
#include "ktiming.h"
#include "getoptions.h"

extern size_t ZERO;
void __attribute__((weak)) dummy(void *p) { return; }
#define ENABLE_UNSAFE_C2CILK_LIBRARY 0xda179e12
#include <c2cilk/c2cilk.h>

#ifndef TIMING_COUNT
#define TIMING_COUNT 0
Expand Down Expand Up @@ -305,11 +303,8 @@ ELM *binsplit(ELM val, ELM *low, ELM *high) {
return low;
}

static void __attribute__ ((noinline))
cilkmerge_spawn_helper(ELM *low1, ELM *high1, ELM *low2, ELM *high2, ELM *lowdest);

void cilkmerge(ELM *low1, ELM *high1,
ELM *low2, ELM *high2, ELM *lowdest) {
C2CILK_VOID_FUNC(cilkmerge, (ELM*, low1, ELM*, high1,
ELM*, low2, ELM*, high2, ELM*, lowdest), {

/*
* Cilkmerge: Merges range [low1, high1] with range [low2, high2]
Expand Down Expand Up @@ -348,55 +343,32 @@ void cilkmerge(ELM *low1, ELM *high1,
return;
}

dummy(alloca(ZERO));
__cilkrts_stack_frame sf;
__cilkrts_enter_frame(&sf);

/*
* Basic approach: Find the middle element of one range (indexed by
* split1). Find where this element would fit in the other range
* (indexed by split 2). Then merge the two lower halves and the two
* upper halves.
*/

split1 = ((high1 - low1 + 1) / 2) + low1;
split2 = binsplit(*split1, low2, high2);
lowsize = split1 - low1 + split2 - low2;

/*
* directly put the splitting element into
* the appropriate location
*/
*(lowdest + lowsize + 1) = *split1;

/* cilk_spawn cilkmerge(low1, split1 - 1, low2, split2, lowdest); */
if (!__cilk_prepare_spawn(&sf)) {
cilkmerge_spawn_helper(low1, split1 - 1, low2, split2, lowdest);
}
cilkmerge(split1 + 1, high1, split2 + 1, high2, lowdest + lowsize + 2);

/* cilk_sync; */
__cilk_sync_nothrow(&sf);

__cilk_parent_epilogue(&sf);

c2cilk_context(
/*
* Basic approach: Find the middle element of one range (indexed by
* split1). Find where this element would fit in the other range
* (indexed by split 2). Then merge the two lower halves and the two
* upper halves.
*/

split1 = ((high1 - low1 + 1) / 2) + low1;
split2 = binsplit(*split1, low2, high2);
lowsize = split1 - low1 + split2 - low2;

/*
* directly put the splitting element into
* the appropriate location
*/
*(lowdest + lowsize + 1) = *split1;

/* cilk_spawn cilkmerge(low1, split1 - 1, low2, split2, lowdest); */
c2cilk_void_spawn(cilkmerge, low1, split1 - 1, low2, split2, lowdest);
cilkmerge(split1 + 1, high1, split2 + 1, high2, lowdest + lowsize + 2);
)
return;
}
})

static void __attribute__ ((noinline))
cilkmerge_spawn_helper(ELM *low1, ELM *high1,
ELM *low2, ELM *high2, ELM *lowdest) {

__cilkrts_stack_frame sf;
__cilkrts_enter_frame_helper(&sf);
__cilkrts_detach(&sf);
cilkmerge(low1, high1, low2, high2, lowdest);
__cilk_helper_epilogue(&sf);
}

static void __attribute__ ((noinline)) cilksort_spawn_helper(ELM *low, ELM *tmp, long size);

void cilksort(ELM *low, ELM *tmp, long size) {
C2CILK_VOID_FUNC(cilksort, (ELM*, low, ELM*, tmp, long, size), {

/*
* divide the input in four parts of the same size (A, B, C, D)
Expand All @@ -415,62 +387,42 @@ void cilksort(ELM *low, ELM *tmp, long size) {
return;
}

dummy(alloca(ZERO));
__cilkrts_stack_frame sf;
__cilkrts_enter_frame(&sf);

A = low;
tmpA = tmp;
B = A + quarter;
tmpB = tmpA + quarter;
C = B + quarter;
tmpC = tmpB + quarter;
D = C + quarter;
tmpD = tmpC + quarter;

/* cilk_spawn cilksort(A, tmpA, quarter); */
if (!__cilk_prepare_spawn(&sf)) {
cilksort_spawn_helper(A, tmpA, quarter);
}

/* cilk_spawn cilksort(B, tmpB, quarter); */
if (!__cilk_prepare_spawn(&sf)) {
cilksort_spawn_helper(B, tmpB, quarter);
}
c2cilk_context(
A = low;
tmpA = tmp;
B = A + quarter;
tmpB = tmpA + quarter;
C = B + quarter;
tmpC = tmpB + quarter;
D = C + quarter;
tmpD = tmpC + quarter;

/* cilk_spawn cilksort(C, tmpC, quarter); */
if (!__cilk_prepare_spawn(&sf)) {
cilksort_spawn_helper(C, tmpC, quarter);
}
cilksort(D, tmpD, size - 3 * quarter);
/* cilk_spawn cilksort(A, tmpA, quarter); */
c2cilk_void_spawn(cilksort, A, tmpA, quarter);

/* cilk_sync */
__cilk_sync_nothrow(&sf);
/* cilk_spawn cilksort(B, tmpB, quarter); */
c2cilk_void_spawn(cilksort, B, tmpB, quarter);

/* cilk_spawn cilkmerge(A, A + quarter - 1, B, B + quarter - 1, tmpA); */
if (!__cilk_prepare_spawn(&sf)) {
cilkmerge_spawn_helper(A, A + quarter - 1, B, B + quarter - 1, tmpA);
}
cilkmerge(C, C + quarter - 1, D, low + size - 1, tmpC);
/* cilk_spawn cilksort(C, tmpC, quarter); */
c2cilk_void_spawn(cilksort, C, tmpC, quarter);

/* cilk_sync */
__cilk_sync_nothrow(&sf);
cilksort(D, tmpD, size - 3 * quarter);

cilkmerge(tmpA, tmpC - 1, tmpC, tmpA + size - 1, A);
/* cilk_sync */
c2cilk_sync;

__cilk_parent_epilogue(&sf);
/* cilk_spawn cilkmerge(A, A + quarter - 1, B, B + quarter - 1, tmpA); */
c2cilk_void_spawn(cilkmerge, A, A + quarter - 1, B, B + quarter - 1, tmpA);
cilkmerge(C, C + quarter - 1, D, low + size - 1, tmpC);

return;
}
/* cilk_sync */
c2cilk_sync;

static void __attribute__ ((noinline)) cilksort_spawn_helper(ELM *low, ELM *tmp, long size) {
cilkmerge(tmpA, tmpC - 1, tmpC, tmpA + size - 1, A);
)

__cilkrts_stack_frame sf;
__cilkrts_enter_frame_helper(&sf);
__cilkrts_detach(&sf);
cilksort(low, tmp, size);
__cilk_helper_epilogue(&sf);
}
return;
})

void scramble_array(ELM *arr, unsigned long size) {
unsigned long i;
Expand Down
48 changes: 13 additions & 35 deletions handcomp_test/fib.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <stdio.h>
#include <stdlib.h>

#include "../runtime/cilk2c.h"
#include "../runtime/cilk2c_inlined.c"
#include "ktiming.h"

#define ENABLE_UNSAFE_C2CILK_LIBRARY 0xda179e12
#include <c2cilk/c2cilk.h>


#ifndef TIMING_COUNT
#define TIMING_COUNT 1
Expand All @@ -31,45 +32,22 @@ int fib(int n) {
}
*/

extern size_t ZERO;
void __attribute__((weak)) dummy(void *p) { return; }

static void __attribute__ ((noinline)) fib_spawn_helper(int *x, int n);

int fib(int n) {
int x = 0, y, _tmp;
C2CILK_FUNC(int, fib, (int, n), {
int x = 0, y;

if(n < 2)
return n;

dummy(alloca(ZERO));
__cilkrts_stack_frame sf;
__cilkrts_enter_frame(&sf);
c2cilk_context(
/* x = spawn fib(n-1) */
c2cilk_spawn(&x, fib, n-1);
y = fib(n - 2);

/* x = spawn fib(n-1) */
if (!__cilk_prepare_spawn(&sf)) {
fib_spawn_helper(&x, n-1);
}

y = fib(n - 2);
// Implicit cilk_sync
)

/* cilk_sync */
__cilk_sync_nothrow(&sf);
_tmp = x + y;

__cilk_parent_epilogue(&sf);

return _tmp;
}

static void __attribute__ ((noinline)) fib_spawn_helper(int *x, int n) {

__cilkrts_stack_frame sf;
__cilkrts_enter_frame_helper(&sf);
__cilkrts_detach(&sf);
*x = fib(n);
__cilk_helper_epilogue(&sf);
}
return x + y;
})

int main(int argc, char * args[]) {
int i;
Expand Down
Loading

0 comments on commit 3ce8b6c

Please sign in to comment.