3.2. pyopus.parallel.mpi — A virtual machine based on the MPI library

Inheritance diagram of pyopus.parallel.mpi

A virtual machine based on MPI and mpi4py (PyOPUS subsystem name: MPI)

Attempts to import mpi4py. If import fails the mpi4py wrapper for the MPI library is not available on the computer and an arror is raised.

class pyopus.parallel.mpi.MPI(debug=0, startupDir=None, translateStartupDir=True, mirrorMap=None, persistentStorage=False)[source]

A virtual machine class based on the mpi4py wrapper for MPI.

One task (rank 0) is the spawner and does all the spawning. This is the main task that runs the main program. Other tasks (rank>0) are workers. Workers run a spawn loop in which they receive spawn requests from the spawner. A worker can run one spawned task at a time. If the task fails (caught exception), the spawner is notified, upon which the loop expects a new spawn requst. If a task finishes cleanly the spawner is also notified. The point at which the spawner and the workers separate is in the spawnerBarrier() method (this is also the place where the worker’s spawn loop is entered. The spawner is the only task that returns from spawnerBarrier(). Workers exit the spawn loop only when they receive an exit request from the spawner (i.e. only when the MPI application is about to terminate).

Assumes homogeneous clusters. This means that LINUX32 and LINUX64 are not homogeneous, as well as LINUX32 and WINDOWS32. Inhomogenous clusters are possible if the undelying MPI library has supports for this feature.

See the VirtualMachine class for more information on the constructor.

checkForIncoming(fromDestination=None)[source]

Returns True if there is a message waiting to be received.

fromDestination is the MPITaskID of the message source. None implies any source.

static finalize()[source]

Asks workers to exit politely. Then calls Finalize().

See VirtualMachine.finalize() for more information.

static formatSpawnerConfig()[source]

Formats the configuration information gathered by a spawner task as a string. Works only if called by a spawner task.

static freeSlots()[source]

Returns the number of free slots for tasks in the virtual machine. The slot bookkeeping is done by hand.

Works only on the spawner.

static freeSlotsSet()[source]

Returns the slot numbers of free slots.

Works only on the spawner.

static hostID(taskID=None)[source]

Returns the MPIHostID object corresponding to task.

If task is not specified, the MPIHostID object corresponding to the caller task is returned.

static hosts()[source]

Returns a list of MPIHostID objects corresponding to all hosts.

Works only on the spawner.

static hostsWithFreeSlots()[source]

Returns set of objects of class HostID corresponding to hosts with at least one free slot.

Works only on the spawner.

static parentTaskID()[source]

Returns the MPITaskID object corresponding to the task that spawned the caller task.

receiveMessage(timeout=-1.0, fromDestination=None)[source]

Receives a message (a Python object) and returns a tuple (senderTaskId, message)

The sender of the message can be identified through the senderTaskId object of class MPITaskID.

If timeout (seconds) is negative the function waits (blocks) until some message arrives. If timeout>0 seconds pass without receiving a message, an empty tuple is returned. Zero timeout performs a nonblocking receive which returns an empty tuple if no message is received. Note that timeout>0 is implemented a bit kludgy with a poll loop.

In case of an error or discarded message returns None.

Handles transparently all

  • task exit notification messages

  • spawned function return value messages

Discards all other low-level MPI messages that were not sent with the sendMessage() method.

sendMessage(destination, message)[source]

Sends message (a Python object) to a task with MPITaskID destination. Returns True on success.

Sends also our own task number and the destination task number.

setWorkerDebug(debug=0)[source]

Sets the worker debug message level to debug.

static slots()[source]

Returns the number of slots for tasks in a virtual machine. Works only on the spawner.

Calls mpi.COMM_WORLD.Get_size().

spawnFunction(function, args=(), kwargs={}, count=None, targetList=None, sendBack=True)[source]

Spawns count instances of a Python function on remote hosts and passes args and kwargs to the function. Spawning a function does not start a new Python interpreter. A worker is running is a spawn loop where spawn requests are executed.

targetList specifies the hosts on which the tasks are started.

If sendBack is True the status and the return value of the spawned function are sent back to the spawner with a pyopus.parallel.base.MsgTaskResult message when the spawned function returns.

Invokes the runFunctionWithArgs() function on the remote host for starting the spawned function.

If spawning succeeds, updates the vmStatus attribute.

Spawns only in free slots. Works only on the spawner.

Returns a list of MPITaskID objects representing the spawned tasks.

See the spawnFunction() method of the VirtualMachine class for more information.

spawnerCall(function, args=(), kwargs={}, asynchr=False)[source]

Calls function with args and kwargs in the spawner task. If asynchr is False waits for the call to finish and returns the function’s return value.

If called in a worker task delegates the call to the spawner.

If called in a spawner task, calls function locally.

This function is useful for sending data to a central point (spawner) where it is processed (e.g. for database writes, logging, etc.).

static taskID()[source]

Returns the MPITaskID object corresponding to the calling task.

vmStatus = {'freeHosts': {}, 'freeSlots': {}, 'hosts': {'calypso': {'freeSlots': {}, 'ncpu': 8, 'slots': {0}}}, 'slot2host': {0: 'calypso'}, 'slots': {0: {'host': 'calypso', 'pid': 665616, 'taskNumber': 0}}, 'usedSlots': {0}}

Static member that appears once per every Python process using the MPI library. Available only on the spawner.

Represents the status of the MPI virtual machine.

class pyopus.parallel.mpi.MPIHostID(name)[source]

A MPI host ID class based on the HostID class.

In the MPI library does not provide host IDs so we use hostnames. Invalid host ID has None for hostname.

The actual hostname can be accessed as the name member.

See the HostID class for more information.

static bad()[source]

A static member function. Called with MPIHostID.bad(). Returns an invalid host ID.

valid()[source]

Returns True if this MPIHostID object is valid.

class pyopus.parallel.mpi.MPITaskID(rank, number=None)[source]

A MPI task ID class based on the TaskID class.

A task ID is composed of MPI rank and task number which is assigned by the spawner. Valid task IDs have nonnegative rank.

The actual rank and task number can be accessed as rank and number members.

See the TaskID class for more information.

static bad()[source]

A static member function. Called with MPITaskID.bad(). Returns an invalid task ID.

valid()[source]

Returns True if this MPITaskID object is valid.