跳转到主要内容

一个用于在本地和远程主机上运行多个可执行脚本的软件包,使用YAML文件进行配置

项目描述

atomic-hpc

travis coveralls PyPI

项目: https://github.com/chrisjsewell/atomic-hpc

一个用于在本地和远程主机上运行多个可执行脚本的软件包,使用现代标准YAML文件进行配置。这个软件包特别为提交到高性能计算(HPC)集群(如帝国HPC设施)而设计。工作示例可以在这里找到。

安装

建议设置一个Anaconda环境。对于帝国HPC,运行以下操作(如wiki上所述)

>> module load anaconda3/personal
>> anaconda-setup

然后简单安装atomic-hpc

>> pip install atomic-hpc

最小示例

  1. 编写一个YAML配置文件;每次运行必须有一个名称和一个唯一的ID,然后可以在(全局)defaults部分或按运行(运行属性将覆盖默认设置)设置属性。

config.yaml:

defaults:
    environment: unix

    process:
      unix:
        run:
          - echo "hallo world" > hallo.txt

    output:
      path: output
runs:
  - id: 1
    name: test_local
  - id: 2
    name: test_other
  1. 使用命令行应用程序提交它(使用-h查看所有选项)

     >> run_config config.yaml
    
  2. 结果将在输出路径中可用,每个运行一个文件夹

     >> ls -R output
     output/1_test_local:
     config_1.yaml     hallo.txt
     output/2_test_other:
     config_2.yaml     hallo.txt
    

最小示例(远程和PBS)

可以将作业提交到远程主机和可选的PBS类型系统。

config_remote.yaml

runs:
  - id: 1
    name: test_qsub
    environment: qsub

    output:
      remote:
        hostname: login.cx1.hpc.imperial.ac.uk
        username: cjs14
      path: /work/cjs14/yaml_test

    process:
      qsub:
        cores_per_node: 16  
        nnodes: 1     
        walltime: 1:00:00
        queue: queue_name
        modules:
            - quantum-espresso
            - intel-suite
            - mpi
        run: 
            - mpiexec pw.x -i script2.in > main.qe.scf.out  

在所有进程运行后,从远程主机检索输出

>> retrieve_config config_remote.yaml -o path/to/local/outputs

输入

输入文件和脚本可以是本地或远程,在运行之前将被复制到输出文件夹。还可以设置变量,在找到相应的@v{var_id}正则表达式时,将替换cmnd行和脚本文件中的变量。同样,可以使用@f{file_id}正则表达式将整个文件内容解析到脚本中。

>> cat path/to/script1.in
@v{var1}
@f{file1}
>> cat path/to/file1
This is file 1

config.yaml:

defaults:
    description: quantum-espresso run
    environment: unix

    input:
        remote:
            hostname: login.cx1.hpc.imperial.ac.uk
            username: cjs14
        variables:
            var1:
            nprocs: 2
        files:
            file1: path/to/input.txt
        scripts:
        - path/to/script1.in

    process:
        unix:
            run:
                - mpirun -np @v{nprocs} pw.x -i script1.in > main.qe.scf.out

runs:
  - id: 1
    name: run1
    input:
        variables:
            var1: value1
  - id: 2
    name: run2
    input:
        variables:
            var1: value2

运行:

>> run_config config.yaml
>> ls -R output
output/1_run1:
config_1.yaml  input.txt  main.qe.scf.out  script.in
output/2_run2:
config_2.yaml  input.txt  main.qe.scf.out  script.in
>> cat output/1_run1/script.in
value1
This is file 1

注意1:所有相对路径都是相对于执行目录解析,除非使用run_config -b base/path/设置。

注意2:对于上述示例,如果脚本或cmndline中包含@v{file1}(而不是@f{file1}),则这将用文件名替换(而不是其内容),即input.txt。

注意3:在qsub: run:内部,关键字@{wrkpath}将被替换为工作文件夹路径。这对于在程序在临时文件夹中运行时保持动态日志文件非常有用,例如:

process:
    qsub:
      start_in_temp: true
      run:
        - my_program > @{wrkpath}/output.log

输出

除了指定输出路径外,还可以配置后处理文件删除和重命名

runs:
  - id: 1
    name: run1
    output:
        path: path/to/output
        remove:
            - tmp/
        rename:
            .other.out: .other.qe.json

完整配置选项

runs:
  description: quantum-espresso run
  environment: qsub
  input:
    path:
    scripts:
      - path/to/script1.in
      - path/to/script2.in
    files:
      file1: path/to/file1
    binaries:
      file2: path/to/file2
    variables:
      var1: overridevalue
      var2: value
      nprocs: 2
    remote:
      hostname: login.cx1.hpc.imperial.ac.uk
      port: 22
      username: cjs14
      password:
      pkey:
      key_filename:
      timeout:
  output:
    remote:
      hostname: login.cx1.hpc.imperial.ac.uk
      port: 22
      username: cjs14
      password:
      pkey:
      key_filename:
      timeout:
    path: path/to/top/level/output
    remove:
      # can also use wildcard characters *, ? and []
      - tmp/
    rename: 
      # renames any segment of file names, i.e. file.other.out.txt -> file.other.qe.txt
      # searches for files (recursively) in all folders
      .other.out: .other.qe
  process:
    unix:
      run:
        - mpirun -np @v{nprocs} pw.x -i script1.in > main.qe.scf.out
    windows:
      run:
        - mpirun -np @v{nprocs} pw.x -i script1.in > main.qe.scf.out
    qsub:
      jobname:
      cores_per_node: 16
      nnodes: 1
      memory_per_node: 1gb
      tmpspace: 500gb # minimum free space required on the temporary directory
      walltime: 1:00:00
      queue: queue_name
      email: bob@hotmail.com # send email on job start/end
      # NB: the emailling feature has recently been disabled on the Imperial HPC
      modules:
        - module1
        - module2
      start_in_temp: true # if true cd to $TMPDIR and copy all files before running executables
      run:
        - mpiexec pw.x -i script2.in > main.qe.scf.out
  id: 1
  name: run1

