omega_h
Reliable mesh adaptation
Omega_h_pool_kokkos.hpp
1 //
2 // Created by Matthew McCall on 6/12/23.
3 //
4 // Derived from
5 // https://github.com/matthew-mccall/kokkos-memory-pool/blob/8e0a45a5b5d6823976d867e20d742314b6a1d268/src/MemoryPool/MemoryPool.hpp
6 
7 #ifndef OMEGA_H_KOKKOS_POOL_HPP
8 #define OMEGA_H_KOKKOS_POOL_HPP
9 
10 #include <cstddef>
11 #include <list>
12 #include <map>
13 #include <set>
14 #include <utility>
15 
16 #include "Kokkos_Core.hpp"
17 #include "Omega_h_kokkos.hpp"
18 
19 namespace Omega_h {
20 
21 constexpr size_t DEFAULT_BYTES_PER_CHUNK = 1000;
22 
23 // Total initial size in bytes will subsequently be DEFAULT_NUMBER_OF_CHUNKS * DEFAULT_BYTES_PER_CHUNK
24 constexpr size_t DEFAULT_NUMBER_OF_CHUNKS = 700'000;
25 
26 // If the largest StaticKokkosPool is 2Kb, then the next StaticKokkosPool will be at least 4kB resulting in a total pool size of 2Kb + 4Kb = 6Kb
27 constexpr size_t DEFAULT_GROWTH_CONSTANT = 2;
28 
29 using IndexPair = std::pair<size_t, size_t>;
30 
32  public:
33  using is_transparent =
34  void; // https://www.fluentcpp.com/2017/06/09/search-set-another-type-key/
35 
36  auto operator()(IndexPair lhs, IndexPair rhs) const -> bool;
37  auto operator()(IndexPair lhs, size_t rhs) const -> bool;
38  auto operator()(size_t lhs, IndexPair rhs) const -> bool;
39 };
40 
41 using MultiSetBySizeT = std::multiset<IndexPair, CompareFreeIndices>;
42 using SetByIndexT = std::set<IndexPair>;
43 
50  public:
51  StaticKokkosPool(size_t numChunks, size_t bytesPerChunk);
52 
53  auto allocate(size_t n) -> void*;
54  void deallocate(void* data);
55 
56  auto getNumAllocations() const -> unsigned;
57  auto getNumFreeChunks() const -> unsigned;
58  auto getNumAllocatedChunks() const -> unsigned;
59  auto getNumChunks() const -> unsigned;
60  auto getNumFreeFragments() const -> unsigned;
61 
62  static auto getRequiredChunks(size_t n, size_t bytesPerChunk) -> size_t;
63 
65 
66  private:
67  auto insertIntoSets(IndexPair indices)
68  -> std::pair<MultiSetBySizeT::iterator, SetByIndexT::iterator>;
69  void removeFromSets(IndexPair indices);
70 
71  const size_t numberOfChunks;
72  const size_t chunkSize;
73 
74  void* pool;
75  MultiSetBySizeT freeSetBySize; // For finding free chunks logarithmically
76  SetByIndexT freeSetByIndex; // For merging adjacent free chunks
77  std::map<void*, IndexPair> allocations;
78 };
79 
85 class KokkosPool {
86  public:
87  explicit KokkosPool(size_t bytesPerChunks);
88 
89  auto allocate(size_t n) -> void*;
90  void deallocate(void* data);
91 
92  template <typename DataType>
93  auto allocateView(size_t n) -> View<DataType*> {
94  return View<DataType*>(
95  reinterpret_cast<DataType*>(allocate(n * sizeof(DataType))), n);
96  }
97 
98  template <typename DataType>
99  void deallocateView(View<DataType*> view) {
100  deallocate(reinterpret_cast<void*>(view.data()));
101  }
102 
103  auto getNumAllocations() const -> unsigned;
104  auto getNumFreeChunks() const -> unsigned;
105  auto getNumAllocatedChunks() const -> unsigned;
106  auto getNumChunks() const -> unsigned;
107  auto getNumFreeFragments() const -> unsigned;
108  auto getChunkSize() const -> size_t;
109 
110  static auto getGlobalPool() -> KokkosPool&;
111  static auto destroyGlobalPool() -> void;
112 
113  private:
114  using PoolListT = std::list<StaticKokkosPool>;
115 
116  size_t chunkSize;
117  PoolListT pools;
118  std::map<void*, PoolListT::iterator> allocations;
119 };
120 
121 } // namespace Omega_h
122 
123 #endif // OMEGA_H_KOKKOS_POOL_HPP
Definition: Omega_h_pool_kokkos.hpp:31
A memory pool for allocating and deallocating chunks of memory.
Definition: Omega_h_pool_kokkos.hpp:85
A memory pool for allocating and deallocating chunks of memory.
Definition: Omega_h_pool_kokkos.hpp:49
Definition: amr_mpi_test.cpp:6