Parallel calculations#

Most functionality provided by ASE itself is serial, with heavy computations often delegated via calculators to external codes which provide their own MPI-based parallelism. In that context, the Python process which imports ASE should run in serial, with the calculator configured to launch a subprocess with mpiexec.

However some codes (notably GPAW and Asap3) can run in a parallel Python process. ase.parallel provides functionality for such in-process MPI parallelism.

By default, ASE runs in serial. To use ASE with MPI, the calling MPI program must define the ASE parallel world hook before ASE is imported. When ASE runs with MPI, the main effect is that many I/O operations only write on MPI world rank 0, or on rank 0 of a communicator passed as input.

Note

ase-3.30.0 introduces a new parallel hook and deprecates the old mechanism.

The old mechanism is: ASE will automatically run in parallel, if it can import an MPI communicator from any of the supported libraries. ASE will attempt to import communicators from these external libraries: GPAW, Asap, and MPI4PY.

The motivation for deprecating the old mechanism is that ASE is a library, not an application. Therefore it should not modify global variables or initialize MPI. It is always the calling program’s responsibility to set globals or initialize MPI at the beginning of the runtime. Another use case for providing the parallel hook is that other clients than GPAW and Asap are now free to provide their own hooks, adding flexibility. There can only be one MPI hook in a given runtime, so MPI programs are mutually exclusive. Therefore, as a general piece of advice, minimize your reliance on a particular hook with the possible exception of the mpi4py hook provided by ASE itself.

As an example, if ASE initializes MPI, it is not possible for a library which uses ASE to start a subprocess which initializes MPI. Initializing MPI at the ASE level would interfere with valid use cases of other software.

Note

It is strongly advised not to set the ASE_PARALLEL_WORLD_HOOK from .bashrc or similar places, because that will conflict with programs on the system that rely on ASE being serial or using a different hook. Set the world hook only if you control the runtime (i.e. if you write the main() function) and make sure to set it before ASE is imported.

If a parallel library is found, the ase.io.read() function will always read only on master (of the MPI world object) and broadcast the atoms to all other cores. Therefore, always when using ase.io.read(), all cores must read the same atoms in same order, for example in the case of a NEB calculation.

If one requires an individual core/cores to read a particular file, please use Trajectory():

>>> from ase.atoms import Atoms
>>> from ase.io import Trajectory
>>> from ase.parallel import world
>>> atoms = Atoms('N2', positions=[[0., 0., 0.], [0., 0., 1.]])
>>> with Trajectory(f'myfile_{world.rank}.traj', mode='w', atoms=atoms) as traj:
>>>     traj.write()
>>> with Trajectory(f'myfile_{world.rank}.traj', mode='r') as traj:
>>>     atoms = traj[-1]
ase.parallel.paropen(name, mode='r', buffering=-1, encoding=None, comm=None)[source]#

MPI-safe version of open function.

In read mode, the file is opened on all nodes. In write and append mode, the file is opened on the master only, and /dev/null is opened on all other nodes.

ase.parallel.parprint(*args, comm=None, **kwargs)[source]#

MPI-safe print - prints only from master.

ase.parallel.broadcast(obj, root=0, comm=<ase.parallel.MPI object>)[source]#

Broadcast a Python object across an MPI communicator and return it.

ase.parallel.parallel_function(func)[source]#

Decorator for broadcasting from master to slaves using MPI.

Disable by passing parallel=False to the function. For a method, you can also disable the parallel behavior by giving the instance a self.serial = True.

ase.parallel.parallel_generator(generator)[source]#

Decorator for broadcasting yields from master to slaves using MPI.

Disable by passing parallel=False to the function. For a method, you can also disable the parallel behavior by giving the instance a self.serial = True.

Communicators#

ase.parallel.world represents the MPI world communicator and exposes a limited set of methods from it as described below.

class ase.parallel.MPICommunicator(*args, **kwargs)[source]#

Limited interface to MPI communicator for ASE.

abstractmethod abort(code: int) → None[source]#

Abort runtime with given error code.

property backend: str#

A string identifier for this Python MPI interface.

The purpose of this string is to be able to check that the correct MPI implementation is in use.

Each implementing subclass of this protocol should normally try to pick a string that will be distinct from those of other implementations, whereas wrappers should normally return the backend of the wrapped communicator.

ASE already provides the implementations ‘serial’ and ‘ase-mpi4py’.

abstractmethod barrier() → None[source]#

Wait until all ranks reach barrier.

abstractmethod broadcast(a: ndarray, root: int) → None[source]#

Broadcast array from root across all ranks.

product(a: ndarray, root: int = -1) → None[source]#

In-place product over ranks.

See sum() for rules about root.

rank: int#

Rank of this process on the communicator.

size: int#

Number of processes on this communicator.

abstractmethod sum(a: ndarray, root: int = -1) → None[source]#

In-place sum over ranks.

The sum is computed only on the rank specified by root, or all ranks if root is -1.

abstractmethod sum_scalar(a, root: int = -1)[source]#

Return the sum over ranks of a scalar.

See sum() for rules about root.

Run a parallel calculation#

To run a parallel calculation using mpi4py, use the ase parallel command:

mpiexec -n 4 ase parallel myscript.py

See also ase parallel --help. If you are using GPAW or Asap3, see the documentation of those projects.

Parallel MPI world hook#

If you want to implement a program that can run ASE in parallel using another object for ase.parallel.world than the one provided by the ase parallel commmand, set the environment variable ASE_PARALLEL_WORLD_HOOK. It must point to a module with a function named ASE_PARALLEL_WORLD_HOOK() which must return an instance of ase.parallel.MPICommunicator or None for serial ASE. This is the mechanism that external programs such as GPAW and Asap3 should use.

ase.hook_mpi4py can be used as an example of this pattern in combination with ase.cli.parallel.