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.hpp>
14 
15 namespace rapidsmpf {
16 
17 class BufferResource;
18 
25 class SpillManager {
26  public:
33  using SpillFunction = std::function<std::size_t(std::size_t)>;
34 
38  using SpillFunctionID = std::size_t;
39 
50  BufferResource* br, std::optional<Duration> periodic_spill_check = std::nullopt
51  );
52 
60 
71  SpillFunctionID add_spill_function(SpillFunction spill_function, int priority);
72 
83 
95  std::size_t spill(std::size_t amount);
96 
113  std::size_t spill_to_make_headroom(std::int64_t headroom = 0);
114 
115  private:
116  mutable std::mutex mutex_;
117  BufferResource* br_;
118  std::size_t spill_function_id_counter_{0};
119  std::map<SpillFunctionID, SpillFunction> spill_functions_;
120  std::multimap<int, SpillFunctionID, std::greater<>> spill_function_priorities_;
121  std::optional<detail::PausableThreadLoop> periodic_spill_thread_;
122 };
123 
124 
125 } // namespace rapidsmpf
Class managing buffer resources.
Definition: resource.hpp:133
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.