From: Mike Lin <mikelin@MIT.EDU>
To: Yang Shouxun <yangsx@fltrp.com>
Cc: caml-list@inria.fr
Subject: Re: Parallel CPS? (was Re: [Caml-list] stack overflow)
Date: Thu, 10 Apr 2003 00:58:20 -0400 [thread overview]
Message-ID: <0CF60E04-6B11-11D7-AC6E-000393AE4242@mit.edu> (raw)
In-Reply-To: <200304101213.02268.yangsx@fltrp.com>
>
>>> I've learned this style in Scheme. Yet I feel paralyzed when trying
>>> to
>>> write in it to build trees. The type declaration may make my point
>>> clearer. --8<--
>>> type dtree = Dnode of dnode | Dtree of (dnode * int * dtree list)
>>> --8<--
>>> The problems are that unless the next call returns, the tree is not
>>> complete yet and it may have several calls on itself.
>
> What if the continuation is not sequential, but parallel? If the tree
> is
> uniformly binary branching, I guess it would be easier.
> Thanks for other listers as well. The major problem is what if the
> continuation is not singular (sequential) but parallel?
I'm not exactly sure what you mean by 'parallel' here. The observation
I'll make is that I'm assuming you're not designing your code to only
work on SMP or distributed machines, so that there must exist an
equivalent sequential control structure for it.
I'll describe a problem which I hope is related to yours.
Say we had code structured like:
type tree =
Leaf of int
| Tree of tree * tree (* left and right branches *)
let build_tree (data:some_type) =
Tree(build_left_subtree data, build_right_subtree data)
...and we wanted to CPS it. So assume we are given:
build_left_subtree' : some_type -> (Tree->unit) -> unit
build_right_subtree' : some_type -> (Tree->unit) -> unit
So sometimes people get confuzled because they have to issue a
tail-call to build_left_subtree to be good CPSers, but they still need
additional information to finish constructing the tree.
The solution is to use an intermediate closure as follows:
let build_tree' (data:some_type) (kont:(Tree->unit)) =
build_left_subtree data
(fun (left:tree) ->
build_right_subtree data
(fun (right:tree) ->
kont (left,right)))
Again, I hope I understood your problem correctly.
By the way, if there's anyone reading who understands the workings of
the compiler better than I do. In the above example, left (the argument
to the first closure) should get stack-allocated since it is an
argument. Thereafter, we issue a tail-call with a downward funarg that
references that argument. On the one hand, you want to blow away the
activation record and allocate the closure on the heap in order to be a
constant-stack-space tail call. On the other hand, you want to allocate
the closure on the stack, since it's a downward funarg and you don't
want to go digging around for memory in the heap. Which does the
compiler do? I hope the former, or else there's a big problem with my
technique...
-Mike
--
Mike Lin
mikelin@mit.edu
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
next prev parent reply other threads:[~2003-04-10 4:58 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-04-09 2:10 [Caml-list] stack overflow Yang Shouxun
2003-04-09 2:19 ` brogoff
2003-04-09 2:45 ` Yang Shouxun
2003-04-09 8:14 ` Markus Mottl
2003-04-09 9:23 ` Yang Shouxun
2003-04-09 11:34 ` Markus Mottl
2003-04-10 4:12 ` Parallel CPS? (was Re: [Caml-list] stack overflow) Yang Shouxun
2003-04-10 4:58 ` Mike Lin [this message]
2003-04-09 14:14 ` CPS folds " Neel Krishnaswami
2003-04-09 16:54 ` brogoff
2003-04-09 17:23 ` Mike Lin
2003-04-09 2:43 ` [Caml-list] stack overflow David Brown
[not found] ` <200304091034.45256.yangsx@fltrp.com>
[not found] ` <16019.34434.468479.586884@barrow.artisan.com>
2003-04-09 2:53 ` Yang Shouxun
2003-04-09 6:45 ` David Monniaux
2003-04-13 15:42 ` John Max Skaller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0CF60E04-6B11-11D7-AC6E-000393AE4242@mit.edu \
--to=mikelin@mit.edu \
--cc=caml-list@inria.fr \
--cc=yangsx@fltrp.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox