edu.rit.pj.reduction
Class SharedLongMatrix

java.lang.Object
  extended by edu.rit.pj.reduction.SharedLongMatrix

public class SharedLongMatrix
extends Object

Class SharedLongMatrix provides a matrix reduction variable with elements of type long.

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

Note: Class SharedLongMatrix is implemented using class java.util.concurrent.atomic.AtomicLongArray.


Constructor Summary
SharedLongMatrix(int rows, int cols)
          Construct a new long matrix reduction variable with the given number of rows and columns.
SharedLongMatrix(long[][] matrix)
          Construct a new long matrix reduction variable whose elements are copied from the given matrix.
 
Method Summary
 long addAndGet(int r, int c, long value)
          Add the given value to this matrix reduction variable at the given row and column and return the new value.
 int cols()
          Returns the number of columns in this matrix reduction variable.
 boolean compareAndSet(int r, int c, long expect, long 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.
 long decrementAndGet(int r, int c)
          Subtract one from this matrix reduction variable at the given row and column and return the new value.
 long get(int r, int c)
          Returns this matrix reduction variable's current value at the given row and column.
 long getAndAdd(int r, int c, long value)
          Add the given value to this matrix reduction variable at the given row and column and return the previous value.
 long getAndDecrement(int r, int c)
          Subtract one from this matrix reduction variable at the given row and column and return the previous value.
 long getAndIncrement(int r, int c)
          Add one to this matrix reduction variable at the given row and column and return the previous value.
 long getAndSet(int r, int c, long value)
          Set this matrix reduction variable at the given row and column to the given value and return the previous value.
 long 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 dstrow, int dstcol, long[][] src, int srcrow, int srccol, int rowlen, int collen, LongOp op)
          Combine a portion of this matrix reduction variable with a portion of the given matrix using the given operation.
 long reduce(int r, int c, long value, LongOp op)
          Combine this matrix reduction variable at the given row and column with the given value using the given operation.
 void reduce(long[][] src, LongOp op)
          Combine this matrix reduction variable with the given matrix using the given operation.
 int rows()
          Returns the number of rows in this matrix reduction variable.
 void set(int r, int c, long value)
          Set this matrix reduction variable at the given row and column to the given value.
 boolean weakCompareAndSet(int r, int c, long expect, long 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 Detail

SharedLongMatrix

public SharedLongMatrix(int rows,
                        int cols)
Construct a new long 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.

SharedLongMatrix

public SharedLongMatrix(long[][] matrix)
Construct a new long 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 Detail

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 long 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,
                long 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 long getAndSet(int r,
                      int c,
                      long 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,
                             long expect,
                             long 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,
                                 long expect,
                                 long 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 long 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 long 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 long getAndAdd(int r,
                      int c,
                      long 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 long 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 long 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 long addAndGet(int r,
                      int c,
                      long 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 long reduce(int r,
                   int c,
                   long value,
                   LongOp 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(long[][] src,
                   LongOp 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,
                   long[][] src,
                   int srcrow,
                   int srccol,
                   int rowlen,
                   int collen,
                   LongOp 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.


Copyright © 2005-2012 by Alan Kaminsky. All rights reserved. Send comments to ark­@­cs.rit.edu.