Skip to content

Commit 8d96630

Browse files
committed
always use shared_ptr for Encapsulation objects
1 parent ddf9376 commit 8d96630

File tree

12 files changed

+154
-159
lines changed

12 files changed

+154
-159
lines changed

examples/advection_diffusion/advection_diffusion_sweeper.hpp

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,41 @@ class AdvectionDiffusionSweeper : public pfasst::encap::IMEXSweeper<time>
5151
cout << "number of f1 evals: " << nf1evals << endl;
5252
}
5353

54-
void exact(Encapsulation* q, time t)
54+
void exact(shared_ptr<Encapsulation> q, time t)
5555
{
56-
DVectorT* q_cast = dynamic_cast<DVectorT*>(q);
57-
assert(q_cast != nullptr);
58-
this->exact(*q_cast, t);
56+
shared_ptr<DVectorT> q_cast = dynamic_pointer_cast<DVectorT>(q);
57+
assert(q_cast);
58+
this->exact(q_cast, t);
5959
}
6060

61-
void exact(DVectorT& q, time t)
61+
void exact(shared_ptr<DVectorT> q, time t)
6262
{
63-
size_t n = q.size();
63+
size_t n = q->size();
6464
double a = 1.0 / sqrt(4 * PI * nu * (t + t0));
6565

6666
for (size_t i = 0; i < n; i++) {
67-
q[i] = 0.0;
67+
q->at(i) = 0.0;
6868
}
6969

7070
for (int ii = -2; ii < 3; ii++) {
7171
for (size_t i = 0; i < n; i++) {
7272
double x = double(i) / n - 0.5 + ii - t * v;
73-
q[i] += a * exp(-x * x / (4 * nu * (t + t0)));
73+
q->at(i) += a * exp(-x * x / (4 * nu * (t + t0)));
7474
}
7575
}
7676
}
7777

7878
void echo_error(time t, bool predict = false)
7979
{
80-
DVectorT* qend = dynamic_cast<DVectorT*>(this->get_state(this->get_nodes().size() - 1));
81-
assert(qend != nullptr);
82-
DVectorT qex = DVectorT(qend->size());
80+
shared_ptr<DVectorT> qend = dynamic_pointer_cast<DVectorT>(this->get_state(this->get_nodes().size() - 1));
81+
assert(qend);
82+
shared_ptr<DVectorT> qex = make_shared<DVectorT>(qend->size());
8383

8484
exact(qex, t);
8585

8686
double max = 0.0;
8787
for (size_t i = 0; i < qend->size(); i++) {
88-
double d = abs(qend->at(i) - qex[i]);
88+
double d = abs(qend->at(i) - qex->at(i));
8989
if (d > max) { max = d; }
9090
}
9191
cout << "err: " << scientific << max << " (" << qend->size() << ", " << predict << ")" << endl;
@@ -103,75 +103,74 @@ class AdvectionDiffusionSweeper : public pfasst::encap::IMEXSweeper<time>
103103
echo_error(t + dt);
104104
}
105105

106-
void f1eval(Encapsulation* f, Encapsulation* q, time t)
106+
void f1eval(shared_ptr<Encapsulation> f, shared_ptr<Encapsulation> q, time t)
107107
{
108-
DVectorT* f_cast = dynamic_cast<DVectorT*>(f);
109-
assert(f_cast != nullptr);
110-
DVectorT* q_cast = dynamic_cast<DVectorT*>(q);
111-
assert(q_cast != nullptr);
108+
shared_ptr<DVectorT> f_cast = dynamic_pointer_cast<DVectorT>(f);
109+
assert(f_cast);
110+
shared_ptr<DVectorT> q_cast = dynamic_pointer_cast<DVectorT>(q);
111+
assert(q_cast);
112112

113113
this->f1eval(f_cast, q_cast, t);
114114
}
115115

116-
void f1eval(DVectorT* f, DVectorT* q, time t)
116+
void f1eval(shared_ptr<DVectorT> f, shared_ptr<DVectorT> q, time t)
117117
{
118118
double c = -v / double(q->size());
119119

120-
auto* z = fft.forward(*q);
120+
auto* z = fft.forward(q);
121121
for (size_t i = 0; i < q->size(); i++) {
122122
z[i] *= c * ddx[i];
123123
}
124-
fft.backward(*f);
124+
fft.backward(f);
125125

126126
nf1evals++;
127127
}
128128

