|
PSCF v1.5.0
|
Psuedo-"host" array used for host-to-device data transfer. More...
#include <HostSendArray.h>
Public Types | |
| using | BackendIdClass = CPT |
| Backend identifier class typename alias. | |
Public Member Functions | |
| ~HostSendArray () | |
| Destructor. | |
| void | associate (BackendArray< Data, CPT > &other) |
| Associate this object with a backend array. | |
| void | dissociate () |
| Release association with a backend array. | |
| Public Member Functions inherited from Util::Array< Data > | |
| Data & | operator[] (int i) |
| Get an element by non-const reference. | |
| Data const & | operator[] (int i) const |
| Get an element by const reference. | |
| void | begin (ArrayIterator< Data > &iterator) |
| Set an iterator to begin this Array. | |
| void | begin (ConstArrayIterator< Data > &iterator) const |
| Set a const iterator to begin this array. | |
| Data * | cArray () |
| Return a pointer to the underlying C array (non-const data). | |
| Data const * | cArray () const |
| Return pointer to the underlying C array (const data). | |
| bool | isAllocated () const |
| Does this array have associated data? | |
| int | size () const |
| Return logical size of array. | |
| int | capacity () const |
| Return allocated size. | |
Additional Inherited Members | |
| Protected Member Functions inherited from Util::Array< Data > | |
| Array () | |
| Constructor (protected to prevent direct instantiation). | |
| ~Array () | |
| Destructor (protected to prevent direct destruction). | |
| Protected Attributes inherited from Util::Array< Data > | |
| Data * | data_ |
| Pointer to an array of Data elements. | |
| int | capacity_ |
| Allocated size of the data_ array. | |
Psuedo-"host" array used for host-to-device data transfer.
This class template should be used in device-independent template code in contexts in which the specialization for the CUDA backend (T=Pscf:CUT) would copy an array from the CPU host to the GPU device.
This specialization for the C++ backend (T=Pscf::CPT) instead creates a temporary shallow copy of the associated backend array to mimic the syntax of a copy operation without the cost of an actual deep copy.
The "associate" member function creates a shallow copy of a C array that is owned by an associated backend array. This function takes a non-const reference to an instance of BackendArray<Data,CPT> as its parameter. This parameter is non-const because the shallow copy provides write access to the underlying shared array. After an association is created, elements of this host array can be modified in order to modify the data owned by the backend array. After the modification is complete, the temporary association should be released.
The lifetime of an association with a backend array should not be allowed to extend beyond the function in which the association is created. This is necessary to guarantee that no such association remains when the backend array is deallocated or destroyed. Such an association may be released by calling the dissociate member function of the host array, or will be released by the host array destructor. A reference counting system is used to detect and report erroneous de-allocation of a backend array when it is still referred to by one or more other containers, which would create dangling references.
Usage :
Typical usage is shown below for host-to-device transfer to a long lived instance of BackendArray<Data,CPT> named dArray from a shorter lived instance of HostSendArray<Data,CPT> named hArray, where Data denotes the type of each array element:
Comments:
Definition at line 108 of file cpp/HostSendArray.h.
| using Pscf::HostSendArray< Data, CPT >::BackendIdClass = CPT |
Backend identifier class typename alias.
Definition at line 116 of file cpp/HostSendArray.h.
| Pscf::HostSendArray< Data, CPT >::~HostSendArray | ( | ) |
Destructor.
Definition at line 184 of file cpp/HostSendArray.h.
| void Pscf::HostSendArray< Data, CPT >::associate | ( | BackendArray< Data, CPT > & | other | ) |
Associate this object with a backend array.
Associates this object with a backend array, thus creating a shallow copy that points to the same memory. After successful return, isAllocated() will return true.
| Exception | if source array is not allocated on entry |
| Exception | if this array is already associated |
| other | backend array that owns the data |
Definition at line 196 of file cpp/HostSendArray.h.
References Util::Array< Data >::capacity_, Util::Array< Data >::data_, and UTIL_CHECK.
| void Pscf::HostSendArray< Data, CPT >::dissociate | ( | ) |
Release association with a backend array.
Upon successful return, isAllocated() will return false.
| Exception | if this is not associated with another array. |
Definition at line 217 of file cpp/HostSendArray.h.
References Util::Array< Data >::capacity_, Util::Array< Data >::data_, and UTIL_CHECK.