aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src
diff options
context:
space:
mode:
authorschnetter <>2004-04-08 09:16:00 +0000
committerschnetter <>2004-04-08 09:16:00 +0000
commitce4b54028e863fe0bb1517a022fe0369412454f8 (patch)
treee6bdc0fb9e99b58ee8f765d6db8e9ac64ba43e2e /Carpet/CarpetLib/src
parent0da2cef8eccd2a2a87db35c84627caf5420be8d2 (diff)
Abort with an error message if CarpetLib cannot allocate memory for
Abort with an error message if CarpetLib cannot allocate memory for grid functions. Also track how much memory is already used. darcs-hash:20040408091635-07bb3-3385e3472c428b3aa562d6ab76f2f6ca32b77f19.gz
Diffstat (limited to 'Carpet/CarpetLib/src')
-rw-r--r--Carpet/CarpetLib/src/data.cc50
-rw-r--r--Carpet/CarpetLib/src/data.hh9
2 files changed, 50 insertions, 9 deletions
diff --git a/Carpet/CarpetLib/src/data.cc b/Carpet/CarpetLib/src/data.cc
index 0d6b9e079..9dc5e42f7 100644
--- a/Carpet/CarpetLib/src/data.cc
+++ b/Carpet/CarpetLib/src/data.cc
@@ -1,4 +1,4 @@
-// $Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetLib/src/data.cc,v 1.49 2004/04/07 16:55:22 schnetter Exp $
+// $Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetLib/src/data.cc,v 1.50 2004/04/08 11:16:35 schnetter Exp $
#include <assert.h>
#include <limits.h>
@@ -26,13 +26,18 @@ using namespace std;
+// Track all allocated storage
+static size_t allocated_bytes = 0;
+
+
+
// Constructors
template<class T, int D>
data<T,D>::data (const int varindex_, const operator_type transport_operator_,
const int vectorlength, const int vectorindex,
data* const vectorleader)
: gdata<D>(varindex_, transport_operator_),
- _storage(0),
+ _storage(0), _allocated_bytes(0),
vectorlength(vectorlength), vectorindex(vectorindex),
vectorleader(vectorleader)
{
@@ -48,7 +53,7 @@ data<T,D>::data (const int varindex_, const operator_type transport_operator_,
data* const vectorleader,
const ibbox& extent_, const int proc_)
: gdata<D>(varindex_, transport_operator_),
- _storage(0),
+ _storage(0), _allocated_bytes(0),
vectorlength(vectorlength), vectorindex(vectorindex),
vectorleader(vectorleader)
{
@@ -79,6 +84,37 @@ data<T,D>* data<T,D>::make_typed (const int varindex_,
// Storage management
template<class T, int D>
+void data<T,D>::getmem (const size_t nelems)
+{
+ const size_t nbytes = nelems * sizeof(T);
+ try {
+ assert (this->_allocated_bytes == 0);
+ this->_storage = new T[nelems];
+ this->_allocated_bytes = nbytes;
+ } catch (...) {
+ T Tdummy;
+ CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
+ "Failed to allocate %z bytes (%.3f MB) of memory for type %s. %z bytes (%.3f MB) are currently allocated.",
+ nbytes, nbytes/1.0e6,
+ typestring(Tdummy),
+ allocated_bytes, allocated_bytes/1.0e6);
+ }
+ allocated_bytes += nbytes;
+}
+
+
+
+template<class T, int D>
+void data<T,D>::freemem ()
+{
+ delete [] _storage;
+ allocated_bytes -= this->_allocated_bytes;
+ this->_allocated_bytes = 0;
+}
+
+
+
+template<class T, int D>
void data<T,D>::allocate (const ibbox& extent_,
const int proc_,
void* const mem)
@@ -102,7 +138,7 @@ void data<T,D>::allocate (const ibbox& extent_,
if (this->_owns_storage) {
if (this->vectorindex == 0) {
assert (! this->vectorleader);
- this->_storage = new T[this->vectorlength * this->_size];
+ getmem (this->vectorlength * this->_size);
} else {
assert (this->vectorleader);
this->_storage = this->vectorleader->vectordata (this->vectorindex);
@@ -119,7 +155,7 @@ template<class T, int D>
void data<T,D>::free ()
{
if (this->_storage && this->_owns_storage && this->vectorindex==0) {
- delete [] _storage;
+ freemem ();
}
_storage = 0;
this->_has_storage = false;
@@ -191,7 +227,7 @@ void data<T,D>::change_processor_recv (const int newproc, void* const mem)
assert (!_storage);
this->_owns_storage = !mem;
if (this->_owns_storage) {
- _storage = new T[this->_size];
+ getmem (this->_size);
} else {
_storage = (T*)mem;
}
@@ -289,7 +325,7 @@ void data<T,D>::change_processor_wait (const int newproc, void* const mem)
this->wtime_isendwait += wtime2 - wtime1;
if (this->_owns_storage) {
- delete [] _storage;
+ freemem ();
}
_storage = 0;
diff --git a/Carpet/CarpetLib/src/data.hh b/Carpet/CarpetLib/src/data.hh
index b8c3e360d..b12b6c48d 100644
--- a/Carpet/CarpetLib/src/data.hh
+++ b/Carpet/CarpetLib/src/data.hh
@@ -1,4 +1,4 @@
-// $Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetLib/src/data.hh,v 1.19 2004/03/23 15:37:21 schnetter Exp $
+// $Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetLib/src/data.hh,v 1.20 2004/04/08 11:16:35 schnetter Exp $
#ifndef DATA_HH
#define DATA_HH
@@ -31,7 +31,8 @@ class data: public gdata<D>
// Fields
T* _storage; // the data (if located on this processor)
-
+ size_t _allocated_bytes;
+
int vectorlength;
int vectorindex;
data* vectorleader;
@@ -56,6 +57,10 @@ public:
const operator_type transport_operator) const;
// Storage management
+private:
+ void getmem (const size_t nelems);
+ void freemem ();
+public:
virtual void allocate (const ibbox& extent, const int proc,
void* const mem=0);
virtual void free ();