PSCF v1.5.0
Pscf::HostRecvArray< Data, CUT > Class Template Reference

Host array to receive host-to-device data transfers. More...

#include <HostRecvArray.h>

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

Public Types

using ValueType = Data
 Alias for data type of each array element.
using BackendIdClass = CUT
 Alias for backend identifier class.

Public Member Functions

 HostRecvArray ()=default
 Default constructor.
 HostRecvArray (int capacity)
 Allocating constructor.
 HostRecvArray (BackendArray< Data, CUT > const &other)
 Conversion constructor (copies from device to host).
virtual ~HostRecvArray ()
 Destructor.
HostRecvArray< Data, CUT > & operator= (Array< Data > const &other)
 Assignment from an Array<Data> container.
HostRecvArray< Data, CUT > & operator= (BackendArray< Data, CUT > const &other)
 Assignment from a BackendArray<Data,CUT>.
void copySlice (BackendArray< Data, CUT > const &other, int beginId)
 Copy a slice of data from a larger BackendArray into this array.
void allocate (int capacity)
 Allocate the underlying C array.
void deallocate ()
 Deallocate the underlying C array.
void dissociate ()
 Release this array after finishing all reading.
template<class Archive>
void serialize (Archive &ar, const unsigned int version)
 Serialize a HostRecvArray to/from an Archive.
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, CUT >

Host array to receive host-to-device data transfers.

This class template should be used in backend-independent template code in which data is copied from a GPU backend array to a CPU host array. This template is derived from ConstArray<Data>, and thus provides read-only access to the data.

Conversion construction or assignment (operator =) from an instance of BackendArray<Data,CUT> to a HostRecvArray<Data,CUT> allocates the array if it is not allocated and then copies all elements of the array from global GPU device memory to host memory. Conversion construction from a backend array is equivalent to default construction followed by assignment.

Usage:

Typical usage is shown below for backend-indepent template code for device-to-host transfer from a longer lived instance of BackendArray<Data,CUT> named deviceArray to a local object that is an instance of HostRecvArray<Data,CUT> named hostArray. The type type of each array element is denoted by Data.

hostArray = deviceArray
// Read data from the hostArray
hostArray.dissociate();
HostRecvArray()=default
Default constructor.

If the hostArray is a local object that is defined with the function in which it is used, the default constructor and assignment operations may instead be combined into a single call of the conversion constructor, giving the shorter version

HostRecvArray<Data,CUT> hostArray(deviceArray)
// Read data from the hostArray
hostArray.dissociate();

In this case, the call of the dissociate function may also be omitted, since an equivalent operation will be performed by the destructor when the function ends and the hostArray goes out of scope.

Comments:

  • In this specialization for a CUDA backend (T=CUT), the assignment operator and conversion constructor each perform a deep copy of data from GPU device memory to CPU host memory.
  • This class template provides read-only access to data that is copied from the GPU device. If the copied data must be modified, it must first be copied to a CPU container that allows this.
  • In this specialization for a CUDA backend (T=CUT), the dissociate function does nothing. It is used in backend-independent code to maintain compatibility with the syntax used by the specialization for a C++ backend (T=CPT), for which the dissociate function releases an association with the backend array.
  • Any call to the dissociate function must occur after all statements that read data from the host array.

Definition at line 95 of file cuda/HostRecvArray.h.

Member Typedef Documentation

◆ ValueType

template<typename Data>
using Pscf::HostRecvArray< Data, CUT >::ValueType = Data

Alias for data type of each array element.

Definition at line 105 of file cuda/HostRecvArray.h.

◆ BackendIdClass

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

Alias for backend identifier class.

Definition at line 110 of file cuda/HostRecvArray.h.

Constructor & Destructor Documentation

◆ HostRecvArray() [1/3]

◆ HostRecvArray() [2/3]

template<typename Data>
Pscf::HostRecvArray< Data, CUT >::HostRecvArray ( int capacity)

Allocating constructor.

Parameters
capacitynumber of elements to allocate

Definition at line 291 of file cuda/HostRecvArray.h.

References allocate(), Util::ConstArray< Data >::capacity(), and HostRecvArray().

◆ HostRecvArray() [3/3]

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

Conversion constructor (copies from device to host).

Conversion construction is equivalent to default construction followed by assignment. This function first allocates this array with a capacity equal to that of the other backend array, and then performs a deep copy of the underlying array from GPU device memory to CPU host memory.

Exceptions
Exceptionif the RHS array is not allocated on entry
Parameters
otherBackendArray<Data,CUT> to be copied (input)

Definition at line 301 of file cuda/HostRecvArray.h.

References HostRecvArray().

◆ ~HostRecvArray()

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

Destructor.

Deletes underlying C array, if allocated previously.

Definition at line 310 of file cuda/HostRecvArray.h.

References Util::ConstArray< Data >::capacity_, Util::ConstArray< Data >::data_, Util::Memory::deallocate(), HostRecvArray(), and Util::ConstArray< Data >::isAllocated().

Member Function Documentation

◆ operator=() [1/2]

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

Assignment from an Array<Data> container.

Performs a deep copy, by copying values of all elements of an Array<Data> container. If this (LHS) array is already allocated on entry, it must have the same capacity as the other (RHS) array. If this LHS array is not allocated on entry, required memory is allocated before copying values.

Exceptions
Exceptionif other array is not allocated
Exceptionif arrays are allocated with unequal capacities
Parameters
otherarray container on RHS of assigment (input)

Definition at line 328 of file cuda/HostRecvArray.h.

References allocate(), Util::Array< Data >::capacity(), Util::ConstArray< Data >::capacity_, Util::ConstArray< Data >::data_, HostRecvArray(), Util::Array< Data >::isAllocated(), Util::ConstArray< Data >::isAllocated(), and UTIL_CHECK.

◆ operator=() [2/2]

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

Assignment from a BackendArray<Data,CUT>.

Performs a deep copy from a RHS BackendArray<Data,CUT> to this LHS HostRecvArray<D>, by first allocating if necessary, and then copying the underlying data from device memory to host memory.

Exceptions
Exceptionif the RHS array is not allocated on entry
Exceptionif LHS and RHS have unequal nonzero capacities
Parameters
otherBackendArray<Data,CUT> on RHS of assignment (input)

Definition at line 360 of file cuda/HostRecvArray.h.

References allocate(), Util::ConstArray< Data >::capacity(), Util::ConstArray< Data >::data_, HostRecvArray(), Util::ConstArray< Data >::isAllocated(), and UTIL_CHECK.

◆ copySlice()

template<typename Data>
void Pscf::HostRecvArray< Data, CUT >::copySlice ( BackendArray< Data, CUT > const & other,
int beginId )

Copy a slice of data from a larger BackendArray into this array.

This function populate this HostRecvArray with data from a slice of a BackendArray. The size of the slice is equal to the capacity of this LHS HostRecvArray, so that this entire array will be fully populated. The position of the beginning of the slice within the RHS BackendArray is indicated by input parameter beginId. The capacity of the RHS BackendArray must thus be greater than or equal to the sum of beginId and the capacity of this LHS HostRecvArray.

Exceptions
Exceptionif RHS array is not allocated
Exceptionif LHS array is not allocated
Exceptionif beginId is negative
Exceptionif the slice extends beyond the end of the RHS
Parameters
otherBackendArray<Data,CUT> object from which to copy slice
beginIdindex of other array at which slice begins

Definition at line 391 of file cuda/HostRecvArray.h.

References Util::ConstArray< Data >::capacity(), Util::ConstArray< Data >::data_, HostRecvArray(), Util::ConstArray< Data >::isAllocated(), and UTIL_CHECK.

Referenced by HostRecvArray().

◆ allocate()

template<typename Data>
void Pscf::HostRecvArray< Data, CUT >::allocate ( int capacity)

Allocate the underlying C array.

Exceptions
Exceptionif the HostRecvArray is already allocated
Parameters
capacitynumber of elements to allocate

Definition at line 415 of file cuda/HostRecvArray.h.

References Util::Memory::allocate(), Util::ConstArray< Data >::capacity(), Util::ConstArray< Data >::capacity_, Util::ConstArray< Data >::data_, HostRecvArray(), Util::ConstArray< Data >::isAllocated(), and UTIL_THROW.

Referenced by HostRecvArray(), HostRecvArray(), operator=(), operator=(), and serialize().

◆ deallocate()

template<typename Data>
void Pscf::HostRecvArray< Data, CUT >::deallocate ( )

Deallocate the underlying C array.

Exceptions
Exceptionif the HostRecvArray is not allocated

Definition at line 431 of file cuda/HostRecvArray.h.

References Util::ConstArray< Data >::capacity_, Util::ConstArray< Data >::data_, Util::Memory::deallocate(), HostRecvArray(), Util::ConstArray< Data >::isAllocated(), and UTIL_THROW.

Referenced by HostRecvArray().

◆ dissociate()

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

Release this array after finishing all reading.

In template code that must work with either CPU or GPU, this should be called after all code that requires read access to this array. In this GPU specialization (T=CUT), this function does nothing.

Definition at line 245 of file cuda/HostRecvArray.h.

◆ serialize()

template<typename Data>
template<class Archive>
void Pscf::HostRecvArray< Data, CUT >::serialize ( Archive & ar,
const unsigned int version )

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