Monday, April 27, 2009

The problem with the current cream of the crops CPUs is that they have so many cores and there are not a lot of programs yet taking advantage of it. Recently i had a request from somebody who wanted to process fotos on his brand new Intel i7 utilizing all of his cores as much as possible. He is using ImageMagick for his batch manipulations, ofcourse it handles only one picture at a time. I thought about it for a while and came up with a simple solution, i called the script 'multirun'.

#!/bin/bash
#
# Prove of concept script to run multiple processes for dual core (or more CPUs)
# The script takes one argument - the number of concurrent jobs
#
# This script uses the 'sleep' command as test, change with your own command
#
# How it works:
# 1. it starts a jobs and puts it in the background
# 2. continue until max number of concurrent jobs are reached
# 3. wait until 2 is no longer true then go to 1.
#

if [ ! $1 ]
then
echo "missing argument"
exit
fi

CMD='/bin/sleep 30'
CONCURJOBS="$1"

while true
do
if [ `jobs | wc -l` -eq ${CONCURJOBS} ]
then
continue
else
echo "starting a new ${CMD}..."
${CMD} &
fi
done

No comments: