I have been trying to parallelize the following script, specifically each of the three FOR loop instances, using GNU Parallel but haven't been able to. The 4 commands contained within the FOR loop run in series, each loop taking around 10 minutes.
#!/bin/bash
kar='KAR5'
runList='run2 run3 run4'
mkdir normFunc
for run in $runList
do
fsl5.0-flirt -in $kar"deformed.nii.gz" -ref normtemp.nii.gz -omat $run".norm1.mat" -bins 256 -cost corratio -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -dof 12
fsl5.0-flirt -in $run".poststats.nii.gz" -ref $kar"deformed.nii.gz" -omat $run".norm2.mat" -bins 256 -cost corratio -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -dof 12
fsl5.0-convert_xfm -concat $run".norm1.mat" -omat $run".norm.mat" $run".norm2.mat"
fsl5.0-flirt -in $run".poststats.nii.gz" -ref normtemp.nii.gz -out $PWD/normFunc/$run".norm.nii.gz" -applyxfm -init $run".norm.mat" -interp trilinear
rm -f *.mat
done
wait
in it basically lets all processes run, until it hits thenth
process, then waits for all of the others to finish running, is that right? – naught101 Nov 26 '15 at 23:03i
is zero, call wait. Incrementi
after the zero test. – Petr Skocik Nov 26 '15 at 23:08wait
w/ no arg waits for all children. That makes it a little wasteful. The pipe-based-semaphore approach gives you more fluent concurrency (I've been using that in a custom shell based build system along with-nt
/-ot
checks successfully for a while now) – Petr Skocik Mar 10 '18 at 20:02mkfifo pipe-$$
command needs appropriate write access to the current directory. So I prefer to specify the full path such as/tmp/pipe-$$
as it most likely has write access available for the current user rather than relying on whatever the current directory is. Yes replace all 3 occurrences ofpipe-$$
. – BeowulfNode42 Jul 29 '19 at 03:13set -e
the fourth solution wouldn't work for you, you'd need to change it to((++i==1))
– lol Aug 20 '20 at 12:14(((++i % $NR_PROCESSES) == 0));
– foudfou Nov 13 '20 at 09:10wait -n
to get away with pure bash without batching. – EFraim Aug 06 '21 at 20:45&
crash X11? – kaiya Feb 01 '22 at 14:03((i=i%N)); ((i++==0)) && wait
: why is it waiting background jobs ? – Stéphane Mar 10 '22 at 15:25wait
at the end? – pmor Nov 01 '22 at 14:11bash -e
exiting problem hit me for((i=i%N)); ((i++==0)) && wait
I found this works instead((i=(i+1)%N)) || wait
. It seems((...))
does a logical-not of the numeric value result (which means it returns bash "true" or "success" if the value is not zero).Also this works instead to keep N tasks running in parallel from the answer below;
– Donovan Baarda Nov 12 '22 at 07:25[[ $(jobs -r -p | wc -l) -lt $N ]] || wait -n