• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

celerity / celerity-runtime / 12009901531

25 Nov 2024 12:20PM UTC coverage: 94.92% (+0.009%) from 94.911%
12009901531

push

github

fknorr
Add missing includes and consistently order them

We can't add the misc-include-cleaner lint because it causes too many
false positives with "interface headers" such as sycl.hpp.

3190 of 3626 branches covered (87.98%)

Branch coverage included in aggregate %.

7049 of 7161 relevant lines covered (98.44%)

1242183.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.1
/src/live_executor.cc
1
#include "live_executor.h"
2

3
#include "affinity.h"
4
#include "backend/backend.h"
5
#include "closure_hydrator.h"
6
#include "communicator.h"
7
#include "grid.h"
8
#include "host_object.h"
9
#include "instruction_graph.h"
10
#include "named_threads.h"
11
#include "out_of_order_engine.h"
12
#include "receive_arbiter.h"
13
#include "system_info.h"
14
#include "tracy.h"
15
#include "types.h"
16
#include "utils.h"
17
#include "version.h"
18

19
#include <deque>
20
#include <memory>
21
#include <optional>
22
#include <string>
23
#include <unordered_map>
24
#include <vector>
25

26
#include <matchbox.hh>
27

28

29
namespace celerity::detail::live_executor_detail {
30

31
#if CELERITY_TRACY_SUPPORT
32

33
struct tracy_integration {
34
        struct instruction_info {
35
                instruction_id iid = -1;
36
                gch::small_vector<instruction_id> dependencies;
37
                int priority = -1;
38
                std::optional<size_t> bytes_processed;
39
                TracyCZoneCtx (*begin_zone)(const instruction_info& info, bool was_eagerly_submitted) = nullptr;
40
        };
41

42
        struct async_zone {
43
                size_t submission_idx_on_lane;
44
                instruction_info info;
45
                std::string trace;
46
        };
47

48
        /// References a position in an `async_lane_state::zone_queue` from within `executor_impl::async_instruction_state`
49
        struct async_lane_cursor {
50
                size_t global_lane_id = 0;
51
                size_t submission_idx_on_lane = 0;
52
        };
53

54
        /// Unique identifier for an `async_lane_state`.
55
        struct async_lane_id {
56
                out_of_order_engine::target target = out_of_order_engine::target::immediate;
57
                std::optional<device_id> device;
58
                size_t local_lane_id = 0;
59
        };
60

61
        /// State for an async (fiber) lane. Keeps the active (suspended) zone as well as the queue of eagerly submitted but not yet begun zones.
62
        struct async_lane_state {
63
                async_lane_id id;
64
                const char* fiber_name = nullptr;
65
                int32_t fiber_order = 0;
66
                size_t next_submission_idx = 0;
67
                std::optional<TracyCZoneCtx> active_zone_ctx;
68
                std::deque<async_zone> zone_queue; ///< front(): currently active zone, front() + 1: zone to start immediately after front() has ended
69

70
                explicit async_lane_state(const async_lane_id& id);
71
        };
72

73
        std::vector<async_lane_state> async_lanes; // vector instead of map, because elements need to be referenced to by global lane id
74

75
        std::string last_instruction_trace; ///< written by `CELERITY_DETAIL_TRACE_INSTRUCTION()`, read by `executor_impl::dispatch()`
76

77
        tracy_detail::plot<int64_t> assigned_instructions_plot{"assigned instructions"};
78
        tracy_detail::plot<int64_t> assignment_queue_length_plot{"assignment queue length"};
79

80
        static instruction_info make_instruction_info(const instruction& instr);
81

82
        /// Open a Tracy zone, setting tag, color - and name, if full tracing is enabled.
83
        static TracyCZoneCtx begin_instruction_zone(const instruction_info& info, bool was_eagerly_submitted);
84

85
        /// Close a Tracy zone - after emitting the instruction trace and generic instruction info if full tracing is enabled.
86
        static void end_instruction_zone(const TracyCZoneCtx& ctx, const instruction_info& info, const std::string& trace, const async_event* opt_event = nullptr);
87

88
        /// Picks the (optionally) pre-existing lane for an in-order queue submission, or an arbitrary free lane for async unordered send/receive instructions.
89
        async_lane_cursor get_async_lane_cursor(const out_of_order_engine::assignment& assignment);
90

91
        /// Adds an async instruction to its designated lane queue; beginning a Tracy zone immediately if it is the only instruction in the queue
92
        async_lane_cursor issue_async_instruction(instruction_info&& info, const out_of_order_engine::assignment& assignment, std::string&& trace);
93

94
        /// Closes the tracy zone for an active async instruction; beginning the next queued zone in the same lane, if any.
95
        void retire_async_instruction(const async_lane_cursor& cursor, const async_event& event);
96
};
97

98

99
tracy_integration::async_lane_state::async_lane_state(const async_lane_id& id) : id(id) {
100
        switch(id.target) {
101
        case out_of_order_engine::target::immediate: {
102
                fiber_name = tracy_detail::leak_name(fmt::format("cy-async-p2p #{}", id.local_lane_id));
103
                fiber_order = tracy_detail::thread_order::send_receive_first_lane + static_cast<int32_t>(id.local_lane_id);
104
                break;
105
        }
106
        case out_of_order_engine::target::alloc_queue:
107
                fiber_name = "cy-async-alloc";
108
                fiber_order = tracy_detail::thread_order::alloc_lane;
109
                break;
110
        case out_of_order_engine::target::host_queue:
111
                fiber_name = tracy_detail::leak_name(fmt::format("cy-async-host #{}", id.local_lane_id));
112
                fiber_order = tracy_detail::thread_order::host_first_lane + static_cast<int32_t>(id.local_lane_id);
113
                break;
114
        case out_of_order_engine::target::device_queue:
115
                fiber_name = tracy_detail::leak_name(fmt::format("cy-async-device D{} #{}", id.device.value(), id.local_lane_id));
116
                fiber_order = tracy_detail::thread_order::first_device_first_lane
117
                              + static_cast<int32_t>(id.device.value()) * tracy_detail::thread_order::num_lanes_per_device + static_cast<int32_t>(id.local_lane_id);
118
                break;
119
        default: utils::unreachable();
120
        }
121
}
122

123
tracy_integration::async_lane_cursor tracy_integration::get_async_lane_cursor(const out_of_order_engine::assignment& assignment) {
124
        const auto target = assignment.target;
125
        // on alloc_queue, assignment.device signals on which device to allocate memory, not on which device to queue the instruction
126
        const auto device = assignment.target == out_of_order_engine::target::device_queue ? assignment.device : std::nullopt;
127
        // out_of_order_engine does not assign a lane for alloc_queue, but there exists a single (in-order) one which we identify as `0`,
128
        // to continue using `nullopt` to pick an arbitrary empty lane in the code below for the immediate-but-async send / receive instruction types.
129
        const auto local_lane_id = assignment.target == out_of_order_engine::target::alloc_queue ? std::optional<size_t>(0) : assignment.lane;
130

131
        size_t next_local_lane_id = 0;
132
        auto lane_it = std::find_if(async_lanes.begin(), async_lanes.end(), [&](const async_lane_state& lane) {
133
                if(lane.id.target != target || lane.id.device != device) return false;
134
                ++next_local_lane_id; // if lambda never returns true, this will identify an unused local lane id for insertion below
135
                return local_lane_id.has_value() ? lane.id.local_lane_id == *local_lane_id /* exact match */ : lane.zone_queue.empty() /* arbitrary empty lane */;
136
        });
137
        if(lane_it == async_lanes.end()) {
138
                lane_it = async_lanes.emplace(async_lanes.end(), async_lane_id{target, device, local_lane_id.value_or(next_local_lane_id)});
139
        }
140
        const auto global_lane_id = static_cast<size_t>(lane_it - async_lanes.begin());
141
        return async_lane_cursor{global_lane_id, lane_it->next_submission_idx++};
142
}
143

144
tracy_integration::instruction_info tracy_integration::make_instruction_info(const instruction& instr) {
145
        tracy_integration::instruction_info info;
146
        info.iid = instr.get_id();
147

148
        // Tracy stages zone tag and color in a static local, so we can't move TracyCZoneNC out of match statement
149
#define CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(INSTR, COLOR)                                                                                                   \
150
        [&](const INSTR##_instruction& /* instr */) {                                                                                                              \
151
                return [](const instruction_info& info, bool was_eagerly_submitted) {                                                                                  \
152
                        TracyCZoneNC(ctx, "executor::" #INSTR, tracy::Color::COLOR, true /* active */);                                                                    \
153
                        if(tracy_detail::is_enabled_full()) {                                                                                                              \
154
                                const auto name = fmt::format("{}I{} " #INSTR, was_eagerly_submitted ? "+" : "", info.iid);                                                    \
155
                                TracyCZoneName(ctx, name.data(), name.size());                                                                                                 \
156
                        }                                                                                                                                                  \
157
                        return ctx;                                                                                                                                        \
158
                };                                                                                                                                                     \
159
        }
160

161
        info.begin_zone = matchbox::match(instr,                                   //
162
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(clone_collective_group, Brown), //
163
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(alloc, Turquoise),              //
164
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(free, Turquoise),               //
165
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(copy, Lime),                    //
166
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(device_kernel, Orange),         //
167
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(host_task, Orange),             //
168
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(send, Violet),                  //
169
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(receive, DarkViolet),           //
170
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(split_receive, DarkViolet),     //
171
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(await_receive, DarkViolet),     //
172
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(gather_receive, DarkViolet),    //
173
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(fill_identity, Blue),           //
174
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(reduce, Blue),                  //
175
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(fence, Blue),                   //
176
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(destroy_host_object, Gray),     //
177
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(horizon, Gray),                 //
178
            CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE(epoch, Gray));
179

180
#undef CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE
181

182
        CELERITY_DETAIL_IF_TRACY_ENABLED_FULL({
183
                info.dependencies = instr.get_dependencies();
184
                info.priority = instr.get_priority();
185
                info.bytes_processed = matchbox::match<std::optional<size_t>>(
186
                    instr,                                                                                                          //
187
                    [](const alloc_instruction& ainstr) { return ainstr.get_size_bytes(); },                                        //
188
                    [](const copy_instruction& cinstr) { return cinstr.get_copy_region().get_area() * cinstr.get_element_size(); }, //
189
                    [](const send_instruction& sinstr) { return sinstr.get_send_range().size() * sinstr.get_element_size(); },      //
190
                    [](const device_kernel_instruction& dkinstr) { return dkinstr.get_estimated_global_memory_traffic_bytes(); },   //
191
                    [](const auto& /* other */) { return std::nullopt; });
192
        })
193

194
        return info;
195
}
196

197
TracyCZoneCtx tracy_integration::begin_instruction_zone(const instruction_info& info, bool was_eagerly_submitted) {
198
        assert(info.begin_zone != nullptr);
199
        return info.begin_zone(info, was_eagerly_submitted);
200
}
201

202
void tracy_integration::end_instruction_zone(const TracyCZoneCtx& ctx, const instruction_info& info, const std::string& trace, const async_event* opt_event) {
203
        if(tracy_detail::is_enabled_full()) {
204
                std::string text;
205
                text.reserve(512); // Observation: typical size for kernel instructions is 200 - 300 characters
206

207
                // Dump the trace collected from CELERITY_DETAIL_TRACE_INSTRUCTION, replacing /; */ with '\n' for better legibility
208
                for(size_t trace_line_start = 0; trace_line_start < trace.size();) {
209
                        const auto trace_line_end = trace.find(';', trace_line_start);
210
                        text.append(trace, trace_line_start, trace_line_end - trace_line_start);
211
                        if(trace_line_end == std::string::npos) break;
212
                        text.push_back('\n');
213
                        trace_line_start = trace.find_first_not_of(' ', trace_line_end + 1);
214
                }
215

216
                // Dump time and throughput measures if available. We pass an `async_event*` instead of a `optional<duration>` to this function because querying
217
                // execution time of SYCL events is comparatively costly (~1µs) and can be skipped when CELERITY_TRACE=fast.
218
                if(opt_event != nullptr) {
219
                        if(const auto native_execution_time = opt_event->get_native_execution_time(); native_execution_time.has_value()) {
220
                                fmt::format_to(std::back_inserter(text), "\nnative execution time: {:.2f}", as_sub_second(*native_execution_time));
221
                                if(info.bytes_processed.has_value()) {
222
                                        fmt::format_to(std::back_inserter(text), "\nthroughput: {:.2f}", as_decimal_throughput(*info.bytes_processed, *native_execution_time));
223
                                }
224
                        }
225
                }
226

227
                for(size_t i = 0; i < info.dependencies.size(); ++i) {
228
                        text += i == 0 ? "\ndepends: " : ", ";
229
                        fmt::format_to(std::back_inserter(text), "I{}", info.dependencies[i]);
230
                }
231
                fmt::format_to(std::back_inserter(text), "\npriority: {}", info.priority);
232

233
                TracyCZoneText(ctx, text.data(), text.size());
234
        }
235

236
        TracyCZoneEnd(ctx);
237
}
238

239
tracy_integration::async_lane_cursor tracy_integration::issue_async_instruction(
240
    instruction_info&& info, const out_of_order_engine::assignment& assignment, std::string&& trace) //
241
{
242
        const auto cursor = get_async_lane_cursor(assignment);
243

244
        auto& lane = async_lanes[cursor.global_lane_id];
245
        lane.zone_queue.push_back({cursor.submission_idx_on_lane, std::move(info), std::move(trace)});
246
        auto& info_enqueued = lane.zone_queue.back().info;
247

248
        if(lane.zone_queue.size() == 1) {
249
                // zone_queue.back() == zone_queue.front(): The instruction starts immediately
250
                assert(!lane.active_zone_ctx.has_value());
251
                TracyFiberEnterHint(lane.fiber_name, lane.fiber_order);
252
                lane.active_zone_ctx = begin_instruction_zone(info_enqueued, false /* eager */);
253
                TracyFiberLeave;
254
        } else if(tracy_detail::is_enabled_full()) {
255
                // The instruction zone will be started as soon as its predecessor is retired - indicate when it was issued
256
                const auto mark = fmt::format("I{} issued", info_enqueued.iid);
257
                TracyFiberEnterHint(lane.fiber_name, lane.fiber_order);
258
                TracyMessageC(mark.data(), mark.size(), tracy::Color::DarkGray);
259
                TracyFiberLeave;
260
        }
261

262
        return cursor;
263
}
264

265
void tracy_integration::retire_async_instruction(const async_lane_cursor& cursor, const async_event& event) {
266
        auto& lane = async_lanes.at(cursor.global_lane_id);
267

268
        TracyFiberEnterHint(lane.fiber_name, lane.fiber_order);
269
        while(!lane.zone_queue.empty() && lane.zone_queue.front().submission_idx_on_lane <= cursor.submission_idx_on_lane) {
270
                // Complete the front() == active zone in the lane.
271
                {
272
                        auto& completed_zone = lane.zone_queue.front();
273
                        assert(lane.active_zone_ctx.has_value());
274
                        end_instruction_zone(*lane.active_zone_ctx, completed_zone.info, completed_zone.trace, &event);
275
                        lane.active_zone_ctx.reset();
276
                        lane.zone_queue.pop_front();
277
                }
278
                // If there remains another (eagerly issued) instruction in the queue after popping the active one, show it as having started immediately.
279
                if(!lane.zone_queue.empty()) {
280
                        auto& eagerly_following_zone = lane.zone_queue.front();
281
                        assert(!lane.active_zone_ctx.has_value());
282
                        lane.active_zone_ctx = begin_instruction_zone(eagerly_following_zone.info, true /* eager */);
283
                }
284
        }
285
        TracyFiberLeave;
286
}
287

288
#endif // CELERITY_DETAIL_ENABLE_TRACY
289

290

291
#if CELERITY_ACCESSOR_BOUNDARY_CHECK
292

293
struct boundary_check_info {
294
        struct accessor_info {
295
                detail::buffer_id buffer_id = 0;
296
                std::string buffer_name;
297
                box<3> accessible_box;
298
        };
299

300
        detail::task_type task_type;
301
        detail::task_id task_id;
302
        std::string task_name;
303

304
        oob_bounding_box* illegal_access_bounding_boxes = nullptr;
305
        std::vector<accessor_info> accessors;
306

307
        boundary_check_info(detail::task_type tt, detail::task_id tid, const std::string& task_name) : task_type(tt), task_id(tid), task_name(task_name) {}
1,994✔
308
};
309

310
#endif // CELERITY_ACCESSOR_BOUNDARY_CHECK
311

312

313
#define CELERITY_DETAIL_TRACE_INSTRUCTION(INSTR, FMT_STRING, ...)                                                                                              \
314
        CELERITY_TRACE("[executor] I{}: " FMT_STRING, INSTR.get_id(), ##__VA_ARGS__);                                                                              \
315
        CELERITY_DETAIL_IF_TRACY_ENABLED_FULL(tracy->last_instruction_trace = fmt::format(FMT_STRING, ##__VA_ARGS__))
316

317

318
struct async_instruction_state {
319
        allocation_id alloc_aid = null_allocation_id; ///< non-null iff instruction is an alloc_instruction
320
        async_event event;
321
        CELERITY_DETAIL_IF_ACCESSOR_BOUNDARY_CHECK(std::unique_ptr<boundary_check_info> oob_info;) // unique_ptr: oob_info is optional and rather large
322
        CELERITY_DETAIL_IF_TRACY_SUPPORTED(std::optional<tracy_integration::async_lane_cursor> tracy_lane_cursor;)
323
};
324

325
struct executor_impl {
326
        const std::unique_ptr<detail::backend> backend;
327
        communicator* const root_communicator;
328
        double_buffered_queue<submission>* const submission_queue;
329
        executor::delegate* const delegate;
330
        const live_executor::policy_set policy;
331

332
        receive_arbiter recv_arbiter{*root_communicator};
333
        out_of_order_engine engine{backend->get_system_info()};
334

335
        bool expecting_more_submissions = true; ///< shutdown epoch has not been executed yet
336
        std::unordered_map<instruction_id, async_instruction_state> in_flight_async_instructions;
337
        std::unordered_map<allocation_id, void*> allocations{{null_allocation_id, nullptr}}; ///< obtained from alloc_instruction or track_user_allocation
338
        std::unordered_map<host_object_id, std::unique_ptr<host_object_instance>> host_object_instances; ///< passed in through track_host_object_instance
339
        std::unordered_map<collective_group_id, std::unique_ptr<communicator>> cloned_communicators;     ///< transitive clones of root_communicator
340
        std::unordered_map<reduction_id, std::unique_ptr<reducer>> reducers; ///< passed in through track_reducer, erased on epochs / horizons
341

342
        std::optional<std::chrono::steady_clock::time_point> last_progress_timestamp; ///< last successful call to check_progress
343
        bool made_progress = false;                                                   ///< progress was made since `last_progress_timestamp`
344
        bool progress_warning_emitted = false;                                        ///< no progress was made since warning was emitted
345

346
        CELERITY_DETAIL_IF_TRACY_SUPPORTED(std::unique_ptr<tracy_integration> tracy;)
347

348
        executor_impl(std::unique_ptr<detail::backend> backend, communicator* root_comm, double_buffered_queue<submission>& submission_queue,
349
            executor::delegate* dlg, const live_executor::policy_set& policy);
350

351
        void run();
352
        void poll_in_flight_async_instructions();
353
        void poll_submission_queue();
354
        void try_issue_one_instruction();
355
        void retire_async_instruction(instruction_id iid, async_instruction_state& async);
356
        void check_progress();
357

358
        // Instruction types that complete synchronously within the executor.
359
        void issue(const clone_collective_group_instruction& ccginstr);
360
        void issue(const split_receive_instruction& srinstr);
361
        void issue(const fill_identity_instruction& fiinstr);
362
        void issue(const reduce_instruction& rinstr);
363
        void issue(const fence_instruction& finstr);
364
        void issue(const destroy_host_object_instruction& dhoinstr);
365
        void issue(const horizon_instruction& hinstr);
366
        void issue(const epoch_instruction& einstr);
367

368
        template <typename Instr>
369
        auto dispatch(const Instr& instr, const out_of_order_engine::assignment& assignment)
370
            // SFINAE: there is a (synchronous) `issue` overload above for the concrete Instr type
371
            -> decltype(issue(instr));
372

373
        // Instruction types that complete asynchronously via async_event, outside the executor.
374
        void issue_async(const alloc_instruction& ainstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async);
375
        void issue_async(const free_instruction& finstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async);
376
        void issue_async(const copy_instruction& cinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async);
377
        void issue_async(const device_kernel_instruction& dkinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async);
378
        void issue_async(const host_task_instruction& htinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async);
379
        void issue_async(const send_instruction& sinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async);
380
        void issue_async(const receive_instruction& rinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async);
381
        void issue_async(const await_receive_instruction& arinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async);
382
        void issue_async(const gather_receive_instruction& grinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async);
383

384
        template <typename Instr>
385
        auto dispatch(const Instr& instr, const out_of_order_engine::assignment& assignment)
386
            // SFINAE: there is an `issue_async` overload above for the concrete Instr type
387
            -> decltype(issue_async(instr, assignment, std::declval<async_instruction_state&>()));
388

389
        std::vector<closure_hydrator::accessor_info> make_accessor_infos(const buffer_access_allocation_map& amap) const;
390

391
#if CELERITY_ACCESSOR_BOUNDARY_CHECK
392
        std::unique_ptr<boundary_check_info> attach_boundary_check_info(std::vector<closure_hydrator::accessor_info>& accessor_infos,
393
            const buffer_access_allocation_map& amap, task_type tt, task_id tid, const std::string& task_name) const;
394
#endif
395

396
        void collect(const instruction_garbage& garbage);
397
};
398

399
executor_impl::executor_impl(std::unique_ptr<detail::backend> backend, communicator* const root_comm, double_buffered_queue<submission>& submission_queue,
242✔
400
    executor::delegate* const dlg, const live_executor::policy_set& policy)
242✔
401
    : backend(std::move(backend)), root_communicator(root_comm), submission_queue(&submission_queue), delegate(dlg), policy(policy) //
726✔
402
{
403
        CELERITY_DETAIL_IF_TRACY_ENABLED(tracy = std::make_unique<tracy_integration>();)
404
}
242✔
405

406
void executor_impl::run() {
242✔
407
        // this closure hydrator instantiation is not necessary in normal execution iff device submission threads are enabled,
408
        // but it is still required for testing purposes, so always making it available on this thread is the simplest solution
409
        closure_hydrator::make_available();
242✔
410
        backend->init();
242✔
411

412
        uint8_t check_overflow_counter = 0;
242✔
413
        for(;;) {
414
                if(engine.is_idle()) {
3,601,862✔
415
                        if(!expecting_more_submissions) break; // shutdown complete
2,610✔
416

417
                        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::starve", DarkSlateGray);
418
                        submission_queue->wait_while_empty(); // we are stalled on the scheduler, suspend thread
2,368✔
419
                        last_progress_timestamp.reset();      // do not treat suspension as being stuck
2,368✔
420
                }
421

422
                recv_arbiter.poll_communicator();
3,601,620✔
423
                poll_in_flight_async_instructions();
3,601,620✔
424
                poll_submission_queue();
3,601,620✔
425
                try_issue_one_instruction(); // potentially expensive, so only issue one per loop to continue checking for async completion in between
3,601,620✔
426

427
                if(++check_overflow_counter == 0) { // once every 256 iterations
3,601,620✔
428
                        backend->check_async_errors();
13,994✔
429
                        check_progress();
13,994✔
430
                }
431
        }
432

433
        assert(in_flight_async_instructions.empty());
242✔
434
        // check that for each alloc_instruction, we executed a corresponding free_instruction
435
        assert(std::all_of(allocations.begin(), allocations.end(),
484✔
436
            [](const std::pair<allocation_id, void*>& p) { return p.first == null_allocation_id || p.first.get_memory_id() == user_memory_id; }));
437
        // check that for each track_host_object_instance, we executed a destroy_host_object_instruction
438
        assert(host_object_instances.empty());
242✔
439

440
        closure_hydrator::teardown();
242✔
441
}
242✔
442

443
void executor_impl::poll_in_flight_async_instructions() {
3,601,620✔
444
        // collect completed instruction ids up-front, since retire_async_instruction would alter the execution front
445
        std::vector<instruction_id> completed_now; // std::vector because it will be empty in the common case
3,601,620✔
446
        for(const auto iid : engine.get_execution_front()) {
8,592,966✔
447
                if(in_flight_async_instructions.at(iid).event.is_complete()) { completed_now.push_back(iid); }
4,991,346✔
448
        }
449
        for(const auto iid : completed_now) {
3,609,120✔
450
                retire_async_instruction(iid, in_flight_async_instructions.at(iid));
7,500✔
451
                in_flight_async_instructions.erase(iid);
7,500✔
452
                made_progress = true;
7,500✔
453
        }
454

455
        CELERITY_DETAIL_IF_TRACY_ENABLED(tracy->assigned_instructions_plot.update(in_flight_async_instructions.size()));
456
}
7,203,240✔
457

458
void executor_impl::poll_submission_queue() {
3,601,620✔
459
        for(auto& submission : submission_queue->pop_all()) {
3,605,849✔
460
                CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::fetch", Gray);
461
                matchbox::match(
4,229✔
462
                    submission,
463
                    [&](const instruction_pilot_batch& batch) {
4,229✔
464
                            for(const auto incoming_instr : batch.instructions) {
13,137✔
465
                                    engine.submit(incoming_instr);
9,118✔
466
                            }
467
                            for(const auto& pilot : batch.pilots) {
4,681✔
468
                                    root_communicator->send_outbound_pilot(pilot);
662✔
469
                            }
470
                            CELERITY_DETAIL_IF_TRACY_ENABLED(tracy->assignment_queue_length_plot.update(engine.get_assignment_queue_length()));
471
                    },
4,019✔
472
                    [&](const user_allocation_transfer& uat) {
8,458✔
473
                            assert(uat.aid != null_allocation_id);
112✔
474
                            assert(uat.aid.get_memory_id() == user_memory_id);
112✔
475
                            assert(allocations.count(uat.aid) == 0);
112✔
476
                            allocations.emplace(uat.aid, uat.ptr);
112✔
477
                    },
112✔
478
                    [&](host_object_transfer& hot) {
8,458✔
479
                            assert(host_object_instances.count(hot.hoid) == 0);
30✔
480
                            host_object_instances.emplace(hot.hoid, std::move(hot.instance));
30✔
481
                    },
30✔
482
                    [&](reducer_transfer& rt) {
8,458✔
483
                            assert(reducers.count(rt.rid) == 0);
68✔
484
                            reducers.emplace(rt.rid, std::move(rt.reduction));
68✔
485
                    });
68✔
486
        }
487
}
3,601,620✔
488

489
void executor_impl::retire_async_instruction(const instruction_id iid, async_instruction_state& async) {
7,500✔
490
        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::retire", Brown);
491

492
#if CELERITY_ACCESSOR_BOUNDARY_CHECK
493
        if(async.oob_info != nullptr) {
7,500✔
494
                CELERITY_DETAIL_TRACY_ZONE_SCOPED_V("executor::oob_check", Red, "I{} bounds check", iid);
495
                const auto& oob_info = *async.oob_info;
1,994✔
496
                for(size_t i = 0; i < oob_info.accessors.size(); ++i) {
4,598✔
497
                        if(const auto oob_box = oob_info.illegal_access_bounding_boxes[i].into_box(); !oob_box.empty()) {
2,604✔
498
                                const auto& accessor_info = oob_info.accessors[i];
12✔
499
                                CELERITY_ERROR("Out-of-bounds access detected in {}: accessor {} attempted to access buffer {} indicies between {} and outside the "
12✔
500
                                               "declared range {}.",
501
                                    utils::make_task_debug_label(oob_info.task_type, oob_info.task_id, oob_info.task_name), i,
502
                                    utils::make_buffer_debug_label(accessor_info.buffer_id, accessor_info.buffer_name), oob_box, accessor_info.accessible_box);
503
                        }
504
                }
505
                if(oob_info.illegal_access_bounding_boxes != nullptr /* i.e. there was at least one accessor */) {
1,994!
506
                        backend->debug_free(oob_info.illegal_access_bounding_boxes);
1,994✔
507
                }
508
        }
509
#endif
510

511
        if(spdlog::should_log(spdlog::level::trace)) {
7,500✔
512
                if(const auto native_time = async.event.get_native_execution_time(); native_time.has_value()) {
3,727!
513
                        CELERITY_TRACE("[executor] retired I{} after {:.2f}", iid, as_sub_second(*native_time));
×
514
                } else {
515
                        CELERITY_TRACE("[executor] retired I{}", iid);
3,727!
516
                }
517
        }
518

519
        CELERITY_DETAIL_IF_TRACY_ENABLED(tracy->retire_async_instruction(*async.tracy_lane_cursor, async.event));
520

521
        if(async.alloc_aid != null_allocation_id) {
7,500✔
522
                const auto ptr = async.event.get_result();
1,014✔
523
                assert(ptr != nullptr && "backend allocation returned nullptr");
1,014✔
524
                CELERITY_TRACE("[executor] {} allocated as {}", async.alloc_aid, ptr);
1,014!
525
                assert(allocations.count(async.alloc_aid) == 0);
1,014✔
526
                allocations.emplace(async.alloc_aid, ptr);
1,014✔
527
        }
528

529
        engine.complete_assigned(iid);
7,500✔
530

531
        CELERITY_DETAIL_IF_TRACY_ENABLED(tracy->assignment_queue_length_plot.update(engine.get_assignment_queue_length()));
532
}
7,500✔
533

534
template <typename Instr>
535
auto executor_impl::dispatch(const Instr& instr, const out_of_order_engine::assignment& assignment)
1,618✔
536
    // SFINAE: there is a (synchronous) `issue` overload above for the concrete Instr type
537
    -> decltype(issue(instr)) //
538
{
539
        assert(assignment.target == out_of_order_engine::target::immediate);
1,618✔
540
        assert(!assignment.lane.has_value());
1,618✔
541

542
        const auto iid = instr.get_id(); // instr may dangle after issue()
1,618✔
543

544
        CELERITY_DETAIL_IF_TRACY_SUPPORTED(TracyCZoneCtx ctx;)
545
        CELERITY_DETAIL_IF_TRACY_SUPPORTED(tracy_integration::instruction_info info);
546
        CELERITY_DETAIL_IF_TRACY_ENABLED({
547
                info = tracy_integration::make_instruction_info(instr);
548
                TracyFiberEnterHint("cy-immediate", tracy_detail::thread_order::immediate_lane);
549
                ctx = tracy->begin_instruction_zone(info, false /* eager */);
550
                tracy->assigned_instructions_plot.update(in_flight_async_instructions.size() + 1);
551
        })
552

553
        issue(instr); // completes immediately - instr may now dangle
1,618✔
554

555
        CELERITY_DETAIL_IF_TRACY_ENABLED({
556
                tracy->end_instruction_zone(ctx, info, tracy->last_instruction_trace);
557
                TracyFiberLeave;
558
        })
559

560
        engine.complete_assigned(iid);
1,618✔
561

562
        CELERITY_DETAIL_IF_TRACY_ENABLED({
563
                tracy->assigned_instructions_plot.update(in_flight_async_instructions.size());
564
                tracy->assignment_queue_length_plot.update(engine.get_assignment_queue_length());
565
        })
566
}
1,618✔
567

568
template <typename Instr>
569
auto executor_impl::dispatch(const Instr& instr, const out_of_order_engine::assignment& assignment)
7,500✔
570
    // SFINAE: there is an `issue_async` overload above for the concrete Instr type
571
    -> decltype(issue_async(instr, assignment, std::declval<async_instruction_state&>())) //
572
{
573
        CELERITY_DETAIL_IF_TRACY_SUPPORTED(tracy_integration::instruction_info info);
574
        CELERITY_DETAIL_IF_TRACY_ENABLED(info = tracy_integration::make_instruction_info(instr));
575

576
        auto& async = in_flight_async_instructions.emplace(assignment.instruction->get_id(), async_instruction_state{}).first->second;
15,000✔
577
        issue_async(instr, assignment, async); // stores event in `async` and completes asynchronously
7,500✔
578
        // instr may now dangle
579

580
        CELERITY_DETAIL_IF_TRACY_ENABLED({
581
                async.tracy_lane_cursor = tracy->issue_async_instruction(std::move(info), assignment, std::move(tracy->last_instruction_trace));
582
                tracy->assigned_instructions_plot.update(in_flight_async_instructions.size());
583
        })
584
}
15,000✔
585

586
void executor_impl::try_issue_one_instruction() {
3,601,620✔
587
        auto assignment = engine.assign_one();
3,601,620✔
588
        if(!assignment.has_value()) return;
3,601,620✔
589

590
        CELERITY_DETAIL_IF_TRACY_ENABLED(tracy->assignment_queue_length_plot.update(engine.get_assignment_queue_length()));
591

592
        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::issue", Blue);
593
        matchbox::match(*assignment->instruction, [&](const auto& instr) { dispatch(instr, *assignment); });
18,236✔
594
        made_progress = true;
9,118✔
595
}
596

597
void executor_impl::check_progress() {
13,994✔
598
        if(!policy.progress_warning_timeout.has_value()) return;
13,994!
599

600
        if(made_progress) {
13,994✔
601
                last_progress_timestamp = std::chrono::steady_clock::now();
304✔
602
                progress_warning_emitted = false;
304✔
603
                made_progress = false;
304✔
604
        } else if(last_progress_timestamp.has_value()) {
13,690!
605
                // being stuck either means a deadlock in the user application, or a bug in Celerity.
606
                const auto elapsed_since_last_progress = std::chrono::steady_clock::now() - *last_progress_timestamp;
13,690✔
607
                if(elapsed_since_last_progress > *policy.progress_warning_timeout && !progress_warning_emitted) {
13,690✔
608
                        std::string instr_list;
1✔
609
                        for(auto& [iid, async] : in_flight_async_instructions) {
2✔
610
                                if(!instr_list.empty()) instr_list += ", ";
1!
611
                                fmt::format_to(std::back_inserter(instr_list), "I{}", iid);
1✔
612
                        }
613
                        CELERITY_WARN("[executor] no progress for {:.0f}, might be stuck. Active instructions: {}", as_sub_second(elapsed_since_last_progress),
1✔
614
                            in_flight_async_instructions.empty() ? "none" : instr_list);
615
                        progress_warning_emitted = true;
1✔
616
                }
1✔
617
        }
618
}
619

620
void executor_impl::issue(const clone_collective_group_instruction& ccginstr) {
33✔
621
        const auto original_cgid = ccginstr.get_original_collective_group_id();
33✔
622
        assert(original_cgid != non_collective_group_id);
33✔
623
        assert(original_cgid == root_collective_group_id || cloned_communicators.count(original_cgid) != 0);
33✔
624

625
        const auto new_cgid = ccginstr.get_new_collective_group_id();
33✔
626
        assert(new_cgid != non_collective_group_id && new_cgid != root_collective_group_id);
33✔
627
        assert(cloned_communicators.count(new_cgid) == 0);
33✔
628

629
        CELERITY_DETAIL_TRACE_INSTRUCTION(ccginstr, "clone collective group CG{} -> CG{}", original_cgid, new_cgid);
33✔
630

631
        const auto original_communicator = original_cgid == root_collective_group_id ? root_communicator : cloned_communicators.at(original_cgid).get();
33✔
632
        cloned_communicators.emplace(new_cgid, original_communicator->collective_clone());
33✔
633
}
33✔
634

635

636
void executor_impl::issue(const split_receive_instruction& srinstr) {
20✔
637
        CELERITY_DETAIL_TRACE_INSTRUCTION(srinstr, "split receive {} {}x{} bytes into {} ({}),", srinstr.get_transfer_id(), srinstr.get_requested_region(),
20✔
638
            srinstr.get_element_size(), srinstr.get_dest_allocation_id(), srinstr.get_allocated_box());
639

640
        const auto allocation = allocations.at(srinstr.get_dest_allocation_id());
20✔
641
        recv_arbiter.begin_split_receive(
20✔
642
            srinstr.get_transfer_id(), srinstr.get_requested_region(), allocation, srinstr.get_allocated_box(), srinstr.get_element_size());
643
}
20✔
644

645
void executor_impl::issue(const fill_identity_instruction& fiinstr) {
19✔
646
        CELERITY_DETAIL_TRACE_INSTRUCTION(
19✔
647
            fiinstr, "fill identity {} x{} values for R{}", fiinstr.get_allocation_id(), fiinstr.get_num_values(), fiinstr.get_reduction_id());
648

649
        const auto allocation = allocations.at(fiinstr.get_allocation_id());
19✔
650
        const auto& reduction = *reducers.at(fiinstr.get_reduction_id());
19✔
651
        reduction.fill_identity(allocation, fiinstr.get_num_values());
19✔
652
}
19✔
653

654
void executor_impl::issue(const reduce_instruction& rinstr) {
47✔
655
        CELERITY_DETAIL_TRACE_INSTRUCTION(rinstr, "reduce {} x{} values into {} for R{}", rinstr.get_source_allocation_id(), rinstr.get_num_source_values(),
47✔
656
            rinstr.get_dest_allocation_id(), rinstr.get_reduction_id());
657

658
        const auto gather_allocation = allocations.at(rinstr.get_source_allocation_id());
47✔
659
        const auto dest_allocation = allocations.at(rinstr.get_dest_allocation_id());
47✔
660
        const auto& reduction = *reducers.at(rinstr.get_reduction_id());
47✔
661
        reduction.reduce(dest_allocation, gather_allocation, rinstr.get_num_source_values());
47✔
662
}
47✔
663

664
void executor_impl::issue(const fence_instruction& finstr) { // NOLINT(readability-make-member-function-const, readability-convert-member-functions-to-static)
66✔
665
        CELERITY_DETAIL_TRACE_INSTRUCTION(finstr, "fence");
66✔
666

667
        finstr.get_promise()->fulfill();
66✔
668
}
66✔
669

670
void executor_impl::issue(const destroy_host_object_instruction& dhoinstr) {
30✔
671
        assert(host_object_instances.count(dhoinstr.get_host_object_id()) != 0);
30✔
672
        CELERITY_DETAIL_TRACE_INSTRUCTION(dhoinstr, "destroy H{}", dhoinstr.get_host_object_id());
30✔
673

674
        host_object_instances.erase(dhoinstr.get_host_object_id());
30✔
675
}
30✔
676

677
void executor_impl::issue(const horizon_instruction& hinstr) {
582✔
678
        CELERITY_DETAIL_TRACE_INSTRUCTION(hinstr, "horizon");
582✔
679

680
        if(delegate != nullptr) { delegate->horizon_reached(hinstr.get_horizon_task_id()); }
582!
681
        collect(hinstr.get_garbage());
582✔
682

683
        CELERITY_DETAIL_IF_TRACY_ENABLED(FrameMarkNamed("Horizon"));
684
}
582✔
685

686
void executor_impl::issue(const epoch_instruction& einstr) {
821✔
687
        switch(einstr.get_epoch_action()) {
821!
688
        case epoch_action::none: //
335✔
689
                CELERITY_DETAIL_TRACE_INSTRUCTION(einstr, "epoch");
335✔
690
                break;
335✔
691
        case epoch_action::init: //
242✔
692
                CELERITY_DETAIL_TRACE_INSTRUCTION(einstr, "epoch (init)");
242✔
693
                break;
242✔
694
        case epoch_action::barrier: //
2✔
695
                CELERITY_DETAIL_TRACE_INSTRUCTION(einstr, "epoch (barrier)");
2✔
696
                root_communicator->collective_barrier();
2✔
697
                break;
2✔
698
        case epoch_action::shutdown: //
242✔
699
                CELERITY_DETAIL_TRACE_INSTRUCTION(einstr, "epoch (shutdown)");
242✔
700
                expecting_more_submissions = false;
242✔
701
                break;
242✔
702
        }
703

704
        // Update the runtime last-epoch *before* fulfilling the promise to ensure that the new state can be observed as soon as runtime::sync returns.
705
        // This in turn allows the TDAG to be pruned before any new work is submitted after the epoch.
706
        if(delegate != nullptr) { delegate->epoch_reached(einstr.get_epoch_task_id()); }
821!
707

708
        if(einstr.get_promise() != nullptr) { einstr.get_promise()->fulfill(); }
821✔
709
        collect(einstr.get_garbage());
821✔
710

711
        CELERITY_DETAIL_IF_TRACY_ENABLED(FrameMarkNamed("Horizon"));
712
        CELERITY_DETAIL_IF_TRACY_ENABLED(FrameMark); // top-level "Frame"
713
}
821✔
714

715
void executor_impl::issue_async(const alloc_instruction& ainstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async) {
1,014✔
716
        assert(ainstr.get_allocation_id().get_memory_id() != user_memory_id);
1,014✔
717
        assert(assignment.target == out_of_order_engine::target::alloc_queue);
1,014✔
718
        assert(!assignment.lane.has_value());
1,014✔
719
        assert(assignment.device.has_value() == (ainstr.get_allocation_id().get_memory_id() > host_memory_id));
1,014✔
720

721
        CELERITY_DETAIL_TRACE_INSTRUCTION(ainstr, "alloc {}, {} % {} bytes", ainstr.get_allocation_id(), ainstr.get_size_bytes(), ainstr.get_alignment_bytes());
1,014✔
722

723
        if(assignment.device.has_value()) {
1,014✔
724
                async.event = backend->enqueue_device_alloc(*assignment.device, ainstr.get_size_bytes(), ainstr.get_alignment_bytes());
482✔
725
        } else {
726
                async.event = backend->enqueue_host_alloc(ainstr.get_size_bytes(), ainstr.get_alignment_bytes());
532✔
727
        }
728
        async.alloc_aid = ainstr.get_allocation_id(); // setting alloc_aid != null will make `retire_async_instruction` insert the result into `allocations`
1,014✔
729
}
1,014✔
730

731
void executor_impl::issue_async(const free_instruction& finstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async) {
1,014✔
732
        const auto it = allocations.find(finstr.get_allocation_id());
1,014✔
733
        assert(it != allocations.end());
1,014✔
734
        const auto ptr = it->second;
1,014✔
735
        allocations.erase(it);
1,014✔
736

737
        CELERITY_DETAIL_TRACE_INSTRUCTION(finstr, "free {}", finstr.get_allocation_id());
1,014✔
738

739
        if(assignment.device.has_value()) {
1,014✔
740
                async.event = backend->enqueue_device_free(*assignment.device, ptr);
482✔
741
        } else {
742
                async.event = backend->enqueue_host_free(ptr);
532✔
743
        }
744
}
1,014✔
745

746
void executor_impl::issue_async(const copy_instruction& cinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async) {
2,275✔
747
        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::issue_copy", Green4);
748

749
        assert(assignment.target == out_of_order_engine::target::host_queue || assignment.target == out_of_order_engine::target::device_queue);
2,275✔
750
        assert((assignment.target == out_of_order_engine::target::device_queue) == assignment.device.has_value());
2,275✔
751
        assert(assignment.lane.has_value());
2,275✔
752

753
        CELERITY_DETAIL_TRACE_INSTRUCTION(cinstr, "copy {} ({}) -> {} ({}); {}x{} bytes, {} bytes total", cinstr.get_source_allocation_id(),
2,275✔
754
            cinstr.get_source_layout(), cinstr.get_dest_allocation_id(), cinstr.get_dest_layout(), cinstr.get_copy_region(), cinstr.get_element_size(),
755
            cinstr.get_copy_region().get_area() * cinstr.get_element_size());
756

757
        const auto source_base = allocations.at(cinstr.get_source_allocation_id());
2,275✔
758
        const auto dest_base = allocations.at(cinstr.get_dest_allocation_id());
2,275✔
759

760
        if(assignment.device.has_value()) {
2,275✔
761
                async.event = backend->enqueue_device_copy(*assignment.device, *assignment.lane, source_base, dest_base, cinstr.get_source_layout(),
4,188✔
762
                    cinstr.get_dest_layout(), cinstr.get_copy_region(), cinstr.get_element_size());
2,094✔
763
        } else {
764
                async.event = backend->enqueue_host_copy(*assignment.lane, source_base, dest_base, cinstr.get_source_layout(), cinstr.get_dest_layout(),
362✔
765
                    cinstr.get_copy_region(), cinstr.get_element_size());
181✔
766
        }
767
}
2,275✔
768

769
std::string format_access_log(const buffer_access_allocation_map& map) {
1,494✔
770
        std::string acc_log;
1,494✔
771
        for(size_t i = 0; i < map.size(); ++i) {
2,992✔
772
                auto& aa = map[i];
1,498✔
773
                const auto accessed_bounding_box_in_allocation = box(aa.accessed_bounding_box_in_buffer.get_min() - aa.allocated_box_in_buffer.get_offset(),
4,494✔
774
                    aa.accessed_bounding_box_in_buffer.get_max() - aa.allocated_box_in_buffer.get_offset());
4,494✔
775
                fmt::format_to(std::back_inserter(acc_log), "{} {} {}", i == 0 ? "; accessing" : ",", aa.allocation_id, accessed_bounding_box_in_allocation);
2,996✔
776
        }
777
        return acc_log;
1,494✔
778
}
×
779

780
void executor_impl::issue_async(const device_kernel_instruction& dkinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async) {
921✔
781
        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::issue_device_kernel", Yellow2);
782

783
        assert(assignment.target == out_of_order_engine::target::device_queue);
921✔
784
        assert(assignment.device == dkinstr.get_device_id());
921✔
785
        assert(assignment.lane.has_value());
921✔
786

787
        CELERITY_DETAIL_TRACE_INSTRUCTION(dkinstr, "device kernel on D{}, {}{}; estimated global memory traffic: {:.2f}", dkinstr.get_device_id(),
921✔
788
            dkinstr.get_execution_range(), format_access_log(dkinstr.get_access_allocations()),
789
            as_decimal_size(dkinstr.get_estimated_global_memory_traffic_bytes()));
790

791
        auto accessor_infos = make_accessor_infos(dkinstr.get_access_allocations());
921✔
792
#if CELERITY_ACCESSOR_BOUNDARY_CHECK
793
        async.oob_info = attach_boundary_check_info(
1,842✔
794
            accessor_infos, dkinstr.get_access_allocations(), dkinstr.get_oob_task_type(), dkinstr.get_oob_task_id(), dkinstr.get_oob_task_name());
921✔
795
#endif
796

797
        const auto& reduction_allocs = dkinstr.get_reduction_allocations();
921✔
798
        std::vector<void*> reduction_ptrs(reduction_allocs.size());
2,763✔
799
        for(size_t i = 0; i < reduction_allocs.size(); ++i) {
1,026✔
800
                reduction_ptrs[i] = allocations.at(reduction_allocs[i].allocation_id);
105✔
801
        }
802

803
        async.event = backend->enqueue_device_kernel(
3,684✔
804
            dkinstr.get_device_id(), *assignment.lane, dkinstr.get_launcher(), std::move(accessor_infos), dkinstr.get_execution_range(), reduction_ptrs);
2,763✔
805
}
1,842✔
806

807
void executor_impl::issue_async(const host_task_instruction& htinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async) {
1,178✔
808
        assert(assignment.target == out_of_order_engine::target::host_queue);
1,178✔
809
        assert(!assignment.device.has_value());
1,178✔
810
        assert(assignment.lane.has_value());
1,178✔
811

812
        CELERITY_DETAIL_TRACE_INSTRUCTION(htinstr, "host task, {}{}", htinstr.get_execution_range(), format_access_log(htinstr.get_access_allocations()));
1,178✔
813

814
        auto accessor_infos = make_accessor_infos(htinstr.get_access_allocations());
1,178✔
815
#if CELERITY_ACCESSOR_BOUNDARY_CHECK
816
        async.oob_info = attach_boundary_check_info(
2,356✔
817
            accessor_infos, htinstr.get_access_allocations(), htinstr.get_oob_task_type(), htinstr.get_oob_task_id(), htinstr.get_oob_task_name());
1,178✔
818
#endif
819

820
        const auto& execution_range = htinstr.get_execution_range();
1,178✔
821
        const auto collective_comm =
822
            htinstr.get_collective_group_id() != non_collective_group_id ? cloned_communicators.at(htinstr.get_collective_group_id()).get() : nullptr;
1,178!
823

824
        async.event = backend->enqueue_host_task(*assignment.lane, htinstr.get_launcher(), std::move(accessor_infos), execution_range, collective_comm);
1,178✔
825
}
2,356✔
826

827
void executor_impl::issue_async(
662✔
828
    const send_instruction& sinstr, [[maybe_unused]] const out_of_order_engine::assignment& assignment, async_instruction_state& async) //
829
{
830
        assert(assignment.target == out_of_order_engine::target::immediate);
662✔
831

832
        CELERITY_DETAIL_TRACE_INSTRUCTION(sinstr, "send {}+{}, {}x{} bytes to N{} (MSG{})", sinstr.get_source_allocation_id(),
662✔
833
            sinstr.get_offset_in_source_allocation(), sinstr.get_send_range(), sinstr.get_element_size(), sinstr.get_dest_node_id(), sinstr.get_message_id());
834

835
        const auto allocation_base = allocations.at(sinstr.get_source_allocation_id());
662✔
836
        const communicator::stride stride{
662✔
837
            sinstr.get_source_allocation_range(),
662✔
838
            subrange<3>{sinstr.get_offset_in_source_allocation(), sinstr.get_send_range()},
839
            sinstr.get_element_size(),
662✔
840
        };
662✔
841
        async.event = root_communicator->send_payload(sinstr.get_dest_node_id(), sinstr.get_message_id(), allocation_base, stride);
662✔
842
}
662✔
843

844
void executor_impl::issue_async(
370✔
845
    const receive_instruction& rinstr, [[maybe_unused]] const out_of_order_engine::assignment& assignment, async_instruction_state& async) //
846
{
847
        assert(assignment.target == out_of_order_engine::target::immediate);
370✔
848

849
        CELERITY_DETAIL_TRACE_INSTRUCTION(rinstr, "receive {} {}x{} bytes into {} ({})", rinstr.get_transfer_id(), rinstr.get_requested_region(),
370✔
850
            rinstr.get_element_size(), rinstr.get_dest_allocation_id(), rinstr.get_allocated_box());
851

852
        const auto allocation = allocations.at(rinstr.get_dest_allocation_id());
370✔
853
        async.event =
370✔
854
            recv_arbiter.receive(rinstr.get_transfer_id(), rinstr.get_requested_region(), allocation, rinstr.get_allocated_box(), rinstr.get_element_size());
370✔
855
}
370✔
856

857
void executor_impl::issue_async(
48✔
858
    const await_receive_instruction& arinstr, [[maybe_unused]] const out_of_order_engine::assignment& assignment, async_instruction_state& async) //
859
{
860
        assert(assignment.target == out_of_order_engine::target::immediate);
48✔
861

862
        CELERITY_DETAIL_TRACE_INSTRUCTION(arinstr, "await receive {} {}", arinstr.get_transfer_id(), arinstr.get_received_region());
48✔
863

864
        async.event = recv_arbiter.await_split_receive_subregion(arinstr.get_transfer_id(), arinstr.get_received_region());
48✔
865
}
48✔
866

867
void executor_impl::issue_async(
18✔
868
    const gather_receive_instruction& grinstr, [[maybe_unused]] const out_of_order_engine::assignment& assignment, async_instruction_state& async) //
869
{
870
        assert(assignment.target == out_of_order_engine::target::immediate);
18✔
871

872
        CELERITY_DETAIL_TRACE_INSTRUCTION(
18✔
873
            grinstr, "gather receive {} into {}, {} bytes / node", grinstr.get_transfer_id(), grinstr.get_dest_allocation_id(), grinstr.get_node_chunk_size());
874

875
        const auto allocation = allocations.at(grinstr.get_dest_allocation_id());
18✔
876
        async.event = recv_arbiter.gather_receive(grinstr.get_transfer_id(), allocation, grinstr.get_node_chunk_size());
18✔
877
}
18✔
878

879
void executor_impl::collect(const instruction_garbage& garbage) {
1,403✔
880
        for(const auto rid : garbage.reductions) {
1,470✔
881
                assert(reducers.count(rid) != 0);
67✔
882
                reducers.erase(rid);
67✔
883
        }
884
        for(const auto aid : garbage.user_allocations) {
1,515✔
885
                assert(aid.get_memory_id() == user_memory_id);
112✔
886
                assert(allocations.count(aid) != 0);
112✔
887
                allocations.erase(aid);
112✔
888
        }
889
}
1,403✔
890

891
std::vector<closure_hydrator::accessor_info> executor_impl::make_accessor_infos(const buffer_access_allocation_map& amap) const {
2,099✔
892
        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::make_accessor_info", Magenta3);
893

894
        std::vector<closure_hydrator::accessor_info> accessor_infos(amap.size());
6,297✔
895
        for(size_t i = 0; i < amap.size(); ++i) {
4,703✔
896
                const auto ptr = allocations.at(amap[i].allocation_id);
2,604✔
897
                accessor_infos[i] = closure_hydrator::accessor_info{ptr, amap[i].allocated_box_in_buffer, amap[i].accessed_bounding_box_in_buffer};
2,604✔
898
        }
899
        return accessor_infos;
2,099✔
900
}
×
901

902
#if CELERITY_ACCESSOR_BOUNDARY_CHECK
903
std::unique_ptr<boundary_check_info> executor_impl::attach_boundary_check_info(std::vector<closure_hydrator::accessor_info>& accessor_infos,
2,099✔
904
    const buffer_access_allocation_map& amap, task_type tt, task_id tid, const std::string& task_name) const //
905
{
906
        if(amap.empty()) return nullptr;
2,099✔
907

908
        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::oob_init", Red);
909
        auto oob_info = std::make_unique<boundary_check_info>(tt, tid, task_name);
1,994✔
910

911
        oob_info->illegal_access_bounding_boxes = static_cast<oob_bounding_box*>(backend->debug_alloc(amap.size() * sizeof(oob_bounding_box)));
1,994✔
912
        std::uninitialized_default_construct_n(oob_info->illegal_access_bounding_boxes, amap.size());
1,994✔
913

914
        oob_info->accessors.resize(amap.size());
1,994✔
915
        for(size_t i = 0; i < amap.size(); ++i) {
4,598✔
916
                oob_info->accessors[i] = boundary_check_info::accessor_info{amap[i].oob_buffer_id, amap[i].oob_buffer_name, amap[i].accessed_bounding_box_in_buffer};
2,604✔
917
                accessor_infos[i].out_of_bounds_indices = oob_info->illegal_access_bounding_boxes + i;
2,604✔
918
        }
919
        return oob_info;
1,994✔
920
}
1,994✔
921
#endif // CELERITY_ACCESSOR_BOUNDARY_CHECK
922

923
} // namespace celerity::detail::live_executor_detail
924

925
namespace celerity::detail {
926

927
live_executor::live_executor(std::unique_ptr<backend> backend, std::unique_ptr<communicator> root_comm, executor::delegate* const dlg, const policy_set& policy)
242✔
928
    : m_root_comm(std::move(root_comm)), m_thread(&live_executor::thread_main, this, std::move(backend), dlg, policy) //
242✔
929
{
930
        set_thread_name(m_thread.native_handle(), "cy-executor");
726✔
931
}
242✔
932

933
live_executor::~live_executor() {
484✔
934
        m_thread.join(); // thread_main will exit only after executing shutdown epoch
242✔
935
}
484✔
936

937
void live_executor::track_user_allocation(const allocation_id aid, void* const ptr) {
112✔
938
        m_submission_queue.push(live_executor_detail::user_allocation_transfer{aid, ptr});
112✔
939
}
112✔
940

941
void live_executor::track_host_object_instance(const host_object_id hoid, std::unique_ptr<host_object_instance> instance) {
30✔
942
        assert(instance != nullptr);
30✔
943
        m_submission_queue.push(live_executor_detail::host_object_transfer{hoid, std::move(instance)});
30✔
944
}
30✔
945

946
void live_executor::track_reducer(const reduction_id rid, std::unique_ptr<reducer> reducer) {
68✔
947
        assert(reducer != nullptr);
68✔
948
        m_submission_queue.push(live_executor_detail::reducer_transfer{rid, std::move(reducer)});
68✔
949
}
68✔
950

951
void live_executor::submit(std::vector<const instruction*> instructions, std::vector<outbound_pilot> pilots) {
4,019✔
952
        m_submission_queue.push(live_executor_detail::instruction_pilot_batch{std::move(instructions), std::move(pilots)});
4,019!
953
}
4,019✔
954

955
void live_executor::thread_main(std::unique_ptr<backend> backend, executor::delegate* const dlg, const policy_set& policy) {
242✔
956
        CELERITY_DETAIL_TRACY_SET_THREAD_NAME_AND_ORDER("cy-executor", tracy_detail::thread_order::executor);
957

958
        thread_pinning::pin_this_thread(thread_pinning::thread_type::executor);
242✔
959

960
        try {
961
                live_executor_detail::executor_impl(std::move(backend), m_root_comm.get(), m_submission_queue, dlg, policy).run();
242✔
962
        }
963
        // LCOV_EXCL_START
964
        catch(const std::exception& e) {
965
                CELERITY_CRITICAL("[executor] {}", e.what());
966
                std::abort();
967
        }
968
        // LCOV_EXCL_STOP
969
}
242✔
970

971
} // namespace celerity::detail
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc