3 use List
::Util
'shuffle'; # standard from Perl 5.8 and later
5 my $tempfile = "multigcc.out";
10 # parse command line arguments
22 my $command = join " ", @params;
24 # shuffle the file list to spread the load as evenly as we can
25 @files = shuffle
(@files);
27 # count number of cores
31 chomp($cores = `sysctl -n hw.ncpu`);
35 $cores = scalar grep /on-line/i, `psrinfo`;
39 if (open CPUINFO
, "</proc/cpuinfo") {
40 $cores = scalar grep /^processor/i, <CPUINFO
>;
51 my $slice = int((scalar @files + $cores) / $cores);
53 # reset $cores to 0 so we can count the number of actually used cores
56 for (my $i=0;$i<scalar @files;$i += $slice)
62 $pids[$cores++] = $pid;
66 # get my slice of the files
67 my @list = @files[$i .. $i + $slice - 1];
70 system("$command @list > $tempfile.$$");
76 for my $i (0 .. $cores - 1)
78 # wait for child to complete
82 if (open F
, "<$tempfile.$pids[$i]")
86 unlink "$tempfile.$pids[$i]";