129-
void f2eval(Encapsulation* f, Encapsulation* q, time t)
129+
void f2eval(shared_ptr<Encapsulation> f, shared_ptr<Encapsulation> q, time t)
130130
{
131-
DVectorT* f_cast = dynamic_cast<DVectorT*>(f);
132-
assert(f_cast != nullptr);
133-
DVectorT* q_cast = dynamic_cast<DVectorT*>(q);
134-
assert(q_cast != nullptr);
131+
shared_ptr<DVectorT> f_cast = dynamic_pointer_cast<DVectorT>(f);
132+
assert(f_cast);
133+
shared_ptr<DVectorT> q_cast = dynamic_pointer_cast<DVectorT>(q);
134+
assert(q_cast);
135135

136136
this->f2eval(f_cast, q_cast, t);
137137
}
138138

139-
void f2eval(DVectorT* f, DVectorT* q, time t)
139+
void f2eval(shared_ptr<DVectorT> f, shared_ptr<DVectorT> q, time t)
140140
{
141141
double c = nu / double(q->size());
142142

143-
auto* z = fft.forward(*q);
143+
auto* z = fft.forward(q);
144144
for (size_t i = 0; i < q->size(); i++) {
145145
z[i] *= c * lap[i];
146146
}
147-
fft.backward(*f);
147+
fft.backward(f);
148148
}
149149

150-
void f2comp(Encapsulation* f, Encapsulation* q, time t, time dt, Encapsulation* rhs)
150+
void f2comp(shared_ptr<Encapsulation> f, shared_ptr<Encapsulation> q, time t, time dt, shared_ptr<Encapsulation> rhs)
151151
{
152-
DVectorT* f_cast = dynamic_cast<DVectorT*>(f);
153-
assert(f_cast != nullptr);
154-
DVectorT* q_cast = dynamic_cast<DVectorT*>(q);
155-
assert(q_cast != nullptr);
156-
DVectorT* rhs_cast = dynamic_cast<DVectorT*>(rhs);
157-
assert(rhs_cast != nullptr);
152+
shared_ptr<DVectorT> f_cast = dynamic_pointer_cast<DVectorT>(f);
153+
assert(f_cast);
154+
shared_ptr<DVectorT> q_cast = dynamic_pointer_cast<DVectorT>(q);
155+
assert(q_cast);
156+
shared_ptr<DVectorT> rhs_cast = dynamic_pointer_cast<DVectorT>(rhs);
157+
assert(rhs_cast);
158158

159159
this->f2comp(f_cast, q_cast, t, dt, rhs_cast);
160160
}
161161

162-
void f2comp(DVectorT* f, DVectorT* q, time t, time dt, DVectorT* rhs)
162+
void f2comp(shared_ptr<DVectorT> f, shared_ptr<DVectorT> q, time t, time dt, shared_ptr<DVectorT> rhs)
163163
{
164-
auto* z = fft.forward(*rhs);
164+
auto* z = fft.forward(rhs);
165165
for (size_t i = 0; i < q->size(); i++) {
166166
z[i] /= (1.0 - nu * double(dt) * lap[i]) * double(q->size());
167167
}
168-
fft.backward(*q);
168+
fft.backward(q);
169169

170170
for (size_t i = 0; i < q->size(); i++) {
171171
f->at(i) = (q->at(i) - rhs->at(i)) / double(dt);
172172
}
173173
}
174-
175174
};
176175

177176
#endif

examples/advection_diffusion/fft.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define _FFT_HPP_
99

1010
#include <map>
11+
#include <memory>
1112
#include <cstddef>
1213

1314
#include <pfasst/encap/vector.hpp>
@@ -25,51 +26,51 @@ class FFT
2526
complex<double>* z;
2627
};
2728

28-
map<size_t, workspace*> workspaces;
29+
map<size_t, shared_ptr<workspace>> workspaces;
2930

3031
public:
3132
~FFT()
3233
{
3334
for (auto& x : workspaces) {
34-
auto* wk = std::get<1>(x);
35+
shared_ptr<workspace> wk = std::get<1>(x);
3536
fftw_free(wk->wk);
3637
fftw_destroy_plan(wk->ffft);
3738
fftw_destroy_plan(wk->ifft);
38-
delete wk;
39+
// delete wk;
3940
}
4041
workspaces.clear();
4142
}
4243

