PSCF v1.5.0
Pscf::BackendArray< Data, CPT > Class Template Reference

Pseudo "device" array for use with C++ backend. More...

#include <BackendArray.h>

Inheritance diagram for Pscf::BackendArray< Data, CPT >:
Pscf::FftwDRArray< Data > Util::Array< Data > Util::ReferenceCounter

Public Types

using BackendIdClass = CPT
 Backend identifier class typename aliase.
Public Types inherited from Pscf::FftwDRArray< Data >
using ValueType = Data
 Data type of each array element.

Public Member Functions

 BackendArray (int capacity)
 Allocating constructor.
BackendArray< Data, CPT > & operator= (HostSendArray< Data, CPT > const &other)
 Pseudo-assignment from a host array.
Public Member Functions inherited from Pscf::FftwDRArray< Data >
 FftwDRArray ()
 Default constructor.
 FftwDRArray (int capacity)
 Allocating constructor.
 FftwDRArray (FftwDRArray< Data > const &other)
 Copy constructor.
virtual ~FftwDRArray ()
 Destructor.
FftwDRArray< Data > & operator= (FftwDRArray< Data > const &other)
 Assignment from another FftwDRArray<Data> container.
FftwDRArray< Data > & operator= (Array< Data > const &other)
 Assignment from an Array<Data> container.
void allocate (int capacity)
 Allocate an underlying C array, which this container then owns.
virtual void deallocate ()
 Dellocate an underlying C array that is owned by this container.
void associate (FftwDRArray< Data > &other, int beginId, int capacity)
 Associate this object with a slice of a different FftwDRArray.
void associate (FftwDRArray< Data > &other)
 Associate this object with all of a different FftwDRArray.
void dissociate ()
 Dissociate this object from an externally owned array slice.
template<class Archive>
void serialize (Archive &ar, const unsigned int version)
 Serialize an FftwDRArray to/from an Archive.
bool isOwner () const
 Does this container own a dynamically allocated C array?
bool isAssociated () const
 Is this container associated with a C array it does not own?
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.
Public Member Functions inherited from Util::ReferenceCounter
 ReferenceCounter ()
 Constructor.
int nRef () const
 Get the number of references to a shareable resource.
bool hasRefs () const
 Does this counter have references (is nRef > 0)?

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::BackendArray< Data, CPT >

Pseudo "device" array for use with C++ backend.

Derived from FftwDRArray, and largely equivalent.

The main difference from the base class is the addition of assignment (operator = ) from a HostSendArray<Data,CTP> to a BackendArray<Data,CTP>. This operator checks for the existence of an association between the backend array (LHS) and host array (RHS) in which both point to the same memory, throws an Exception if such an association does not already exist, or does nothing if it does. Reason for this usage is discussed below.

Host to device data transfer:

Usage for transfer of data from an instance of HostSendArray<Data,CPT> to an instance of BackendArray<Data,CPT> is discussed in the class documentation for the HostSendArray class template. The most explicit version of this pattern involves the following steps:

  • Construct and allocate a backend array (denoted by dArray)
  • Default construct a host array (denoted by hArray)
  • Create an association: hArray.associate(dArray)
  • Initialize elements of hArray
  • Assign from host to device: dArray = hArray;
  • Destroy the association: hArray.dissociate()
  • Deallocate or destroy the backend array dArray

Comments:

  • The operator v = u that assigns from host u to device v merely checks that an association already exists and does nothing if it does. This is because the association must exist before the data is initialized on the host array, which must occur before the assignment operator is invoked. In analogous GPU code, the assignment operator would instead actually transfer data from CPU to GPU memory.
  • The dissociate member function of the host array class destroys an association in CPU code (T=CPT) but does nothing in analogous GPU code (T=CUT).
  • The host array must never be used to modify data after the dissociate function is invoked. To enforce this, dissociate should usually be called immediately after the assignment operator.

Definition at line 82 of file cpp/BackendArray.h.

Member Typedef Documentation

◆ BackendIdClass

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

Backend identifier class typename aliase.

Definition at line 92 of file cpp/BackendArray.h.

Constructor & Destructor Documentation

◆ BackendArray()

template<typename Data>
Pscf::BackendArray< Data, CPT >::BackendArray ( int capacity)

Allocating constructor.

Parameters
capacitynumber of elements to allocate

Definition at line 152 of file cpp/BackendArray.h.

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

Member Function Documentation

◆ operator=()

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

Pseudo-assignment from a host array.

This function performs a sanity test and does nothing if the test passes. It checks that both LHS and RHS are allocated and both refer to the to the same underlying C array.

Rationale: Since the host array acts as a shallow copy of this backend array, the association must have been created before data was initialized on the host array, which must occurs before the pseudo host-to-device transfer is finalized by this operator.

Exceptions
Exceptionif this is not allocated
Exceptionif other array is not allocated
Exceptionif this and other do not point to the same memory
Parameters
otherarray container on RHS of assigment (input)

Definition at line 161 of file cpp/BackendArray.h.

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


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