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

Template for dynamic array stored in host CPU memory. More...

#include <HostSendArray.h>

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

Public Types

using ValueType = Data
 Data type of each element.
using BackendIdClass = CUT
 Backend identifier class type.

Public Member Functions

 HostSendArray (int capacity)
 Allocating constructor.
void associate (BackendArray< Data, CUT > &deviceArray)
 Allocate if not allocated previously.
HostSendArray< Data, CUT > & operator= (BackendArray< Data, CUT > const &other)
 Assignment operator, assign from a BackendArray<Data,CUT>.
void copySlice (BackendArray< Data, CUT > const &other, int beginId)
 Copy a slice of the data from a larger BackendArray into this array.
void dissociate ()
 Release host array.
Public Member Functions inherited from Util::DArray< Data >
 DArray ()
 Default constructor.
 DArray (int capacity)
 Allocating constructor.
 DArray (DArray< Data > const &other)
 Copy constructor.
virtual ~DArray ()
 Destructor.
DArray< Data > & operator= (DArray< Data > const &other)
 Assignment from a DArray<Data> container.
DArray< Data > & operator= (Array< Data > const &other)
 Assignment from an Array<Data> container.
void allocate (int capacity)
 Allocate the underlying C array.
void deallocate ()
 Deallocate the underlying C array.
void reallocate (int capacity)
 Reallocate the underlying C array and copy to new location.
template<class Archive>
void serialize (Archive &ar, const unsigned int version)
 Serialize a DArray to/from an Archive.
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, CUT >

Template for dynamic array stored in host CPU memory.

This class template should be used in device-independent template code in which an array is copied from host to device. The BackendArray<Data,CPT> template defines an assignment operator that assigns from a host array to a backend array.

The "associate" member function allocates the array if not allocated previously, or does nothing if the array is already allocated with the correct capacity.

The assignment (=) operator that copies a host array (RHS) to a backend array (LHS) performs a deep copy from host to device memory. This function is a member function of the backend array, defined by the BackendArray<Data,CUT> class template.

Usage :

Typical usage for backend-independent template code 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. Here, the alias 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 CUDA backend (T=CUT), the associate function allocates the host array, if not allocated previously, or does nothing if it is already allocated with the same capacity as the backend array. In the specialization for a C++ backend (T=CPT), the associate function creates an association that make the host array refer to memory owned by the device array.
  • In this specialization for a CUDA backend (T=CUT), the assignment (=) operator that assigns a RHS HostSendArray<Data,CPT> to a LHS BackendArray<Data,CPT> template copies all elements of an array from CPU host memory to GPU device memory. In the corresponding specialization for a C++ backend (T=CPT), the assignment operator does nothing.
  • In this specialization for a CUDA backend (T=CUT), the dissociate function does nothing. In the corresponding specialization for a C++ backend (T=CPT), this function destroys the association between the host and backend arrays, by nullifying a pointer held by the host array.
  • The host array may 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.
See also
Pscf::HostSendArray<Data,CPT>

Definition at line 93 of file cuda/HostSendArray.h.

Member Typedef Documentation

◆ ValueType

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

Data type of each element.

Definition at line 101 of file cuda/HostSendArray.h.

◆ BackendIdClass

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

Backend identifier class type.

Definition at line 106 of file cuda/HostSendArray.h.

Constructor & Destructor Documentation

◆ HostSendArray()

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

Allocating constructor.

Parameters
capacitynumber of elements to allocate

Definition at line 214 of file cuda/HostSendArray.h.

References Util::Array< Data >::capacity(), and Util::DArray< Data >::DArray().

Member Function Documentation

◆ associate()

template<typename Data>
void Pscf::HostSendArray< Data, CUT >::associate ( BackendArray< Data, CUT > & deviceArray)

Allocate if not allocated previously.

GPU specialization allocates host array with same dimensions as the backend array, unless this is already the case.

Parameters
deviceArraybackend array (must be allocated on entry)

Definition at line 274 of file cuda/HostSendArray.h.

References Util::DArray< Data >::allocate(), Util::Array< Data >::capacity(), Util::Array< Data >::isAllocated(), and UTIL_CHECK.

◆ operator=()

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

Assignment operator, assign from a BackendArray<Data,CUT>.

Performs a deep copy from a RHS BackendArray<Data,CUT> to this LHS HostSendArray<D>, by copying the underlying data from device memory to host memory.

Preconditions: The RHS BackendArray<Data,CUT> object must be allocated. If this LHS HostSendArray<D> is not allocated, the required memory will be allocated before values are copied. Otherwise, if this LHS array is allocated on entry, capacites for LHS and RHS objects must be equal.

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 223 of file cuda/HostSendArray.h.

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

◆ copySlice()

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

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

This method will populate this HostSendArray with data from a slice of a BackendArray. The size of the slice is the capacity of this HostSendArray (i.e., this entire array will be populated), and the position of the slice within the BackendArray is indicated by the 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 HostSendArray.

Parameters
otherBackendArray<Data,CUT> object from which to copy slice
beginIdindex of other array at which slice begins

Definition at line 252 of file cuda/HostSendArray.h.

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

◆ dissociate()

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

Release host array.

GPU specialization (T=CUT) does nothing. The CPU specialization (T=CPT) removes an association between the host array and some other array.

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


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