43-
workspace* get_workspace(size_t ndofs)
44+
shared_ptr<workspace> get_workspace(size_t ndofs)
4445
{
4546
if (workspaces.find(ndofs) == workspaces.end()) {
46-
workspace* wk = new workspace;
47+
shared_ptr<workspace> wk = make_shared<workspace>();
4748
wk->wk = fftw_alloc_complex(ndofs);
4849
wk->ffft = fftw_plan_dft_1d(ndofs, wk->wk, wk->wk, FFTW_FORWARD, FFTW_ESTIMATE);
4950
wk->ifft = fftw_plan_dft_1d(ndofs, wk->wk, wk->wk, FFTW_BACKWARD, FFTW_ESTIMATE);
5051
wk->z = reinterpret_cast<complex<double>*>(wk->wk);
51-
workspaces.insert(pair<size_t, workspace*>(ndofs, wk));
52+
workspaces.insert(pair<size_t, shared_ptr<workspace>>(ndofs, wk));
5253
}
5354

5455
return workspaces[ndofs];
5556
}
5657

57-
complex<double>* forward(const DVectorT& x)
58+
complex<double>* forward(shared_ptr<const DVectorT> x)
5859
{
59-
workspace* wk = get_workspace(x.size());
60-
for (size_t i = 0; i < x.size(); i++) {
61-
wk->z[i] = x[i];
60+
shared_ptr<workspace> wk = get_workspace(x->size());
61+
for (size_t i = 0; i < x->size(); i++) {
62+
wk->z[i] = x->at(i);
6263
}
6364
fftw_execute_dft(wk->ffft, wk->wk, wk->wk);
6465
return wk->z;
6566
}
6667

67-
void backward(DVectorT& x)
68+
void backward(shared_ptr<DVectorT> x)
6869
{
69-
workspace* wk = get_workspace(x.size());
70+
shared_ptr<workspace> wk = get_workspace(x->size());
7071
fftw_execute_dft(wk->ifft, wk->wk, wk->wk);
71-
for (size_t i = 0; i < x.size(); i++) {
72-
x[i] = real(wk->z[i]);
72+
for (size_t i = 0; i < x->size(); i++) {
73+
x->at(i) = real(wk->z[i]);
7374
}
7475
}
7576

examples/advection_diffusion/serial_mlsdc.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* This example uses a (serial) multi-level SDC sweeper.
55
*/
66

7+
#include <memory>
8+
79
#include <fftw3.h>
810

911
#include <pfasst.hpp>
@@ -38,9 +40,9 @@ int main(int argc, char** argv)
3840
*/
3941
for (size_t l = 0; l < nlevs; l++) {
4042
auto nodes = compute_nodes<double>(nnodes, "gauss-lobatto");
41-
auto* factory = new VectorFactory<double>(ndofs);
42-
auto* sweeper = new AdvectionDiffusionSweeper<>(ndofs);
43-
auto* transfer = new SpectralTransfer1D<>();
43+
shared_ptr<VectorFactory<double>> factory = make_shared<VectorFactory<double>>(ndofs);
44+
AdvectionDiffusionSweeper<>* sweeper = new AdvectionDiffusionSweeper<>(ndofs);
45+
SpectralTransfer1D<>* transfer = new SpectralTransfer1D<>();
4446

4547
sweeper->set_nodes(nodes);
4648
sweeper->set_factory(factory);
@@ -62,7 +64,7 @@ int main(int argc, char** argv)
6264
* set initial conditions on each level
6365
*/
6466
auto* sweeper = mlsdc.get_level<AdvectionDiffusionSweeper<>>(mlsdc.nlevels() - 1);
65-
auto* q0 = sweeper->get_state(0);
67+
auto q0 = sweeper->get_state(0);
6668
sweeper->exact(q0, 0.0);
6769

6870
/*

examples/advection_diffusion/serial_mlsdc_autobuild.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* controller.
88
*/
99

10+
#include <memory>
11+
#include <cassert>
12+
1013
#include <fftw3.h>
1114

1215
#include <pfasst.hpp>
@@ -43,7 +46,7 @@ int main(int argc, char** argv)
4346
* captures the 'ndofs' variable from above.
4447
*/
4548
auto build_level = [ndofs](size_t level) {
46-
auto* factory = new VectorFactory<double>(ndofs[level]);
49+
auto factory = make_shared<VectorFactory<double>>(ndofs[level]);
4750
auto* sweeper = new AdvectionDiffusionSweeper<>(ndofs[level]);
4851
auto* transfer = new SpectralTransfer1D<>();
4952

@@ -54,9 +57,9 @@ int main(int argc, char** argv)
5457
* the 'initial' function is called once for each level to set the
5558
* intial conditions.
5659
*/
57-
auto initial = [](EncapSweeper<>* sweeper,
58-
Encapsulation<>* q0) {
60+
auto initial = [](EncapSweeper<>* sweeper, shared_ptr<Encapsulation<>> q0) {
5961
auto* ad = dynamic_cast<AdvectionDiffusionSweeper<>*>(sweeper);
62+
assert(ad != nullptr);
6063
ad->exact(q0, 0.0);
6164
};
6265

examples/advection_diffusion/spectral_transfer_1d.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define _SPECTRAL_TRANSFER_1D_HPP_
77

88
#include <cassert>
9+
#include <memory>
910

1011
#include <pfasst/encap/vector.hpp>
1112
#include <pfasst/encap/poly_interp.hpp>
@@ -21,19 +22,19 @@ class SpectralTransfer1D : public pfasst::encap::PolyInterpMixin<time>
2122
FFT fft;
2223

2324
public:
24-
void interpolate(Encapsulation* dst, const Encapsulation* src)
25+
void interpolate(shared_ptr<Encapsulation> dst, shared_ptr<const Encapsulation> src)
2526
{
26-
DVectorT* fine = dynamic_cast<DVectorT*>(dst);
27-
assert(fine != nullptr);
28-
const DVectorT* crse = dynamic_cast<const DVectorT*>(src);
29-
assert(crse != nullptr);
27+
shared_ptr<DVectorT> fine = dynamic_pointer_cast<DVectorT>(dst);
28+
assert(fine);
29+
shared_ptr<const DVectorT> crse = dynamic_pointer_cast<const DVectorT>(src);
30+
assert(crse);
3031

3132
this->interpolate(fine, crse);
3233
}
3334

34-
void interpolate(DVectorT* fine, const DVectorT* crse)
35+
void interpolate(shared_ptr<DVectorT> fine, shared_ptr<const DVectorT> crse)
3536
{
36-
auto* crse_z = fft.forward(*crse);
37+
auto* crse_z = fft.forward(crse);
3738
auto* fine_z = fft.get_workspace(fine->size())->z;
3839

3940
for (size_t i = 0; i < fine->size(); i++) {
@@ -50,20 +51,20 @@ class SpectralTransfer1D : public pfasst::encap::PolyInterpMixin<time>
5051
fine_z[fine->size() - crse->size() / 2 + i] = c * crse_z[crse->size() / 2 + i];
5152
}
5253

53-
fft.backward(*fine);
54+
fft.backward(fine);
5455
}
5556

56-
void restrict(Encapsulation* dst, const Encapsulation* src)
57+
void restrict(shared_ptr<Encapsulation> dst, shared_ptr<const Encapsulation> src)
5758
{
58-
DVectorT* crse = dynamic_cast<DVectorT*>(dst);
59-
assert(crse != nullptr);
60-
const DVectorT* fine = dynamic_cast<const DVectorT*>(src);
61-
assert(fine != nullptr);
59+
shared_ptr<DVectorT> crse = dynamic_pointer_cast<DVectorT>(dst);
60+
assert(crse);
61+
shared_ptr<const DVectorT> fine = dynamic_pointer_cast<const DVectorT>(src);
62+
assert(fine);
6263

6364
this->restrict(crse, fine);
6465
}
6566

66-
void restrict(DVectorT* crse, const DVectorT* fine)
67+
void restrict(shared_ptr<DVectorT> crse, shared_ptr<const DVectorT> fine)
6768
{
6869
size_t xrat = fine->size() / crse->size();
6970

examples/advection_diffusion/vanilla_sdc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ int main(int argc, char** argv)
2222
const size_t ndofs = 64;
2323
const size_t niters = 4;
2424

25-
auto nodes = pfasst::compute_nodes(nnodes, "gauss-lobatto");
26-
auto* factory = new pfasst::encap::VectorFactory<double>(ndofs);
25+
auto nodes = pfasst::compute_nodes(nnodes, "gauss-lobatto");
26+
auto factory = make_shared<pfasst::encap::VectorFactory<double>>(ndofs);
2727
auto* sweeper = new AdvectionDiffusionSweeper<>(ndofs);
2828

2929
sweeper->set_nodes(nodes);
@@ -33,7 +33,7 @@ int main(int argc, char** argv)
3333
sdc.set_duration(dt, nsteps, niters);
3434
sdc.setup();
3535

36-
auto* q0 = sweeper->get_state(0);
36+
auto q0 = sweeper->get_state(0);
3737
sweeper->exact(q0, 0.0);
3838

3939
sdc.run();

0 commit comments

Comments
 (0)