[Edinburgh-pm] parallel semantics in perl

Wim Vanderbauwhede wim.vanderbauwhede at gmail.com
Tue Dec 14 02:30:49 PST 2010


Thanks Aaron and Marco,

Your suggestions are really helpful, thank sa lot! In fact, I too
dislike overloading do {} and for/foreach. So I'd rather have another
approach. On the other hand I don't want new commands/keywords because
they might put people off, so I'm not in favour of things like "pmap"
-- even though they would make my life as compiler writer much easier
:-)

I think the "use parallel qw<...>; " is the way to go, as it is very
close to my current approach in my Gannet-C language, which is to mark
blocks with a keyword par or seq. Maybe I could have a pragma "seq"
instead, as the default would be to try parallel evaluation.
Pragmas are lexically scoped, aren't they? So I could write

{ use seq qw<foreach>;
    foreach my $i (1..$n) {

    }
}

and only the foreach in that block will be sequential, right?

(
This is not quite the same as I have in my current language, I would
need attribute support on blocks for that -- anyone ever mentioned
something like that? That would be ideal:

    foreach my $i (1..$n) :seq {

    }

would be neat, but AFAICT these are only supported on subs...
)

I didn't know about qw<:all>. Are there others apart from :all, (I
just came across a mention of ":default") or can I implement my own?
Problem with something like this is that you can't google it, so any
pointer to the perldoc would be much appreciated.

Cheers,

WIm




On 14 December 2010 09:23, Marco Fontani <fontani at gmail.com> wrote:
>> However, the programmer might want to control this behaviour. So my
>> question is, what would be an acceptable way of "overloading" Perl
>> semantics to indicate sequential evaluation?
>
> I think the acceptable way would be to _not_ overload them; see below.
>
>> For example, whereas statements in a bare {...} block will be
>> evaluated in parallel,  a do {...} could mean sequential evaluation. A
>> "foreach" in full could be parallel, a "for" sequential.
>
> foreach and for are the same thing really
>
> $ perl -MO=Deparse -e 'foreach($i=0;$i<10;$i++){ print $i }'
> for ($i = 0; $i < 10; ++$i) {
>    print $i;
> }
> -e syntax OK
>
> $ perl -MO=Deparse -e '@a=1..5;for(@a){print};foreach(@a){print}'
> @a = 1..5;
> foreach $_ (@a) {
>    print $_;
> }
> foreach $_ (@a) {
>    print $_;
> }
> -e syntax OK
>
>> Any
>> ideas/suggestion about this, e.g. how would you indicate that "map"
>> should be sequential, etc?
>> I obviously don't want to add new constructs to Perl 5.
>
> I think you do.
>
> At the LPW2010 Paul Evans presented his CPS module
> (http://search.cpan.org/dist/CPS/lib/CPS.pm), which attempts to "do
> stuff" in Continuation Passing Style. Basically you have new "kmap",
> "kfoldl", "kfor" keywords which DWYW without changing the "usual" Perl
> semantics.
>
> You may want to go the same path: having a "pfor", a "pwhile" and
> "pmap" rather than overloading the for, while etc. would mean that one
> would know at-a-glance whether a certain block is executed in parallel
> or not.
>
> It would give you the best of both worlds:
> - the programmer would be able to use the parallel version _or_ the
> standard version, at will
> - the programmer would still be able to keep specific stuff from
> running in parallel (fors within fors?)
>
> Since some *may* want instead to say, "all for/while/map in this
> _scope_ are to be done in parallel", you may want to introduce
> something similar to Aaron's suggested "use parallel" and "no
> parallel" to import your parallel functions, locally, as the real
> for/while/map, just only within the declared scope.
>
> What are your thoughts?
>
> -marco-
>
> --
> Marco Fontani
>
> ----
> Glasgow Perl Mongers - http://glasgow.pm.org/
> Join the RackSpace Cloud at: http://www.rackspacecloud.com/277.html
>



-- 
If it's pointless, what's the point?
If there is a point to it, what's the point?
(Tibor Fischer, "The Thought Gang")


More information about the Edinburgh-pm mailing list