NumericAL
A header-only C++20 lexer built on REAL
Loading...
Searching...
No Matches
memory.hpp
Go to the documentation of this file.
1
5#ifndef NUMERICAL_MEMORY_HPP
6#define NUMERICAL_MEMORY_HPP
7
8#include <chrono>
9
10#if defined(__GLIBC__)
11# include <malloc.h>
12#elif defined(__APPLE__)
13# include <malloc/malloc.h>
14#elif defined(_WIN32)
15# include <malloc.h>
16#endif
17
18#include "buffer.hpp"
19#include "parallel.hpp"
20
21namespace numerical {
22
23 namespace detail {
24
27 inline void release_and_trim()
28 {
30#if defined(__GLIBC__)
31 static_cast<void>(malloc_trim(0));
32#endif
33 }
34
38 {
40#if defined(__GLIBC__)
41 // glibc sets its allocator up on the first call that needs it, unguarded against a second at once: the workers
42 // trim together below, so the calling thread trims first (under a sanitizer, which replaces malloc, this may be
43 // that first call; two at once read the arenas half set, the null page).
44 static_cast<void>(malloc_trim(0));
45#endif
46 if (pool != nullptr) {
47 static_cast<void>(pool->release_on_workers(&release_and_trim, std::chrono::seconds {1}));
48 }
49#if defined(__linux__)
50 kept_huge_blocks::shared().clear();
51#endif
53 // The allocator's own free pages to the system too: glibc keeps what its arenas freed (a session of order 1500
54 // kept 19 MB after the rest went, none after this), macOS's zones likewise.
55#if defined(__GLIBC__)
56 static_cast<void>(malloc_trim(0));
57#elif defined(__APPLE__)
58 static_cast<void>(malloc_zone_pressure_relief(nullptr, 0));
59#elif defined(_WIN32)
60 static_cast<void>(_heapmin());
61#endif
62 }
63 } // namespace detail
64
85} // namespace numerical
86
87#endif // NUMERICAL_MEMORY_HPP
numerical::buffer, the storage of the library's vectors, matrices and tensors: a std::vector whose el...
static kept_blocks & shared()
The process's blocks, never destroyed: a buffer of static storage may be freed after any destructor.
Definition buffer.hpp:107
void clear() noexcept
Frees every block kept (numerical::release_memory).
Definition buffer.hpp:182
The threads every kernel shares, started once: a parallel_for costs a wake-up, not a thread start.
Definition parallel.hpp:410
static thread_pool * started() noexcept
The pool if shared has started it, else nullptr (asking starts nothing).
Definition parallel.hpp:455
void release_thread_memory()
Empties every workspace the calling thread keeps between calls: its thread_releases,...
Definition buffer.hpp:77
void release_memory_on(thread_pool *pool)
numerical::release_memory with pool's workers, none where it is nullptr (a pool never started holds n...
Definition memory.hpp:37
void release_and_trim()
release_thread_memory, then the allocator's free pages to the system, on the calling thread: glibc ke...
Definition memory.hpp:27
The NumericAL public API.
Definition amx.hpp:44
void release_memory()
Frees the workspaces NumericAL keeps between calls, as oneMKL's mkl_free_buffers: each thread keeps t...
Definition memory.hpp:81
A minimal std::thread parallel-for for the hot kernels.