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

Dynamic array on the GPU device with aligned data. More...

#include <BackendArray.h>

Public Types

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

Public Member Functions

 BackendArray ()
 Default constructor.
 BackendArray (int capacity)
 Allocating constructor.
 BackendArray (BackendArray< Data, CUT > const &other)
 Copy constructor.
 BackendArray (Array< Data > const &other)
 Copy constructor, deep copy Array<Data> from host to device.
virtual ~BackendArray ()
 Destructor.
void allocate (int capacity)
 Allocate the underlying C array on the device.
void deallocate ()
 Dellocate the underlying C array.
void associate (BackendArray< Data, CUT > &arr, int beginId, int capacity)
 Associate this object with a slice of a different BackendArray.
void associate (DeviceMemory &arr, int capacity)
 Associate this object with a DeviceMemory container.
void dissociate ()
 Dissociate this object from an externally owned memory block.
void addReference (CountedReference &reference)
 Associate a reference with reference counter for this container.
virtual BackendArray< Data, CUT > & operator= (BackendArray< Data, CUT > const &other)
 Assignment operator, assign from other BackendArray<Data,CUT>.
virtual BackendArray< Data, CUT > & operator= (const Array< Data > &other)
 Assignment operator, assignment from Util::Array<Data> host array.
int capacity () const
 Return array capacity.
bool isAllocated () const
 Return true if the array has allocated data, false otherwise.
bool isOwner () const
 Does this container own an allocated memory block?
bool isAssociated () const
 Is this container associated with a memory block it does not own?
Data * cArray ()
 Return pointer to underlying C array.
Data const * cArray () const
 Return const pointer to underlying C array.

Protected Attributes

Data * dataPtr_
 Pointer to a C array of Data elements on the GPU device.
int capacity_
 Allocated size (capacity) of the array.
ReferenceCounter refCounter_
 Counter for any arrays that use data owned by this.
CountedReference ref_
 Reference to another array that owns memory used by this one.

Detailed Description

template<typename Data>
class Pscf::BackendArray< Data, CUT >

Dynamic array on the GPU device with aligned data.

This class wraps an aligned C array with elements of type Data that is allocated in GPU device global memory. All member functions may be called from the CPU host, but this class does not offer access to individual elements via the subscript operator, operator[].

A BackendArray can be in any of three states:

(1) Unallocated: In this state, there is no associated memory, so capacity() returns 0, while isAllocated(), isOwner() and isAssociated() all return false.

(2) A data owner: In this case, this object owns a block of memory that it is responsible for de-allocating. In this state, capacity() returns a positive integer value, isAllocated() and isOwner() return true, and isAssociated() returns false.

(3) A data user: In this case, this object has a pointer to a block of device memory that is owned by a different container. We describe this by saying this BackendArray (the data user) is "associated" with or "references" memory owned by another container (the data owner). In this state, capacity() returns a positive value, isAllocated() and isAssociated() return true, and isOwner() returns false.

Memory that is owned by a BackendArray may be allocated and deallocated by the allocate() and deallocate() member functions. Memory owned by a BackendArray is also dellocated when the BackendArray is destroyed.

A BackendArray<Data,CUT> object may be associated as a data user with:

Associations with either type of data owner can be created and destroyed by calling the associate and dissociate member functions of the data user. Attempts to create an association cause an Exception to be thrown if the requested slice of shared data would exceed the bounds of the block owned by the data owner.

A BackendArray<Data,CUT> that serves as an owner of data that is used by one or more other associated BackendArray<Data,CUT> objects maintains a count of how many associated objects refer to its data. This counter is incremented when an association is created by a data user and decremented when the user destroys the association.

When a BackendArray<Data,CUT> object is destroyed, any data that it owns is automatically deallocated, and any association with external data is destroyed.

It is an error to deallocate memory owned by a BackendArray or DeviceMemory container that is still referred to by one or more associated BackendArray data users. Doing so is illegal because it creates dangling pointers. Attempts to prematurely deallocate such a shared memory block either cause an Exception to be thrown, if it occurs within the deallocate function, or cause an error message to be written to std::cout if the error occurs in the BackendArray destructor function. All references to such a shared data block must thus be released, by calling the dissociate() member function of each data user, before the data owner can be safely destroyed.

Assignment from an Array<Data> copies all elements of an array from CPU to GPU memory.

Definition at line 103 of file cuda/BackendArray.h.

Member Typedef Documentation

◆ ValueType

template<typename Data>
typedef Data Pscf::BackendArray< Data, CUT >::ValueType

Data type of each element.

Definition at line 113 of file cuda/BackendArray.h.

◆ BackendIdClass

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

Backend identifier class type.

Definition at line 118 of file cuda/BackendArray.h.

Constructor & Destructor Documentation

◆ BackendArray() [1/4]

◆ BackendArray() [2/4]

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

Allocating constructor.

This function calls allocate(capacity) internally.

Parameters
capacitynumber of elements to allocate

Definition at line 383 of file cuda/BackendArray.h.

References allocate(), BackendArray(), capacity(), capacity_, and dataPtr_.

◆ BackendArray() [3/4]

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

Copy constructor.

Parameters
otherBackendArray<Data,CUT> to be copied (input)

Definition at line 394 of file cuda/BackendArray.h.

References allocate(), BackendArray(), capacity_, and dataPtr_.

◆ BackendArray() [4/4]

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

Copy constructor, deep copy Array<Data> from host to device.

Parameters
otherArray<Data> to be copied (input)

Definition at line 414 of file cuda/BackendArray.h.

References allocate(), BackendArray(), Util::Array< Data >::capacity(), capacity_, Util::Array< Data >::cArray(), dataPtr_, and Util::Array< Data >::isAllocated().

◆ ~BackendArray()

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

Destructor.

Deletes underlying C array, if allocated previously.

Definition at line 432 of file cuda/BackendArray.h.

References BackendArray(), dataPtr_, isOwner(), ref_, and refCounter_.

Member Function Documentation

◆ allocate()

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

Allocate the underlying C array on the device.

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

Definition at line 452 of file cuda/BackendArray.h.

References BackendArray(), capacity(), capacity_, dataPtr_, isAllocated(), ref_, and UTIL_THROW.

Referenced by BackendArray(), BackendArray(), BackendArray(), operator=(), and operator=().

◆ deallocate()

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

Dellocate the underlying C array.

Exceptions
Exceptionif the array is not allocated.
Exceptionif this object does not own the array.

Definition at line 473 of file cuda/BackendArray.h.

References BackendArray(), capacity_, dataPtr_, ref_, refCounter_, and UTIL_CHECK.

◆ associate() [1/2]

template<typename Data>
void Pscf::BackendArray< Data, CUT >::associate ( BackendArray< Data, CUT > & arr,
int beginId,
int capacity )

Associate this object with a slice of a different BackendArray.

Exceptions
Exceptionif the array is already allocated.
Parameters
arrparent array that owns the data
beginIdindex in the parent array at which this array starts
capacitynumber of elements associated with this container

Definition at line 487 of file cuda/BackendArray.h.

References BackendArray(), capacity(), capacity_, dataPtr_, ref_, and UTIL_CHECK.

◆ associate() [2/2]

template<typename Data>
void Pscf::BackendArray< Data, CUT >::associate ( DeviceMemory & arr,
int capacity )

Associate this object with a DeviceMemory container.

When a BackendArray<Data,CUT> is associated with a DeviceMemory container, the shared data block begins at the first byte of the block owned by the DeviceMemory. The number of byes in such a shared block is equal to capacity * sizeof(Data). This must be less than or equal to the number of byes in the block owned by the DeviceMemory, or this function will throw an Exception.

Exceptions
Exceptionif the array is already allocated
Exceptionif the requested memory block is too large
Parameters
arrDeviceMemory container that owns the data
capacitynumber of elements of type Data

Definition at line 510 of file cuda/BackendArray.h.

References Pscf::DeviceMemory::addReference(), BackendArray(), capacity(), Pscf::DeviceMemory::capacity(), capacity_, Pscf::DeviceMemory::cArray(), dataPtr_, Pscf::DeviceMemory::isAllocated(), ref_, and UTIL_CHECK.

◆ dissociate()

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

Dissociate this object from an externally owned memory block.

This function can destroy an association with either another BackendArray or with a DeviceMemory. After successful exit, isAllocated() and isAssociated() will both return false.

Exceptions
Exceptionif this is not associated with external memory

Definition at line 530 of file cuda/BackendArray.h.

References BackendArray(), capacity_, dataPtr_, ref_, and UTIL_CHECK.

◆ addReference()

template<typename Data>
void Pscf::BackendArray< Data, CUT >::addReference ( CountedReference & reference)

Associate a reference with reference counter for this container.

This function should normally only be called within the associate member function of a container that is referring to data owned by this array, as part of the process of creating an association. It should not be called by users in any other context.

On exit, the ReferenceCounter owned by this DeviceMemory is incremented by one and the CountedReference contains a pointer to the ReferenceCounter. This pointer can later be used as part of the process of destroying the association.

Parameters
referencereference to be included by reference counter

Definition at line 548 of file cuda/BackendArray.h.

References Util::CountedReference::associate(), BackendArray(), and refCounter_.

◆ operator=() [1/2]

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

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

Performs a deep copy, by copying values of all elements.

This function will allocate memory if this (LHS) array is not allocated. If this array is arleady allocated, it must have the same capacity as the other (RHS) BackendArray<Data,CUT>.

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

Definition at line 556 of file cuda/BackendArray.h.

References allocate(), BackendArray(), capacity_, dataPtr_, isAllocated(), and UTIL_THROW.

◆ operator=() [2/2]

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

Assignment operator, assignment from Util::Array<Data> host array.

Performs a deep copy from a RHS Array<Data> host array to this LHS BackendArray<Data,CUT>, by copying underlying C array from host memory to device memory.

If this (LHS) BackendArray<Data,CUT> is not allocated on entry, memory will be allocated before data is copied. If this LHS object is already allocated, it must have the same capacity as the RHS Array<Data>.

Exceptions
Exceptionif RHS other array is not allocated
Exceptionif RHS and LHS have unequal nonzero capacities
Parameters
otherArray<Data> on RHS of assignent (input)

Definition at line 589 of file cuda/BackendArray.h.

References allocate(), BackendArray(), Util::Array< Data >::capacity(), capacity_, Util::Array< Data >::cArray(), dataPtr_, isAllocated(), Util::Array< Data >::isAllocated(), and UTIL_THROW.

◆ capacity()

template<typename Data>
int Pscf::BackendArray< Data, CUT >::capacity ( ) const
inline

Return array capacity.

Returns
number of elements in array

Definition at line 336 of file cuda/BackendArray.h.

References BackendArray(), and capacity_.

Referenced by allocate(), associate(), associate(), and BackendArray().

◆ isAllocated()

template<typename Data>
bool Pscf::BackendArray< Data, CUT >::isAllocated ( ) const
inline

Return true if the array has allocated data, false otherwise.

A BackendArray is considered allocated if it has non-null pointer to an data block, which may either be a block that it owns or a block owned by another container.

Definition at line 343 of file cuda/BackendArray.h.

References BackendArray(), and dataPtr_.

Referenced by allocate(), operator=(), and operator=().

◆ isOwner()

template<typename Data>
bool Pscf::BackendArray< Data, CUT >::isOwner ( ) const
inline

Does this container own an allocated memory block?

If isAllocated() is false, isOwner() must be false.

Definition at line 350 of file cuda/BackendArray.h.

References BackendArray(), dataPtr_, and ref_.

Referenced by ~BackendArray().

◆ isAssociated()

template<typename Data>
bool Pscf::BackendArray< Data, CUT >::isAssociated ( ) const
inline

Is this container associated with a memory block it does not own?

If isAllocated() is false, isAssociated() must be false.

Definition at line 357 of file cuda/BackendArray.h.

References BackendArray(), dataPtr_, and ref_.

◆ cArray() [1/2]

template<typename Data>
Data * Pscf::BackendArray< Data, CUT >::cArray ( )

Return pointer to underlying C array.

Definition at line 618 of file cuda/BackendArray.h.

References BackendArray(), dataPtr_, and UTIL_CHECK.

◆ cArray() [2/2]

template<typename Data>
const Data * Pscf::BackendArray< Data, CUT >::cArray ( ) const

Return const pointer to underlying C array.

Definition at line 628 of file cuda/BackendArray.h.

References BackendArray(), dataPtr_, and UTIL_CHECK.

Member Data Documentation

◆ dataPtr_

template<typename Data>
Data* Pscf::BackendArray< Data, CUT >::dataPtr_
protected

◆ capacity_

template<typename Data>
int Pscf::BackendArray< Data, CUT >::capacity_
protected

Allocated size (capacity) of the array.

Definition at line 316 of file cuda/BackendArray.h.

Referenced by allocate(), associate(), associate(), BackendArray(), BackendArray(), BackendArray(), BackendArray(), capacity(), deallocate(), dissociate(), operator=(), and operator=().

◆ refCounter_

template<typename Data>
ReferenceCounter Pscf::BackendArray< Data, CUT >::refCounter_
protected

Counter for any arrays that use data owned by this.

Definition at line 319 of file cuda/BackendArray.h.

Referenced by addReference(), deallocate(), and ~BackendArray().

◆ ref_

template<typename Data>
CountedReference Pscf::BackendArray< Data, CUT >::ref_
protected

Reference to another array that owns memory used by this one.

Definition at line 322 of file cuda/BackendArray.h.

Referenced by allocate(), associate(), associate(), deallocate(), dissociate(), isAssociated(), isOwner(), and ~BackendArray().


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