repository for arbitrary
collections of serializable objects. MPIJava can then be used to communicate
the map contents via broadcast, send, and merge operations.
The magic of MPI and Java Serialization make it all work. A "master" task
can create a map and fill it with a variety of serializable objects:
IParallelContext pc = new MPIContext();
ParallelMap pmap = new ParallelMap;
if (pc.isMaster()) {
DataContext dataContext = new DataContext( args );
Coordinates xyz = new Coordinates( 0, 0, 0 );
Integer rank = new Integer( pc.rank() );
Integer size = new Integer( pc.size() );
pmap.put("DataContext", dataContext );
pmap.put("Coordinates", xyz );
xyz.setValues( 1, 1, 1 );
pmap.put("Size", size );
pmap.put("Rank", rank );
}
pmap.broadcast();
Integer rank = pmap.get("Rank");
pc.serialPrint("Rank after broadcast = " + rank.intValue() );
pc.serialPrint("XYZ Coorinates = " + xyz.toString() );
In the above code fragment, all tasks create a parallel map. The master task
fills it with a variety of serializable objects. In the broadcast, the master
task serializes the map, which in turn serializes every object in the map.
The serialized data stream is sent to all tasks in the parallel context.
On the receiving end, the map is "deserialzed". As each object in the
map is received it is created and placed in the map. Note that the
receiving task does not need to allocate anything other than the map.
Of course the receiving task has to be able to provide a matching object
type when an object is retrieved from the map. In the example above, the retrieved
rank will be 0, as received from the master.
Note that the value of the Coordinates object will be (1,1,1) rather than
(0,0,0) as originally placed in the map, since the "xyz.setValues" updated
the object referred to by the map.
Typical usage is to maintain a synchronous parallel state within a
for a set of tools in a processing stream. Two modes are currently supported.
"broadcast" sends the contents of the map from a master node to all other
tasks. "merge" collects the map from all nodes in rank order and merges
the results into the map on the master task, followed by a broadcast that
replaces the map on all tasks with the merged map.
- Author:
- Chuck Mosher for JavaSeis.org
- See Also:
- Serialized Form
Constructor Summary |
ParallelMap()
Create a new parallel map. |
Method Summary |
void |
broadcast(IParallelContext pc)
Broadcast the contents of the map to all tasks in the
parallel context |
java.lang.Object |
get(java.lang.String key)
Retrieve an object from the map |
java.util.Map<java.lang.String,java.lang.Object> |
getMap()
Return the Map object for the ParallelMap |
java.util.Set<java.lang.String> |
keySet()
Return the set of keys contained in the parallel map |
static void |
main(java.lang.String[] args)
|
void |
merge(IParallelContext pc)
Merge map entries from all tasks in a parallel context. |
void |
put(java.lang.String key,
java.lang.Object value)
Put an object into the parallel map |
void |
putAll(java.util.Map m)
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
MAP_TAG
public static final int MAP_TAG
- See Also:
- Constant Field Values
ParallelMap
public ParallelMap()
- Create a new parallel map. The current map implementation is
"LinkedHashMap", which keeps the map entries in the order in which
they are placed in the map.
broadcast
public void broadcast(IParallelContext pc)
- Broadcast the contents of the map to all tasks in the
parallel context
- Parameters:
pc
- parallel context to be used for the broadcast
get
public java.lang.Object get(java.lang.String key)
- Retrieve an object from the map
- Parameters:
key
- the key value for the object
- Returns:
- the object that was stored
getMap
public java.util.Map<java.lang.String,java.lang.Object> getMap()
- Return the Map object for the ParallelMap
- Returns:
- Map object
keySet
public java.util.Set<java.lang.String> keySet()
- Return the set of keys contained in the parallel map
- Returns:
- key set for the map
main
public static void main(java.lang.String[] args)
- Parameters:
args
-
merge
public void merge(IParallelContext pc)
- Merge map entries from all tasks in a parallel context.
The maps are retrieved in rank order from each task, and the
map on the master task is updated. The master task then
broadcasts the map to all tasks, which first clear their
local copy, and then replace the map with the merged version.
- Parameters:
pc
- parallel context for the merge
put
public void put(java.lang.String key,
java.lang.Object value)
- Put an object into the parallel map
- Parameters:
key
- the key to be used for retrieving the objectvalue
- the serializable object to be placed in the map
putAll
public void putAll(java.util.Map m)