Reputation: 31
I have a file a.txt and each line contains a parameter. Now I want to use mpiexec to call my program such as a.out to calculate with each parameter. So I use linux shell script to handle this. The code is sample
cat a.txt | while read line
do
mpiexec -v -hostfile hosts -np 16 ./a.out ${line}
done
Unexpectedly, the script end after processing only one line of file a.txt. So, it is because of the wrong use of pipe? How can I tackle with this problem?
Upvotes: 0
Views: 1670
Reputation: 1
I had this issue too. Claudio's solution helped set me on the right path to understanding why the loop exits after the first iteration. First off, here is a solution which is pretty close to what you wrote:
cat a.txt | while read line; do
</dev/null mpiexec -np 16 ./a.out ${line}
done
Note that I am just using mpiexec
on a local computer, (python's threading situation is bad enough to need this) so I can't test if this works with separate hosts. You can try adding that back in yourself.
The reason that your script didn't work is that mpiexec
seems to gobble up whatever is attached to the standard input. I assume it does this so that in case a.out
needs that input, it would gobble all the input and send it along with the command to run a.out
that gets sent to the other servers. The result is that on the first iteration, read
reads the first line from your file. Then mpiexec
reads the rest of the lines, even though a.out
probably doesn't use them in your case. Then on the second iteration, read
tries to read more lines, but since mpiexec
already read the rest, read
is told that the end of file has been reached, so the loop exits.
Since we want to prevent mpiexec
from reading the standard in, we redirect mpiexec
's standard in to come from /dev/null. Since /dev/null always contains nothing, mpiexec
will read nothing and leave the standard input alone.
Upvotes: 0
Reputation: 10947
#!/bin/bash
for LINE in `cat a.txt | xargs -r`; do
mpiexec -v -hostfile hosts -np 16 ./a.out $LINE
done
Upvotes: 0