spill_manager.hpp
1 
6 #pragma once
7 
8 #include <map>
9 #include <mutex>
10 #include <optional>
11 
12 #include <rapidsmpf/pausable_thread_loop.hpp>
13 #include <rapidsmpf/utils/misc.hpp>
14 
15 namespace rapidsmpf {
16 
17 class BufferResource;
18 
25 class SpillManager {
26  public:
43  using SpillFunction = std::function<std::size_t(std::size_t)>;
44 
48  using SpillFunctionID = std::size_t;
49 
60  BufferResource* br, std::optional<Duration> periodic_spill_check = std::nullopt
61  );
62 
70 
81  SpillFunctionID add_spill_function(SpillFunction spill_function, int priority);
82 
93 
105  std::size_t spill(std::size_t amount);
106 
123  std::size_t spill_to_make_headroom(std::int64_t headroom = 0);
124 
125  private:
126  mutable std::mutex mutex_;
127  BufferResource* br_;
128  std::size_t spill_function_id_counter_{0};
129  std::map<SpillFunctionID, SpillFunction> spill_functions_;
130  std::multimap<int, SpillFunctionID, std::greater<>> spill_function_priorities_;
131  std::optional<detail::PausableThreadLoop> periodic_spill_thread_;
132 };
133 
134 
135 } // namespace rapidsmpf
Class managing buffer resources.
Manages memory spilling to free up device memory when needed.
std::size_t SpillFunctionID
Represents a unique identifier for a registered spill function.
std::size_t spill_to_make_headroom(std::int64_t headroom=0)
Attempts to free up memory by spilling data until the requested headroom is available.
~SpillManager()
Destructor for SpillManager.
SpillManager(BufferResource *br, std::optional< Duration > periodic_spill_check=std::nullopt)
Constructs a SpillManager instance.
SpillFunctionID add_spill_function(SpillFunction spill_function, int priority)
Adds a spill function with a given priority to the spill manager.
std::function< std::size_t(std::size_t)> SpillFunction
Spill function type.
std::size_t spill(std::size_t amount)
Initiates spilling to free up a specified amount of memory.
void remove_spill_function(SpillFunctionID fid)
Removes a spill function from the spill manager.
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:14