Class SharedIntegerMatrix

java.lang.Object
edu.rit.pj.reduction.SharedIntegerMatrix

public class SharedIntegerMatrix extends Object
Class SharedIntegerMatrix provides a matrix reduction variable with elements of type int.

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

Note: Class SharedIntegerMatrix is implemented using class java.util.concurrent.atomic.AtomicIntegerArray.

Version:
18-Feb-2010
Author:
Alan Kaminsky
  • Constructor Summary

    Constructors
    Constructor
    Description
    SharedIntegerMatrix(int[][] matrix)
    Construct a new integer matrix reduction variable whose elements are copied from the given matrix.
    SharedIntegerMatrix(int rows, int cols)
    Construct a new integer matrix reduction variable with the given number of rows and columns.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    addAndGet(int r, int c, int value)
    Add the given value to this matrix reduction variable at the given row and column and return the new value.
    int
    Returns the number of columns in this matrix reduction variable.
    boolean
    compareAndSet(int r, int c, int expect, int update)
    Atomically set this matrix reduction variable at the given row and column to the given updated value if the current value equals the expected value.
    int
    decrementAndGet(int r, int c)
    Subtract one from this matrix reduction variable at the given row and column and return the new value.
    int
    get(int r, int c)
    Returns this matrix reduction variable's current value at the given row and column.
    int
    getAndAdd(int r, int c, int value)
    Add the given value to this matrix reduction variable at the given row and column and return the previous value.
    int
    getAndDecrement(int r, int c)
    Subtract one from this matrix reduction variable at the given row and column and return the previous value.
    int
    getAndIncrement(int r, int c)
    Add one to this matrix reduction variable at the given row and column and return the previous value.
    int
    getAndSet(int r, int c, int value)
    Set this matrix reduction variable at the given row and column to the given value and return the previous value.
    int
    incrementAndGet(int r, int c)
    Add one to this matrix reduction variable at the given row and column and return the new value.
    void
    reduce(int[][] src, IntegerOp op)
    Combine this matrix reduction variable with the given matrix using the given operation.
    void
    reduce(int dstrow, int dstcol, int[][] src, int srcrow, int srccol, int rowlen, int collen, IntegerOp op)
    Combine a portion of this matrix reduction variable with a portion of the given matrix using the given operation.
    int
    reduce(int r, int c, int value, IntegerOp op)
    Combine this matrix reduction variable at the given row and column with the given value using the given operation.
    int
    Returns the number of rows in this matrix reduction variable.
    void
    set(int r, int c, int value)
    Set this matrix reduction variable at the given row and column to the given value.
    boolean
    weakCompareAndSet(int r, int c, int expect, int update)
    Atomically set this matrix reduction variable at the given row and column to the given updated value if the current value equals the expected value.

    Methods inherited from class java.lang.Object

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

    • SharedIntegerMatrix

      public SharedIntegerMatrix(int rows, int cols)
      Construct a new integer matrix reduction variable with the given number of rows and columns. Each matrix element is initially 0.
      Parameters:
      rows - Number of rows.
      cols - Number of columns.
      Throws:
      NegativeArraySizeException - (unchecked exception) Thrown if rows < 0 or cols < 0.
    • SharedIntegerMatrix

      public SharedIntegerMatrix(int[][] matrix)
      Construct a new integer matrix reduction variable whose elements are copied from the given matrix. It is assumed that all rows of the matrix have the same number of columns.
      Parameters:
      matrix - Matrix to copy.
      Throws:
      NullPointerException - (unchecked exception) Thrown if matrix is null or any row of matrix is null.
  • Method Details

    • rows

      public int rows()
      Returns the number of rows in this matrix reduction variable.
      Returns:
      Rows.
    • cols

      public int cols()
      Returns the number of columns in this matrix reduction variable.
      Returns:
      Columns.
    • get

      public int get(int r, int c)
      Returns this matrix reduction variable's current value at the given row and column.
      Parameters:
      r - Row index.
      c - Column index.
      Returns:
      Current value.
    • set

      public void set(int r, int c, int value)
      Set this matrix reduction variable at the given row and column to the given value.
      Parameters:
      r - Row index.
      c - Column index.
      value - New value.
    • getAndSet

      public int getAndSet(int r, int c, int value)
      Set this matrix reduction variable at the given row and column to the given value and return the previous value.
      Parameters:
      r - Row index.
      c - Column index.
      value - New value.
      Returns:
      Previous value.
    • compareAndSet

      public boolean compareAndSet(int r, int c, int expect, int update)
      Atomically set this matrix reduction variable at the given row and column to the given updated value if the current value equals the expected value.
      Parameters:
      r - Row index.
      c - Column index.
      expect - Expected value.
      update - Updated value.
      Returns:
      True if the update happened, false otherwise.
    • weakCompareAndSet

      public boolean weakCompareAndSet(int r, int c, int expect, int update)
      Atomically set this matrix reduction variable at the given row and column to the given updated value if the current value equals the expected value. May fail spuriously.
      Parameters:
      r - Row index.
      c - Column index.
      expect - Expected value.
      update - Updated value.
      Returns:
      True if the update happened, false otherwise.
    • getAndIncrement

      public int getAndIncrement(int r, int c)
      Add one to this matrix reduction variable at the given row and column and return the previous value.
      Parameters:
      r - Row index.
      c - Column index.
      Returns:
      Previous value.
    • getAndDecrement

      public int getAndDecrement(int r, int c)
      Subtract one from this matrix reduction variable at the given row and column and return the previous value.
      Parameters:
      r - Row index.
      c - Column index.
      Returns:
      Previous value.
    • getAndAdd

      public int getAndAdd(int r, int c, int value)
      Add the given value to this matrix reduction variable at the given row and column and return the previous value.
      Parameters:
      r - Row index.
      c - Column index.
      value - Value to add.
      Returns:
      Previous value.
    • incrementAndGet

      public int incrementAndGet(int r, int c)
      Add one to this matrix reduction variable at the given row and column and return the new value.
      Parameters:
      r - Row index.
      c - Column index.
      Returns:
      New value.
    • decrementAndGet

      public int decrementAndGet(int r, int c)
      Subtract one from this matrix reduction variable at the given row and column and return the new value.
      Parameters:
      r - Row index.
      c - Column index.
      Returns:
      New value.
    • addAndGet

      public int addAndGet(int r, int c, int value)
      Add the given value to this matrix reduction variable at the given row and column and return the new value.
      Parameters:
      r - Row index.
      c - Column index.
      value - Value to add.
      Returns:
      New value.
    • reduce

      public int reduce(int r, int c, int value, IntegerOp op)
      Combine this matrix reduction variable at the given row and column with the given value using the given operation. (This matrix [r,c]) is set to (this matrix [r,c]) op (value), then (this matrix [r,c]) is returned.
      Parameters:
      r - Row index.
      c - Column index.
      value - Value.
      op - Binary operation.
      Returns:
      (This matrix [r,c]) op (value).
    • reduce

      public void reduce(int[][] src, IntegerOp op)
      Combine this matrix reduction variable with the given matrix using the given operation. For every row r and column c in this matrix, (this matrix [r,c]) is set to (this matrix [r,c]) op (src[r,c]).

      The reduce() method is multiple thread safe on a per-element basis. Each individual matrix element is updated atomically, but the matrix as a whole is not updated atomically.

      Parameters:
      src - Source matrix.
      op - Binary operation.
      Throws:
      NullPointerException - (unchecked exception) Thrown if src is null. Thrown if op is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if any matrix index would be out of bounds.
    • reduce

      public void reduce(int dstrow, int dstcol, int[][] src, int srcrow, int srccol, int rowlen, int collen, IntegerOp op)
      Combine a portion of this matrix reduction variable with a portion of the given matrix using the given operation. For each row index r from 0 to rowlen-1 inclusive, and for each column index c from 0 to collen-1 inclusive, (this matrix [dstrow+r,dstcol+c]) is set to (this matrix [dstrow+r,dstcol+c]) op (src[srcrow+r,srccol+c]).

      The reduce() method is multiple thread safe on a per-element basis. Each individual matrix element is updated atomically, but the matrix as a whole is not updated atomically.

      Parameters:
      dstrow - Row index of first element to update in this matrix.
      dstcol - Column index of first element to update in this matrix.
      src - Source matrix.
      srcrow - Row index of first element to update from in the source matrix.
      srccol - Column index of first element to update from in the source matrix.
      rowlen - Number of rows to update.
      collen - Number of columns to update.
      op - Binary operation.
      Throws:
      NullPointerException - (unchecked exception) Thrown if src is null. Thrown if op is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if rowlen < 0. Thrown if collen < 0. Thrown if any matrix index would be out of bounds.