/usr/share/pyshared/ase/parallel.py is in python-ase 3.6.0.2515-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import sys
import time
import atexit
import numpy as np
def paropen(name, mode='r', buffering=0):
"""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.
"""
if rank > 0 and mode[0] != 'r':
name = '/dev/null'
return open(name, mode, buffering)
def parprint(*args, **kwargs):
"""MPI-safe print - prints only from master.
Tries to adopt python 3 behaviour.
"""
if rank > 0:
return
defaults = {'end': '\n',
'file': sys.stdout }
for key in defaults:
if not key in kwargs:
kwargs[key] = defaults[key]
for arg in args[:-1]:
print >> kwargs['file'], arg,
if len(args):
last = args[-1]
else:
last = ''
if kwargs['end'] == '\n':
print >> kwargs['file'], last
else:
print >> kwargs['file'], last,
class DummyMPI:
rank = 0
size = 1
def sum(self, a):
if isinstance(a, np.ndarray) and a.ndim > 0:
pass
else:
return a
def barrier(self):
pass
def broadcast(self, a, rank):
pass
class MPI4PY:
def __init__(self):
from mpi4py import MPI
self.comm = MPI.COMM_WORLD
self.rank = self.comm.rank
self.size = self.comm.size
def sum(self, a):
return self.comm.allreduce(a)
def barrier(self):
self.comm.barrier()
def broadcast(self, a, rank):
a[:] = self.comm.bcast(a, rank)
# Check for special MPI-enabled Python interpreters:
if '_gpaw' in sys.modules:
# http://wiki.fysik.dtu.dk/gpaw
from gpaw.mpi import world
elif 'asapparallel3' in sys.modules:
# http://wiki.fysik.dtu.dk/Asap
# We cannot import asap3.mpi here, as that creates an import deadlock
#from asap3.mpi import world
import asapparallel3
world = asapparallel3.Communicator()
elif 'Scientific_mpi' in sys.modules:
from Scientific.MPI import world
elif 'mpi4py' in sys.modules:
world = MPI4PY()
else:
# This is a standard Python interpreter:
world = DummyMPI()
rank = world.rank
size = world.size
barrier = world.barrier
def register_parallel_cleanup_function():
"""Call MPI_Abort if python crashes.
This will terminate the processes on the other nodes."""
if size == 1:
return
def cleanup(sys=sys, time=time, world=world):
error = getattr(sys, 'last_type', None)
if error:
sys.stdout.flush()
sys.stderr.write(('ASE CLEANUP (node %d): %s occurred. ' +
'Calling MPI_Abort!\n') % (world.rank, error))
sys.stderr.flush()
# Give other nodes a moment to crash by themselves (perhaps
# producing helpful error messages):
time.sleep(3)
world.abort(42)
atexit.register(cleanup)
def distribute_cpus(parsize_calculator, comm):
"""Distribute cpus to tasks and calculators"""
assert parsize_calculator <= comm.size
assert comm.size % parsize_calculator == 0
tasks_rank = comm.rank // parsize_calculator
r0 = tasks_rank * parsize_calculator
ranks = np.arange(r0, r0 + parsize_calculator)
calc_comm = comm.new_communicator(ranks)
# print 'comm.rank, ranks=', comm.rank, ranks
tasks_comm = np.arange(0, comm.size, parsize_calculator)
return calc_comm, tasks_comm, tasks_rank
|