Package edu.rit.pj.reduction
Class SharedLongMatrix
java.lang.Object
edu.rit.pj.reduction.SharedLongMatrix
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.
- Version:
- 12-Feb-2010
- Author:
- Alan Kaminsky
-
Constructor Summary
ConstructorDescriptionSharedLongMatrix
(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
Modifier and TypeMethodDescriptionlong
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
Combine this matrix reduction variable at the given row and column with the given value using the given operation.void
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.
-
Constructor Details
-
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 ifrows
< 0 orcols
< 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 thematrix
have the same number of columns.- Parameters:
matrix
- Matrix to copy.- Throws:
NullPointerException
- (unchecked exception) Thrown ifmatrix
is null or any row ofmatrix
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 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
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
Combine this matrix reduction variable with the given matrix using the given operation. For every rowr
and columnc
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 ifsrc
is null. Thrown ifop
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 indexr
from 0 torowlen-1
inclusive, and for each column indexc
from 0 tocollen-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 ifsrc
is null. Thrown ifop
is null.IndexOutOfBoundsException
- (unchecked exception) Thrown ifrowlen
< 0. Thrown ifcollen
< 0. Thrown if any matrix index would be out of bounds.
-