Realm
A distributed, event-based tasking library
Loading...
Searching...
No Matches
ib_memory.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// IB (Intermediate Buffer) Memory implementations for Realm
19
20#ifndef REALM_IB_MEMORY_H
21#define REALM_IB_MEMORY_H
22
23#include "realm/realm_config.h"
24
25#include "realm/mem_impl.h"
26
27#include <limits>
28
29namespace Realm {
30
31 // IBMemory::do_alloc rounds every allocation up to this alignment; planner-side
32 // size math must account for it so any plan the planner accepts is allocatable.
33 static constexpr size_t IB_ALLOC_ALIGNMENT = 256;
34
35 // round n up to the next multiple of mult (matches the semantics of the
36 // file-local roundup() in inst_impl.cc)
37 inline size_t ib_align_up(size_t n, size_t mult = IB_ALLOC_ALIGNMENT)
38 {
39 if(mult == 0) {
40 return n;
41 }
42 size_t leftover = n % mult;
43 if(leftover == 0) {
44 return n;
45 }
46 size_t extra = mult - leftover;
47 if(n > (std::numeric_limits<size_t>::max() - extra)) {
48 return std::numeric_limits<size_t>::max();
49 }
50 return n + extra;
51 }
52
53 // a simple memory used for intermediate buffers in dma system
55 public:
56 IBMemory(RuntimeImpl *_runtime_impl, Memory _me, size_t _size, MemoryKind _kind,
57 Memory::Kind _lowlevel_kind, void *prealloc_base, NetworkSegment *_segment);
58
59 virtual ~IBMemory();
60
61 // old-style allocation used by IB memories
62 virtual off_t alloc_bytes_local(size_t size);
63 void free_bytes_local(off_t offset, size_t size, TimeLimit work_until);
64
65 virtual void *get_direct_ptr(off_t offset, size_t size);
66
67 // not used by IB memories
69 bool need_alloc_result,
70 bool poisoned,
71 TimeLimit work_until);
72
73 virtual void release_storage_immediate(RegionInstanceImpl *inst, bool poisoned,
74 TimeLimit work_until);
75
76 virtual void get_bytes(off_t offset, void *dst, size_t size);
77 virtual void put_bytes(off_t offset, const void *src, size_t size);
78
79 // attempts to allocate one or more IBs - either all succeed or all fail
80 bool attempt_immediate_allocation(NodeID requestor, uintptr_t req_op, size_t count,
81 const size_t *sizes, off_t *offsets);
82
83 // enqueues a batch of PendingIBRequests to be satisfied as soon as possible
85
86 void free_multiple(size_t count, const off_t *offsets, const size_t *sizes,
87 TimeLimit work_until);
88
89 protected:
90 // these must be called with the mutex held
91 off_t do_alloc(size_t size);
92 void do_free(off_t offset, size_t size);
95
96 Mutex mutex; // protection for resizing vectors
97 std::map<off_t, off_t> free_blocks;
98 char *base;
101 };
102
103 // helper routine to free IB whether it is local or remote
104 void free_intermediate_buffer(Memory mem, off_t offset, size_t size,
105 TimeLimit work_until);
106
107 // active messages related to IB allocation/release
108
111 size_t size;
112 uintptr_t req_op;
113 unsigned req_index;
115
116 static void handle_message(NodeID sender, const RemoteIBAllocRequestSingle &args,
117 const void *data, size_t msglen, TimeLimit work_until);
118 };
119
123 uintptr_t req_op;
125
126 static void handle_message(NodeID sender, const RemoteIBAllocRequestMultiple &args,
127 const void *data, size_t msglen, TimeLimit work_until);
128 };
129
131 uintptr_t req_op;
132 unsigned req_index;
133 off_t offset;
134
135 static void handle_message(NodeID sender, const RemoteIBAllocResponseSingle &args,
136 const void *data, size_t msglen, TimeLimit work_until);
137 };
138
140 uintptr_t req_op;
142
143 static void handle_message(NodeID sender, const RemoteIBAllocResponseMultiple &args,
144 const void *data, size_t msglen, TimeLimit work_until);
145 };
146
149 size_t size;
150 off_t offset;
151
152 static void handle_message(NodeID sender, const RemoteIBReleaseSingle &args,
153 const void *data, size_t msglen, TimeLimit work_until);
154 };
155
157 unsigned count;
158
159 static void handle_message(NodeID sender, const RemoteIBReleaseMultiple &args,
160 const void *data, size_t msglen, TimeLimit work_until);
161 };
162
163}; // namespace Realm
164
165#endif
Definition ib_memory.h:54
off_t do_alloc(size_t size)
virtual void * get_direct_ptr(off_t offset, size_t size)
std::map< off_t, off_t > free_blocks
Definition ib_memory.h:97
void free_multiple(size_t count, const off_t *offsets, const size_t *sizes, TimeLimit work_until)
PendingIBRequests * satisfy_pending_reqs()
Mutex mutex
Definition ib_memory.h:96
char * base
Definition ib_memory.h:98
virtual AllocationResult allocate_storage_immediate(RegionInstanceImpl *inst, bool need_alloc_result, bool poisoned, TimeLimit work_until)
bool attempt_immediate_allocation(NodeID requestor, uintptr_t req_op, size_t count, const size_t *sizes, off_t *offsets)
PendingIBRequests ** ibreq_tail
Definition ib_memory.h:100
virtual off_t alloc_bytes_local(size_t size)
PendingIBRequests * ibreq_head
Definition ib_memory.h:99
void enqueue_requests(PendingIBRequests *reqs, TimeLimit work_until)
void forward_satisfied_reqs(PendingIBRequests *reqs, TimeLimit work_until)
virtual void put_bytes(off_t offset, const void *src, size_t size)
IBMemory(RuntimeImpl *_runtime_impl, Memory _me, size_t _size, MemoryKind _kind, Memory::Kind _lowlevel_kind, void *prealloc_base, NetworkSegment *_segment)
void free_bytes_local(off_t offset, size_t size, TimeLimit work_until)
virtual ~IBMemory()
void do_free(off_t offset, size_t size)
virtual void get_bytes(off_t offset, void *dst, size_t size)
virtual void release_storage_immediate(RegionInstanceImpl *inst, bool poisoned, TimeLimit work_until)
Definition mem_impl.h:50
MemoryKind
Definition mem_impl.h:53
AllocationResult
Definition mem_impl.h:89
Definition memory.h:33
Kind
Definition memory.h:59
Definition network.h:409
Definition mem_impl.h:220
Definition inst_impl.h:54
Definition runtime_impl.h:267
Definition timers.h:129
Definition mutex.h:223
#define REALM_INTERNAL_API_EXTERNAL_LINKAGE
Definition compiler_support.h:218
Definition activemsg.h:42
int NodeID
Definition nodeset.h:40
void free_intermediate_buffer(Memory mem, off_t offset, size_t size, TimeLimit work_until)
size_t ib_align_up(size_t n, size_t mult=IB_ALLOC_ALIGNMENT)
Definition ib_memory.h:37
Definition ib_memory.h:120
unsigned curr_index
Definition ib_memory.h:122
unsigned count
Definition ib_memory.h:122
static void handle_message(NodeID sender, const RemoteIBAllocRequestMultiple &args, const void *data, size_t msglen, TimeLimit work_until)
NodeID requestor
Definition ib_memory.h:121
bool immediate
Definition ib_memory.h:124
unsigned first_index
Definition ib_memory.h:122
uintptr_t req_op
Definition ib_memory.h:123
Definition ib_memory.h:109
bool immediate
Definition ib_memory.h:114
static void handle_message(NodeID sender, const RemoteIBAllocRequestSingle &args, const void *data, size_t msglen, TimeLimit work_until)
uintptr_t req_op
Definition ib_memory.h:112
Memory memory
Definition ib_memory.h:110
size_t size
Definition ib_memory.h:111
unsigned req_index
Definition ib_memory.h:113
Definition ib_memory.h:139
static void handle_message(NodeID sender, const RemoteIBAllocResponseMultiple &args, const void *data, size_t msglen, TimeLimit work_until)
uintptr_t req_op
Definition ib_memory.h:140
unsigned first_index
Definition ib_memory.h:141
unsigned count
Definition ib_memory.h:141
Definition ib_memory.h:130
static void handle_message(NodeID sender, const RemoteIBAllocResponseSingle &args, const void *data, size_t msglen, TimeLimit work_until)
off_t offset
Definition ib_memory.h:133
uintptr_t req_op
Definition ib_memory.h:131
unsigned req_index
Definition ib_memory.h:132
Definition ib_memory.h:156
unsigned count
Definition ib_memory.h:157
static void handle_message(NodeID sender, const RemoteIBReleaseMultiple &args, const void *data, size_t msglen, TimeLimit work_until)
Definition ib_memory.h:147
size_t size
Definition ib_memory.h:149
static void handle_message(NodeID sender, const RemoteIBReleaseSingle &args, const void *data, size_t msglen, TimeLimit work_until)
off_t offset
Definition ib_memory.h:150
Memory memory
Definition ib_memory.h:148
NodeID src
Definition ucp_internal.h:1