Skip to content

Commit f23daf2

Browse files
committed
Add: parallel-execute
1 parent 403776c commit f23daf2

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

src/eceval.c

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#define AREA "ECEVAL"
1818

19+
static obj parallel_eval(obj actions, obj env);
1920
static obj parallel_execute(obj actions, obj env);
2021

2122
// ln 182
@@ -136,8 +137,10 @@ static obj ecevalgoto(struct core *cr, bool yield)
136137
goto ev_cons_stream;
137138
if (is_time(cr->expr))
138139
goto ev_timed;
140+
if (is_parallel_eval(cr->expr))
141+
goto ev_parallel_eval;
139142
if (is_parallel_execute(cr->expr))
140-
goto ev_concurrent_execute;
143+
goto ev_parallel_execute;
141144
if (is_ecapply(cr->expr))
142145
goto ev_apply;
143146
if (is_application(cr->expr))
@@ -394,8 +397,16 @@ static obj ecevalgoto(struct core *cr, bool yield)
394397
goto go_cont;
395398

396399
// new
397-
ev_concurrent_execute:
398-
return parallel_execute(cdr(cr->expr), cr->env);
400+
ev_parallel_eval:
401+
cr->val = parallel_eval(cdr(cr->expr), cr->env);
402+
go_obj = cr->cont;
403+
goto go_obj;
404+
405+
ev_parallel_execute:
406+
cr->val = parallel_execute(cdr(cr->expr), cr->env);
407+
go_obj = cr->cont;
408+
goto go_obj;
409+
399410
// new
400411
ev_apply:
401412
save(cr->cont, cr);
@@ -437,6 +448,7 @@ static obj ecevalgoto(struct core *cr, bool yield)
437448
}
438449
go_obj = cr->cont;
439450
go_obj:
451+
// printf("obj: %s\n", to_string(go_obj));
440452
if (is_eq(go_obj, ev_return_caller))
441453
return cr->val;
442454
if (is_eq(go_obj, ev_appl_accum_last_arg))
@@ -477,15 +489,26 @@ obj eceval(obj expression, obj _environment)
477489
return ecevalgoto(cr, false);
478490
}
479491

480-
static int rand_below(int n)
492+
static obj parallel_execute(obj procs, obj env)
493+
{
494+
obj actions = emptylst;
495+
496+
for (; is_pair(procs); procs = cdr(procs)) {
497+
actions = cons(list1(car(procs)), actions);
498+
}
499+
return parallel_eval(actions, env);
500+
}
501+
502+
static int rand_clicks(int n)
481503
{
482-
return plat_rand() % n;
504+
Integer r = plat_rand();
505+
return ((r % 16) > 11) ? (r % n) + 1 : 0;
483506
}
484507

485-
static obj parallel_execute(obj actions, obj env)
508+
static obj parallel_eval(obj actions, obj env)
486509
{
487-
static bool running[NCORE];
488-
static int free = 0;
510+
bool running[NCORE];
511+
int free = 0;
489512
int i, j, runcount;
490513

491514
while (is_pair(actions)) {
@@ -512,9 +535,10 @@ static obj parallel_execute(obj actions, obj env)
512535
if (!running[i]) {
513536
continue;
514537
}
515-
int clicks = rand_below(32);
538+
int clicks = rand_clicks(2);
516539
for (j = 0; j < clicks; j++) {
517-
if (!is_yielded(ecevalgoto(getcore(i), true))) {
540+
obj r = ecevalgoto(getcore(i), true);
541+
if (!is_yielded(r)) {
518542
running[i] = false;
519543
runcount--;
520544
break;

src/mceval.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,14 @@ obj quasi_to_combination(obj exp)
615615
return quasi_dat(cadr(exp));
616616
}
617617

618+
// new - parallel
619+
618620
bool is_parallel_execute(obj exp)
619621
{
620622
return is_tagged_list(exp, parallel_execute_s);
621623
}
624+
625+
bool is_parallel_eval(obj exp)
626+
{
627+
return is_tagged_list(exp, parallel_eval_s);
628+
}

src/mceval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,6 @@ bool is_quasiquote(obj exp);
7171
obj quasi_to_combination(obj exp);
7272

7373
bool is_parallel_execute(obj exp);
74+
bool is_parallel_eval(obj exp);
7475

7576
#endif

src/obj.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ SYMBOL_VAR(letrec)
258258
const obj letstar = SYMBOL("let*");
259259
const obj memo_proc = SYMBOL("memo-proc");
260260
const obj or_s = SYMBOL("or");
261+
const obj parallel_eval_s = SYMBOL("parallel-eval");
261262
const obj parallel_execute_s = SYMBOL("parallel-execute");
262263
SYMBOL_VAR(quasiquote)
263264
SYMBOL_VAR(quote)

src/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ extern const obj letrec;
140140
extern const obj letstar;
141141
extern const obj memo_proc;
142142
extern const obj or_s;
143+
extern const obj parallel_eval_s;
143144
extern const obj parallel_execute_s;
144145
extern const obj quasiquote;
145146
extern const obj quote;

test/run

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ test_dir=$(dirname "${BASH_SOURCE[0]}")
44
export bin_dir=$test_dir/../bin
55
source_dir=$test_dir/code
66

7-
# file=scratch.sicp
8-
file=ch5/ex5.50/ex5.50-cbf.sicp
7+
file=scratch.sicp
8+
# file=ch5/ex5.50/ex5.50-cbf.sicp
99

1010
echo running: $file
1111
$bin_dir/sicp $source_dir/$file

0 commit comments

Comments
 (0)