Class ObjectBuf<T>
- Type Parameters:
T
- Data type of the objects in the buffer.
- Direct Known Subclasses:
EmptyObjectBuf
,ObjectArrayBuf
,ObjectItemBuf
,ObjectMatrixBuf
,SharedObjectArrayBuf
,SharedObjectBuf
A buffer may be used to send one or more messages at the same time in multiple threads. If a buffer is being used to send a message or messages, the buffer must not be used to receive a message at the same time.
A buffer may be used to receive one message at a time. If a buffer is being used to receive a message, the buffer must not be used to receive another message in a different thread, and the buffer must not be used to send a message or messages.
A buffer is a conduit for retrieving and storing data in some underlying data structure. If the underlying data structure is multiple thread safe, then one thread can be retrieving or storing data via the buffer at the same time as other threads are accessing the data structure. If the underlying data structure is not multiple thread safe, then other threads must not access the data structure while one thread is retrieving or storing data via the buffer.
To create an ObjectBuf, call one of the following static factory methods:
emptyBuffer()
buffer()
buffer (T)
buffer (T[])
sliceBuffer (T[], Range)
sliceBuffers (T[], Range[])
objectBuffer (T[])
buffer (T[][])
rowSliceBuffer (T[][], Range)
rowSliceBuffers (T[][], Range[])
colSliceBuffer (T[][], Range)
colSliceBuffers (T[][], Range[])
patchBuffer (T[][], Range, Range)
patchBuffers (T[][], Range[], Range[])
objectBuffer (T[][])
buffer (SharedObject<T>)
buffer (SharedObjectArray<T>)
sliceBuffer (SharedObjectArray<T>, Range)
sliceBuffers (SharedObjectArray<T>, Range[])
There are two ways to create a buffer for an array of objects (type
T[]
):
-
With the
buffer(T[])
,sliceBuffer(T[],Range)
, andsliceBuffers(T[],Range[])
methods. These methods create a buffer that sends and receives the array elements as multiple separate objects of typeT
. The receiver must allocate an array of the proper dimension to receive the incoming objects and must create a buffer that receives the array elements as separate objects. -
With the
objectBuffer(T[])
method. This method creates a buffer that sends and receives the entire array as one object of typeT[]
. The receiver must also create a buffer that receives the entire array as one object; the buffer'sitem
field is automatically set to an array of the proper dimension.
There are two ways to create a buffer for a matrix of objects (type
T[][]
):
-
With the
buffer(T[][])
,rowSliceBuffer(T[][],Range)
,rowSliceBuffers(T[][],Range[])
,colSliceBuffer(T[][],Range)
,colSliceBuffers(T[][],Range[])
,patchBuffer(T[][],Range)
, andpatchBuffers(T[][],Range[])
methods. These methods create a buffer that sends and receives the matrix elements as multiple separate objects of typeT
. The receiver must allocate a matrix of the proper dimensions to receive the incoming objects and must create a buffer that receives the matrix elements as separate objects. -
With the
objectBuffer(T[][])
method. This method creates a buffer that sends and receives the entire matrix as one object of typeT[][]
. The receiver must also create a buffer that receives the matrix as one object; the buffer'sitem
field is automatically set to a matrix of the proper dimensions.
Important Note: An ObjectBuf uses the protected field
mySerializedItems
to store the serialized representation of the
objects in the buffer. If the buffer is used to receive a message, the
serialized representation of the received objects is cached in
mySerializedItems
. If the buffer is used to send a message and
mySerializedItems
is empty, the objects in the buffer are
serialized, the serialized representation is cached in
mySerializedItems
, and the serialized representation is sent in the
message. If the buffer is used to send a message and
mySerializedItems
is not empty, the objects in the buffer are
not serialized; rather, the cached serialized representation is sent.
This is done to avoid re-serializing the objects if the buffer is used to
send copies of a message to multiple destinations, or if the buffer is used
to receive and then immediately send a message. However, if the state of any
object in the buffer changes, the buffer's reset()
method must be
called; this tells the buffer to discard the cached serialized representation
and re-serialize the objects in the buffer.
- Version:
- 03-Jul-2008
- Author:
- Alan Kaminsky
-
Field Summary
Modifier and TypeFieldDescriptionprotected byte[]
Byte array containing this buffer's object items in serialized form. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> ObjectItemBuf
<T> buffer()
Create a buffer for an object item.static <T> ObjectBuf
<T> buffer
(SharedObject<T> item) Create a buffer for a shared object item.static <T> ObjectBuf
<T> buffer
(SharedObjectArray<T> theArray) Create a buffer for the entire given shared object array.static <T> ObjectItemBuf
<T> buffer
(T item) Create a buffer for an object item with the given initial value.static <T> ObjectBuf
<T> buffer
(T[] theArray) Create a buffer for the entire given object array.static <T> ObjectBuf
<T> buffer
(T[][] theMatrix) Create a buffer for the entire given object matrix.static <T> ObjectBuf
<T> colSliceBuffer
(T[][] theMatrix, Range theColRange) Create a buffer for one column slice of the given object matrix.static <T> ObjectBuf<T>[]
colSliceBuffers
(T[][] theMatrix, Range[] theColRanges) Create an array of buffers for multiple column slices of the given object matrix.void
Copy items from the given buffer to this buffer.protected static <T> void
defaultCopy
(ObjectBuf<T> theSrc, ObjectBuf<T> theDst) Copy items from the given source buffer to the given destination buffer.Create an empty buffer.void
Fill this buffer with the given item.abstract T
get
(int i) Obtain the given item from this buffer.Create a temporary buffer with the same type of items and the same length as this buffer.static <T> ObjectItemBuf
<T[]> objectBuffer
(T[] theArray) Create a buffer for the entire given object array.static <T> ObjectItemBuf
<T[][]> objectBuffer
(T[][] theMatrix) Create a buffer for the entire given object matrix.static <T> ObjectBuf
<T> patchBuffer
(T[][] theMatrix, Range theRowRange, Range theColRange) Create a buffer for one patch of the given object matrix.static <T> ObjectBuf<T>[]
patchBuffers
(T[][] theMatrix, Range[] theRowRanges, Range[] theColRanges) Create an array of buffers for multiple patches of the given object matrix.abstract void
Store the given item in this buffer.protected int
receiveItems
(int i, int num, ByteBuffer buffer) Receive as many items as possible from the given byte buffer to this buffer.void
reset()
Reset this buffer.static <T> ObjectBuf
<T> rowSliceBuffer
(T[][] theMatrix, Range theRowRange) Create a buffer for one row slice of the given object matrix.static <T> ObjectBuf<T>[]
rowSliceBuffers
(T[][] theMatrix, Range[] theRowRanges) Create an array of buffers for multiple row slices of the given object matrix.protected int
sendItems
(int i, ByteBuffer buffer) Send as many items as possible from this buffer to the given byte buffer.static <T> ObjectBuf
<T> sliceBuffer
(SharedObjectArray<T> theArray, Range theRange) Create a buffer for one slice of the given shared object array.static <T> ObjectBuf
<T> sliceBuffer
(T[] theArray, Range theRange) Create a buffer for one slice of the given object array.static <T> ObjectBuf<T>[]
sliceBuffers
(SharedObjectArray<T> theArray, Range[] theRanges) Create an array of buffers for multiple slices of the given shared object array.static <T> ObjectBuf<T>[]
sliceBuffers
(T[] theArray, Range[] theRanges) Create an array of buffers for multiple slices of the given object array.Methods inherited from class edu.rit.mp.Buf
getReductionBuf, length
-
Field Details
-
mySerializedItems
protected byte[] mySerializedItemsByte array containing this buffer's object items in serialized form. If null, the object items need to be serialized.
-
-
Constructor Details
-
ObjectBuf
protected ObjectBuf(int theLength) Construct a new object buffer.- Parameters:
theLength
- Number of items.- Throws:
IllegalArgumentException
- (unchecked exception) Thrown iftheLength
< 0.
-
-
Method Details
-
emptyBuffer
Create an empty buffer. The buffer's length is 0. The buffer's item type is Object.- Returns:
- Empty buffer.
-
buffer
Create a buffer for an object item. The item is stored in theitem
field of the buffer.- Type Parameters:
T
- Data type of the objects in the buffer.- Returns:
- Buffer.
-
buffer
Create a buffer for an object item with the given initial value. The item is stored in theitem
field of the buffer.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
item
- Initial value of theitem
field.- Returns:
- Buffer.
-
buffer
Create a buffer for the entire given object array. The returned buffer encompasses all the elements intheArray
. The array elements are sent and received as multiple separate objects of typeT
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theArray
- Array.- Returns:
- Buffer.
- Throws:
NullPointerException
- (unchecked exception) Thrown iftheArray
is null.
-
sliceBuffer
Create a buffer for one slice of the given object array. The returned buffer encompassestheRange
of elements intheArray
. The range's stride may be 1 or greater than 1. The array elements are sent and received as multiple separate objects of typeT
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theArray
- Array.theRange
- Range of elements to include.- Returns:
- Buffer.
- Throws:
NullPointerException
- (unchecked exception) Thrown iftheArray
is null ortheRange
is null.IndexOutOfBoundsException
- (unchecked exception) Thrown iftheArray
does not include all the indexes intheRange
.
-
sliceBuffers
Create an array of buffers for multiple slices of the given object array. The returned buffer array has the same length astheRanges
. Each element [i] of the returned buffer array encompasses the elements oftheArray
specified bytheRanges[i]
. Each range's stride may be 1 or greater than 1. The array elements are sent and received as multiple separate objects of typeT
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theArray
- Array.theRanges
- Array of ranges of elements to include.- Returns:
- Array of buffers.
- Throws:
NullPointerException
- (unchecked exception) Thrown iftheArray
is null ortheRanges
or any element thereof is null.IndexOutOfBoundsException
- (unchecked exception) Thrown iftheArray
's allocation does not include any element oftheRanges
.
-
objectBuffer
Create a buffer for the entire given object array. The returned buffer encompasses all the elements intheArray
. The array is sent and received as a single object of typeT[]
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theArray
- Array. May be null.- Returns:
- Buffer.
-
buffer
Create a buffer for the entire given object matrix. The returned buffer encompasses all the rows and all the columns intheMatrix
. The matrix elements are sent and received as multiple separate objects of typeT
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theMatrix
- Matrix.- Returns:
- Buffer.
- Throws:
NullPointerException
- (unchecked exception) Thrown iftheMatrix
is null.
-
rowSliceBuffer
Create a buffer for one row slice of the given object matrix. The returned buffer encompassestheRowRange
of rows, and all the columns, intheMatrix
. The range's stride may be 1 or greater than 1. The matrix elements are sent and received as multiple separate objects of typeT
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theMatrix
- Matrix.theRowRange
- Range of rows to include.- Returns:
- Buffer.
- Throws:
NullPointerException
- (unchecked exception) Thrown iftheMatrix
is null ortheRowRange
is null.IndexOutOfBoundsException
- (unchecked exception) Thrown iftheMatrix
's allocation does not includetheRowRange
.
-
rowSliceBuffers
Create an array of buffers for multiple row slices of the given object matrix. The returned buffer array has the same length astheRowRanges
. Each element [i] of the returned buffer array encompasses the rows oftheMatrix
specified bytheRowRanges[i]
and all the columns oftheMatrix
. Each range's stride may be 1 or greater than 1. The matrix elements are sent and received as multiple separate objects of typeT
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theMatrix
- Matrix.theRowRanges
- Array of ranges of rows to include.- Returns:
- Array of buffers.
- Throws:
NullPointerException
- (unchecked exception) Thrown iftheMatrix
is null ortheRowRanges
or any element thereof is null.IndexOutOfBoundsException
- (unchecked exception) Thrown iftheMatrix
's allocation does not include any element oftheRowRanges
.
-
colSliceBuffer
Create a buffer for one column slice of the given object matrix. The returned buffer encompasses all the rows, andtheColRange
of columns, intheMatrix
. The range's stride may be 1 or greater than 1. The matrix elements are sent and received as multiple separate objects of typeT
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theMatrix
- Matrix.theColRange
- Range of columns to include.- Returns:
- Buffer.
- Throws:
NullPointerException
- (unchecked exception) Thrown iftheMatrix
is null ortheColRange
is null.IndexOutOfBoundsException
- (unchecked exception) Thrown iftheMatrix
's allocation does not includetheColRange
.
-
colSliceBuffers
Create an array of buffers for multiple column slices of the given object matrix. The returned buffer array has the same length astheColRanges
. Each element [i] of the returned buffer array encompasses all the rows oftheMatrix
and the columns oftheMatrix
specified bytheColRanges[i]
. Each range's stride may be 1 or greater than 1. The matrix elements are sent and received as multiple separate objects of typeT
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theMatrix
- Matrix.theColRanges
- Array of ranges of columns to include.- Returns:
- Array of buffers.
- Throws:
NullPointerException
- (unchecked exception) Thrown iftheMatrix
is null ortheColRanges
or any element thereof is null.IndexOutOfBoundsException
- (unchecked exception) Thrown iftheMatrix
's allocation does not include any element oftheColRanges
.
-
patchBuffer
Create a buffer for one patch of the given object matrix. The returned buffer encompassestheRowRange
of rows, andtheColRange
of columns, intheMatrix
. Each range's stride may be 1 or greater than 1. The matrix elements are sent and received as multiple separate objects of typeT
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theMatrix
- Matrix.theRowRange
- Range of rows to include.theColRange
- Range of columns to include.- Returns:
- Buffer.
- Throws:
NullPointerException
- (unchecked exception) Thrown iftheMatrix
is null,theRowRange
is null, ortheColRange
is null.IndexOutOfBoundsException
- (unchecked exception) Thrown iftheMatrix
's allocation does not includetheRowRange
andtheColRange
.
-
patchBuffers
public static <T> ObjectBuf<T>[] patchBuffers(T[][] theMatrix, Range[] theRowRanges, Range[] theColRanges) Create an array of buffers for multiple patches of the given object matrix. The length of the returned buffer array is equal to the length oftheRowRanges
times the length oftheColRanges
. Each element of the returned buffer array encompasses the rows given in one element oftheRowRanges
array, and the columns given in one element oftheColRanges
array, in all possible combinations, oftheMatrix
. Each range's stride may be 1 or greater than 1. The matrix elements are sent and received as multiple separate objects of typeT
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theMatrix
- Matrix.theRowRanges
- Array of ranges of rows to include.theColRanges
- Array of ranges of columns to include.- Returns:
- Array of buffers.
- Throws:
NullPointerException
- (unchecked exception) Thrown iftheMatrix
is null,theRowRanges
or any element thereof is null, ortheColRanges
or any element thereof is null.IndexOutOfBoundsException
- (unchecked exception) Thrown iftheMatrix
's allocation does not include any element oftheRowRanges
ortheColRanges
.
-
objectBuffer
Create a buffer for the entire given object matrix. The returned buffer encompasses all the rows and all the columns intheMatrix
. The matrix is sent and received as a single object of typeT[][]
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theMatrix
- Matrix. May be null.- Returns:
- Buffer.
-
get
Obtain the given item from this buffer.The
get()
method must not block the calling thread; if it does, all message I/O in MP will be blocked.- Parameters:
i
- Item index in the range 0 ..length()
-1.- Returns:
- Item at index
i
.
-
put
Store the given item in this buffer.The
put()
method must not block the calling thread; if it does, all message I/O in MP will be blocked.- Parameters:
i
- Item index in the range 0 ..length()
-1.item
- Item to be stored at indexi
.
-
copy
Copy items from the given buffer to this buffer. The number of items copied is this buffer's length ortheSrc
's length, whichever is smaller. IftheSrc
is this buffer, thecopy()
method does nothing. Copy items from the given buffer to this buffer. The number of items copied is this buffer's length ortheSrc
's length, whichever is smaller. IftheSrc
is this buffer, thecopy()
method does nothing.The default implementation of the
copy()
method calls thedefaultCopy()
method. A subclass can override thecopy()
method to use a more efficient algorithm.The default implementation of the
copy()
method also calls thereset()
method.- Specified by:
copy
in classBuf
- Parameters:
theSrc
- Source of items to copy into this buffer.- Throws:
ClassCastException
- (unchecked exception) Thrown iftheSrc
's item data type is not the same as this buffer's item data type.
-
fill
Fill this buffer with the given item. Theitem
is assigned to each element in this buffer.If this buffer's item data type is a primitive type, the
item
must be an instance of the corresponding primitive wrapper class -- class Integer for typeint
, class Double for typedouble
, and so on. If theitem
is null, the item data type's default initial value is assigned to each element in this buffer.If this buffer's item data type is a nonprimitive type, the
item
must be an instance of the item class or a subclass thereof. Theitem
may be null. Note that sinceitem
is assigned to every buffer element, every buffer element ends up referring to the sameitem
. Fill this buffer with the given item. Theitem
is assigned to each element in this buffer.The
item
must be an instance of class T or a subclass thereof. Theitem
may be null. Note that sinceitem
is assigned to every buffer element, every buffer element ends up referring to the sameitem
.The
fill()
method calls thereset()
method.- Specified by:
fill
in classBuf
- Parameters:
item
- Item.- Throws:
ClassCastException
- (unchecked exception) Thrown if theitem
's data type is not the same as this buffer's item data type.
-
getTemporaryBuf
Create a temporary buffer with the same type of items and the same length as this buffer. The new buffer items are stored in a newly created array, separate from the storage for this buffer's items.- Specified by:
getTemporaryBuf
in classBuf
- Returns:
- a
Buf
object.
-
reset
public void reset()Reset this buffer. Callreset()
if the state of any object in this buffer changes. -
sendItems
Send as many items as possible from this buffer to the given byte buffer.The
sendItems()
method must not block the calling thread; if it does, all message I/O in MP will be blocked. Send as many items as possible from this buffer to the given byte buffer.The
sendItems()
method must not block the calling thread; if it does, all message I/O in MP will be blocked. -
receiveItems
Receive as many items as possible from the given byte buffer to this buffer.The
receiveItems()
method must not block the calling thread; if it does, all message I/O in MP will be blocked. Receive as many items as possible from the given byte buffer to this buffer.The
receiveItems()
method must not block the calling thread; if it does, all message I/O in MP will be blocked.- Specified by:
receiveItems
in classBuf
- Parameters:
i
- Index of first item to receive, in the range 0 ..length
-1.num
- Maximum number of items to receive.buffer
- Byte buffer.- Returns:
- Number of items received.
-
defaultCopy
Copy items from the given source buffer to the given destination buffer. The number of items copied istheSrc
's length ortheDst
's length, whichever is smaller. Each item is copied individually using theget()
andput()
methods. It is assumed thattheSrc
is not the same astheDst
.- Type Parameters:
T
- Data type of the objects in the buffer.- Parameters:
theSrc
- Source of items to copy.theDst
- Destination of items to copy.
-