in reply to loop over to utilize all cpus

If you're running through a loop using system and exe, you're single threaded. That means you'll only use one processor.

If you want to use more processor cores, you'll want to use fork or threads. Of course, these will be randomly assigned to a processor core by the OS. It looks like you might be on a *nix platform. If so, you can look up the man page on numactl to see about binding a process to specific core(s).

Replies are listed 'Best First'.
Re^2: loop over to utilize all cpus
by blakew (Monk) on Sep 17, 2010 at 15:38 UTC
    Here's something I wrote recently to fork 6 processes at a time (on more than 6 jobs):
    #!/usr/bin/perl use strict; use warnings; use Parallel::ForkManager; my $pm = Parallel::ForkManager->new( 6 ); my %kids; local $SIG{ INT } = sub { print "\nCaught interrupt, shutting down..."; kill 9, keys %kids; $pm->wait_all_children; print "done.\n"; exit; }; # Remove child pid from running table $pm->run_on_finish( sub { my $pid = shift; delete $kids{ $pid }; } ); for my $job ( @jobs ) { # Whatever if ( my $pid = $pm->start ) { $kids{ $pid }++; print "Started $job...\n"; } else { local $SIG{ INT } = sub { $pm->finish }; system( $job ); $pm->finish; } } $pm->wait_all_children;

    (edit) looks like I didn't reply to the parent, whoops.