首页 > 解决方案 > 如何在 Slurm 脚本中编写主机文件

问题描述

目前我正在关注

 #!/bin/bash -l
 #SBATCH --nodes=2
 #SBATCH --ntasks-per-node=4

 scontrol show hostname $SLURM_JOB_NODELIST | perl -ne 'chomb; print "$_" x4' > myhostfile

这会生成以下 myhostfile

 compute-0
 compute-0
 compute-0
 compute-0
 compute-1
 compute-1
 compute-1
 compute-1

我想得到以下结果

 compute-0
 compute-1
 compute-0
 compute-1
 compute-0
 compute-1
 compute-0
 compute-1

这样我们在所有指定节点之间交替

标签: perlslurmsbatch

解决方案


你可以这样做:

$ perl -e 'print +(<>) x 4'

这会从您的代码中删除-n循环,而是一次性读取整个循环STDIN。我们需要括号()将读取操作符放入列表上下文中,因此它会一次读取所有行。告诉 perl 解释器+括号是一个列表,而不是print(as print()) 的一部分。最后x,列表上下文中的重复运算符重复整个列表。

$ cat foo
0
1
$ cat foo | perl -e 'print +(<>) x 4'
0
1
0
1
0
1
0
1

推荐阅读