PSCF v1.5.0
cuda/BackendArray.h
1#ifndef PSCF_BACKEND_ARRAY_CU_H
2#define PSCF_BACKEND_ARRAY_CU_H
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "CUT.h" // template argument
12#include <util/misc/ReferenceCounter.h> // member
13#include <util/misc/CountedReference.h> // member
14
15
16// Forward declarations
17namespace Util {
18 template <typename Data> class Array;
19}
20namespace Pscf {
21 class DeviceMemory;
22}
23
24namespace Pscf {
25
26 using namespace Util;
27
28 // Declare primary template
29 template <typename Data, typename T> class BackendArray;
30
102 template <typename Data>
103 class BackendArray<Data,CUT>
104 {
105
106 public:
107
108 // Public typename aliases
109
113 typedef Data ValueType;
114
119
120 // Public member functions
121
125 BackendArray();
126
135
142
148 BackendArray(Array<Data> const & other);
149
155 virtual ~BackendArray();
156
164 void allocate(int capacity);
165
172 void deallocate();
173
184 int beginId, int capacity);
185
202 void associate(DeviceMemory& arr, int capacity);
203
213 void dissociate();
214
230 void addReference(CountedReference& reference);
231
246 virtual
248 operator = (BackendArray<Data,CUT> const& other);
249
267 virtual
268 BackendArray<Data,CUT>& operator = (const Array<Data>& other);
269
275 int capacity() const;
276
284 bool isAllocated() const;
285
291 bool isOwner() const;
292
298 bool isAssociated() const;
299
303 Data* cArray();
304
308 Data const * cArray() const;
309
310 protected:
311
313 Data* dataPtr_;
314
317
320
323
324 };
325
326 // Explicit instantiation declarations
327 extern template class BackendArray<cudaReal,CUT>;
328 extern template class BackendArray<cudaComplex,CUT>;
329
330 // Inline member function definitions
331
332 /*
333 * Return array capacity.
334 */
335 template <typename Data> inline
337 { return capacity_; }
338
339 /*
340 * Return true if this object has access to a memory block.
341 */
342 template <typename Data> inline
344 { return (bool) dataPtr_; }
345
346 /*
347 * Does this object own data?
348 */
349 template <typename Data> inline
351 { return ((bool) dataPtr_ && !ref_.isAssociated()); }
352
353 /*
354 * Is this object associated with data it does not own?
355 */
356 template <typename Data> inline
358 { return ((bool) dataPtr_ && ref_.isAssociated()); }
359
360}
361
362#include "DeviceMemory.h"
363#include "cudaErrorCheck.h"
364#include <util/containers/Array.h>
365#include <util/global.h>
366#include <cuda_runtime.h>
367
368namespace Pscf {
369
370 /*
371 * Default constructor.
372 */
373 template <typename Data>
378
379 /*
380 * Allocating constructor.
381 */
382 template <typename Data>
387
388 /*
389 * Copy constructor.
390 *
391 * Allocates new memory and copies all elements by value.
392 */
393 template <typename Data>
395 : dataPtr_(nullptr),
396 capacity_(0)
397 {
398 if (!other.isAllocated()) {
399 UTIL_THROW("Other array must be allocated.");
400 }
401
402 allocate(other.capacity_);
403 cudaErrorCheck( cudaMemcpy(dataPtr_, other.cArray(),
404 capacity_ * sizeof(Data),
405 cudaMemcpyDeviceToDevice) );
406 }
407
408 /*
409 * Copy constructor, deep copy from host Array<Data>
410 *
411 * Allocates new memory and copies all elements by value.
412 */
413 template <typename Data>
415 : dataPtr_(nullptr),
416 capacity_(0)
417 {
418 if (!other.isAllocated()) {
419 UTIL_THROW("Other array must be allocated.");
420 }
421
422 allocate(other.capacity());
423 cudaErrorCheck( cudaMemcpy(dataPtr_, other.cArray(),
424 capacity_ * sizeof(Data),
425 cudaMemcpyHostToDevice) );
426 }
427
428 /*
429 * Destructor.
430 */
431 template <typename Data>
433 {
434 if (isOwner()) {
435 if (refCounter_.hasRefs()) {
436 std::cout
437 << "Error: Destroying BackendArray that is referred"
438 << "to by at least one CountedReference"
439 << std::endl;
440 }
441 cudaFree(dataPtr_);
442 }
443 if (ref_.isAssociated()) {
444 ref_.dissociate();
445 }
446 }
447
448 /*
449 * Allocate device memory, with this object then owns.
450 */
451 template <typename Data>
453 {
454 if (capacity <= 0) {
455 UTIL_THROW("Attempt to allocate with capacity <= 0");
456 }
457 if (isAllocated()) {
458 UTIL_THROW("Attempt to re-allocate an array");
459 }
460 if (ref_.isAssociated()) {
461 UTIL_THROW("Attempt to allocate array that uses external data");
462 }
463
464 cudaErrorCheck(cudaMalloc((void**) &dataPtr_,
465 capacity * sizeof(Data)));
467 }
468
469 /*
470 * Deallocate device memory owned by this object, if any.
471 */
472 template <typename Data>
474 {
476 UTIL_CHECK(!ref_.isAssociated());
477 UTIL_CHECK(!refCounter_.hasRefs());
478 cudaErrorCheck( cudaFree(dataPtr_) );
479 dataPtr_ = nullptr;
480 capacity_ = 0;
481 }
482
483 /*
484 * Associate this object with memory owned by a different BackendArray.
485 */
486 template <typename Data>
488 int beginId, int capacity)
489 {
490 UTIL_CHECK(arr.isAllocated());
491 UTIL_CHECK(arr.isOwner());
492 UTIL_CHECK(beginId >= 0);
493 UTIL_CHECK(capacity > 0);
494 UTIL_CHECK(beginId + capacity <= arr.capacity());
496 UTIL_CHECK(!ref_.isAssociated());
497
498 // Copy data pointer and capacity
499 dataPtr_ = arr.cArray() + beginId;
501
502 // Associate ref_ member with reference counter of data owner
503 arr.addReference(ref_);
504 }
505
506 /*
507 * Associate this object with memory owned by a DeviceMemory object.
508 */
509 template <typename Data>
511 {
512 UTIL_CHECK(arr.isAllocated());
513 UTIL_CHECK(capacity > 0);
514 UTIL_CHECK(capacity * sizeof(Data) <= arr.capacity());
516 UTIL_CHECK(!ref_.isAssociated());
517
518 // Copy data pointer and capacity
519 dataPtr_ = (Data *) arr.cArray();
521
522 // Associate ref_ member with reference counter of data owner
523 arr.addReference(ref_);
524 }
525
526 /*
527 * Dissociate this object from external device memory
528 */
529 template <typename Data>
531 {
533 UTIL_CHECK(ref_.isAssociated());
534
535 dataPtr_ = nullptr; // reset to null
536 capacity_ = 0;
537 ref_.dissociate();
538 }
539
540 /*
541 * Associate an external CountedReference with the ReferenceCounter.
542 *
543 * This function should called to create an association in which
544 * this object is the data owner. The CountedReference parameter
545 * should be owned by the data user.
546 */
547 template <typename Data>
550
551 /*
552 * Assignment from another BackendArray<Data,CUT>.
553 */
554 template <typename Data>
557 {
558 // Check for self assignment
559 if (this == &other) return *this;
560
561 // Precondition - RHS BackendArray<Data,CUT> must be allocated
562 if (!other.isAllocated()) {
563 UTIL_THROW("Other BackendArray<Data,CUT> must be allocated.");
564 }
565
566 // If this is not allocated, then allocate
567 if (!isAllocated()) {
568 allocate(other.capacity());
569 }
570
571 // Require equal capacity values
572 if (capacity_ != other.capacity_) {
573 UTIL_THROW("Cannot assign arrays of unequal capacity");
574 }
575
576 // Copy elements
577 cudaErrorCheck( cudaMemcpy(dataPtr_, other.cArray(),
578 capacity_ * sizeof(Data),
579 cudaMemcpyDeviceToDevice) );
580
581 return *this;
582 }
583
584 /*
585 * Assignment from RHS Util::Array<Data>.
586 */
587 template <typename Data>
590 {
591 // Precondition
592 if (!other.isAllocated()) {
593 UTIL_THROW("RHS Array<Data> must be allocated.");
594 }
595
596 // Allocate this if necessary
597 if (!isAllocated()) {
598 allocate(other.capacity());
599 }
600
601 // Require equal capacity values
602 if (capacity_ != other.capacity()) {
603 UTIL_THROW("Cannot assign arrays of unequal capacity");
604 }
605
606 // Copy elements
607 cudaErrorCheck( cudaMemcpy(dataPtr_, other.cArray(),
608 capacity_ * sizeof(Data),
609 cudaMemcpyHostToDevice) );
610
611 return *this;
612 }
613
614 /*
615 * Get a pointer to the underlying C array.
616 */
617 template <typename Data>
619 {
621 return dataPtr_;
622 }
623
624 /*
625 * Get a pointer to const to the underlying C array.
626 */
627 template <typename Data>
629 {
631 return dataPtr_;
632 }
633
634} // namespace Pscf
635#endif
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.
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.
Definition CUT.h:31
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.
Definition Array.h:41
Data * cArray()
Return a pointer to the underlying C array (non-const data).
Definition Array.h:222
int capacity() const
Return allocated size.
Definition Array.h:250
bool isAllocated() const
Does this array have associated data?
Definition Array.h:236
Reference to a shared resource.
void associate(ReferenceCounter const &counter)
Create an association with a ReferenceCounter.
Reference counter.
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
PSCF package top-level namespace.
Utility classes for scientific computation.