PSCF v1.5.0
cuda/HostRecvArray.h
1#ifndef PSCF_HOST_RECV_ARRAY_CU_H
2#define PSCF_HOST_RECV_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 <pscf/backend/cuda/CUT.h> // template argument
12#include <util/containers/ConstArray.h> // base class template
13
14// Forward declarations
15namespace Util {
16 template <typename Data> class Array;
17}
18namespace Pscf {
19 template <typename Data, typename T> class HostRecvArray;
20 template <typename Data, typename T> class BackendArray;
21}
22
23namespace Pscf {
24
25 using namespace Util;
26
94 template <typename Data>
95 class HostRecvArray<Data,CUT> : public ConstArray<Data>
96 {
97
98 public:
99
100 // Public types
101
105 using ValueType = Data;
106
111
112 // Public member functions
113
117 HostRecvArray() = default;
118
125
126 // Default copy constructor (delete).
127 HostRecvArray(HostRecvArray<Data,CUT> const &) = delete;
128
143
149 virtual ~HostRecvArray();
150
151 // Default assignment (delete)
153 operator = (HostRecvArray<Data,CUT> const &) = delete;
154
169 HostRecvArray<Data,CUT>& operator = (Array<Data> const & other);
170
184 operator = (BackendArray<Data,CUT> const & other);
185
205 void copySlice(BackendArray<Data,CUT> const & other, int beginId);
206
214 void allocate(int capacity);
215
221 void deallocate();
222
223 #if 0
235 void reallocate(int capacity);
236 #endif
237
246 {}
247
254 template <class Archive>
255 void serialize(Archive& ar, const unsigned int version);
256
257 // Inherited public member functions (selected)
258 using ConstArray<Data>::operator [];
259 using ConstArray<Data>::cArray;
260 using ConstArray<Data>::isAllocated;
261 using ConstArray<Data>::capacity;
262 using ConstArray<Data>::size;
263
264 private:
265
266 // Hide inherited protected member variables
267 using ConstArray<Data>::data_;
268 using ConstArray<Data>::capacity_;
269
270 };
271
272 // Explicit instantiation declarations
273 extern template class HostRecvArray<cudaReal,CUT>;
274 extern template class HostRecvArray<cudaComplex,CUT>;
275
276} // namespace Pscf
277
278#include "BackendArray.h"
279#include "cudaErrorCheck.h"
280#include <util/containers/Array.h>
281#include <util/misc/Memory.h>
282#include <util/global.h>
283#include <cuda_runtime.h>
284
285namespace Pscf {
286
287 /*
288 * Allocating constructor.
289 */
290 template <typename Data>
292 : ConstArray<Data>()
293 { allocate(capacity); }
294
295 /*
296 * Conversion constructor from BackendArray<Data,CUT>.
297 *
298 * Allocate and perform a deep copy from device to host.
299 */
300 template <typename Data>
302 BackendArray<Data,CUT> const& other)
303 : ConstArray<Data>()
304 { (*this) = other; }
305
306 /*
307 * Destructor.
308 */
309 template <typename Data>
311 {
312 if (isAllocated()) {
313 try {
315 } catch (...) {
316 data_ = nullptr;
317 std::cout << "Exception in HostRecvArray destructor";
318 }
319 capacity_ = 0;
320 }
321 }
322
323 /*
324 * Assignment from an Array<Data> (deep copy on host).
325 */
326 template <typename Data>
329 {
330 // Check for pseudo self assignment
331 if (dynamic_cast< Array<Data>* >(this) == &other) return *this;
332
333 // Preconditions - require that other (RHS) array is allocated
334 UTIL_CHECK(other.isAllocated());
335 UTIL_CHECK(other.capacity() > 0);
336
337 // If this LHS array is not allocated, then allocate
338 if (!isAllocated()) {
339 allocate(other.capacity());
340 }
341
342 // Require equal capacities
343 UTIL_CHECK (capacity_ == other.capacity());
344
345 // Copy elements
346 for (int i = 0; i < capacity_; ++i) {
347 data_[i] = other[i];
348 }
349
350 return *this;
351 }
352
353 /*
354 * Assignment from a BackendArray<Data,CUT> (device-to-host copy).
355 *
356 * Allocate if necessary, and perform a deep copy from device to host.
357 */
358 template <typename Data>
361 BackendArray<Data,CUT> const & other)
362 {
363 // Preconditions on other array
364 UTIL_CHECK(other.isAllocated());
365 const int n = other.capacity();
366 UTIL_CHECK(n > 0);
367
368 // Allocate this if not allocated previously.
369 if (!isAllocated()) {
370 allocate(n);
371 }
372 UTIL_CHECK(capacity() == n);
373 // If this was allocated with capacity() == n, do nothing.
374 // If this was allocated with capacity() != n, throw Exception.
375
376 // Copy all elements
377 cudaErrorCheck(
378 cudaMemcpy(ConstArray<Data>::data_,
379 other.cArray(),
380 capacity() * sizeof(Data),
381 cudaMemcpyDeviceToHost)
382 );
383
384 return *this;
385 }
386
387 /*
388 * Copy a slice of the data from a larger BackendArray into this array.
389 */
390 template <typename Data>
392 BackendArray<Data,CUT> const & other,
393 int beginId)
394 {
395 // Preconditions
396 UTIL_CHECK (other.isAllocated());
398 UTIL_CHECK(beginId >= 0);
399 UTIL_CHECK(capacity() + beginId <= other.capacity());
400
401 // Copy all elements
402 cudaErrorCheck(
403 cudaMemcpy(ConstArray<Data>::data_,
404 other.cArray() + beginId,
405 capacity() * sizeof(Data),
406 cudaMemcpyDeviceToHost)
407 );
408
409 }
410
411 /*
412 * Allocate the underlying C array.
413 */
414 template <typename Data>
416 {
417 if (capacity <= 0) {
418 UTIL_THROW("Attempt to allocate with capacity <= 0");
419 }
420 if (isAllocated()) {
421 UTIL_THROW("Attempt to re-allocate a HostRecvArray");
422 }
425 }
426
427 /*
428 * Deallocate the underlying C array.
429 */
430 template <typename Data>
432 {
433 if (!isAllocated()) {
434 UTIL_THROW("Array is not allocated");
435 }
437 capacity_ = 0;
438 }
439
440 #if 0
441 /*
442 * Reallocate the underlying C array, if necessary.
443 */
444 template <typename Data>
445 void HostRecvArray<Data,CUT>::reallocate(int capacity)
446 {
447 UTIL_CHECK(capacity >= 0);
448 if (capacity == capacity_) return;
449
450 UTIL_CHECK(capacity > capacity_);
451 if (isAllocated()) {
452 Memory::reallocate<Data>(data_, capacity_, capacity);
453 } else {
454 Memory::allocate<Data>(data_, capacity);
455 }
456 capacity_ = capacity;
457 }
458 #endif
459
460 /*
461 * Serialize a HostRecvArray to/from an Archive.
462 */
463 template <typename Data>
464 template <class Archive>
466 const unsigned int version)
467 {
468 int capacity;
469 if (Archive::is_saving()) {
471 }
472 ar & capacity;
473 if (Archive::is_loading()) {
474 if (!isAllocated()) {
475 if (capacity > 0) {
477 }
478 } else {
479 if (capacity != capacity_) {
480 UTIL_THROW("Inconsistent HostRecvArray capacities");
481 }
482 }
483 }
484 if (isAllocated()) {
485 for (int i = 0; i < capacity_; ++i) {
486 ar & data_[i];
487 }
488 }
489 }
490
491}
492#endif
Backend identifier class for the GPU-enabled CUDA backend.
Definition CUT.h:31
HostRecvArray()=default
Default constructor.
CUT BackendIdClass
Alias for backend identifier class.
Data ValueType
Alias for data type of each array element.
void dissociate()
Release this array after finishing all reading.
void copySlice(BackendArray< Data, CUT > const &other, int beginId)
Copy a slice of data from a larger BackendArray into this array.
void allocate(int capacity)
Allocate the underlying C array.
void deallocate()
Deallocate the underlying C array.
Array container class template.
Definition Array.h:41
int capacity() const
Return allocated size.
Definition Array.h:250
bool isAllocated() const
Does this array have associated data?
Definition Array.h:236
Read-only array container class template.
Definition ConstArray.h:38
bool isAllocated() const
Does this array have associated data?
Definition ConstArray.h:174
int capacity_
Allocated size of the data_ array.
Definition ConstArray.h:117
int capacity() const
Return allocated size of this array.
Definition ConstArray.h:188
Data * data_
Pointer to an array of Data elements.
Definition ConstArray.h:114
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
Definition Memory.h:202
static void reallocate(Data *&ptr, size_t oldSize, size_t newSize)
Reallocate a C++ array.
Definition Memory.h:230
static void allocate(Data *&ptr, size_t size)
Allocate a C++ array.
Definition Memory.h:172
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.