Cookbook#

本页收集了对 AiiDA 日常使用有用的简短脚本和代码片段。

检查调度程序上的排队作业#

如果您想知道当前调度程序上有哪些作业(例如,动态决定在哪台计算机上提交,或延迟提交等),可以使用下面的脚本作为示例:

def get_scheduler_jobs(computer_label='localhost', only_current_user=True):
    """Return a list of all current jobs in the scheduler.

    .. note:: an SSH connection is open and closed at every launch of this function.

    :param computer_label: the label of the computer.
    :param only_current_user: if True, only retrieve jobs of the current default user.
        (if this feature is supported by the scheduler plugin). Otherwise show all jobs.
    """
    from aiida import orm

    computer = Computer.collection.get(label=computer_label)
    transport = computer.get_transport()
    scheduler = computer.get_scheduler()
    scheduler.set_transport(transport)

    # This opens the SSH connection, for SSH transports
    with transport:
        if only_current_user:
            remote_username = transport.whoami()
            all_jobs = scheduler.get_jobs(user=remote_username, as_dict=True)
        else:
            all_jobs = scheduler.get_jobs(as_dict=True)

    return all_jobs

if __name__ == '__main__':
    all_jobs = get_scheduler_jobs(only_current_user=False)
    user_jobs = get_scheduler_jobs(only_current_user=True)

    print(f'Current user has {len(user_jobs)} jobs out of {len(all_jobs)} in the scheduler'
    print('Detailed job view:')

    for job_id, job_info in user_jobs.items():
        print(f'Job ID: {job_id}')
        for k, v in job_info.items():
            if k == 'raw_data':
                continue
            print(f'  {k}: {v}')
        print('')

使用 verdi run 执行:

verdi run file_with_script.py

重要

每次调用该函数,都会打开两个 SSH 连接!因此要小心谨慎,尽量少用这个函数,否则超级计算机中心可能会封杀你的账户。解决这一限制的一个可行办法是将传输作为参数传递,这样就可以重复使用。

输出示例如下::

Current user has 5 jobs out of 1425 in the scheduler
Detailed job view:
Job ID: 1658497
    job_id: 1658497
    wallclock_time_seconds: 38052
    title: aiida-2324985
    num_machines: 4
    job_state: RUNNING
    queue_name: parallel
    num_mpiprocs: 64
    allocated_machines_raw: r02-node[17-18,53-54]
    submission_time: 2018-03-28 09:21:35
    job_owner: some_remote_username
    dispatch_time: 2018-03-28 09:21:35
    annotation: None
    requested_wallclock_time_seconds: 82800

(...)

了解计算机和用户,获取 AuthInfo#

要打开到计算机的传输,需要相应的 AuthInfo 对象,其中包含特定用户所需的信息。一旦获得相关的 ComputerUser 集合,就可以按如下方式获取:

computer.get_authinfo(user)

下面举例说明一个有用的效用函数:

def get_authinfo_from_computer_label(computer_label):
    from aiida.orm import load_computer, User
    computer = load_computer(computer_label)
    user = User.collection.get_default()
    return computer.get_authinfo(user)

例如,您可以如下使用它:

authinfo = get_authinfo_from_computer_label('localhost')
with authinfo.get_transport() as transport:
    print(transport.listdir())