|
PSCF v1.5.0
|
Dynamic array on the GPU device with aligned data. More...
#include <BackendArray.h>
Public Types | |
| typedef Data | ValueType |
| Data type of each element. | |
| using | BackendIdClass = CUT |
| Backend identifier class type. | |
Public Member Functions | |
| BackendArray () | |
| Default constructor. | |
| BackendArray (int capacity) | |
| Allocating constructor. | |
| BackendArray (BackendArray< Data, CUT > const &other) | |
| Copy constructor. | |
| BackendArray (Array< Data > const &other) | |
| Copy constructor, deep copy Array<Data> from host to device. | |
| virtual | ~BackendArray () |
| Destructor. | |
| void | allocate (int capacity) |
| Allocate the underlying C array on the device. | |
| void | deallocate () |
| Dellocate the underlying C array. | |
| void | associate (BackendArray< Data, CUT > &arr, int beginId, int capacity) |
| Associate this object with a slice of a different BackendArray. | |
| void | associate (DeviceMemory &arr, int capacity) |
| Associate this object with a DeviceMemory container. | |
| void | dissociate () |
| Dissociate this object from an externally owned memory block. | |
| void | addReference (CountedReference &reference) |
| Associate a reference with reference counter for this container. | |
| virtual BackendArray< Data, CUT > & | operator= (BackendArray< Data, CUT > const &other) |
| Assignment operator, assign from other BackendArray<Data,CUT>. | |
| virtual BackendArray< Data, CUT > & | operator= (const Array< Data > &other) |
| Assignment operator, assignment from Util::Array<Data> host array. | |
| int | capacity () const |
| Return array capacity. | |
| bool | isAllocated () const |
| Return true if the array has allocated data, false otherwise. | |
| bool | isOwner () const |
| Does this container own an allocated memory block? | |
| bool | isAssociated () const |
| Is this container associated with a memory block it does not own? | |
| Data * | cArray () |
| Return pointer to underlying C array. | |
| Data const * | cArray () const |
| Return const pointer to underlying C array. | |
Protected Attributes | |
| Data * | dataPtr_ |
| Pointer to a C array of Data elements on the GPU device. | |
| int | capacity_ |
| Allocated size (capacity) of the array. | |
| ReferenceCounter | refCounter_ |
| Counter for any arrays that use data owned by this. | |
| CountedReference | ref_ |
| Reference to another array that owns memory used by this one. | |
Dynamic array on the GPU device with aligned data.
This class wraps an aligned C array with elements of type Data that is allocated in GPU device global memory. All member functions may be called from the CPU host, but this class does not offer access to individual elements via the subscript operator, operator[].
A BackendArray can be in any of three states:
(1) Unallocated: In this state, there is no associated memory, so capacity() returns 0, while isAllocated(), isOwner() and isAssociated() all return false.
(2) A data owner: In this case, this object owns a block of memory that it is responsible for de-allocating. In this state, capacity() returns a positive integer value, isAllocated() and isOwner() return true, and isAssociated() returns false.
(3) A data user: In this case, this object has a pointer to a block of device memory that is owned by a different container. We describe this by saying this BackendArray (the data user) is "associated" with or "references" memory owned by another container (the data owner). In this state, capacity() returns a positive value, isAllocated() and isAssociated() return true, and isOwner() returns false.
Memory that is owned by a BackendArray may be allocated and deallocated by the allocate() and deallocate() member functions. Memory owned by a BackendArray is also dellocated when the BackendArray is destroyed.
A BackendArray<Data,CUT> object may be associated as a data user with:
Associations with either type of data owner can be created and destroyed by calling the associate and dissociate member functions of the data user. Attempts to create an association cause an Exception to be thrown if the requested slice of shared data would exceed the bounds of the block owned by the data owner.
A BackendArray<Data,CUT> that serves as an owner of data that is used by one or more other associated BackendArray<Data,CUT> objects maintains a count of how many associated objects refer to its data. This counter is incremented when an association is created by a data user and decremented when the user destroys the association.
When a BackendArray<Data,CUT> object is destroyed, any data that it owns is automatically deallocated, and any association with external data is destroyed.
It is an error to deallocate memory owned by a BackendArray or DeviceMemory container that is still referred to by one or more associated BackendArray data users. Doing so is illegal because it creates dangling pointers. Attempts to prematurely deallocate such a shared memory block either cause an Exception to be thrown, if it occurs within the deallocate function, or cause an error message to be written to std::cout if the error occurs in the BackendArray destructor function. All references to such a shared data block must thus be released, by calling the dissociate() member function of each data user, before the data owner can be safely destroyed.
Assignment from an Array<Data> copies all elements of an array from CPU to GPU memory.
Definition at line 103 of file cuda/BackendArray.h.
| typedef Data Pscf::BackendArray< Data, CUT >::ValueType |
Data type of each element.
Definition at line 113 of file cuda/BackendArray.h.
| using Pscf::BackendArray< Data, CUT >::BackendIdClass = CUT |
Backend identifier class type.
Definition at line 118 of file cuda/BackendArray.h.
| Pscf::BackendArray< Data, CUT >::BackendArray | ( | ) |
Default constructor.
Definition at line 374 of file cuda/BackendArray.h.
References BackendArray(), capacity_, and dataPtr_.
Referenced by addReference(), allocate(), associate(), associate(), BackendArray(), BackendArray(), BackendArray(), BackendArray(), capacity(), cArray(), cArray(), deallocate(), dissociate(), isAllocated(), isAssociated(), isOwner(), operator=(), operator=(), and ~BackendArray().
| Pscf::BackendArray< Data, CUT >::BackendArray | ( | int | capacity | ) |
Allocating constructor.
This function calls allocate(capacity) internally.
| capacity | number of elements to allocate |
Definition at line 383 of file cuda/BackendArray.h.
References allocate(), BackendArray(), capacity(), capacity_, and dataPtr_.
| Pscf::BackendArray< Data, CUT >::BackendArray | ( | BackendArray< Data, CUT > const & | other | ) |
Copy constructor.
| other | BackendArray<Data,CUT> to be copied (input) |
Definition at line 394 of file cuda/BackendArray.h.
References allocate(), BackendArray(), capacity_, and dataPtr_.
| Pscf::BackendArray< Data, CUT >::BackendArray | ( | Array< Data > const & | other | ) |
Copy constructor, deep copy Array<Data> from host to device.
| other | Array<Data> to be copied (input) |
Definition at line 414 of file cuda/BackendArray.h.
References allocate(), BackendArray(), Util::Array< Data >::capacity(), capacity_, Util::Array< Data >::cArray(), dataPtr_, and Util::Array< Data >::isAllocated().
|
virtual |
Destructor.
Deletes underlying C array, if allocated previously.
Definition at line 432 of file cuda/BackendArray.h.
References BackendArray(), dataPtr_, isOwner(), ref_, and refCounter_.
| void Pscf::BackendArray< Data, CUT >::allocate | ( | int | capacity | ) |
Allocate the underlying C array on the device.
| Exception | if the array is already allocated. |
| capacity | number of elements to allocate. |
Definition at line 452 of file cuda/BackendArray.h.
References BackendArray(), capacity(), capacity_, dataPtr_, isAllocated(), ref_, and UTIL_THROW.
Referenced by BackendArray(), BackendArray(), BackendArray(), operator=(), and operator=().
| void Pscf::BackendArray< Data, CUT >::deallocate | ( | ) |
Dellocate the underlying C array.
Definition at line 473 of file cuda/BackendArray.h.
References BackendArray(), capacity_, dataPtr_, ref_, refCounter_, and UTIL_CHECK.
| void Pscf::BackendArray< Data, CUT >::associate | ( | BackendArray< Data, CUT > & | arr, |
| int | beginId, | ||
| int | capacity ) |
Associate this object with a slice of a different BackendArray.
| Exception | if the array is already allocated. |
| arr | parent array that owns the data |
| beginId | index in the parent array at which this array starts |
| capacity | number of elements associated with this container |
Definition at line 487 of file cuda/BackendArray.h.
References BackendArray(), capacity(), capacity_, dataPtr_, ref_, and UTIL_CHECK.
| void Pscf::BackendArray< Data, CUT >::associate | ( | DeviceMemory & | arr, |
| int | capacity ) |
Associate this object with a DeviceMemory container.
When a BackendArray<Data,CUT> is associated with a DeviceMemory container, the shared data block begins at the first byte of the block owned by the DeviceMemory. The number of byes in such a shared block is equal to capacity * sizeof(Data). This must be less than or equal to the number of byes in the block owned by the DeviceMemory, or this function will throw an Exception.
| Exception | if the array is already allocated |
| Exception | if the requested memory block is too large |
| arr | DeviceMemory container that owns the data |
| capacity | number of elements of type Data |
Definition at line 510 of file cuda/BackendArray.h.
References Pscf::DeviceMemory::addReference(), BackendArray(), capacity(), Pscf::DeviceMemory::capacity(), capacity_, Pscf::DeviceMemory::cArray(), dataPtr_, Pscf::DeviceMemory::isAllocated(), ref_, and UTIL_CHECK.
| void Pscf::BackendArray< Data, CUT >::dissociate | ( | ) |
Dissociate this object from an externally owned memory block.
This function can destroy an association with either another BackendArray or with a DeviceMemory. After successful exit, isAllocated() and isAssociated() will both return false.
| Exception | if this is not associated with external memory |
Definition at line 530 of file cuda/BackendArray.h.
References BackendArray(), capacity_, dataPtr_, ref_, and UTIL_CHECK.
| void Pscf::BackendArray< Data, CUT >::addReference | ( | CountedReference & | reference | ) |
Associate a reference with reference counter for this container.
This function should normally only be called within the associate member function of a container that is referring to data owned by this array, as part of the process of creating an association. It should not be called by users in any other context.
On exit, the ReferenceCounter owned by this DeviceMemory is incremented by one and the CountedReference contains a pointer to the ReferenceCounter. This pointer can later be used as part of the process of destroying the association.
| reference | reference to be included by reference counter |
Definition at line 548 of file cuda/BackendArray.h.
References Util::CountedReference::associate(), BackendArray(), and refCounter_.
|
virtual |
Assignment operator, assign from other BackendArray<Data,CUT>.
Performs a deep copy, by copying values of all elements.
This function will allocate memory if this (LHS) array is not allocated. If this array is arleady allocated, it must have the same capacity as the other (RHS) BackendArray<Data,CUT>.
| Exception | if RHS other array is not allocated |
| Exception | if RHS and LHS have unequal nonzero capacities |
| other | BackendArray<Data,CUT> on RHS of assignent (input) |
Definition at line 556 of file cuda/BackendArray.h.
References allocate(), BackendArray(), capacity_, dataPtr_, isAllocated(), and UTIL_THROW.
|
virtual |
Assignment operator, assignment from Util::Array<Data> host array.
Performs a deep copy from a RHS Array<Data> host array to this LHS BackendArray<Data,CUT>, by copying underlying C array from host memory to device memory.
If this (LHS) BackendArray<Data,CUT> is not allocated on entry, memory will be allocated before data is copied. If this LHS object is already allocated, it must have the same capacity as the RHS Array<Data>.
| Exception | if RHS other array is not allocated |
| Exception | if RHS and LHS have unequal nonzero capacities |
| other | Array<Data> on RHS of assignent (input) |
Definition at line 589 of file cuda/BackendArray.h.
References allocate(), BackendArray(), Util::Array< Data >::capacity(), capacity_, Util::Array< Data >::cArray(), dataPtr_, isAllocated(), Util::Array< Data >::isAllocated(), and UTIL_THROW.
|
inline |
Return array capacity.
Definition at line 336 of file cuda/BackendArray.h.
References BackendArray(), and capacity_.
Referenced by allocate(), associate(), associate(), and BackendArray().
|
inline |
Return true if the array has allocated data, false otherwise.
A BackendArray is considered allocated if it has non-null pointer to an data block, which may either be a block that it owns or a block owned by another container.
Definition at line 343 of file cuda/BackendArray.h.
References BackendArray(), and dataPtr_.
Referenced by allocate(), operator=(), and operator=().
|
inline |
Does this container own an allocated memory block?
If isAllocated() is false, isOwner() must be false.
Definition at line 350 of file cuda/BackendArray.h.
References BackendArray(), dataPtr_, and ref_.
Referenced by ~BackendArray().
|
inline |
Is this container associated with a memory block it does not own?
If isAllocated() is false, isAssociated() must be false.
Definition at line 357 of file cuda/BackendArray.h.
References BackendArray(), dataPtr_, and ref_.
| Data * Pscf::BackendArray< Data, CUT >::cArray | ( | ) |
Return pointer to underlying C array.
Definition at line 618 of file cuda/BackendArray.h.
References BackendArray(), dataPtr_, and UTIL_CHECK.
| const Data * Pscf::BackendArray< Data, CUT >::cArray | ( | ) | const |
Return const pointer to underlying C array.
Definition at line 628 of file cuda/BackendArray.h.
References BackendArray(), dataPtr_, and UTIL_CHECK.
|
protected |
Pointer to a C array of Data elements on the GPU device.
Definition at line 313 of file cuda/BackendArray.h.
Referenced by allocate(), associate(), associate(), BackendArray(), BackendArray(), BackendArray(), BackendArray(), cArray(), cArray(), deallocate(), dissociate(), isAllocated(), isAssociated(), isOwner(), operator=(), operator=(), and ~BackendArray().
|
protected |
Allocated size (capacity) of the array.
Definition at line 316 of file cuda/BackendArray.h.
Referenced by allocate(), associate(), associate(), BackendArray(), BackendArray(), BackendArray(), BackendArray(), capacity(), deallocate(), dissociate(), operator=(), and operator=().
|
protected |
Counter for any arrays that use data owned by this.
Definition at line 319 of file cuda/BackendArray.h.
Referenced by addReference(), deallocate(), and ~BackendArray().
|
protected |
Reference to another array that owns memory used by this one.
Definition at line 322 of file cuda/BackendArray.h.
Referenced by allocate(), associate(), associate(), deallocate(), dissociate(), isAssociated(), isOwner(), and ~BackendArray().