From 0900ba10fab08dbf97afae6d9e35f2d86bfb9912 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Sat, 2 Feb 2008 15:43:00 +0000 Subject: CarpetLib: Add function memoryof to measure size of data structures Add an overloaded function memoryof which measures the size in bytes of CarpetLib's data structures. darcs-hash:20080202154302-dae7b-de41c79cb04617327695fae27928c05c5d431ee8.gz --- Carpet/CarpetLib/src/bbox.hh | 14 +++++++ Carpet/CarpetLib/src/bboxset.hh | 9 +++++ Carpet/CarpetLib/src/data.cc | 16 ++++++++ Carpet/CarpetLib/src/data.hh | 10 +++++ Carpet/CarpetLib/src/defs.cc | 86 +++++++++++++++++++++++++++++++++++++++++ Carpet/CarpetLib/src/defs.hh | 24 ++++++++++++ Carpet/CarpetLib/src/dh.cc | 47 ++++++++++++++++++++++ Carpet/CarpetLib/src/dh.hh | 14 ++++++- Carpet/CarpetLib/src/gf.cc | 12 ++++++ Carpet/CarpetLib/src/gf.hh | 1 + Carpet/CarpetLib/src/ggf.cc | 20 ++++++++++ Carpet/CarpetLib/src/ggf.hh | 8 +++- Carpet/CarpetLib/src/gh.cc | 22 +++++++++++ Carpet/CarpetLib/src/gh.hh | 6 +++ Carpet/CarpetLib/src/mem.cc | 38 ++++++++++++++++++ Carpet/CarpetLib/src/mem.hh | 11 ++++++ Carpet/CarpetLib/src/region.hh | 21 ++++++++++ Carpet/CarpetLib/src/th.cc | 15 +++++++ Carpet/CarpetLib/src/th.hh | 5 +++ Carpet/CarpetLib/src/vect.hh | 10 +++++ 20 files changed, 386 insertions(+), 3 deletions(-) (limited to 'Carpet') diff --git a/Carpet/CarpetLib/src/bbox.hh b/Carpet/CarpetLib/src/bbox.hh index 0b813d39d..a79fbe87b 100644 --- a/Carpet/CarpetLib/src/bbox.hh +++ b/Carpet/CarpetLib/src/bbox.hh @@ -2,6 +2,7 @@ #define BBOX_HH #include +#include #include #include @@ -177,6 +178,12 @@ public: which means that it also points to the first point. */ iterator end () const; + // Memory usage + size_t memory () const + { + return memoryof (_lower) + memoryof (_upper) + memoryof (_stride); + } + // Input/Output helpers void input (istream& is); void output (ostream& os) const; @@ -184,6 +191,13 @@ public: +// Memory usage + +template +inline size_t memoryof (bbox const & b) { return b.memory(); } + + + // Input /** Read a formatted bbox from a stream. */ diff --git a/Carpet/CarpetLib/src/bboxset.hh b/Carpet/CarpetLib/src/bboxset.hh index 1a797e2c5..45e0dc0f2 100644 --- a/Carpet/CarpetLib/src/bboxset.hh +++ b/Carpet/CarpetLib/src/bboxset.hh @@ -142,6 +142,9 @@ public: // iterator begin () const { return bs.begin(); } // iterator end () const { return bs.end(); } + // Memory usage + size_t memory () const { return memoryof (bs); } + // Output void output (ostream& os) const; }; @@ -246,6 +249,12 @@ inline bool operator>= (const bboxset& s, const bbox& b) { +// Memory usage +template +inline size_t memoryof (bboxset const & s) { return s.memory(); } + + + // Output template inline ostream& operator<< (ostream& os, const bboxset& s) { diff --git a/Carpet/CarpetLib/src/data.cc b/Carpet/CarpetLib/src/data.cc index 195e75627..2c334da5a 100644 --- a/Carpet/CarpetLib/src/data.cc +++ b/Carpet/CarpetLib/src/data.cc @@ -916,6 +916,22 @@ time_interpolate (vector const & /*srcs*/, +// Memory usage +template +size_t +data:: +memory () + const +{ + return + memoryof (_memory) + + memoryof (vectorlength) + + memoryof (vectorindex) + + memoryof (vectorleader); +} + + + // Output template ostream & diff --git a/Carpet/CarpetLib/src/data.hh b/Carpet/CarpetLib/src/data.hh index fa11e2852..be30a96c7 100644 --- a/Carpet/CarpetLib/src/data.hh +++ b/Carpet/CarpetLib/src/data.hh @@ -155,10 +155,20 @@ private: public: + // Memory usage + size_t memory () const; + // Output ostream & output (ostream& os) const; friend ostream & operator<< (ostream & os, data const & d); }; + +// Memory usage +template +inline size_t memoryof (data const & d) +{ + return d.memory(); +} #endif // DATA_HH diff --git a/Carpet/CarpetLib/src/defs.cc b/Carpet/CarpetLib/src/defs.cc index 1dd6cb34f..a3d43d88d 100644 --- a/Carpet/CarpetLib/src/defs.cc +++ b/Carpet/CarpetLib/src/defs.cc @@ -81,6 +81,63 @@ void consume (istream& is, const char * const s) { +// Container memory usage +template +size_t +memoryof (list const & c) +{ + size_t s = sizeof c; + for (typename list::const_iterator i=c.begin(); i!=c.end(); ++i) { + // Assume that there are two pointers per list element, pointing + // to the previous and next element, respectively + s += 2 * sizeof (void *) + memoryof(*i); + } + return s; +} + +template +size_t +memoryof (set const & c) +{ + size_t s = sizeof c; + for (typename set::const_iterator i=c.begin(); i!=c.end(); ++i) { + // Assume that there are three pointers per list element, forming + // a tree structure + s += 3 * sizeof (void *) + memoryof(*i); + } + return s; +} + +template +size_t +memoryof (stack const & c) +{ + size_t s = sizeof c; +#if 0 + for (typename stack::const_iterator i=c.begin(); i!=c.end(); ++i) { + // Assume that a stack is stored in an efficient manner + s += memoryof(*i); + } +#endif + // Cannot access elements... a stack is LIFO! + s += c.size() * sizeof (T); + return s; +} + +template +size_t +memoryof (vector const & c) +{ + size_t s = sizeof c; + for (typename vector::const_iterator i=c.begin(); i!=c.end(); ++i) { + // Vectors are stored contiguously + s += memoryof(*i); + } + return s; +} + + + // Vector input template istream& input (istream& is, vector& v) { @@ -166,12 +223,41 @@ ostream& output (ostream& os, const vector& v) { #include "bbox.hh" #include "bboxset.hh" +#include "dh.hh" +#include "gdata.hh" +#include "ggf.hh" +#include "region.hh" +#include "th.hh" #include "vect.hh" template int ipow (int x, int y); template CCTK_REAL ipow (CCTK_REAL x, int y); template vect ipow (vect x, int y); +template size_t memoryof (list > const & l); +template size_t memoryof (list > const & l); +template size_t memoryof (list const & l); +template size_t memoryof (list const & l); +template size_t memoryof (list const & l); +template size_t memoryof (stack const & s); +template size_t memoryof (vector const & v); +template size_t memoryof (vector const & v); +template size_t memoryof (vector const & v); +template size_t memoryof (vector > const & v); +template size_t memoryof (vector > const & v); +template size_t memoryof (vector const & v); +template size_t memoryof (vector const & v); +template size_t memoryof (vector const & v); +template size_t memoryof (vector > const & v); +template size_t memoryof (vector > const & v); +template size_t memoryof (vector > > const & v); +template size_t memoryof (vector > const & v); +template size_t memoryof (vector > const & v); +template size_t memoryof (vector > > const & v); +template size_t memoryof (vector > > const & v); +template size_t memoryof (vector > > const & v); +template size_t memoryof (vector > > > const & v); + template istream& input (istream& os, vector& v); template istream& input (istream& os, vector& v); template istream& input (istream& os, vector >& v); diff --git a/Carpet/CarpetLib/src/defs.hh b/Carpet/CarpetLib/src/defs.hh index 807e9fc59..59d22f4c8 100644 --- a/Carpet/CarpetLib/src/defs.hh +++ b/Carpet/CarpetLib/src/defs.hh @@ -235,6 +235,30 @@ namespace CarpetLib { +// Container memory usage +inline size_t memoryof (char e) { return sizeof e; } +inline size_t memoryof (short e) { return sizeof e; } +inline size_t memoryof (int e) { return sizeof e; } +inline size_t memoryof (long e) { return sizeof e; } +inline size_t memoryof (long long e) { return sizeof e; } +inline size_t memoryof (unsigned char e) { return sizeof e; } +inline size_t memoryof (unsigned short e) { return sizeof e; } +inline size_t memoryof (unsigned int e) { return sizeof e; } +inline size_t memoryof (unsigned long e) { return sizeof e; } +inline size_t memoryof (unsigned long long e) { return sizeof e; } +inline size_t memoryof (float e) { return sizeof e; } +inline size_t memoryof (double e) { return sizeof e; } +inline size_t memoryof (long double e) { return sizeof e; } +inline size_t memoryof (void * e) { return sizeof e; } +template inline size_t memoryof (T * e) { return sizeof e; } +template inline size_t memoryof (T const * e) { return sizeof e; } +template size_t memoryof (list const & c); +template size_t memoryof (set const & c); +template size_t memoryof (stack const & c); +template size_t memoryof (vector const & c); + + + // Container input template istream& input (istream& is, vector& v); diff --git a/Carpet/CarpetLib/src/dh.cc b/Carpet/CarpetLib/src/dh.cc index 5190c8217..d7825925d 100644 --- a/Carpet/CarpetLib/src/dh.cc +++ b/Carpet/CarpetLib/src/dh.cc @@ -1042,7 +1042,54 @@ remove (ggf * const f) +// Memory usage + +size_t +dh:: +memory () + const +{ + return + memoryof (ghost_width) + + memoryof (buffer_width) + + memoryof (prolongation_order_space) + + memoryof (boxes) + + memoryof (oldboxes) + + memoryof (gfs); +} + +size_t +dh::dboxes:: +memory () + const +{ + return + memoryof (exterior) + + memoryof (is_outer_boundary) + + memoryof (outer_boundaries) + + memoryof (communicated) + + memoryof (boundaries) + + memoryof (owned) + + memoryof (buffers) + + memoryof (active) + + memoryof (sync) + + memoryof (bndref) + + memoryof (ghosts) + + memoryof (interior) + + memoryof (fast_mg_rest_sendrecv) + + memoryof (fast_mg_prol_sendrecv) + + memoryof (fast_ref_prol_sendrecv) + + memoryof (fast_ref_rest_sendrecv) + + memoryof (fast_sync_sendrecv) + + memoryof (fast_ref_bnd_prol_sendrecv) + + memoryof (fast_old2new_sync_sendrecv) + + memoryof (fast_old2new_ref_prol_sendrecv); +} + + + // Output + ostream & dh:: output (ostream & os) diff --git a/Carpet/CarpetLib/src/dh.hh b/Carpet/CarpetLib/src/dh.hh index ea9b0f294..09d3579e5 100644 --- a/Carpet/CarpetLib/src/dh.hh +++ b/Carpet/CarpetLib/src/dh.hh @@ -2,6 +2,7 @@ #define DH_HH #include +#include #include #include #include @@ -23,7 +24,6 @@ class ggf; class dh; - // A data hierarchy (grid hierarchy plus ghost zones) class dh { @@ -75,6 +75,7 @@ public: srpvect fast_old2new_sync_sendrecv; srpvect fast_old2new_ref_prol_sendrecv; + size_t memory () const; ostream & output (ostream & os) const; }; @@ -125,11 +126,22 @@ public: void remove (ggf * f); // Output + size_t memory () const; ostream & output (ostream & os) const; }; +inline size_t memoryof (dh::dboxes const & b) +{ + return b.memory (); +} + +inline size_t memoryof (dh const & d) +{ + return d.memory (); +} + inline ostream & operator<< (ostream & os, dh::dboxes const & b) { return b.output (os); diff --git a/Carpet/CarpetLib/src/gf.cc b/Carpet/CarpetLib/src/gf.cc index b765b3fd2..37b06db75 100644 --- a/Carpet/CarpetLib/src/gf.cc +++ b/Carpet/CarpetLib/src/gf.cc @@ -62,6 +62,18 @@ data* gf::operator() (int tl, int rl, int c, int ml) +// Memory usage +template +size_t +gf:: +memory () + const +{ + return ggf::memory(); +} + + + // Output template ostream& gf::output (ostream& os) const diff --git a/Carpet/CarpetLib/src/gf.hh b/Carpet/CarpetLib/src/gf.hh index b4f2e211d..0ca125631 100644 --- a/Carpet/CarpetLib/src/gf.hh +++ b/Carpet/CarpetLib/src/gf.hh @@ -71,6 +71,7 @@ public: // Output + virtual size_t memory () const; virtual ostream& output (ostream& os) const; private: gf (); // canonical default construtor diff --git a/Carpet/CarpetLib/src/ggf.cc b/Carpet/CarpetLib/src/ggf.cc index b0c9baa56..3e397e999 100644 --- a/Carpet/CarpetLib/src/ggf.cc +++ b/Carpet/CarpetLib/src/ggf.cc @@ -519,3 +519,23 @@ transfer_from_all (comm_state & state, total.stop (0); } + + + +// Memory usage +size_t +ggf:: +memory () + const +{ + return + memoryof (varindex) + + memoryof (transport_operator) + + memoryof (prolongation_order_time) + + memoryof (timelevels_) + + memoryof (storage) + + memoryof (vectorlength) + + memoryof (vectorindex) + + memoryof (vectorleader) + + memoryof (oldstorage); +} diff --git a/Carpet/CarpetLib/src/ggf.hh b/Carpet/CarpetLib/src/ggf.hh index bd0ab64b0..63bb2f59b 100644 --- a/Carpet/CarpetLib/src/ggf.hh +++ b/Carpet/CarpetLib/src/ggf.hh @@ -182,12 +182,12 @@ public: // Access to the data virtual const gdata* operator() (int tl, int rl, int c, int ml) const = 0; - virtual gdata* operator() (int tl, int rl, int c, int ml) = 0; // Output + virtual size_t memory () const; virtual ostream& output (ostream& os) const = 0; private: @@ -199,7 +199,11 @@ private: -template +inline size_t memoryof (ggf const & f) +{ + return f.memory (); +} + inline ostream& operator<< (ostream& os, const ggf& f) { return f.output(os); diff --git a/Carpet/CarpetLib/src/gh.cc b/Carpet/CarpetLib/src/gh.cc index 66722e11d..6a7738048 100644 --- a/Carpet/CarpetLib/src/gh.cc +++ b/Carpet/CarpetLib/src/gh.cc @@ -301,6 +301,28 @@ remove (dh * const d) +// Memory usage + +size_t +gh:: +memory () + const +{ + return + memoryof (reffacts) + + memoryof (refcent) + + memoryof (mgfact) + + memoryof (mgcent) + + memoryof (baseextents) + + memoryof (boundary_width) + + memoryof (regions) + + memoryof (oldregions) + + memoryof (ths) + + memoryof (dhs); +} + + + // Output void diff --git a/Carpet/CarpetLib/src/gh.hh b/Carpet/CarpetLib/src/gh.hh index 6346e1509..4d1592120 100644 --- a/Carpet/CarpetLib/src/gh.hh +++ b/Carpet/CarpetLib/src/gh.hh @@ -128,6 +128,7 @@ public: void remove (dh * d); // Output + size_t memory () const; ostream & output (ostream & os) const; private: @@ -138,6 +139,11 @@ private: +inline size_t memoryof (gh const & g) +{ + return g.memory (); +} + inline ostream & operator<< (ostream & os, gh const & h) { return h.output(os); diff --git a/Carpet/CarpetLib/src/mem.cc b/Carpet/CarpetLib/src/mem.cc index b6b1c9c8e..d8603591b 100644 --- a/Carpet/CarpetLib/src/mem.cc +++ b/Carpet/CarpetLib/src/mem.cc @@ -150,6 +150,25 @@ has_clients () const +// Memory usage +template +size_t +mem:: +memory () + const +{ + return + memoryof (storage_) + + memoryof (nelems_) + + memoryof (vectorlength_) + + memoryof (owns_storage_) + + memoryof (clients_) + + memoryof (num_clients_) + + (owns_storage_ ? sizeof (T) * vectorlength_ * nelems_ : 0); +} + + + size_t const mempool::chunksize; size_t const mempool::align; @@ -205,6 +224,25 @@ alloc (size_t nbytes) +// Memory usage +size_t +mempool:: +memory () + const +{ + return + memoryof (chunks) + + memoryof (freeptr) + + memoryof (freesize) + + (chunksize * chunks.size()); +} + + + +//////////////////////////////////////////////////////////////////////////////// + + + extern "C" void CarpetLib_setmemlimit (CCTK_ARGUMENTS); void CarpetLib_setmemlimit (CCTK_ARGUMENTS) diff --git a/Carpet/CarpetLib/src/mem.hh b/Carpet/CarpetLib/src/mem.hh index b38d5459b..084383104 100644 --- a/Carpet/CarpetLib/src/mem.hh +++ b/Carpet/CarpetLib/src/mem.hh @@ -33,8 +33,14 @@ public: void register_client (size_t vectorindex); void unregister_client (size_t vectorindex); bool has_clients () const; + + // Memory usage + size_t memory () const; }; +template +inline size_t memoryof (mem const & m) { return m.memory(); } + // A mempool (memory pool) is a large chunk of memory. You can @@ -73,6 +79,11 @@ public: // Allocate some memory and return a pointer to it. This cannot // fail. void * alloc (size_t nbytes); + + // Memory usage + size_t memory () const; }; +inline size_t memoryof (mempool const & m) { return m.memory(); } + #endif // ifndef MEM_HH diff --git a/Carpet/CarpetLib/src/region.hh b/Carpet/CarpetLib/src/region.hh index 73b60004e..dcf618133 100644 --- a/Carpet/CarpetLib/src/region.hh +++ b/Carpet/CarpetLib/src/region.hh @@ -29,6 +29,15 @@ bool operator!= (region_t const & a, region_t const & b) +inline size_t memoryof (region_t const & reg) +{ + return + memoryof (reg.extent) + + memoryof (reg.outer_boundaries) + + memoryof (reg.map) + + memoryof (reg.processor); +} + istream & operator>> (istream & is, region_t & reg); ostream & operator<< (ostream & os, region_t const & reg); @@ -50,6 +59,13 @@ struct pseudoregion_t { } }; +inline size_t memoryof (pseudoregion_t const & p) +{ + return + memoryof (p.extent) + + memoryof (p.processor); +} + ostream & operator<< (ostream & os, pseudoregion_t const & p); struct sendrecv_pseudoregion_t { @@ -65,6 +81,11 @@ struct sendrecv_pseudoregion_t { } }; +inline size_t memoryof (sendrecv_pseudoregion_t const & srp) +{ + return memoryof (srp.send) + memoryof (srp.recv); +} + ostream & operator<< (ostream & os, sendrecv_pseudoregion_t const & srp); diff --git a/Carpet/CarpetLib/src/th.cc b/Carpet/CarpetLib/src/th.cc index e8d5bbb49..a10dcb289 100644 --- a/Carpet/CarpetLib/src/th.cc +++ b/Carpet/CarpetLib/src/th.cc @@ -62,6 +62,21 @@ void th::regrid () +// Memory usage +size_t +th:: +memory () + const +{ + return + memoryof (reffacts) + + memoryof (delta) + + memoryof (times) + + memoryof (deltas); +} + + + // Output void th::output (ostream& os) const { diff --git a/Carpet/CarpetLib/src/th.hh b/Carpet/CarpetLib/src/th.hh index b3dc315b0..34e9af999 100644 --- a/Carpet/CarpetLib/src/th.hh +++ b/Carpet/CarpetLib/src/th.hh @@ -91,11 +91,16 @@ public: } // Output + size_t memory () const; void output (ostream& os) const; }; +inline size_t memoryof (th const & t) +{ + return t.memory (); +} inline ostream& operator<< (ostream& os, const th& t) { t.output(os); return os; diff --git a/Carpet/CarpetLib/src/vect.hh b/Carpet/CarpetLib/src/vect.hh index 29b8a3ba8..e3f6b086b 100644 --- a/Carpet/CarpetLib/src/vect.hh +++ b/Carpet/CarpetLib/src/vect.hh @@ -293,6 +293,9 @@ public: }; #endif + // Memory usage + size_t memory () const { return D * memoryof (*elt); } + // Input/Output helpers void input (istream& is); void output (ostream& os) const; @@ -509,6 +512,13 @@ inline vect scan1 (U (* const func)(U val, T x), U val, +// Memory usage + +template +inline size_t memoryof (vect const & a) { return a.memory(); } + + + // Input /** Read a formatted vector from a stream. */ -- cgit v1.2.3