edu.rit.pj.reduction
Class SharedDoubleArray
- java.lang.Object
-
- edu.rit.pj.reduction.SharedDoubleArray
-
public class SharedDoubleArray extends java.lang.ObjectClass SharedDoubleArray provides an array reduction variable with elements of type double.Class SharedDoubleArray is multiple thread safe. The methods use lock-free atomic compare-and-set.
Note: Class SharedDoubleArray is implemented using class java.util.concurrent.atomic.AtomicLongArray. Each double array element is stored as a long whose bit pattern is the same as the double value.
-
-
Constructor Summary
Constructors Constructor and Description SharedDoubleArray(double[] array)Construct a new double array reduction variable whose elements are copied from the given array.SharedDoubleArray(int len)Construct a new double array reduction variable with the given length.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description doubleaddAndGet(int i, double value)Add the given value to this array reduction variable at the given index and return the new value.booleancompareAndSet(int i, double expect, double update)Atomically set this array reduction variable at the given index to the given updated value if the current value equals the expected value.doubledecrementAndGet(int i)Subtract one from this array reduction variable at the given index and return the new value.doubleget(int i)Returns this array reduction variable's current value at the given index.doublegetAndAdd(int i, double value)Add the given value to this array reduction variable at the given index and return the previous value.doublegetAndDecrement(int i)Subtract one from this array reduction variable at the given index and return the previous value.doublegetAndIncrement(int i)Add one to this array reduction variable at the given index and return the previous value.doublegetAndSet(int i, double value)Set this array reduction variable at the given index to the given value and return the previous value.doubleincrementAndGet(int i)Add one to this array reduction variable at the given index and return the new value.intlength()Returns this array reduction variable's length.voidreduce(double[] src, DoubleOp op)Combine this array reduction variable with the given array using the given operation.voidreduce(int dstoff, double[] src, int srcoff, int len, DoubleOp op)Combine a portion of this array reduction variable with a portion of the given array using the given operation.doublereduce(int i, double value, DoubleOp op)Combine this array reduction variable at the given index with the given value using the given operation.voidset(int i, double value)Set this array reduction variable at the given index to the given value.java.lang.StringtoString()Returns a string version of this array reduction variable.booleanweakCompareAndSet(int i, double expect, double update)Atomically set this array reduction variable at the given index to the given updated value if the current value equals the expected value.
-
-
-
Constructor Detail
-
SharedDoubleArray
public SharedDoubleArray(int len)
Construct a new double array reduction variable with the given length. Each array element is initially 0.- Parameters:
len- Length.- Throws:
java.lang.NegativeArraySizeException- (unchecked exception) Thrown if len < 0.
-
SharedDoubleArray
public SharedDoubleArray(double[] array)
Construct a new double array reduction variable whose elements are copied from the given array.- Parameters:
array- Array to copy.- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if array is null.
-
-
Method Detail
-
length
public int length()
Returns this array reduction variable's length.- Returns:
- Length.
-
get
public double get(int i)
Returns this array reduction variable's current value at the given index.- Parameters:
i- Index.- Returns:
- Current value.
-
set
public void set(int i, double value)Set this array reduction variable at the given index to the given value.- Parameters:
i- Index.value- New value.
-
getAndSet
public double getAndSet(int i, double value)Set this array reduction variable at the given index to the given value and return the previous value.- Parameters:
i- Index.value- New value.- Returns:
- Previous value.
-
compareAndSet
public boolean compareAndSet(int i, double expect, double update)Atomically set this array reduction variable at the given index to the given updated value if the current value equals the expected value.- Parameters:
i- Index.expect- Expected value.update- Updated value.- Returns:
- True if the update happened, false otherwise.
-
weakCompareAndSet
public boolean weakCompareAndSet(int i, double expect, double update)Atomically set this array reduction variable at the given index to the given updated value if the current value equals the expected value. May fail spuriously.- Parameters:
i- Index.expect- Expected value.update- Updated value.- Returns:
- True if the update happened, false otherwise.
-
getAndIncrement
public double getAndIncrement(int i)
Add one to this array reduction variable at the given index and return the previous value.- Parameters:
i- Index.- Returns:
- Previous value.
-
getAndDecrement
public double getAndDecrement(int i)
Subtract one from this array reduction variable at the given index and return the previous value.- Parameters:
i- Index.- Returns:
- Previous value.
-
getAndAdd
public double getAndAdd(int i, double value)Add the given value to this array reduction variable at the given index and return the previous value.- Parameters:
i- Index.value- Value to add.- Returns:
- Previous value.
-
incrementAndGet
public double incrementAndGet(int i)
Add one to this array reduction variable at the given index and return the new value.- Parameters:
i- Index.- Returns:
- New value.
-
decrementAndGet
public double decrementAndGet(int i)
Subtract one from this array reduction variable at the given index and return the new value.- Parameters:
i- Index.- Returns:
- New value.
-
addAndGet
public double addAndGet(int i, double value)Add the given value to this array reduction variable at the given index and return the new value.- Parameters:
i- Index.value- Value to add.- Returns:
- New value.
-
reduce
public double reduce(int i, double value, DoubleOp op)Combine this array reduction variable at the given index with the given value using the given operation. (This array [i]) is set to (this array [i]) op (value), then (this array [i]) is returned.- Parameters:
i- Index.value- Value.op- Binary operation.- Returns:
- (This array [i]) op (value).
-
reduce
public void reduce(double[] src, DoubleOp op)Combine this array reduction variable with the given array using the given operation. For each index i from 0 to this array's length-1, (this array [i]) is set to (this array [i]) op (src[i]).The reduce() method is multiple thread safe on a per-element basis. Each individual array element is updated atomically, but the array as a whole is not updated atomically.
- Parameters:
src- Source array.op- Binary operation.- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if src is null. Thrown if op is null.java.lang.IndexOutOfBoundsException- (unchecked exception) Thrown if any array index would be out of bounds.
-
reduce
public void reduce(int dstoff, double[] src, int srcoff, int len, DoubleOp op)Combine a portion of this array reduction variable with a portion of the given array using the given operation. For each index i from 0 to len-1, (this array [dstoff+i]) is set to (this array [dstoff+i]) op (src[srcoff+i]).The reduce() method is multiple thread safe on a per-element basis. Each individual array element is updated atomically, but the array as a whole is not updated atomically.
- Parameters:
dstoff- Index of first element to update in this array.src- Source array.srcoff- Index of first element to update from in the source array.len- Number of array elements to update.op- Binary operation.- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if src is null. Thrown if op is null.java.lang.IndexOutOfBoundsException- (unchecked exception) Thrown if len < 0. Thrown if any array index would be out of bounds.
-
toString
public java.lang.String toString()
Returns a string version of this array reduction variable.- Overrides:
toStringin classjava.lang.Object- Returns:
- String version.
-
-
DMelt 3.0 © DataMelt by jWork.ORG