Skip to content

Commit 133f7a7

Browse files
committed
Start on memory utility
1 parent 228cb26 commit 133f7a7

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

cpp/dolfinx/common/memory.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (C) 2025 Paul T. Kühner
2+
//
3+
// This file is part of DOLFINx (https://www.fenicsproject.org)
4+
//
5+
// SPDX-License-Identifier: LGPL-3.0-or-later
6+
7+
#include <cstddef>
8+
#include <cstdint>
9+
#include <vector>
10+
11+
namespace dolfinx::common
12+
{
13+
14+
namespace impl
15+
{
16+
17+
template <typename T>
18+
std::size_t memory(const T& obj)
19+
{
20+
static_assert(false, "Memory usage not supported for provided type.");
21+
}
22+
23+
template <typename T>
24+
std::size_t memory(const std::vector<T>& vec)
25+
{
26+
using value_type = typename std::vector<T>::value_type;
27+
28+
std::size_t size_type = sizeof(vec);
29+
std::size_t size_data = vec.capacity() * sizeof(value_type);
30+
return size_type + size_data;
31+
}
32+
33+
} // namespace impl
34+
35+
constexpr std::int64_t byte = 1;
36+
constexpr std::int64_t kilobyte = 1024;
37+
constexpr std::int64_t megabyte = 1'048'576;
38+
constexpr std::int64_t gigabyte = 1'073'741'824;
39+
constexpr std::int64_t terabyte = 1'099'511'627'776;
40+
41+
template <typename T>
42+
double memory(const T& obj, std::int64_t bytes_per_unit = byte)
43+
{
44+
std::size_t bytes = impl::memory(obj);
45+
return static_cast<double>(bytes) / bytes_per_unit;
46+
}
47+
48+
} // namespace dolfinx::common

cpp/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ add_executable(
3838
common/CIFailure.cpp
3939
common/sub_systems_manager.cpp
4040
common/index_map.cpp
41+
common/memory.cpp
4142
common/sort.cpp
4243
fem/form.cpp
4344
fem/functionspace.cpp

cpp/test/common/memory.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (C) 2025 Paul T. Kühner
2+
//
3+
// This file is part of DOLFINx (https://www.fenicsproject.org)
4+
//
5+
// SPDX-License-Identifier: LGPL-3.0-or-later
6+
7+
#include <catch2/catch_approx.hpp>
8+
#include <catch2/catch_template_test_macros.hpp>
9+
#include <catch2/catch_test_macros.hpp>
10+
11+
#include <complex>
12+
#include <vector>
13+
14+
#include "dolfinx/common/memory.h"
15+
16+
using namespace dolfinx::common;
17+
18+
TEMPLATE_TEST_CASE("memory-vector", "[memory]", std::int16_t, std::int32_t,
19+
std::int64_t, std::uint16_t, std::uint32_t, std::uint64_t,
20+
float, double, std::complex<float>, std::complex<double>)
21+
{
22+
std::vector<TestType> v;
23+
v.reserve(10);
24+
25+
std::size_t bytes = sizeof(std::vector<TestType>) + 10 * sizeof(TestType);
26+
27+
for (auto unit : {byte, kilobyte, megabyte, gigabyte, terabyte})
28+
CHECK(memory(v, unit) == Catch::Approx(static_cast<double>(bytes) / unit));
29+
}

0 commit comments

Comments
 (0)