Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Parallel ForkManager

by aditya007 (Initiate)
on Apr 07, 2010 at 07:26 UTC ( #833215=perlquestion: print w/ replies, xml ) Need Help??
aditya007 has asked for the wisdom of the Perl Monks concerning the following question:

Hello everybody.

I am new to "Parallel ForkManager".
I have downloaded it.

But I do not know the usage.

I want to use all my "processors".
for any type of perl program.

Example.

A simple perl program.

###### count a length of a string ##########

$string="abcdefg";
$ln=length($string);
print "length of a string = $ln \n";

###### end of a program ##########

This is just an example,
I want to make any perl program
in such a manner that it will use
all processor.

So, how can I make any perl
program in that manner?

Would anyone help me?

Thanks,
Naman

Comment on Parallel ForkManager
Re: Parallel ForkManager
by moritz (Cardinal) on Apr 07, 2010 at 07:51 UTC
    It doesn't work that way. With Parallel::ForkManager you can easily start multiple processes that run at the same time - but you have to decide on your own what you do in which process. Determining string length can't easily be parallelized that way.
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Parallel ForkManager
by almut (Canon) on Apr 07, 2010 at 07:52 UTC
    I want to make any perl program in such a manner that it will use all processor.

    Parallel::ForkManager is for running multiple processes. Whether these will run on multiple processors is a question of how the OS handles those processes.

    Also, there's no "apply to any program" option... you'll have to adapt every program in question separately.  Have you seen the examples in the docs and played with them? If so, where did you get stuck?

Re: Parallel ForkManager
by tirwhan (Abbot) on Apr 07, 2010 at 08:30 UTC

    To further expand on the two (perfectly correct and valid) comments by almut and moritz above, here is a script that does exactly what you asked for:

    #!/usr/bin/perl use warnings; use strict; use Parallel::ForkManager; use Sys::CPU; my $number_of_cpus = Sys::CPU::cpu_count(); my $pm = new Parallel::ForkManager($number_of_cpus); for (1..$number_of_cpus) { my $pid = $pm->start and next; my $string="abcdefg"; my $ln=length($string); print "length of a string = $ln \n"; $pm->finish(); }

    This uses Sys::Cpu to find the number of CPUs the system has, then forks off that number of processes and executes the code in them. Note you'll get the output ("length of a string...") multiple times (depending on the number of processors you have in the system), because exactly the same code is run in each process. So this approach is rather nonsensical, it uses all processors (normally, depends on your OS) but simply multiplies the workload. Something more sensible would be to have each child measure a different string, e.g.:

    #!/usr/bin/perl use warnings; use strict; use Parallel::ForkManager; use Sys::CPU; my $number_of_cpus = Sys::CPU::cpu_count(); my $pm = new Parallel::ForkManager($number_of_cpus); my @strings = ("abcde","abcdefghij","ab"); for (1..scalar @strings) { my $pid = $pm->start and next; my $string=$strings[$_-1]; my $ln=length($string); print "length of string \"$string\" = $ln \n"; $pm->finish(); }

    This will hand each child a different string and thus "parallelize" the work done. This is slightly less senseless than the previous example, but only slightly, because the work done by your machine to fork off a separate process is much more than the simple act of measuring the length of a string. So, as the previous commenters have already pointed out, you need to do some thinking on exactly what you want to parallelize and how to do it in a worthwhile fashion. Anyway, hope this helps clear up some confusion.


    All dogma is stupid.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://833215]
Approved by almut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2014-07-24 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls