aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/mem.cc
diff options
context:
space:
mode:
authorErik Schnetter <schnetter@aei.mpg.de>2005-07-27 20:18:00 +0000
committerErik Schnetter <schnetter@aei.mpg.de>2005-07-27 20:18:00 +0000
commite35a21fdcc0c6aca0543caafde37b2ff74a4e264 (patch)
tree80807bf2580fe320760157a81bf50cff1f9c49a7 /Carpet/CarpetLib/src/mem.cc
parent4b4e7cee5c2580bcf3b47b6af99b0e5bf0e5c9c5 (diff)
CarpetLib: Output memory statistics, and limit maximum memory usage
Introduce a new parameter print_memstats_every. When non-zero, output the current and total allocated amount of memory (per process). Introduce a new parameter max_allowed_memory_MB. When more than that amount should be allocated on the current processor, abort the run. Only memory for grid variables counts; memory for administrative overhead is ignored. darcs-hash:20050727201851-891bb-c1ff9fc30ff949d576d500fbf70ad7fb5084836a.gz
Diffstat (limited to 'Carpet/CarpetLib/src/mem.cc')
-rw-r--r--Carpet/CarpetLib/src/mem.cc47
1 files changed, 47 insertions, 0 deletions
diff --git a/Carpet/CarpetLib/src/mem.cc b/Carpet/CarpetLib/src/mem.cc
index ae906d6c5..c32cc053b 100644
--- a/Carpet/CarpetLib/src/mem.cc
+++ b/Carpet/CarpetLib/src/mem.cc
@@ -1,7 +1,11 @@
#include <algorithm>
#include <cassert>
+#include <iomanip>
+#include <iostream>
#include "cctk.h"
+#include "cctk_Arguments.h"
+#include "cctk_Parameters.h"
#include "defs.hh"
@@ -13,6 +17,10 @@
static size_t total_allocated_bytes = 0;
static size_t total_allocated_objects = 0;
+// Maximum of the above (over time)
+static size_t max_allocated_bytes = 0;
+static size_t max_allocated_objects = 0;
+
template<typename T>
@@ -25,8 +33,23 @@ mem (size_t const vectorlength, size_t const nelems, T * const memptr)
clients_ (vectorlength, false),
num_clients_ (0)
{
+ DECLARE_CCTK_PARAMETERS;
if (memptr == NULL) {
const size_t nbytes = vectorlength * nelems * sizeof (T);
+ if (max_allowed_memory_MB
+ and (total_allocated_bytes + nbytes
+ > size_t(1000000) * max_allowed_memory_MB))
+ {
+ T Tdummy;
+ CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
+ "Refusing to allocate %.0f bytes (%.3f MB) of memory for type %s. %.0f bytes (%.3f MB) are currently allocated in %d objects. The parameter file specifies a maximum of %d MB",
+ double(nbytes), double(nbytes/1.0e6),
+ typestring(Tdummy),
+ double(total_allocated_bytes),
+ double(total_allocated_bytes/1.0e6),
+ int(total_allocated_objects),
+ int(max_allowed_memory_MB));
+ }
try {
storage_ = new T [vectorlength * nelems];
owns_storage_ = true;
@@ -41,8 +64,10 @@ mem (size_t const vectorlength, size_t const nelems, T * const memptr)
int(total_allocated_objects));
}
total_allocated_bytes += nbytes;
+ max_allocated_bytes = max (max_allocated_bytes, total_allocated_bytes);
}
++ total_allocated_objects;
+ max_allocated_objects = max (max_allocated_objects, total_allocated_objects);
}
template<typename T>
@@ -90,6 +115,28 @@ has_clients () const
+extern "C" void CarpetLib_printmemstats (CCTK_ARGUMENTS);
+
+void CarpetLib_printmemstats (CCTK_ARGUMENTS)
+{
+ DECLARE_CCTK_ARGUMENTS;
+ DECLARE_CCTK_PARAMETERS;
+ if (print_memstats_every
+ and cctk_iteration % print_memstats_every == 0)
+ {
+ cout << "Memory statistics from CarpetLib:" << endl
+ << " Current number of objects: " << total_allocated_objects << endl
+ << " Current allocated memory: "
+ << setprecision(3) << total_allocated_bytes / 1.0e6 << " MB" << endl
+ << " Maximum number of objects: " << max_allocated_objects << endl
+ << " Maximum allocated memory: "
+ << setprecision(3) << max_allocated_bytes / 1.0e6 << " MB" << endl
+ << endl;
+ }
+}
+
+
+
#define INSTANTIATE(T) \
template class mem<T>;