1#ifndef PSCF_BACKEND_ARRAY_CU_H
2#define PSCF_BACKEND_ARRAY_CU_H
12#include <util/misc/ReferenceCounter.h>
13#include <util/misc/CountedReference.h>
18 template <
typename Data>
class Array;
102 template <
typename Data>
308 Data
const *
cArray()
const;
335 template <
typename Data>
inline
342 template <
typename Data>
inline
349 template <
typename Data>
inline
356 template <
typename Data>
inline
362#include "DeviceMemory.h"
363#include "cudaErrorCheck.h"
364#include <util/containers/Array.h>
366#include <cuda_runtime.h>
373 template <
typename Data>
382 template <
typename Data>
393 template <
typename Data>
398 if (!other.isAllocated()) {
399 UTIL_THROW(
"Other array must be allocated.");
403 cudaErrorCheck( cudaMemcpy(
dataPtr_, other.cArray(),
405 cudaMemcpyDeviceToDevice) );
413 template <
typename Data>
419 UTIL_THROW(
"Other array must be allocated.");
425 cudaMemcpyHostToDevice) );
431 template <
typename Data>
437 <<
"Error: Destroying BackendArray that is referred"
438 <<
"to by at least one CountedReference"
443 if (
ref_.isAssociated()) {
451 template <
typename Data>
455 UTIL_THROW(
"Attempt to allocate with capacity <= 0");
458 UTIL_THROW(
"Attempt to re-allocate an array");
460 if (
ref_.isAssociated()) {
461 UTIL_THROW(
"Attempt to allocate array that uses external data");
464 cudaErrorCheck(cudaMalloc((
void**) &
dataPtr_,
472 template <
typename Data>
478 cudaErrorCheck( cudaFree(
dataPtr_) );
486 template <
typename Data>
503 arr.addReference(
ref_);
509 template <
typename Data>
529 template <
typename Data>
547 template <
typename Data>
554 template <
typename Data>
559 if (
this == &other)
return *
this;
562 if (!other.isAllocated()) {
563 UTIL_THROW(
"Other BackendArray<Data,CUT> must be allocated.");
573 UTIL_THROW(
"Cannot assign arrays of unequal capacity");
577 cudaErrorCheck( cudaMemcpy(
dataPtr_, other.cArray(),
579 cudaMemcpyDeviceToDevice) );
587 template <
typename Data>
593 UTIL_THROW(
"RHS Array<Data> must be allocated.");
603 UTIL_THROW(
"Cannot assign arrays of unequal capacity");
609 cudaMemcpyHostToDevice) );
617 template <
typename Data>
627 template <
typename Data>
void associate(BackendArray< Data, CUT > &arr, int beginId, int capacity)
Associate this object with a slice of a different BackendArray.
void dissociate()
Dissociate this object from an externally owned memory block.
void allocate(int capacity)
Allocate the underlying C array on the device.
Data ValueType
Data type of each element.
bool isOwner() const
Does this container own an allocated memory block?
bool isAllocated() const
Return true if the array has allocated data, false otherwise.
void deallocate()
Dellocate the underlying C array.
ReferenceCounter refCounter_
Counter for any arrays that use data owned by this.
int capacity() const
Return array capacity.
void addReference(CountedReference &reference)
Associate a reference with reference counter for this container.
Data * dataPtr_
Pointer to a C array of Data elements on the GPU device.
CountedReference ref_
Reference to another array that owns memory used by this one.
BackendArray()
Default constructor.
int capacity_
Allocated size (capacity) of the array.
bool isAssociated() const
Is this container associated with a memory block it does not own?
CUT BackendIdClass
Backend identifier class type.
Data * cArray()
Return pointer to underlying C array.
Backend identifier class for the GPU-enabled CUDA backend.
Block of bare memory allocated on the GPU.
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.
Array container class template.
Data * cArray()
Return a pointer to the underlying C array (non-const data).
int capacity() const
Return allocated size.
bool isAllocated() const
Does this array have associated data?
Reference to a shared resource.
void associate(ReferenceCounter const &counter)
Create an association with a ReferenceCounter.
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.