2009-07-24 21:53:32 +00:00
|
|
|
#!/usr/bin/perl
|
2011-07-19 19:16:54 +00:00
|
|
|
use feature "switch";
|
2009-07-24 21:53:32 +00:00
|
|
|
use List::Util 'shuffle'; # standard from Perl 5.8 and later
|
|
|
|
|
|
|
|
my $tempfile = "multigcc.out";
|
|
|
|
my @params;
|
|
|
|
my @files;
|
|
|
|
my $list = \@params;
|
|
|
|
|
|
|
|
# parse command line arguments
|
|
|
|
for my $a (@ARGV) {
|
|
|
|
if ($a eq "--") {
|
|
|
|
$list = \@files;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
push @{$list}, $a;
|
|
|
|
}
|
|
|
|
|
2009-08-23 21:28:23 +00:00
|
|
|
exit if (not @files);
|
|
|
|
|
2009-07-24 21:53:32 +00:00
|
|
|
my $command = join " ", @params;
|
|
|
|
|
|
|
|
# shuffle the file list to spread the load as evenly as we can
|
|
|
|
@files = shuffle(@files);
|
|
|
|
|
|
|
|
# count number of cores
|
2010-03-21 14:35:03 +00:00
|
|
|
my $cores;
|
2011-07-19 19:16:54 +00:00
|
|
|
given ($^O) {
|
|
|
|
when ("darwin") {
|
2010-03-21 14:35:03 +00:00
|
|
|
chomp($cores = `sysctl -n hw.ncpu`);
|
|
|
|
$cores = 1 if ($?);
|
|
|
|
}
|
2011-07-19 19:16:54 +00:00
|
|
|
when ("solaris") {
|
2010-03-21 14:35:03 +00:00
|
|
|
$cores = scalar grep /on-line/i, `psrinfo`;
|
|
|
|
$cores = 1 if ($?);
|
|
|
|
}
|
2011-07-19 19:16:54 +00:00
|
|
|
default {
|
2010-03-21 14:35:03 +00:00
|
|
|
if (open CPUINFO, "</proc/cpuinfo") {
|
|
|
|
$cores = scalar grep /^processor/i, <CPUINFO>;
|
|
|
|
close CPUINFO;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$cores = 1;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 21:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# fork children
|
|
|
|
my @pids;
|
2009-07-24 22:50:16 +00:00
|
|
|
my $slice = int((scalar @files + $cores) / $cores);
|
2010-06-30 16:39:28 +00:00
|
|
|
|
|
|
|
# reset $cores to 0 so we can count the number of actually used cores
|
|
|
|
$cores=0;
|
|
|
|
|
|
|
|
for (my $i=0;$i<scalar @files;$i += $slice)
|
2009-07-24 21:53:32 +00:00
|
|
|
{
|
|
|
|
my $pid = fork;
|
|
|
|
if ($pid)
|
|
|
|
{
|
|
|
|
# mother
|
2010-06-30 16:39:28 +00:00
|
|
|
$pids[$cores++] = $pid;
|
2009-07-24 21:53:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# get my slice of the files
|
2010-06-30 16:39:28 +00:00
|
|
|
my @list = @files[$i .. $i + $slice - 1];
|
2009-07-24 21:53:32 +00:00
|
|
|
|
|
|
|
# run command
|
|
|
|
system("$command @list > $tempfile.$$");
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for my $i (0 .. $cores - 1)
|
|
|
|
{
|
|
|
|
# wait for child to complete
|
|
|
|
waitpid $pids[$i], 0;
|
|
|
|
|
|
|
|
# read & print result
|
|
|
|
if (open F, "<$tempfile.$pids[$i]")
|
|
|
|
{
|
|
|
|
print <F>;
|
|
|
|
close F;
|
|
|
|
unlink "$tempfile.$pids[$i]";
|
|
|
|
}
|
|
|
|
}
|