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

Array for pseudo device-to-host array transfer with C++ backend. More...

#include <HostRecvArray.h>

Inheritance diagram for Pscf::HostRecvArray< Data, CPT >:
Util::ConstArray< Data >

Public Types

using BackendIdClass = CPT
 Backend identifier class typename alias.

Public Member Functions

 ~HostRecvArray ()
 Destructor.
 HostRecvArray (BackendArray< Data, CPT > const &other)
 Conversion construction from a BackendArray.
HostRecvArray< Data, CPT > & operator= (BackendArray< Data, CPT > const &other)
 Create a read-only association with a BackendArray.
void dissociate ()
 Release association with a backend array.
Public Member Functions inherited from Util::ConstArray< Data >
Data const & operator[] (int i) const
 Get an element by const reference.
void begin (ConstArrayIterator< Data > &iterator) const
 Set a const iterator to begin this ConstArray.
Data const * cArray () const
 Return a pointer to the underlying C array (pointer to const data).
bool isAllocated () const
 Does this array have associated data?
int size () const
 Return logical size of this array.
int capacity () const
 Return allocated size of this array.

Additional Inherited Members

Protected Member Functions inherited from Util::ConstArray< Data >
 ConstArray ()
 Constructor (protected to prevent direct instantiation).
 ~ConstArray ()
 Destructor (protected to prevent direct destruction).
Protected Attributes inherited from Util::ConstArray< 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::HostRecvArray< Data, CPT >

Array for pseudo device-to-host array transfer with C++ backend.

This template specialization should be used in backend-independent template code as the host array in contexts in which the specialization for the CUDA backend (T=Pscf:CUT) would copy an array from the GPU device to the CPU host. This specialization for the C++ backend (T=Pscf::CPT) instead makes the host array a shallow copy of the backend array, thus avoiding the cost of an actual deep copy. This specialization supports random read-only access to array elements via a subscript ([]) operator that returns a const reference, or via a ConstArrayIterator.

Conversion construction and assignment (operator =) from an instance of BackendArray<Data,CPT> to a HostRecvArray<Data,CPT> each create a shallow read-only copy of a C array that is owned by the associated backend array, via a private pointer to this shared array. These two functions each take a const reference to BackendArray<Data,CPT> as a parameter, and so can be used in contexts in which the backend array must be passed as a const reference. Conversion construction is equivalent to default construction followed by assignment.

An association with a backend array can be released by calling the dissociate member function, or will be released upon destruction of this host array. Such an association must be released by one of these two methods before the backend array that owns the associated data is de-allocated or destroyed. A reference counting system is used to detect and report dangerous de-allocation of a backend array that is still referred to by one or more other containers, which would create a dangling reference.

Usage:

Typical usage is shown below for a pseudo device-to-host transfer from a longer lived instance of BackendArray<Data,CPT> named deviceArray to a shorter lived instance of HostRecvArray<Data,CPT> named hostArray. The alias Data denotes the data type of each array element.

HostRecvArray<Data,CPT> hostArray;
hostArray = deviceArray
// Read the data in hostArray, and possibly peform a computation
hostArray.dissociate();

The default constructor and assignment operations may be combined into a single call of the conversion constructor, giving the shorter version:

HostRecvArray<Data,CPT> hostArray(deviceArray)
// Read the data in hostArray, and possibly peform a computation
hostArray.dissociate();

Comments:

  • The assignment (=) operator or conversion constructor each make hostArray a shallow copy of deviceArray, which references an array that is owned by deviceArray.
  • The dissociate member function releases the association with the backend array, by nullifying the private Data* pointer member of host array that is used to point to an array.
  • A call to the dissociate function call must occur after all statements that access elements of the host array.
  • The call to the dissociate function may be omitted if hostArray is a local variable that is defined within a function but deviceArray lives beyond the end of the function. In this case, any remaining association will be released by the host array destructor when function ends and the host array is destroyed. association.
  • The HostRecvArray template does not define an public "associate" member function like that defined by the HostSendArray template. By convention, an association may only be created by the conversion constructor or the assignment operator.
See also
Pscf::HostRecvArray<Data, CUT>

Definition at line 114 of file cpp/HostRecvArray.h.

Member Typedef Documentation

◆ BackendIdClass

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

Backend identifier class typename alias.

Definition at line 122 of file cpp/HostRecvArray.h.

Constructor & Destructor Documentation

◆ ~HostRecvArray()

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

Destructor.

Releases any remaining association.

Definition at line 215 of file cpp/HostRecvArray.h.

◆ HostRecvArray()

template<typename Data>
Pscf::HostRecvArray< Data, CPT >::HostRecvArray ( BackendArray< Data, CPT > const & other)

Conversion construction from a BackendArray.

Create a read-only association with the backend array, thus creating a shallow copy that points to the memory owned by the backend array.

Parameters
otherarray container

Definition at line 228 of file cpp/HostRecvArray.h.

Member Function Documentation

◆ operator=()

template<typename Data>
HostRecvArray< Data, CPT > & Pscf::HostRecvArray< Data, CPT >::operator= ( BackendArray< Data, CPT > const & other)

Create a read-only association with a BackendArray.

Create an association with the other backend array, thus creating a shallow copy that points to the memory owned by the backend array. This array may not be allocated on entry.

Exceptions
Exceptionif other array is not allocated
Exceptionif this is already associated with an array
Parameters
otherarray container on RHS of assigment (input)

Definition at line 240 of file cpp/HostRecvArray.h.

◆ dissociate()

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

Release association with a backend array.

Exceptions
Exceptionif no such association exists.

Definition at line 250 of file cpp/HostRecvArray.h.

References Util::ConstArray< Data >::capacity_, Util::ConstArray< Data >::data_, and UTIL_CHECK.


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