Skip to content

Commit f6a3b54

Browse files
author
guo.2154
committed
WIP: avoiding shared_ptr
1 parent 96a1d01 commit f6a3b54

File tree

7 files changed

+58
-21
lines changed

7 files changed

+58
-21
lines changed

include/ftk/filters/mpas_ocean_tracker.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace ftk {
99

1010
struct mpas_ocean_tracker : public virtual tracker {
11-
mpas_ocean_tracker(diy::mpi::communicator comm, std::shared_ptr<mpas_mesh<>> m_) :
11+
mpas_ocean_tracker(diy::mpi::communicator comm, const mpas_mesh<> *m_) :
1212
tracker(comm),
1313
m(m_) {}
1414
virtual ~mpas_ocean_tracker() {}
@@ -17,7 +17,7 @@ public:
1717
void initialize() {}
1818

1919
protected:
20-
std::shared_ptr<mpas_mesh<>> m;
20+
const mpas_mesh<> *m;
2121
};
2222

2323
}

include/ftk/filters/particle_tracer.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected:
5353
virtual std::vector<std::string> scalar_names() const { return {}; }
5454

5555
protected:
56-
std::shared_ptr<ndarray<double>> V[2];
56+
ndarray<double> *V[2];
5757

5858
protected:
5959
std::vector<feature_point_lite_t> particles;

