http://qs321.pair.com?node_id=651934


in reply to Re: Parallelization of heterogenous (runs itself Fortran executables) code
in thread Parallelization of heterogenous (runs itself Fortran executables) code

You didn't steal it; it was a gift.

It has evolved a little since I posted it. Here is the current version:

#!/usr/bin/perl use Getopt::Std; my %opt = (n => 1); getopts('r:n:v', \%opt) or usage(); my $cmd = shift; @ARGV = shuffle(@ARGV) if $opt{r}; my %pid; while (@ARGV) { if (keys(%pid) < $opt{n}) { $pid{spawn($cmd, split /\s+/, shift @ARGV)} = 1; } else { delete $pid{wait()}; } } 1 while wait() >= 0; sub spawn { my $pid = fork; die "fork: $!" unless defined $pid; return $pid if $pid; warn "@_\n" if $opt{v}; exec @_; die "exec: $!"; } sub usage { print STDERR "Usage: $0 [-n N] [-r] [-v] command arg1 arg2... Run command arg1, command arg2, etc., concurrently. Run no more than N processes simultaneously (default 1) -r: run commands in random order instead of specified order (unimp +l.) -v: verbose mode "; exit 1; }
The major missing feature is that at present there's no way to get it to run cmd -x arg1, cmd -x arg2...; there's no way to get the constant -x in there.

I hereby put this program in the public domain.

Share and enjoy!