From: Wolfgang Bangerth Date: Sun, 3 Apr 2016 15:05:27 +0000 (-0500) Subject: Add memory_consumption() for std_cxx11::array objects. X-Git-Tag: v8.5.0-rc1~1136^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d17f5f010260bc6dbfc9325f65c3ea77a11d0e3;p=dealii.git Add memory_consumption() for std_cxx11::array objects. --- diff --git a/include/deal.II/base/memory_consumption.h b/include/deal.II/base/memory_consumption.h index 58e3aba37b..d59a50fd81 100644 --- a/include/deal.II/base/memory_consumption.h +++ b/include/deal.II/base/memory_consumption.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -145,7 +146,7 @@ namespace MemoryConsumption /** * Determine the amount of memory in bytes consumed by a - * std::vector of elements of type T by recursively + * std::vector of elements of type T by * calling memory_consumption() for each entry. * * This function loops over all entries of the vector and determines their @@ -174,6 +175,30 @@ namespace MemoryConsumption inline std::size_t memory_consumption (const std::vector &v); + /** + * Determine the amount of memory in bytes consumed by a + * std_cxx11::array of N elements of type T by + * calling memory_consumption() for each entry. + * + * This function loops over all entries of the array and determines their + * sizes using memory_consumption() for each v[i]. If the entries + * are of constant size, there might be another global function + * memory_consumption() for this data type or if there is a member function + * of that class of that names that returns a constant value and the + * compiler will unroll this loop so that the operation is fast. If the size + * of the data elements is variable, for example if they do memory + * allocation themselves, then the operation will necessarily be more + * expensive. + * + * Using the algorithm, in particular the loop over all elements, it is + * possible to also compute the memory consumption of arrays of vectors, + * arrays of strings, etc, where the individual elements may have vastly + * different sizes. + */ + template + inline + std::size_t memory_consumption (const std_cxx11::array &v); + /** * Estimate the amount of memory (in bytes) occupied by a C-style array. * Since in this library we do not usually store simple data elements like @@ -316,6 +341,25 @@ namespace MemoryConsumption + template + std::size_t memory_consumption (const std_cxx11::array &v) + { + // shortcut for types that do not allocate memory themselves + if (std_cxx11::is_fundamental::value || std_cxx11::is_pointer::value) + { + return sizeof(v); + } + else + { + std::size_t mem = 0; + for (std::size_t i=0; i std::size_t memory_consumption (const T (&v)[N]) {