proc ::pwtk::remote_exec {args} {  


remote_exec [options] host run_script

Options are:

     -rdir  <value>
     -rpwtk <value>


The remote_exec is not meant to be run on HPC supercomputers, where PWTK batch-queuing should be used instead (see section: 2.7 Batch Queuing Support).

Currently, the use of this routine is discouraged.


The remote_exec runs the Quantum-ESPRESSO (QE) calculation(s) on remote host.

The connection to remote host is managed via ssh tools. User must be able to connect to remote host without password via the ssh key (i.e., the content ~/.ssh/ key file must be added to the ~/.ssh/authorized_keys file on the remote host). The way the ssh is used by PWTK can be configured by ~/.pwtk/remote.tcl file (for a template see the $PWTK/config/remote.tcl).



There are two possible remote execution modes, alias foreground and background (although technically the calculation is always run in background on the remote host).

In the "foreground" mode the PWTK waits for the calculation to finish before any further processing. This is the default.

In the "background" mode the PWTK submits the calculation to remote host and forgets about it; the control is immediately returned for further processing. This mode is requested by the "-bg" option. Note that any output files as produced by remote calculation will not be copied to current local directory.

By default remote_exec implies that (i) the remote host uses the current file-system on the local host, that is, the running directory, where stdin and stdout files are located, is the current directory on the local host, and (ii) the PWTK configuration (e.g. ~/.pwtk/pwtk.tcl and the location of QE binaries) on the remote host is identical to that on the local host.

If the remote host uses its own file-system, then the "-rfs" options should be used. By default it is assumed that the file-system on the remote host is a mirror-like compared to the local file-system. For example, if the current working directory on local host is ~/path/to/project, then the calculation will be run on ~/path/to/project directory on remote host (remote file-system); note that the value of $HOME on remote and local hosts can differ.

If the file-system on remote-home is not mirror-like or if the calculation on the remote host should be run in some specific directory (which is not a mirror of the current working directory) then the remote running directory can be specified with -rdir <value> option.

The "-rfull" option implies aka "full remote" mode. This implies (i) -rfs and that (ii) the remote PWTK configuration is used (e.g. the ~/.pwtk/pwtk.tcl and other configuration files are loaded from remote file-system).

The location of the PWTK on remote host can be explicitly specified with the -rpwtk <value> option. Note that this option automatically implies -rfull.



The fully remote mode (-rfull) has the following caveat related to the use of outdir_prefix/outdir_postfix mechanism. If the script relies on the implicit outdir_prefix value as set in ~/.pwtk/pwtk.tcl and the remote host uses different definition of outdir_prefix in its own ~/.pwtk/pwtk.tcl, then the value of outdir_prefix as set in the local ~/.pwtk/pwtk.tcl will be used; this may be seen as an inconsistency that may potentially cause problems. The best way to avoid any associated problems is to explicitly define the outdir_prefix (or to explicitly define the QE's outdir variable) in the pwtk scripts that use the remote_exec machinery.

TODO -- ANOTHER PROBLEMATIC ISSUE: pseudo_dir (It cannot be handled as follow: check if CONTROL's pseudo_dir is defined. If not, implicit pseudo_dir is used; try to load from remote ~/.pwtk/pwtk.tcl. The reason is that user may use the pseudo_dir within the pwtk script and loading from remote's ~/.pwtk/pwtk.tcl would overwrite user explicit setting. There should be a special mechanism by which it would be known from where the pseudo_dir is defined (i.e. like an input stacking, where the level 0 is reserved for configuration files; this would also solve the above outdir_prefix issue. But then we need to define which variables to add to stacking.


Run a given pw.x calculation on remote host.

       remote_exec -rpwtk ~/prog/q-e/pwtk/pwtk -rdir ~/calc/QE/myProject user@remotehost {
          runPW -ihandle {<} -prefix {mpirun -np 8} -postfix {-npool 2} -exec ~/bin/QE-5.1/pw.x relax.slab-2x2-5L


    variable remote_exec

    # set remote mode on

    # parse the options

    set options {
        {bg            "run the calculation in background and forget about it (default = foreground)"}
        {rfs           "run on remote file-system, i.e., remote host has its own filesystem that is distinct from the parent filesystem"}
        {rfull         "full-remote, i.e., implies -rfs; bin_dir, prefix, and postfix are queried on remote-host"}
        {rpwtk.arg {}  "full pathname of pwtk launcher on remote-host (e.g. /full/path/to/pwtk); automatically implies -rfull"}
        {rdir.arg  {}  "directory on the remote host where calculation will be run (default = mirror of the current directory on local file-system); automatically implies -rfs"}

    set usage "Usage: ::pwtk::remote_exec \[options\] host run_script\noptions:"

    array set remote_exec [::cmdline::getoptions args $options $usage]

    if { [llength $args] != 2 } {
        error [::cmdline::usage $options $usage]
    set remote_exec(host) [lindex $args 0]
    set run_script        [lindex $args 1]
    # check if remote host is alive

    if { [catch {exec ping -cd 2 $remote_exec(host)} result] } {
        # it seems dead; abort ...

        ::pwtk::error "host \"$remote_exec(host)\" either is not alive or does not exist;\n$result" 1

    # handle the -rpwtk & -rfull options

    set pwtk_ pwtk
    if { $remote_exec(rpwtk) != "" } {
        set remote_exec(rfull) 1        
        set pwtk_ $remote_exec(rpwtk)
    if { $remote_exec(rfull) } {
        set remote_exec(rfs) 1
        # check if pwtk program is executable on remote-host
        remote_execok_ $pwtk_ complain
    # create the running directory if requested

    if { $remote_exec(rdir) != "" } {
        set remote_exec(rfs) 1

    if { $remote_exec(rfs) } {
        if { $remote_exec(rdir) != "" } {
            set dir $remote_exec(rdir)
        } else {
            global env
            set dir [::fileutil::stripPath $env(HOME) [pwd]]
        ::pwtk::print "Remote running directory on $remote_exec(host): $dir"
        remote_mkdir_ $dir

    # run the calculation (script) remotely    
    uplevel "$run_script"

    # set remote mode off