PSCF v1.5.0
Pscf::HostSendArray< Data, CPT > Class Template Referencefinal

Psuedo-"host" array used for host-to-device data transfer. More...

#include <HostSendArray.h>

Inheritance diagram for Pscf::HostSendArray< Data, CPT >:
Util::Array< Data >

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.

Detailed Description

template<typename Data>
class Pscf::HostSendArray< Data, CPT >

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:

HostSendArray<Data,CPT> hArray;
hArray.associate(dArray);
\\ ( Initialize data in hArray )
dArray = hArray
hArray.dissociate();

Comments:

  • In this specialization for a CPU backend (T=CPT), the assignment (=) operator that assigns a RHS HostSendArray<Data,CPT> to a LHS BackendArray<Data,CPT> template normally does nothing. It is provided for compatability with the syntax required for GPU code, with T=CUT, for which the analogous assignment operator actually copies data from the CPU host memory to GPU device global memory.
  • In this specialization for a CPU backend (T=CPT), the dissociate function destroys the shallow copy owned by the host array, by nullifying a pointer held by the host array. In the corresponding specialization for a Cuda GPU backend, with T=CUT, the analogous function does nothing.
  • The host array should never be used to modify data after the assignment operator and before the dissociate function is invoked. Doing so would modify data owned by the backend array in CPU code (T=CPT) but would have no effect on data owned by the backend array in GPU code (T=CUT), causing inconsistent behavior. To enforce this, it is good practice to invoke the dissociate member function immediately after host-to-device assignment, as shown above.
  • The invocation of dissociate may sometimes be omitted when the host array is a local variable that is defined within a function and the assignment operator occurs near the end of that function, because any remaining association with a backend array will be destroyed by the host array destructor function when the host array goes out of scope.

Definition at line 108 of file cpp/HostSendArray.h.

Member Typedef Documentation

◆ BackendIdClass

template<typename Data>
using Pscf::HostSendArray< Data, CPT >::BackendIdClass = CPT

Backend identifier class typename alias.

Definition at line 116 of file cpp/HostSendArray.h.

Constructor & Destructor Documentation

◆ ~HostSendArray()

template<typename Data>
Pscf::HostSendArray< Data, CPT >::~HostSendArray ( )

Destructor.

Definition at line 184 of file cpp/HostSendArray.h.

Member Function Documentation

◆ associate()

template<typename Data>
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.

Exceptions
Exceptionif source array is not allocated on entry
Exceptionif this array is already associated
Parameters
otherbackend 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.

◆ dissociate()

template<typename Data>
void Pscf::HostSendArray< Data, CPT >::dissociate ( )

Release association with a backend array.

Upon successful return, isAllocated() will return false.

Exceptions
Exceptionif 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.


The documentation for this class was generated from the following file: