1#ifndef PSCF_DEVICE_ARRAY_H
2#define PSCF_DEVICE_ARRAY_H
11#include <util/misc/ReferenceCounter.h>
12#include <util/misc/CountedReference.h>
16 template <
typename Data>
class DArray;
94 template <
typename Data>
307 template <
typename Data>
inline
314 template <
typename Data>
inline
321 template <
typename Data>
inline
328 template <
typename Data>
inline
334#include "DeviceMemory.h"
335#include "cudaErrorCheck.h"
336#include <util/containers/DArray.h>
338#include <cuda_runtime.h>
345 template <
typename Data>
354 template <
typename Data>
365 template <
typename Data>
371 UTIL_THROW(
"Other array must be allocated.");
377 cudaMemcpyDeviceToDevice) );
385 template <
typename Data>
391 UTIL_THROW(
"Other array must be allocated.");
397 cudaMemcpyHostToDevice) );
403 template <
typename Data>
409 <<
"Error: Destroying DeviceArray that is referred"
410 <<
"to by at least one CountedReference"
415 if (
ref_.isAssociated()) {
423 template <
typename Data>
427 UTIL_THROW(
"Attempt to allocate with capacity <= 0");
430 UTIL_THROW(
"Attempt to re-allocate an array");
432 if (
ref_.isAssociated()) {
433 UTIL_THROW(
"Attempt to allocate array that uses external data");
436 cudaErrorCheck(cudaMalloc((
void**) &
dataPtr_,
444 template <
typename Data>
450 cudaErrorCheck( cudaFree(
dataPtr_) );
458 template <
typename Data>
481 template <
typename Data>
501 template <
typename Data>
519 template <
typename Data>
526 template <
typename Data>
531 if (
this == &other)
return *
this;
535 UTIL_THROW(
"Other DeviceArray<Data> must be allocated.");
545 UTIL_THROW(
"Cannot assign arrays of unequal capacity");
551 cudaMemcpyDeviceToDevice) );
559 template <
typename Data>
565 UTIL_THROW(
"RHS DArray<Data> must be allocated.");
575 UTIL_THROW(
"Cannot assign arrays of unequal capacity");
581 cudaMemcpyHostToDevice) );
589 template <
typename Data>
599 template <
typename Data>
Dynamic array on the GPU device with aligned data.
bool isAssociated() const
Is this container associated with a memory block it does not own?
DeviceArray()
Default constructor.
bool isOwner() const
Does this container own an allocated memory block?
void addReference(CountedReference &reference)
Associate a reference with reference counter for this container.
void associate(DeviceMemory &arr, int capacity)
Associate this object with a DeviceMemory container.
void deallocate()
Dellocate the underlying C array.
ReferenceCounter refCounter_
DeviceArray(int capacity)
Allocating constructor.
void dissociate()
Dissociate this object from an externally owned memory block.
virtual ~DeviceArray()
Destructor.
virtual DeviceArray< Data > & operator=(const DeviceArray< Data > &other)
Assignment operator, assign from another DeviceArray<Data> array.
bool isAllocated() const
Return true if the array has allocated data, false otherwise.
DeviceArray(DArray< Data > const &other)
Copy constructor, deep copy DArray<Data> from host to device.
void associate(DeviceArray< Data > &arr, int beginId, int capacity)
Associate this object with a slice of a different DeviceArray.
Pscf::cudaReal * dataPtr_
DeviceArray(DeviceArray< Data > const &other)
Copy constructor.
void allocate(int capacity)
Allocate the underlying C array on the device.
Data const * cArray() const
Return const pointer to underlying C array.
Data * cArray()
Return pointer to underlying C array.
Data ValueType
Data type of each element.
Block of bare memory allocated on the GPU device.
void addReference(CountedReference &reference)
Associate a reference with the reference counter.
bool isAllocated() const
Return true if the array has been allocated, false otherwise.
void * cArray() const
Return pointer to underlying C array.
int capacity() const
Return allocated capacity.
Data * cArray()
Return a pointer to the underlying C array.
int capacity() const
Return allocated size.
Reference to a shared resource.
void associate(ReferenceCounter &counter)
Create an association with a ReferenceCounter.
Dynamically allocatable contiguous array template.
bool isAllocated() const
Return true if this DArray has been allocated, false otherwise.
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
PSCF package top-level namespace.
Utility classes for scientific computation.