Class ReplicatedInteger

java.lang.Object
java.lang.Number
edu.rit.pj.replica.ReplicatedInteger
All Implemented Interfaces:
Serializable

public class ReplicatedInteger extends Number
Class ReplicatedInteger provides a replicated, shared reduction variable for a value of type int.

A replicated, shared reduction variable is intended to be used in a cluster or hybrid parallel program for a data item shared among all the processes in the program and all the threads in each process. To use a replicated, shared reduction variable, do the following in each process of the parallel program:

  1. Construct an instance of class ReplicatedInteger, specifying the reduction operator (class Op) to use when performing updates, and specifying the communicator (class Comm) and the message tag to use for sending updates among the processes. At this point a replica of the variable exists in each process.
  2. To read the variable, call the get() method. The current value of the local process's replicated variable is returned.
  3. To update the variable, call the reduce() method, specifying a new value. The reduce() method performs an atomic reduction (described below) on the local process's replicated variable with the new value. If the variable changed as a result of the reduction, the variable's (updated) value is flooded to all the processes in the communicator. Finally, the reduce() method returns the variable's value.

    Whenever one of the aforementioned flooded messages arrives, a separate thread performs an atomic reduction on the local process's variable with the received value.

    An atomic reduction consists of these steps, performed atomically: Call the reduction operator's op() method, passing in the current value of the local process's replicated variable and the new value (either the new value specified as an argument of reduce(), or the new value received in a flooded message). Then store the op() method's return value back into the local process's replicated variable.

Class ReplicatedInteger does not itself guarantee consistency of the replicas' values. This is to avoid the message passing overhead of a distributed state update protocol. Instead, the parallel program must be written to operate correctly when the variable is updated as described above. Note that the value of a process's local replica can change asynchronously at any time, either because a thread in the current process updated the variable, or because a flooded message updated the variable.

Class ReplicatedInteger is multiple thread safe. The methods use lock-free atomic compare-and-set.

Note: Class ReplicatedInteger is implemented using class java.util.concurrent.atomic.AtomicInteger.

Version:
12-Sep-2008
Author:
Alan Kaminsky
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a new replicated, shared integer reduction variable with the given reduction operator.
    ReplicatedInteger(IntegerOp op, int initialValue)
    Construct a new replicated, shared integer reduction variable with the given reduction operator and initial value.
    ReplicatedInteger(IntegerOp op, int initialValue, int tag)
    Construct a new replicated, shared integer reduction variable with the given reduction operator, initial value, and message tag.
    ReplicatedInteger(IntegerOp op, int initialValue, int tag, Comm comm)
    Construct a new replicated, shared integer reduction variable with the given reduction operator, initial value, message tag, and communicator.
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    Returns this reduction variable's current value converted to type double.
    float
    Returns this reduction variable's current value converted to type float.
    int
    get()
    Returns this replicated, shared reduction variable's current value.
    int
    Returns this reduction variable's current value converted to type int.
    long
    Returns this reduction variable's current value converted to type long.
    int
    reduce(int value)
    Update this replicated, shared reduction variable's current value.
    Returns a string version of this reduction variable.

    Methods inherited from class java.lang.Number

    byteValue, shortValue

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ReplicatedInteger

      public ReplicatedInteger(IntegerOp op)
      Construct a new replicated, shared integer reduction variable with the given reduction operator. The initial value is 0. A message tag of 0 is used. The world communicator is used.
      Parameters:
      op - Reduction operator.
      Throws:
      NullPointerException - (unchecked exception) Thrown if op is null.
    • ReplicatedInteger

      public ReplicatedInteger(IntegerOp op, int initialValue)
      Construct a new replicated, shared integer reduction variable with the given reduction operator and initial value. A message tag of 0 is used. The world communicator is used.
      Parameters:
      op - Reduction operator.
      initialValue - Initial value.
      Throws:
      NullPointerException - (unchecked exception) Thrown if op is null.
    • ReplicatedInteger

      public ReplicatedInteger(IntegerOp op, int initialValue, int tag)
      Construct a new replicated, shared integer reduction variable with the given reduction operator, initial value, and message tag. The world communicator is used.
      Parameters:
      op - Reduction operator.
      initialValue - Initial value.
      tag - Message tag.
      Throws:
      NullPointerException - (unchecked exception) Thrown if op is null. Thrown if comm is null.
    • ReplicatedInteger

      public ReplicatedInteger(IntegerOp op, int initialValue, int tag, Comm comm)
      Construct a new replicated, shared integer reduction variable with the given reduction operator, initial value, message tag, and communicator.
      Parameters:
      op - Reduction operator.
      initialValue - Initial value.
      tag - Message tag.
      comm - Communicator.
      Throws:
      NullPointerException - (unchecked exception) Thrown if op is null. Thrown if comm is null.
  • Method Details

    • get

      public int get()
      Returns this replicated, shared reduction variable's current value.
      Returns:
      Current value.
    • reduce

      public int reduce(int value) throws IOException
      Update this replicated, shared reduction variable's current value. This variable is combined with the given value using the reduction operation specified to the constructor (op). The result is stored back into this variable and is returned; the result may also be flooded to all processes in the communicator.
      Parameters:
      value - Value.
      Returns:
      (This variable) op (value).
      Throws:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • toString

      public String toString()
      Returns a string version of this reduction variable.
      Overrides:
      toString in class Object
      Returns:
      String version.
    • intValue

      public int intValue()
      Returns this reduction variable's current value converted to type int.
      Specified by:
      intValue in class Number
      Returns:
      Current value.
    • longValue

      public long longValue()
      Returns this reduction variable's current value converted to type long.
      Specified by:
      longValue in class Number
      Returns:
      Current value.
    • floatValue

      public float floatValue()
      Returns this reduction variable's current value converted to type float.
      Specified by:
      floatValue in class Number
      Returns:
      Current value.
    • doubleValue

      public double doubleValue()
      Returns this reduction variable's current value converted to type double.
      Specified by:
      doubleValue in class Number
      Returns:
      Current value.