PSCF v1.4.0
Propagator.cu
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2025, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "Propagator.h"
9#include "Block.h"
10#include <prdc/cuda/RField.h>
11#include <pscf/cuda/VecOp.h>
12#include <pscf/cuda/Reduce.h>
13#include <pscf/mesh/Mesh.h>
14
15#include <rp/solvers/Propagator.tpp> // base class template implementation
16
17namespace Pscf {
18namespace Rpg {
19
20 using namespace Util;
21
22 // Public member functions
23
24 /*
25 * Constructor.
26 */
27 template <int D>
31
32 /*
33 * Destructor.
34 */
35 template <int D>
37 {
38 dissociateQFields();
39
40 /*
41 * The above function dissociates elements of qFields_ from memory
42 * owned by qFieldsAll_. Because this destructor body is called
43 * before the destructors for members, disassociation will occur
44 * before either qFieldsAll_ or qFields_ is destroyed.
45 */
46
47 }
48
49 /*
50 * Allocate memory used by this propagator.
51 */
52 template <int D>
54 {
56
57 const int meshSize = mesh.size();
58 IntVec<D> const & meshDimensions = mesh.dimensions();
59
60 // Allocate memory in qFieldsAll_ using value of ns
61 qFieldsAll_.allocate(ns * meshSize);
62
63 // Set up array of associated RField<D> arrays
64 qFields_.allocate(ns);
65 for (int i = 0; i < ns; ++i) {
66 qFields_[i].associate(qFieldsAll_, i*meshSize, meshDimensions);
67 }
68 isAllocated_ = true;
69
71 }
72
73 /*
74 * Reallocate memory used by this propagator using new ns value.
75 */
76 template <int D>
78 {
80
81 // Deallocate memory previously used by this propagator.
82 dissociateQFields(); // dissociate, nulliify data pointers
83 qFields_.deallocate(); // destroy RField<D> objects for slices
84 qFieldsAll_.deallocate(); // destroy contiguous data block
85
86 // Store mesh properties
88 const int meshSize = mesh.size();
89 IntVec<D> const & meshDimensions = mesh.dimensions();
90
91 // Allocate memory in qFieldsAll_ using new value of ns
92 qFieldsAll_.allocate(ns * meshSize);
93
94 // Recreate associations between qFields_ and qFieldsAll_
95 qFields_.allocate(ns);
96 for (int i = 0; i < ns; ++i) {
97 qFields_[i].associate(qFieldsAll_, i*meshSize, meshDimensions);
98 }
99
101 }
102
103 // Private member function
104
105 /*
106 * Dissociate qFields_ from associated memory blocks in qFieldsAll_.
107 *
108 * These associations must be destroyed before qFieldsAll_ is
109 * de-allocated or destroyed, because it is an error to deallocate
110 * a DeviceArray<T> container that is still referred to by one or
111 * more other such containers. Associations are kept track of via
112 * a private ReferenceCounter owned by qFieldsAll_ (which stores
113 * the number of remaining references), and via a data pointer and
114 * CountedReference object owned by each element of qFields_ that
115 * has a pointer to the associated ReferenceCounter. For each
116 * element of qFields_, invoking the dissociate() member function
117 * nullifies the array data pointer, sets the array capacity to
118 * zero, decrements the number of references in the associated
119 * ReferenceCounter owned by qFieldsAll_, and nullifies the pointer
120 * to this ReferenceCounter.
121 */
122 template <int D>
123 void Propagator<D>::dissociateQFields()
124 {
125 if (qFields_.isAllocated()) {
126 int ns = qFields_.capacity();
127 for (int i = 0; i < ns; ++i) {
128 if (qFields_[i].isAssociated()) {
129 qFields_[i].dissociate();
130 }
131 }
132 }
133 }
134
135}
136}
137
138// Explicit Instantiation definitions
139namespace Pscf {
140 namespace Rp {
141 template class Propagator<1, Rpg::Types<1> >;
142 template class Propagator<2, Rpg::Types<2> >;
143 template class Propagator<3, Rpg::Types<3> >;
144 }
145 namespace Rpg {
146 template class Propagator<1>;
147 template class Propagator<2>;
148 template class Propagator<3>;
149 }
150}
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Description of a regular grid of points in a periodic domain.
Definition Mesh.h:61
void setIsSolved(bool isSolved)
Set the isSolved flag to true or false.
MDE solver for one direction of one block.
Mesh< D > const & mesh() const
virtual void allocate(int ns, const Mesh< D > &mesh)
MDE solver for one direction of one block.
void reallocate(int ns) override
Reallocate memory used by this propagator.
Definition Propagator.cu:77
DArray< typename Types< D >::RField > qFields_
Array of propagator slices at different contour variable values.
Rp::Propagator< D, Types< D > > RpPropagatorT
Direct base class.
Propagator()
Constructor.
Definition Propagator.cu:28
void allocate(int ns, const Mesh< D > &mesh) override
Allocate memory used by this propagator.
Definition Propagator.cu:53
bool isAllocated_
Is this propagator allocated?
~Propagator()
Destructor.
Definition Propagator.cu:36
Class templates for real-valued periodic fields.
SCFT and PS-FTS with real periodic fields (GPU)
PSCF package top-level namespace.