code: def mpi_run(settings, nics, env, command, stdout=None, stderr=None):
      """
      Runs mpi_run.
  
      Args:
          settings: Settings for running MPI.
                    Note: settings.num_proc and settings.hosts must not be None.
          nics: Interfaces to include by MPI.
          env: Environment dictionary to use for running command.
          command: Command and arguments to run as a list of string.
          stdout: Stdout of the mpi process.
                  Only used when settings.run_func_mode is True.
          stderr: Stderr of the mpi process.
                  Only used when settings.run_func_mode is True.
      """
      if env is not None and not isinstance(env, dict):
          raise Exception(
              "env argument must be a dict, not {type}: {env}".format(
                  type=type(env), env=env
              )
          )
  
      (
          mpi_impl_flags,
          impl_binding_args,
          mpi,
      ) = _get_mpi_implementation_flags(settings.tcp_flag, env=env)
      if mpi_impl_flags is None:
          raise Exception(_MPI_NOT_FOUND_ERROR_MSG)
  
      impi_or_mpich = mpi in (_IMPI_IMPL, _MPICH_IMPL)
  
      ssh_args = []
      if settings.ssh_port:
          ssh_args += [f"-p {settings.ssh_port}"]
      if settings.ssh_identity_file:
          ssh_args += [f"-i {settings.ssh_identity_file}"]
  
      mpi_ssh_args = ""
      if ssh_args:
          joined_ssh_args = " ".join(ssh_args)
          mpi_ssh_args = (
              f'-bootstrap=ssh -bootstrap-exec-args "{joined_ssh_args}"'
              if impi_or_mpich
              else f'-mca plm_rsh_args "{joined_ssh_args}"'
          )
  
      if settings.nics and impi_or_mpich:
          if len(nics) > 1:
              raise Exception(
                  "Intel MPI and MPICH do not support multiple interfaces."
              )
          tcp_intf_arg = "-iface {nic}".format(nic=list(nics)[0])
      else:
          tcp_intf_arg = (
              "-mca btl_tcp_if_include {nics}".format(
                  nics=",".join(nics)
              )
              if nics and not impi_or_mpich
              else ""
          )
      nccl_socket_intf_arg = (
          "-{opt} NCCL_SOCKET_IFNAME={nics}".format(
              opt="genv" if impi_or_mpich else "x", nics=",".join(nics)
          )
          if nics
          else ""
      )
  
      # On large cluster runs (e.g. Summit), we need extra settings to work around OpenMPI issues
      host_names, host_to_slots = hosts.parse_hosts_and_slots(
          settings.hosts
      )
      if (
          not impi_or_mpich
          and host_names
          and len(host_names) >= _LARGE_CLUSTER_THRESHOLD
      ):
          mpi_impl_flags.append("-mca plm_rsh_no_tree_spawn true")
          mpi_impl_flags.append(
              "-mca plm_rsh_num_concurrent {}".format(len(host_names))
          )
  
      # if user does not specify any hosts, mpirun by default uses local host.
      # There is no need to specify localhost.
      hosts_arg = "-{opt} {hosts}".format(
          opt="hosts" if impi_or_mpich else "H",
          hosts=",".join(host_names)
          if host_names and impi_or_mpich
          else settings.hosts,
      )
  
      ppn_arg = " "
      if host_to_slots and impi_or_mpich:
          ppn = host_to_slots[host_names[0]]
          for h_name in host_names[1:]:
              if ppn != host_to_slots[h_name]:
                  raise Exception(
                      """Different slots in -hosts parameter are not supported in Intel(R) MPI.
                                   Use -machinefile <machine_file> for this purpose."""
                  )
          ppn_arg = " -ppn {} ".format(ppn)
  
      if settings.prefix_output_with_timestamp and not impi_or_mpich:
          mpi_impl_flags.append("--timestamp-output")
  
      binding_args = (
          settings.binding_args
          if settings.binding_args and not impi_or_mpich
          else " ".join(impl_binding_args)
      )
  
      basic_args = (
          "-l" if impi_or_mpich else "--allow-run-as-root --tag-output"
      )
  
      output = []
      if settings.output_filename:
          output.append(
              "-outfile-pattern"
              if impi_or_mpich
              else "--output-filename"
          )
          output.append(settings.output_filename)
  
      env_list = (
          ""
          if impi_or_mpich
          else " ".join(
              "-x %s" % key
              for key in sorted(env.keys())
              if env_util.is_exportable(key)
          )
      )
  
      # Pass all the env variables to the mpirun command.
      mpirun_command = (
          "mpirun {basic_args} "
          "-np {num_proc}{ppn_arg}{hosts_arg} "
          "{binding_args} "
          "{mpi_args} "
          "{mpi_ssh_args} "
          "{tcp_intf_arg} "
          "{nccl_socket_intf_arg} "
          "{output_filename_arg} "
          "{env} {extra_mpi_args} {command}".format(  # expect a lot of environment variables
              basic_args=basic_args,
              num_proc=settings.num_proc,
              ppn_arg=ppn_arg,
              hosts_arg=hosts_arg,
              binding_args=binding_args,
              mpi_args=" ".join(mpi_impl_flags),
              tcp_intf_arg=tcp_intf_arg,
              nccl_socket_intf_arg=nccl_socket_intf_arg,
              mpi_ssh_args=mpi_ssh_args,
              output_filename_arg=" ".join(output),
              env=env_list,
              extra_mpi_args=settings.extra_mpi_args
              if settings.extra_mpi_args
              else "",
              command=" ".join(quote(par) for par in command),
          )
      )
  
      if settings.verbose >= 2:
          print(mpirun_command)
  
      # we need the driver's PATH and PYTHONPATH in env to run mpirun,
      # env for mpirun is different to env encoded in mpirun_command
      for var in ["PATH", "PYTHONPATH"]:
          if var not in env and var in os.environ:
              # copy env so we do not leak env modifications
              env = copy.copy(env)
              # copy var over from os.environ
              env[var] = os.environ[var]
  
      # Execute the mpirun command.
      if settings.run_func_mode:
          exit_code = safe_shell_exec.execute(
              mpirun_command, env=env, stdout=stdout, stderr=stderr
          )
          if exit_code != 0:
              raise RuntimeError(
                  "mpirun failed with exit code {exit_code}".format(
                      exit_code=exit_code
                  )
              )
      else:
          os.execve("/bin/sh", ["/bin/sh", "-c", mpirun_command], env)
  
file_path: horovod/horovod/runner/mpi_run.py
imports: import copy
  import os
  import sys
  from shlex import quote
  from horovod.runner.common.util import env, hosts, safe_shell_exec, tiny_shell_exec
label_desc: Use image node_id 8 for calling a global function with example usage: mpi_run(settings, nics, env, command, stdout, stderr) without return types