Realm
A distributed, event-based tasking library
Loading...
Searching...
No Matches
dynamic_table.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 Stanford University, NVIDIA Corporation
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18// monotonic dynamic lookup table for Realm
19
20#ifndef REALM_DYNAMIC_TABLE_H
21#define REALM_DYNAMIC_TABLE_H
22
23#include "realm/atomics.h"
24#include "realm/id.h"
25#include "realm/mutex.h"
26
27#include <functional>
28
29namespace Realm {
30
31 // we have a base type that's element-type agnostic
32 template <typename LT, typename IT>
34 public:
35 DynamicTableNodeBase(int _level, IT _first_index, IT _last_index);
36 virtual ~DynamicTableNodeBase(void);
37
38 int level;
40 LT lock;
41 // all nodes in a table are linked in a list for destruction
43 };
44
45 template <typename ET, size_t _SIZE, typename LT, typename IT>
46 struct DynamicTableNode : public DynamicTableNodeBase<LT, IT> {
47 public:
48 static const size_t SIZE = _SIZE;
49
50 DynamicTableNode(int _level, IT _first_index, IT _last_index);
51 virtual ~DynamicTableNode(void);
52
54 };
55
56 template <typename ALLOCATOR>
58
59 template <typename ET>
61 void construct(ET *storage, ID id, unsigned owner) const { storage->init(id, owner); }
62 };
63
64 template <typename ALLOCATOR>
66 public:
67 typedef typename ALLOCATOR::IT IT;
68 typedef typename ALLOCATOR::ET ET;
69 typedef typename ALLOCATOR::LT LT;
71 using Constructor = typename ALLOCATOR::Constructor;
72
76
77 size_t max_entries(void) const;
78 bool has_entry(IT index) const;
79 ET *lookup_entry(IT index, int owner, ET **free_list_head = 0,
80 ET **free_list_tail = 0);
81
82 void set_constructor(Constructor &_elem_ctor);
83
84 protected:
85 NodeBase *new_tree_node(int level, IT first_index, IT last_index, int owner,
86 ET **free_list_head, ET **free_list_tail);
87
88 // lock protects _changes_ to 'root', but not access to it
90 // encode level of root directly in value - saves an extra memory load
91 // per level
93 static intptr_t encode_root_and_level(NodeBase *root, int level);
94 static NodeBase *extract_root(intptr_t rlval);
95 static int extract_level(intptr_t rlval);
96
97 // all nodes in a table are linked in a list for destruction
100
102 };
103
104 template <typename ALLOCATOR>
106 public:
107 typedef typename ALLOCATOR::IT IT;
108 typedef typename ALLOCATOR::ET ET;
109 typedef typename ALLOCATOR::LT LT;
110
112 DynamicTableFreeList<ALLOCATOR> *_parent_list = nullptr);
113
114 void push_front(ET *entry);
115 void push_front(ET *head, ET *tail);
117 ET *pop_front(void);
118
120 void free_entry(ET *entry);
121
122 // allocates a range of IDs that can be given to a remote node for remote allocation
123 // these entries do not go on the local free list unless they are deleted after being
124 // used
125 void alloc_range(int requested, IT &first_id, IT &last_id);
126
128 // Free list from which we will coordinate reservation of IDs from
130 int owner;
134 };
135
136 template <typename _ET, size_t _INNER_BITS, size_t _LEAF_BITS,
137 typename CONSTRUCTOR = DefaultElementConstructor<_ET>>
139 public:
140 typedef _ET ET;
141 static const size_t INNER_BITS = _INNER_BITS;
142 static const size_t LEAF_BITS = _LEAF_BITS;
143
144 using Constructor = CONSTRUCTOR;
145 typedef Mutex LT;
146 typedef ID::IDType IT;
148 IT>
151 typedef DynamicTableFreeList<
154
155 static std::vector<FreeList *> &get_registered_freelists(Mutex *&lock);
156
157 static void register_freelist(FreeList *free_list);
158
159 static ET *steal_freelist_element(FreeList *requestor = nullptr);
160
161 static LEAF_TYPE *new_leaf_node(IT first_index, IT last_index, int owner,
162 ET **free_list_head, ET **free_list_tail,
163 const Constructor &elem_ctor_obj);
164 };
165
166}; // namespace Realm
167
168#include "realm/dynamic_table.inl"
169
170#endif // ifndef REALM_DYNAMIC_TABLE_H
Definition dynamic_table.h:138
Mutex LT
Definition dynamic_table.h:145
static ET * steal_freelist_element(FreeList *requestor=nullptr)
static std::vector< FreeList * > & get_registered_freelists(Mutex *&lock)
static void register_freelist(FreeList *free_list)
DynamicTableNode< atomic< DynamicTableNodeBase< LT, IT > * >, 1<< INNER_BITS, LT, IT > INNER_TYPE
Definition dynamic_table.h:149
static const size_t INNER_BITS
Definition dynamic_table.h:141
DynamicTableFreeList< DynamicTableAllocator< ET, _INNER_BITS, _LEAF_BITS, Constructor > > FreeList
Definition dynamic_table.h:153
_ET ET
Definition dynamic_table.h:140
static LEAF_TYPE * new_leaf_node(IT first_index, IT last_index, int owner, ET **free_list_head, ET **free_list_tail, const Constructor &elem_ctor_obj)
DynamicTableNode< ET, 1<< LEAF_BITS, LT, IT > LEAF_TYPE
Definition dynamic_table.h:150
CONSTRUCTOR Constructor
Definition dynamic_table.h:144
ID::IDType IT
Definition dynamic_table.h:146
static const size_t LEAF_BITS
Definition dynamic_table.h:142
Definition dynamic_table.h:105
int owner
Definition dynamic_table.h:130
ALLOCATOR::IT IT
Definition dynamic_table.h:107
IT next_alloc
Definition dynamic_table.h:133
LT lock
Definition dynamic_table.h:131
DynamicTable< ALLOCATOR > & table
Definition dynamic_table.h:127
ALLOCATOR::ET ET
Definition dynamic_table.h:108
ALLOCATOR::LT LT
Definition dynamic_table.h:109
atomic< ET * > first_free
Definition dynamic_table.h:132
void alloc_range(int requested, IT &first_id, IT &last_id)
void free_entry(ET *entry)
DynamicTableFreeList(DynamicTable< ALLOCATOR > &_table, int _owner, DynamicTableFreeList< ALLOCATOR > *_parent_list=nullptr)
DynamicTableFreeList< ALLOCATOR > * parent_list
Definition dynamic_table.h:129
void push_front(ET *head, ET *tail)
void push_front(ET *entry)
Definition dynamic_table.h:65
ALLOCATOR::LT LT
Definition dynamic_table.h:69
typename ALLOCATOR::Constructor Constructor
Definition dynamic_table.h:71
size_t max_entries(void) const
Constructor elem_ctor
Definition dynamic_table.h:99
ALLOCATOR::ET ET
Definition dynamic_table.h:68
static intptr_t encode_root_and_level(NodeBase *root, int level)
ET * lookup_entry(IT index, int owner, ET **free_list_head=0, ET **free_list_tail=0)
static int extract_level(intptr_t rlval)
DynamicTableNodeBase< LT, IT > NodeBase
Definition dynamic_table.h:70
atomic< NodeBase * > first_alloced_node
Definition dynamic_table.h:98
void set_constructor(Constructor &_elem_ctor)
static NodeBase * extract_root(intptr_t rlval)
DynamicTable(Constructor elem_ctor)
ALLOCATOR::IT IT
Definition dynamic_table.h:67
bool has_entry(IT index) const
void prepend_alloced_node(NodeBase *new_node)
atomic< intptr_t > root_and_level
Definition dynamic_table.h:92
LT lock
Definition dynamic_table.h:89
NodeBase * new_tree_node(int level, IT first_index, IT last_index, int owner, ET **free_list_head, ET **free_list_tail)
Definition id.h:30
::realm_id_t IDType
Definition id.h:32
Definition mutex.h:223
Definition atomics.h:31
Definition activemsg.h:38
Definition dynamic_table.h:60
void construct(ET *storage, ID id, unsigned owner) const
Definition dynamic_table.h:61
Definition dynamic_table.h:33
virtual ~DynamicTableNodeBase(void)
LT lock
Definition dynamic_table.h:40
IT first_index
Definition dynamic_table.h:39
IT last_index
Definition dynamic_table.h:39
DynamicTableNodeBase(int _level, IT _first_index, IT _last_index)
int level
Definition dynamic_table.h:38
DynamicTableNodeBase< LT, IT > * next_alloced_node
Definition dynamic_table.h:42
Definition dynamic_table.h:46
virtual ~DynamicTableNode(void)
DynamicTableNode(int _level, IT _first_index, IT _last_index)
ET elems[SIZE]
Definition dynamic_table.h:53
static const size_t SIZE
Definition dynamic_table.h:48