我是一名生物信息学家,最近陷入了一个问题,需要一些脚本来加速我的过程.我们有一个名为PHASE和Command的软件,我在我的命令行输入以解雇软件
./PHASE test.inp test.out
其中PHASE是程序的名称,test.ip是输入文件,test.out是输出文件.它需要一个核心来运行上述过程,大约需要3个小时才能完成.
现在我有1000个输入文件说test1.inp,test2.inp,test3.inp …..等等到test1000.inp并希望生成所有1000个输出文件..test1.out,test2.out .. … test100.out使用我的系统的全部容量,有4个核心.
要使用我的系统的全部容量,我想激活上面脚本的4个实例,它接受4个这样的输入文件…并生成4个不同的输出
./PHASE test1.inp test1.out
./PHASE test2.inp test2.out
./PHASE test3.inp test3.out
./PHASE test4.inp test4.out
每个作业完成并生成输出文件后,脚本应再次启动剩余的输入文件,直到所有文件都结束.
./PHASE test5.inp test5.out
./PHASE test6.inp test6.out
./PHASE test7.inp test7.out
./PHASE test8.inp test8.out
等等…..
如何编写脚本利用4个内核并加快进程的上述过程的脚本.
解决方法:
如果您有GNU xargs,请考虑以下内容:
printf '%s\0' *.inp | xargs -0 -P 4 -n 1 \
sh -c 'for f; do ./PHASE "$f" "${f%.inp}.out"' _
-P 4在这里很重要,表示并行运行的进程数.
如果你有很多输入并且处理速度很快,可以考虑用更大的数字替换-n 1,以增加每个shell实例迭代的输入数量 – 降低shell启动成本,同时减少粒度和可能是并行程度.
也就是说,如果你真的想做四个批次(根据你的问题),让所有四个人在开始接下来的四个之前完成(这会引入一些低效率,但是你要求的),你可以做这样的事情……
set -- *.inp # set $@ to list of files matching *.imp
while (( $# )); do # until we exhaust that list...
for ((i=0; i<4; i++)); do # loop over batches of four...
# as long as there's a next argument, start a process for it, and take it off the list
[[ $1 ]] && ./PHASE "$1" "${1%.imp}.out" & shift
done
wait # ...and wait for running processes to finish before proceeding
done
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。