设置SSH公钥和私钥

而不是直接使用密码访问远程主机,建议使用公钥认证,这是一种更安全的认证方法。互联网上有许多解释(包括这里)以下是一个简短的设置指南(来自这里

首先打开您要连接的计算机上的shell。输入cd ~/.ssh。如果ls显示有名为'id_rsa'和'id_rsa.pub'的两个文件,您已经有了一个密钥对。如果没有,输入ssh-keygen以下是结果应该看起来像什么:

heiko@clove:~/.ssh$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/heiko/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
f0:da:dc:77:cf:71:12:c8:50:dc:18:a9:8d:66:38:ae heiko@clove.ch.ic.ac.uk
The key's randomart image is:
+--[ RSA 2048]----+
|           .o=   |
|           .+ .  |
|      .  ..+     |
|       oo =o..   |
|       .S+  o .  |
|       +..     . |
|      ..o . . o..|
|      E    . . +o|
|                o|
+-----------------+

您应该保持标准目录并选择一个适当的困难密码。

您刚才创建的两个文件是密钥和钥匙孔。第一个文件'id_rsa'是密钥。您永远不应该将其提供给任何人或允许任何人复制它。第二个文件'id_rsa.pub'是钥匙孔。它是公开的,您可以将其提供给任何人。在这种情况下,将其提供给hpc。

如果您打开'id_rsa.pub',它应该包含一行,类似于:

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAwRDgM+iQg7OaX/CFq1sZ9jl206nYIhW9SMBqsOIRvGM68/6o6uxZo/D4IlmQI9sAcU5FVNEt9dvDanRqUlC7ZtcOGOCqZsj1HTGD3LcOiPNHYPvi1auEwrXv1hDh4pmJwdgZCRnpewNl+I6RNBiZUyzLzp0/2eIyf4TqG1rpHRNjmtS9turANIv1GK1ONIO7RfVmmIk/jjTQJU9iJqje9ZSXTSm7rUG4W8q+mWcnACReVChc+9mVZDOb3gUZV1Vs8e7G36nj6XfHw51y1B1lrlnPQJ7U3JdqPz6AG3Je39cR1vnfALxBSpF5QbTHTJOX5ke+sNKo//kDyWWlfzz3rQ== heiko@clove.ch.ic.ac.uk

现在登录到HPC并打开(或创建)文件 '~/.ssh/authorized_keys'。在新的一行中,您应该添加一个关于该密钥对来源的注释(以#开头),然后在第二行中复制并粘贴您的'id_rsa.pub'文件的完整内容。

#MAC in the office
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAwRDgM+iQg7OaX/CFq1sZ9jl206nYIhW9SMBqsOIRvGM68/6o6uxZo/D4IlmQI9sAcU5FVNEt9dvDanRqUlC7ZtcOGOCqZsj1HTGD3LcOiPNHYPvi1auEwrXv1hDh4pmJwdgZCRnpewNl+I6RNBiZUyzLzp0/2eIyf4TqG1rpHRNjmtS9turANIv1GK1ONIO7RfVmmIk/jjTQJU9iJqje9ZSXTSm7rUG4W8q+mWcnACReVChc+9mVZDOb3gUZV1Vs8e7G36nj6XfHw51y1B1lrlnPQJ7U3JdqPz6AG3Je39cR1vnfALxBSpF5QbTHTJOX5ke+sNKo//kDyWWlfzz3rQ== heiko@clove.ch.ic.ac.uk

关闭'authorized_keys'文件并断开与HPC的连接。现在再次连接。您将要求输入密钥文件的密码。输入它。现在您应该已登录到HPC。如果您没有要求输入密码而是要求输入账户密码,则服务器不接受您的密钥对。

到目前为止,我们已经将输入账户密码替换为输入密钥对密码短语。这就是所谓的SSH代理派上用场的地方。代理会为您存储密码短语,这样您就无需再次输入它们。幸运的是,MacOS内置了一个,应该已经弹出并询问您是否希望代理处理您的密码短语。如果您回答“是”,那么这就是您最后一次听到或看到有关它或您的密码短语的事情。类似的代理存在于几乎所有操作系统上。从现在起,您只需输入主机名和用户名,即可登录。

注意

如果字符串中包含特殊字符(如*),请确保将其放在""内或使用>或| yaml组件(见 https://en.wikipedia.org/wiki/YAML#Basic_components

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

atomic-hpc-0.4.0.tar.gz (40.8 kB 查看散列)

上传时间

构建分布

atomic_hpc-0.4.0-py2.py3-none-any.whl (48.2 kB 查看散列)

上传时间 Python 2 Python 3

由以下机构支持