Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: OpenMP from Perl?

by Moron (Curate)
on Jun 21, 2007 at 12:45 UTC ( #622556=note: print w/replies, xml ) Need Help??

in reply to OpenMP from Perl?

It is unclear if the Fortran only or the C++ as well made you shiver. I am hoping it was only the Fortran, because you might need to get at least your hair wet in respect of the C++ to solve this.

I know absolutely nothing about OpenMP (update: but Google is my friend - it already tells me that ANSI C is enough, so already the water got warmer).

It appears that OpenMP is dissembled across embedded, environment variable and shared object facilities. To use Perl instead of C for all three of these requires then three separate techniques:

1) compiler directives: For this part of the interface you could translate Perl main programs to C main programs without knowledge of C using B::C (update: or perlcc) and then apply the OpenMP directives to that as directed by the OpenMP documentation.

2) Environment variables: For these, I tend to make a subroutine that gets them parametrically and then uses IPC::Open3 to create a session with whatever foreign program (could be your own C from Perl program in thes case) is being run with syntax something like:

use IPC::Open3; sub NameOfCommandInterface { # subcommand, envvar, val, envvar, val... my $subcommand = shift; my $env = ''.; while ( @_ ) { $env .= 'export ' . shift(); $env .= '="' . shift() . '";'; } my $pid = open3 my $wh, my $rh, my $eh, $env . 'NameOfCommand' or +die $!; print $wh $subcommand; close $wh; my @er = <$eh> and die ( join ('',@er )); close $eh; my @rt = <$rh>; close $rh; waitpid $pid, 0; return @rt; }
(update: strictly speaking that is a translation technique of shell to Perl rather than C to Perl of course)

3) shared objects - load those into Perl using P5NCI::Library.

That at least completes the 3 requirements to build the interfacing I found by googling - if there are more, do tell and we'll have a look for you. But (update) to address more of your questions:

"Is Perl 5 multi-threading robust enough to take such a standard?" Nothing to do with robustness - the OpenMP standard is suitable for C. You could try to take Perl's multithreading at source level and interface that to OpenMP, but I suspect the water is too cold for you there. (Update: to be clear, for an OpenMP implementation you would have to achieve the multithreading via something like the three techniques I outline above, and not by using Perl's own multithreading) Will Perl 6 support OpenMP? I presume that the modules I have mentioned will get ported to Perl 6 so I presume yes insofar as you need to build your own main glue interfacing along lines such as above.


^M Free your mind!

Replies are listed 'Best First'.
Re^2: OpenMP from Perl?
by cdarke (Prior) on Jun 21, 2007 at 13:31 UTC
    Thanks for that. Yes, it is the thought of FORTRAN that makes me shiver. I am considering my own implementation, but did not wish to reinvent the wheel. I had not considered B::C (is it still supported?) and that could be the easiest route.
    Update: B::C must be still supported, since there is a 5.9.4 version, but I thought I read somewhere that it is going to be dropped? Maybe I was dreaming.
      Come to think of it, for that part of the story you could also use perlcc

      ^M Free your mind!

        I've just had a play with both, and it is not so simple. The most basic structure in OpenMP is to parallel a (C-style) 'for' loop by prefixing with a #pragma in C/C++. A Perl 'for' loop is not translated into a C 'for' loop by B::C, so it is not just a case of hacking the resulting C. Looks like I'll have to convert the OpenMP directives into Perl threading constructs.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://622556]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2018-01-17 01:25 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (194 votes). Check out past polls.