Realm
A distributed, event-based tasking library
Loading...
Searching...
No Matches
lowlevel_dma.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#ifndef LOWLEVEL_DMA_H
19#define LOWLEVEL_DMA_H
20
21#include "realm/network.h"
22#include "realm/id.h"
23#include "realm/memory.h"
24#include "realm/redop.h"
25#include "realm/instance.h"
26#include "realm/event.h"
27#include "realm/runtime_impl.h"
28#include "realm/inst_impl.h"
30#include "realm/circ_queue.h"
31
32namespace Realm {
33
34 namespace Config {
35 // the size of the LRU of the cache
36 extern size_t path_cache_lru_size;
37 }; // namespace Config
38
39 extern void init_dma_handler(void);
40
42
43 extern void stop_dma_system(void);
44
45 extern void init_path_cache(void);
46
47 extern void finalize_path_cache(void);
48
49 struct MemPathInfo {
50 std::vector<Memory> path;
51 std::vector<Channel *> xd_channels;
52 // std::vector<XferDesKind> xd_kinds;
53 // std::vector<NodeID> xd_target_nodes;
54 friend std::ostream &operator<<(std::ostream &out, const MemPathInfo &info);
55 };
56
57 // The LRU is implemented using a vector. Each item in the vector
58 // has a atomic timestamp to track the last accessed's timestamp.
59 // In this case, we allow multiple threads calling hit with a rdlock.
60 class PathLRU {
61 // We use parameters of find_fastest_path function
62 // except src_mem and dst_mem as the LRU key
63 public:
64 class LRUKey {
65 public:
66 // the timestamp is used to record when the item is accessed (miss/hit)
68
69 private:
70 CustomSerdezID serdez_id;
71 ReductionOpID redop_id;
72 size_t total_bytes;
73 std::vector<size_t> src_frags;
74 std::vector<size_t> dst_frags;
75
76 public:
77 LRUKey(const CustomSerdezID serdez_id, const ReductionOpID redop_id,
78 const size_t total_bytes, const std::vector<size_t> *src_frags,
79 const std::vector<size_t> *dst_frags);
80
81 // 2 LRUKeys are equal only if all private members are the same
82 bool operator==(const LRUKey &rhs) const;
83 friend std::ostream &operator<<(std::ostream &out, const LRUKey &lru_key);
84 };
85
86 typedef std::vector<std::pair<LRUKey, MemPathInfo>>::iterator PathLRUIterator;
87
88 public:
90 size_t max_size;
91
92 private:
93 // It is used to record the current timestamp,
94 // which is increated by 1 in miss/hit.
95 atomic<unsigned long> timestamp;
96 std::vector<std::pair<LRUKey, MemPathInfo>> item_list;
97
98 public:
99 PathLRU(size_t size);
100
101 // assume key is NOT existed in the item_list
102 void miss(LRUKey &key, const MemPathInfo &path);
103
104 // assume key is existed in the item_list before calling hit
106
109 };
110
111 typedef std::map<std::pair<realm_id_t, realm_id_t>, PathLRU *> PathCache;
112
113 bool find_shortest_path(const Node *nodes_info, Memory src_mem, Memory dst_mem,
114 CustomSerdezID serdez_id, ReductionOpID redop_id,
115 MemPathInfo &info, bool skip_final_memcpy = false);
116
117 // Returns true if successfully found a DMA channel that has a minimum
118 // transfer cost from source to destination memories.
120 const Node *nodes_info, ChannelCopyInfo channel_copy_info,
121 CustomSerdezID src_serdez_id, CustomSerdezID dst_serdez_id, ReductionOpID redop_id,
122 size_t total_bytes, const std::vector<size_t> *src_frags,
123 const std::vector<size_t> *dst_frags, uint64_t &best_cost, Channel *&best_channel,
124 XferDesKind &best_kind);
125
126 bool find_fastest_path(const Node *nodes_info, PathCache &path_cache,
127 ChannelCopyInfo channel_copy_info, CustomSerdezID serdez_id,
128 ReductionOpID redop_id, size_t total_bytes,
129 const std::vector<size_t> *src_frags,
130 const std::vector<size_t> *dst_frags, MemPathInfo &info,
131 bool skip_final_memcpy = false);
132
134 public:
135 AsyncFileIOContext(int _max_depth);
137
138 void enqueue_write(int fd, size_t offset, size_t bytes, const void *buffer,
139 Request *req = NULL);
140 void enqueue_read(int fd, size_t offset, size_t bytes, void *buffer,
141 Request *req = NULL);
143
144 bool empty(void);
145 long available(void);
146
148
149 virtual bool do_work(TimeLimit work_until);
150
152 public:
153 virtual ~AIOOperation(void) {}
154 virtual void launch(void) = 0;
155 virtual bool check_completion(void) = 0;
157 void *req;
158 };
159
160 protected:
161 void make_progress(void);
162
164 std::deque<AIOOperation *> launched_operations, pending_operations;
166#ifdef REALM_USE_KERNEL_AIO
167 aio_context_t aio_ctx;
168#endif
169 };
170
172 public:
173 WrappingFIFOIterator(size_t _base, size_t _size);
174
175 template <typename S>
176 static TransferIterator *deserialize_new(S &deserializer);
177
178 virtual void reset(void);
179 virtual bool done(void);
180
181 virtual size_t get_base_offset(void) const;
182
183 virtual size_t step(size_t max_bytes, AddressInfo &info, unsigned flags,
184 bool tentative = false);
185 virtual size_t step_custom(size_t max_bytes, AddressInfoCustom &info,
186 bool tentative = false);
187 virtual void confirm_step(void);
188 virtual void cancel_step(void);
189
190 virtual bool get_addresses(AddressList &addrlist,
191 const InstanceLayoutPieceBase *&nonaffine);
192
196
197 template <typename S>
198 bool serialize(S &serializer) const;
199
200 protected:
203 };
204
205}; // namespace Realm
206
207#endif
Definition address_list.h:55
Definition lowlevel_dma.h:151
virtual ~AIOOperation(void)
Definition lowlevel_dma.h:153
bool completed
Definition lowlevel_dma.h:156
void * req
Definition lowlevel_dma.h:157
Definition lowlevel_dma.h:133
void enqueue_read(int fd, size_t offset, size_t bytes, void *buffer, Request *req=NULL)
void enqueue_fence(Operation *req)
std::deque< AIOOperation * > pending_operations
Definition lowlevel_dma.h:164
int max_depth
Definition lowlevel_dma.h:163
static AsyncFileIOContext * get_singleton(void)
std::deque< AIOOperation * > launched_operations
Definition lowlevel_dma.h:164
AsyncFileIOContext(int _max_depth)
Mutex mutex
Definition lowlevel_dma.h:165
void enqueue_write(int fd, size_t offset, size_t bytes, const void *buffer, Request *req=NULL)
virtual bool do_work(TimeLimit work_until)
Definition bgwork.h:129
Definition bgwork.h:36
Definition channel.h:713
Definition inst_layout.h:266
Definition memory.h:33
Definition operation.h:32
Definition lowlevel_dma.h:64
bool operator==(const LRUKey &rhs) const
atomic< unsigned long > timestamp
Definition lowlevel_dma.h:67
LRUKey(const CustomSerdezID serdez_id, const ReductionOpID redop_id, const size_t total_bytes, const std::vector< size_t > *src_frags, const std::vector< size_t > *dst_frags)
friend std::ostream & operator<<(std::ostream &out, const LRUKey &lru_key)
Definition lowlevel_dma.h:60
void miss(LRUKey &key, const MemPathInfo &path)
PathLRU(size_t size)
PathLRUIterator end(void)
PathLRUIterator find(const LRUKey &key)
void hit(PathLRUIterator it)
RWLock rwlock
Definition lowlevel_dma.h:89
size_t max_size
Definition lowlevel_dma.h:90
std::vector< std::pair< LRUKey, MemPathInfo > >::iterator PathLRUIterator
Definition lowlevel_dma.h:86
Definition mutex.h:398
Definition channel.h:103
Definition timers.h:129
Definition transfer.h:41
Definition mutex.h:223
Definition lowlevel_dma.h:171
bool serialize(S &serializer) const
size_t offset
Definition lowlevel_dma.h:201
virtual bool done(void)
virtual size_t step(size_t max_bytes, AddressInfo &info, unsigned flags, bool tentative=false)
virtual void confirm_step(void)
size_t base
Definition lowlevel_dma.h:201
static Serialization::PolymorphicSerdezSubclass< TransferIterator, WrappingFIFOIterator > serdez_subclass
Definition lowlevel_dma.h:195
bool tentative_valid
Definition lowlevel_dma.h:202
virtual bool get_addresses(AddressList &addrlist, const InstanceLayoutPieceBase *&nonaffine)
WrappingFIFOIterator(size_t _base, size_t _size)
virtual void cancel_step(void)
virtual size_t step_custom(size_t max_bytes, AddressInfoCustom &info, bool tentative=false)
static TransferIterator * deserialize_new(S &deserializer)
size_t size
Definition lowlevel_dma.h:201
virtual size_t get_base_offset(void) const
virtual void reset(void)
size_t prev_offset
Definition lowlevel_dma.h:201
Definition atomics.h:31
size_t path_cache_lru_size
Definition activemsg.h:38
void finalize_path_cache(void)
std::map< std::pair< realm_id_t, realm_id_t >, PathLRU * > PathCache
Definition lowlevel_dma.h:111
void init_dma_handler(void)
void stop_dma_system(void)
void start_dma_system(BackgroundWorkManager *bgwork)
void init_path_cache(void)
XferDesKind
Definition channel.h:85
int CustomSerdezID
Definition custom_serdez.h:148
bool find_best_channel_for_memories(const Node *nodes_info, ChannelCopyInfo channel_copy_info, CustomSerdezID src_serdez_id, CustomSerdezID dst_serdez_id, ReductionOpID redop_id, size_t total_bytes, const std::vector< size_t > *src_frags, const std::vector< size_t > *dst_frags, uint64_t &best_cost, Channel *&best_channel, XferDesKind &best_kind)
bool find_shortest_path(const Node *nodes_info, Memory src_mem, Memory dst_mem, CustomSerdezID serdez_id, ReductionOpID redop_id, MemPathInfo &info, bool skip_final_memcpy=false)
bool find_fastest_path(const Node *nodes_info, PathCache &path_cache, ChannelCopyInfo channel_copy_info, CustomSerdezID serdez_id, ReductionOpID redop_id, size_t total_bytes, const std::vector< size_t > *src_frags, const std::vector< size_t > *dst_frags, MemPathInfo &info, bool skip_final_memcpy=false)
::realm_reduction_op_id_t ReductionOpID
Definition event.h:38
Definition channel.h:684
Definition lowlevel_dma.h:49
std::vector< Channel * > xd_channels
Definition lowlevel_dma.h:51
std::vector< Memory > path
Definition lowlevel_dma.h:50
friend std::ostream & operator<<(std::ostream &out, const MemPathInfo &info)
Definition runtime_impl.h:88
Definition transfer.h:78