include/ftk/filters/particle_tracer_mpas_ocean.hh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace ftk {
1919
struct particle_tracer_mpas_ocean : public particle_tracer, public mpas_ocean_tracker
2020
{
2121
particle_tracer_mpas_ocean(diy::mpi::communicator comm,
22-
std::shared_ptr<mpas_mesh<>> m) :
22+
const mpas_mesh<>* m) :
2323
particle_tracer(comm, 3),
2424
mpas_ocean_tracker(comm, m),
2525
tracker(comm)
@@ -61,10 +61,10 @@ protected:
6161

6262
protected:
6363
// std::shared_ptr<ndarray<double>> V[2]; // inherited from particle_tracer
64-
std::shared_ptr<ndarray<double>> zTop[2];
65-
std::shared_ptr<ndarray<double>> vertVelocityTop[2];
66-
std::shared_ptr<ndarray<double>> salinity[2];
67-
std::shared_ptr<ndarray<double>> temperature[2];
64+
ndarray<double> *zTop[2];
65+
ndarray<double> *vertVelocityTop[2];
66+
ndarray<double> *salinity[2];
67+
ndarray<double> *temperature[2];
6868
std::tm timestamp[2];
6969

7070
#if FTK_HAVE_CUDA

include/ftk/io/mpas_stream.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct mpas_stream : public object {
2121
void initialize();
2222
void set_callback(std::function<void(int, std::shared_ptr<ndarray_group>)> f) { callback = f; }
2323

24-
std::shared_ptr<mpas_mesh<>> mesh() { return m; }
24+
mpas_mesh<>* mesh() { return m; }
2525

2626
void set_ntimesteps(int n) { ntimesteps = n; }
2727
bool advance_timestep();
@@ -33,7 +33,7 @@ public:
3333
diy::mpi::communicator comm;
3434
std::string path, mesh_path;
3535

36-
std::shared_ptr<mpas_mesh<>> m;
36+
mpas_mesh<> *m;
3737
std::function<void(int, std::shared_ptr<ndarray_group>)> callback;
3838

3939
int ncid;
@@ -50,7 +50,7 @@ public:
5050
//////
5151
void mpas_stream::initialize()
5252
{
53-
m.reset(new mpas_mesh<>);
53+
m = new mpas_mesh<>();
5454
m->read_netcdf(mesh_path.empty() ? path : mesh_path);
5555
m->initialize();
5656

include/ftk/io/xgc_stream_adios2.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,17 @@ inline std::shared_ptr<ndarray_group> xgc_stream_adios2::request_step(int step)
106106
const auto f = filename_step(step);
107107
if (is_directory(f1d) && is_directory(f)) {
108108
try {
109+
ndarray_base *psi_mks = new ndarray<double>,
110+
*e_gc_density_avg = new ndarray<double>,
111+
*e_parallel_mean_en_avg = new ndarray<double>,
112+
*e_perp_temperature_avg = new ndarray<double>;
113+
#if 0
109114
std::shared_ptr<ndarray_base> // 1d profiles
110115
psi_mks( new ndarray<double> ),
111116
e_gc_density_avg( new ndarray<double> ),
112117
e_parallel_mean_en_avg( new ndarray<double> ),
113118
e_perp_temperature_avg( new ndarray<double> );
119+
#endif
114120

115121
psi_mks->read_bp(f1d, "psi_mks", istep);
116122
g->set("psi_mks", psi_mks);
@@ -151,11 +157,17 @@ inline std::shared_ptr<ndarray_group> xgc_stream_adios2::request_step(int step)
151157
}
152158
g->set("e_perp_temperature_avg", e_perp_temperature_avg);
153159

160+
ndarray_base *dpot = new ndarray<double>,
161+
*pot0 = new ndarray<double>,
162+
*potm0 = new ndarray<double>,
163+
*eden = new ndarray<double>;
164+
#if 0
154165
std::shared_ptr<ndarray_base> // 3d
155166
dpot( new ndarray<double> ),
156167
pot0( new ndarray<double> ),
157168
potm0( new ndarray<double> ),
158169
eden( new ndarray<double> );
170+
#endif
159171

160172
dpot->read_bp(f, "dpot");
161173
g->set("dpot", dpot);
@@ -202,7 +214,7 @@ inline bool xgc_stream_adios2::advance_timestep()
202214
fprintf(stderr, "advancing.., %d, %d, %d, filename=%s\n", start_timestep, current_timestep, ntimesteps, current_filename.c_str());
203215

204216
if (is_directory( current_filename )) {
205-
std::shared_ptr<ndarray_base> density( new ndarray<double> );
217+
ndarray_base *density = new ndarray<double>;
206218
density->read_bp(current_filename, "dpot");
207219
g->set("density", density);
208220

include/ftk/io/xgc_stream_h5.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ inline bool xgc_stream_h5::advance_timestep()
4444
const auto current_filename = filename( current_timestep );
4545

4646
if (file_exists( current_filename )) {
47-
std::shared_ptr<ndarray_base> density( new ndarray<double> );
47+
ndarray_base *density = new ndarray<double>;
4848
density->read_h5(current_filename, "dneOverne0");
4949
g->set("density", density);
5050

51-
std::shared_ptr<ndarray_base> Er( new ndarray<double> );
51+
ndarray_base *Er = new ndarray<double>;
5252
Er->read_h5(current_filename, "Er");
5353
g->set("Er", Er);
5454

include/ftk/ndarray/ndarray_group.hh

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66

77
namespace ftk {
88

9-
struct ndarray_group : public std::unordered_map<std::string, std::shared_ptr<ndarray_base>> {
9+
struct ndarray_group : public std::unordered_map<std::string, ndarray_base*> {
1010
ndarray_group() {}
11+
virtual ~ndarray_group();
12+
13+
void remove(const std::string key);
1114

1215
bool has(const std::string key) const { return this->find(key) != this->end(); }
1316

14-
void set(const std::string key, std::shared_ptr<ndarray_base> ptr) { this->emplace(key, ptr); }
17+
void set(const std::string key, ndarray_base *ptr) { this->emplace(key, ptr); }
1518
template <typename T> void set(const std::string key, const ndarray<T> &arr);
1619

17-
template <typename T> std::shared_ptr<ndarray<T>> get_ptr(const std::string key) {
18-
if (has(key)) return std::dynamic_pointer_cast<ndarray<T>>(at(key));
20+
template <typename T> ndarray<T>* get_ptr(const std::string key) {
21+
// if (has(key)) return std::dynamic_pointer_cast<ndarray<T>>(at(key));
22+
if (has(key)) return (ndarray<T>*)at(key);
1923
else return nullptr;
2024
}
2125

@@ -28,10 +32,31 @@ template <typename T>
2832
void ndarray_group::set(const std::string key, const ndarray<T> &arr)
2933
{
3034
// std::cerr << arr << std::endl;
31-
std::shared_ptr<ndarray_base> parr(new ndarray<T>);
32-
*(std::dynamic_pointer_cast<ndarray<T>>(parr)) = arr;
35+
36+
ndarray<T> *p = new ndarray<T>;
37+
ndarray_base *pb = p;
38+
*p = arr;
39+
40+
// *(std::dynamic_pointer_cast<ndarray<T>>(parr)) = arr;
3341
// std::cerr << *(std::dynamic_pointer_cast<ndarray<T>>(parr)) << std::endl;
34-
this->set(key, parr);
42+
this->set(key, pb);
43+
}
44+
45+
inline void ndarray_group::remove(const std::string key)
46+
{
47+
auto p = this->find(key);
48+
if (p != this->end()) {
49+
delete p->second;
50+
this->erase(p);
51+
}
52+
}
53+
54+
inline ndarray_group::~ndarray_group()
55+
{
56+
for (auto kv : *this) {
57+
delete kv.second;
58+
this->erase(kv.first);
59+
}
3560
}
3661

3762
}

0 commit comments

Comments
 (0)