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

celerity / celerity-runtime / 11385484997

17 Oct 2024 01:01PM UTC coverage: 95.202% (-0.01%) from 95.216%
11385484997

Pull #293

github

fknorr
Update benchmark results for execution-front polling
Pull Request #293: Only poll events of instructions that are actively executing

3058 of 3452 branches covered (88.59%)

Branch coverage included in aggregate %.

20 of 21 new or added lines in 2 files covered. (95.24%)

1 existing line in 1 file now uncovered.

6824 of 6928 relevant lines covered (98.5%)

1444315.1 hits per line

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

94.86
/src/live_executor.cc
1
#include "live_executor.h"
2
#include "backend/backend.h"
3
#include "closure_hydrator.h"
4
#include "communicator.h"
5
#include "host_object.h"
6
#include "instruction_graph.h"
7
#include "named_threads.h"
8
#include "out_of_order_engine.h"
9
#include "receive_arbiter.h"
10
#include "system_info.h"
11
#include "tracy.h"
12
#include "types.h"
13
#include "utils.h"
14
#include "version.h"
15

16
#include <deque>
17
#include <memory>
18
#include <optional>
19
#include <string>
20
#include <unordered_map>
21
#include <vector>
22

23
#include <matchbox.hh>
24

25

26
namespace celerity::detail::live_executor_detail {
27

28
#if CELERITY_TRACY_SUPPORT
29

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

39
        struct async_zone {
40
                size_t submission_idx_on_lane;
41
                instruction_info info;
42
                std::string trace;
43
        };
44

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

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

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

67
                explicit async_lane_state(const async_lane_id& id);
68
        };
69

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

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

74
        tracy_detail::plot<int64_t> assigned_instructions_plot{"assigned instructions"};
75
        tracy_detail::plot<int64_t> assignment_queue_length_plot{"assignment queue length"};
76

77
        static instruction_info make_instruction_info(const instruction& instr);
78

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

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

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

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

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

95

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

120
tracy_integration::async_lane_cursor tracy_integration::get_async_lane_cursor(const out_of_order_engine::assignment& assignment) {
121
        const auto target = assignment.target;
122
        // on alloc_queue, assignment.device signals on which device to allocate memory, not on which device to queue the instruction
123
        const auto device = assignment.target == out_of_order_engine::target::device_queue ? assignment.device : std::nullopt;
124
        // 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`,
125
        // to continue using `nullopt` to pick an arbitrary empty lane in the code below for the immediate-but-async send / receive instruction types.
126
        const auto local_lane_id = assignment.target == out_of_order_engine::target::alloc_queue ? std::optional<size_t>(0) : assignment.lane;
127

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

141
tracy_integration::instruction_info tracy_integration::make_instruction_info(const instruction& instr) {
142
        tracy_integration::instruction_info info;
143
        info.iid = instr.get_id();
144

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

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

177
#undef CELERITY_DETAIL_BEGIN_INSTRUCTION_ZONE
178

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

191
        return info;
192
}
193

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

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

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

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

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

230
                TracyCZoneText(ctx, text.data(), text.size());
231
        }
232

233
        TracyCZoneEnd(ctx);
234
}
235

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

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

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

259
        return cursor;
260
}
261

262
void tracy_integration::retire_async_instruction(const async_lane_cursor& cursor, const async_event& event) {
263
        auto& lane = async_lanes.at(cursor.global_lane_id);
264

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

285
#endif // CELERITY_DETAIL_ENABLE_TRACY
286

287

288
#if CELERITY_ACCESSOR_BOUNDARY_CHECK
289

290
struct boundary_check_info {
291
        struct accessor_info {
292
                detail::buffer_id buffer_id = 0;
293
                std::string buffer_name;
294
                box<3> accessible_box;
295
        };
296

297
        detail::task_type task_type;
298
        detail::task_id task_id;
299
        std::string task_name;
300

301
        oob_bounding_box* illegal_access_bounding_boxes = nullptr;
302
        std::vector<accessor_info> accessors;
303

304
        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) {}
2,960✔
305
};
306

307
#endif // CELERITY_ACCESSOR_BOUNDARY_CHECK
308

309

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

314

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

322
struct executor_impl {
323
        const std::unique_ptr<detail::backend> backend;
324
        communicator* const root_communicator;
325
        double_buffered_queue<submission>* const submission_queue;
326
        live_executor::delegate* const delegate;
327
        const live_executor::policy_set policy;
328

329
        receive_arbiter recv_arbiter{*root_communicator};
330
        out_of_order_engine engine{backend->get_system_info()};
331

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

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

343
        CELERITY_DETAIL_IF_TRACY_SUPPORTED(std::unique_ptr<tracy_integration> tracy;)
344

345
        executor_impl(std::unique_ptr<detail::backend> backend, communicator* root_comm, double_buffered_queue<submission>& submission_queue,
346
            live_executor::delegate* dlg, const live_executor::policy_set& policy);
347

348
        void run();
349
        void poll_in_flight_async_instructions();
350
        void poll_submission_queue();
351
        void try_issue_one_instruction();
352
        void retire_async_instruction(instruction_id iid, async_instruction_state& async);
353
        void check_progress();
354

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

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

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

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

386
        std::vector<closure_hydrator::accessor_info> make_accessor_infos(const buffer_access_allocation_map& amap) const;
387

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

393
        void collect(const instruction_garbage& garbage);
394
};
395

396
executor_impl::executor_impl(std::unique_ptr<detail::backend> backend, communicator* const root_comm, double_buffered_queue<submission>& submission_queue,
233✔
397
    live_executor::delegate* const dlg, const live_executor::policy_set& policy)
233✔
398
    : backend(std::move(backend)), root_communicator(root_comm), submission_queue(&submission_queue), delegate(dlg), policy(policy) //
699✔
399
{
400
        CELERITY_DETAIL_IF_TRACY_ENABLED(tracy = std::make_unique<tracy_integration>();)
401
}
233✔
402

403
void executor_impl::run() {
233✔
404
        closure_hydrator::make_available();
233✔
405
        backend->init();
233✔
406

407
        uint8_t check_overflow_counter = 0;
233✔
408
        for(;;) {
409
                if(engine.is_idle()) {
4,308,361✔
410
                        if(!expecting_more_submissions) break; // shutdown complete
3,507✔
411

412
                        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::starve", DarkSlateGray);
413
                        submission_queue->wait_while_empty(); // we are stalled on the scheduler, suspend thread
3,274✔
414
                        last_progress_timestamp.reset();      // do not treat suspension as being stuck
3,274✔
415
                }
416

417
                recv_arbiter.poll_communicator();
4,308,128✔
418
                poll_in_flight_async_instructions();
4,308,128✔
419
                poll_submission_queue();
4,308,128✔
420
                try_issue_one_instruction(); // potentially expensive, so only issue one per loop to continue checking for async completion in between
4,308,128✔
421

422
                if(++check_overflow_counter == 0) { // once every 256 iterations
4,308,128✔
423
                        backend->check_async_errors();
16,766✔
424
                        check_progress();
16,766✔
425
                }
426
        }
427

428
        assert(in_flight_async_instructions.empty());
233✔
429
        // check that for each alloc_instruction, we executed a corresponding free_instruction
430
        assert(std::all_of(allocations.begin(), allocations.end(),
466✔
431
            [](const std::pair<allocation_id, void*>& p) { return p.first == null_allocation_id || p.first.get_memory_id() == user_memory_id; }));
432
        // check that for each track_host_object_instance, we executed a destroy_host_object_instruction
433
        assert(host_object_instances.empty());
233✔
434

435
        closure_hydrator::teardown();
233✔
436
}
233✔
437

438
void executor_impl::poll_in_flight_async_instructions() {
4,308,128✔
439
        // collect completed instruction ids up-front, since retire_async_instruction would alter the execution front
440
        std::vector<instruction_id> completed_now; // std::vector because it will be empty in the common case
4,308,128✔
441
        for(const auto iid : engine.get_execution_front()) {
11,126,372✔
442
                if(in_flight_async_instructions.at(iid).event.is_complete()) { completed_now.push_back(iid); }
6,818,244✔
443
        }
444
        for(const auto iid : completed_now) {
4,317,828✔
445
                retire_async_instruction(iid, in_flight_async_instructions.at(iid));
9,700✔
446
                in_flight_async_instructions.erase(iid);
9,700✔
447
                made_progress = true;
9,700✔
448
        }
449

450
        CELERITY_DETAIL_IF_TRACY_ENABLED(tracy->assigned_instructions_plot.update(in_flight_async_instructions.size()));
451
}
8,616,256✔
452

453
void executor_impl::poll_submission_queue() {
4,308,128✔
454
        for(auto& submission : submission_queue->pop_all()) {
4,314,573✔
455
                CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::fetch", Gray);
456
                matchbox::match(
6,445✔
457
                    submission,
458
                    [&](const instruction_pilot_batch& batch) {
6,445✔
459
                            for(const auto incoming_instr : batch.instructions) {
17,789✔
460
                                    engine.submit(incoming_instr);
11,550✔
461
                            }
462
                            for(const auto& pilot : batch.pilots) {
6,901✔
463
                                    root_communicator->send_outbound_pilot(pilot);
662✔
464
                            }
465
                            CELERITY_DETAIL_IF_TRACY_ENABLED(tracy->assignment_queue_length_plot.update(engine.get_assignment_queue_length()));
466
                    },
6,239✔
467
                    [&](const user_allocation_transfer& uat) {
12,890✔
468
                            assert(uat.aid != null_allocation_id);
108✔
469
                            assert(uat.aid.get_memory_id() == user_memory_id);
108✔
470
                            assert(allocations.count(uat.aid) == 0);
108✔
471
                            allocations.emplace(uat.aid, uat.ptr);
108✔
472
                    },
108✔
473
                    [&](host_object_transfer& hot) {
12,890✔
474
                            assert(host_object_instances.count(hot.hoid) == 0);
30✔
475
                            host_object_instances.emplace(hot.hoid, std::move(hot.instance));
30✔
476
                    },
30✔
477
                    [&](reducer_transfer& rt) {
12,890✔
478
                            assert(reducers.count(rt.rid) == 0);
68✔
479
                            reducers.emplace(rt.rid, std::move(rt.reduction));
68✔
480
                    });
68✔
481
        }
482
}
4,308,128✔
483

484
void executor_impl::retire_async_instruction(const instruction_id iid, async_instruction_state& async) {
9,700✔
485
        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::retire", Brown);
486

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

506
        if(spdlog::should_log(spdlog::level::trace)) {
9,700✔
507
                if(const auto native_time = async.event.get_native_execution_time(); native_time.has_value()) {
5,699!
NEW
508
                        CELERITY_TRACE("[executor] retired I{} after {:.2f}", iid, as_sub_second(*native_time));
×
509
                } else {
510
                        CELERITY_TRACE("[executor] retired I{}", iid);
5,699!
511
                }
512
        }
513

514
        CELERITY_DETAIL_IF_TRACY_ENABLED(tracy->retire_async_instruction(*async.tracy_lane_cursor, async.event));
515

516
        if(async.alloc_aid != null_allocation_id) {
9,700✔
517
                const auto ptr = async.event.get_result();
1,097✔
518
                assert(ptr != nullptr && "backend allocation returned nullptr");
1,097✔
519
                CELERITY_TRACE("[executor] {} allocated as {}", async.alloc_aid, ptr);
1,097!
520
                assert(allocations.count(async.alloc_aid) == 0);
1,097✔
521
                allocations.emplace(async.alloc_aid, ptr);
1,097✔
522
        }
523

524
        engine.complete_assigned(iid);
9,700✔
525

526
        CELERITY_DETAIL_IF_TRACY_ENABLED(tracy->assignment_queue_length_plot.update(engine.get_assignment_queue_length()));
527
}
9,700✔
528

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

537
        const auto iid = instr.get_id(); // instr may dangle after issue()
1,850✔
538

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

548
        issue(instr); // completes immediately - instr may now dangle
1,850✔
549

550
        CELERITY_DETAIL_IF_TRACY_ENABLED({
551
                tracy->end_instruction_zone(ctx, info, tracy->last_instruction_trace);
552
                TracyFiberLeave;
553
        })
554

555
        engine.complete_assigned(iid);
1,850✔
556

557
        CELERITY_DETAIL_IF_TRACY_ENABLED({
558
                tracy->assigned_instructions_plot.update(in_flight_async_instructions.size());
559
                tracy->assignment_queue_length_plot.update(engine.get_assignment_queue_length());
560
        })
561
}
1,850✔
562

563
template <typename Instr>
564
auto executor_impl::dispatch(const Instr& instr, const out_of_order_engine::assignment& assignment)
9,700✔
565
    // SFINAE: there is an `issue_async` overload above for the concrete Instr type
566
    -> decltype(issue_async(instr, assignment, std::declval<async_instruction_state&>())) //
567
{
568
        CELERITY_DETAIL_IF_TRACY_SUPPORTED(tracy_integration::instruction_info info);
569
        CELERITY_DETAIL_IF_TRACY_ENABLED(info = tracy_integration::make_instruction_info(instr));
570

571
        auto& async = in_flight_async_instructions.emplace(assignment.instruction->get_id(), async_instruction_state{}).first->second;
19,400✔
572
        issue_async(instr, assignment, async); // stores event in `async` and completes asynchronously
9,700✔
573
        // instr may now dangle
574

575
        CELERITY_DETAIL_IF_TRACY_ENABLED({
576
                async.tracy_lane_cursor = tracy->issue_async_instruction(std::move(info), assignment, std::move(tracy->last_instruction_trace));
577
                tracy->assigned_instructions_plot.update(in_flight_async_instructions.size());
578
        })
579
}
19,400✔
580

581
void executor_impl::try_issue_one_instruction() {
4,308,128✔
582
        auto assignment = engine.assign_one();
4,308,128✔
583
        if(!assignment.has_value()) return;
4,308,128✔
584

585
        CELERITY_DETAIL_IF_TRACY_ENABLED(tracy->assignment_queue_length_plot.update(engine.get_assignment_queue_length()));
586

587
        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::issue", Blue);
588
        matchbox::match(*assignment->instruction, [&](const auto& instr) { dispatch(instr, *assignment); });
23,100✔
589
        made_progress = true;
11,550✔
590
}
591

592
void executor_impl::check_progress() {
16,766✔
593
        if(!policy.progress_warning_timeout.has_value()) return;
16,766!
594

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

615
void executor_impl::issue(const clone_collective_group_instruction& ccginstr) {
33✔
616
        const auto original_cgid = ccginstr.get_original_collective_group_id();
33✔
617
        assert(original_cgid != non_collective_group_id);
33✔
618
        assert(original_cgid == root_collective_group_id || cloned_communicators.count(original_cgid) != 0);
33✔
619

620
        const auto new_cgid = ccginstr.get_new_collective_group_id();
33✔
621
        assert(new_cgid != non_collective_group_id && new_cgid != root_collective_group_id);
33✔
622
        assert(cloned_communicators.count(new_cgid) == 0);
33✔
623

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

626
        const auto original_communicator = original_cgid == root_collective_group_id ? root_communicator : cloned_communicators.at(original_cgid).get();
33✔
627
        cloned_communicators.emplace(new_cgid, original_communicator->collective_clone());
33✔
628
}
33✔
629

630

631
void executor_impl::issue(const split_receive_instruction& srinstr) {
20✔
632
        CELERITY_DETAIL_TRACE_INSTRUCTION(srinstr, "split receive {} {}x{} bytes into {} ({}),", srinstr.get_transfer_id(), srinstr.get_requested_region(),
20✔
633
            srinstr.get_element_size(), srinstr.get_dest_allocation_id(), srinstr.get_allocated_box());
634

635
        const auto allocation = allocations.at(srinstr.get_dest_allocation_id());
20✔
636
        recv_arbiter.begin_split_receive(
20✔
637
            srinstr.get_transfer_id(), srinstr.get_requested_region(), allocation, srinstr.get_allocated_box(), srinstr.get_element_size());
638
}
20✔
639

640
void executor_impl::issue(const fill_identity_instruction& fiinstr) {
19✔
641
        CELERITY_DETAIL_TRACE_INSTRUCTION(
19✔
642
            fiinstr, "fill identity {} x{} values for R{}", fiinstr.get_allocation_id(), fiinstr.get_num_values(), fiinstr.get_reduction_id());
643

644
        const auto allocation = allocations.at(fiinstr.get_allocation_id());
19✔
645
        const auto& reduction = *reducers.at(fiinstr.get_reduction_id());
19✔
646
        reduction.fill_identity(allocation, fiinstr.get_num_values());
19✔
647
}
19✔
648

649
void executor_impl::issue(const reduce_instruction& rinstr) {
47✔
650
        CELERITY_DETAIL_TRACE_INSTRUCTION(rinstr, "reduce {} x{} values into {} for R{}", rinstr.get_source_allocation_id(), rinstr.get_num_source_values(),
47✔
651
            rinstr.get_dest_allocation_id(), rinstr.get_reduction_id());
652

653
        const auto gather_allocation = allocations.at(rinstr.get_source_allocation_id());
47✔
654
        const auto dest_allocation = allocations.at(rinstr.get_dest_allocation_id());
47✔
655
        const auto& reduction = *reducers.at(rinstr.get_reduction_id());
47✔
656
        reduction.reduce(dest_allocation, gather_allocation, rinstr.get_num_source_values());
47✔
657
}
47✔
658

659
void executor_impl::issue(const fence_instruction& finstr) { // NOLINT(readability-make-member-function-const, readability-convert-member-functions-to-static)
63✔
660
        CELERITY_DETAIL_TRACE_INSTRUCTION(finstr, "fence");
63✔
661

662
        finstr.get_promise()->fulfill();
63✔
663
}
63✔
664

665
void executor_impl::issue(const destroy_host_object_instruction& dhoinstr) {
30✔
666
        assert(host_object_instances.count(dhoinstr.get_host_object_id()) != 0);
30✔
667
        CELERITY_DETAIL_TRACE_INSTRUCTION(dhoinstr, "destroy H{}", dhoinstr.get_host_object_id());
30✔
668

669
        host_object_instances.erase(dhoinstr.get_host_object_id());
30✔
670
}
30✔
671

672
void executor_impl::issue(const horizon_instruction& hinstr) {
840✔
673
        CELERITY_DETAIL_TRACE_INSTRUCTION(hinstr, "horizon");
840✔
674

675
        if(delegate != nullptr) { delegate->horizon_reached(hinstr.get_horizon_task_id()); }
840!
676
        collect(hinstr.get_garbage());
840✔
677

678
        CELERITY_DETAIL_IF_TRACY_ENABLED(FrameMarkNamed("Horizon"));
679
}
840✔
680

681
void executor_impl::issue(const epoch_instruction& einstr) {
798✔
682
        switch(einstr.get_epoch_action()) {
798!
683
        case epoch_action::none: //
563✔
684
                CELERITY_DETAIL_TRACE_INSTRUCTION(einstr, "epoch");
563✔
685
                break;
563✔
686
        case epoch_action::barrier: //
2✔
687
                CELERITY_DETAIL_TRACE_INSTRUCTION(einstr, "epoch (barrier)");
2✔
688
                root_communicator->collective_barrier();
2✔
689
                break;
2✔
690
        case epoch_action::shutdown: //
233✔
691
                CELERITY_DETAIL_TRACE_INSTRUCTION(einstr, "epoch (shutdown)");
233✔
692
                expecting_more_submissions = false;
233✔
693
                break;
233✔
694
        }
695
        if(delegate != nullptr && einstr.get_epoch_task_id() != 0 /* TODO task_manager doesn't expect us to actually execute the init epoch */) {
798!
696
                delegate->epoch_reached(einstr.get_epoch_task_id());
565✔
697
        }
698
        collect(einstr.get_garbage());
798✔
699

700
        CELERITY_DETAIL_IF_TRACY_ENABLED(FrameMarkNamed("Horizon"));
701
        CELERITY_DETAIL_IF_TRACY_ENABLED(FrameMark); // top-level "Frame"
702
}
798✔
703

704
void executor_impl::issue_async(const alloc_instruction& ainstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async) {
1,097✔
705
        assert(ainstr.get_allocation_id().get_memory_id() != user_memory_id);
1,097✔
706
        assert(assignment.target == out_of_order_engine::target::alloc_queue);
1,097✔
707
        assert(!assignment.lane.has_value());
1,097✔
708
        assert(assignment.device.has_value() == (ainstr.get_allocation_id().get_memory_id() > host_memory_id));
1,097✔
709

710
        CELERITY_DETAIL_TRACE_INSTRUCTION(ainstr, "alloc {}, {} % {} bytes", ainstr.get_allocation_id(), ainstr.get_size_bytes(), ainstr.get_alignment_bytes());
1,097✔
711

712
        if(assignment.device.has_value()) {
1,097✔
713
                async.event = backend->enqueue_device_alloc(*assignment.device, ainstr.get_size_bytes(), ainstr.get_alignment_bytes());
551✔
714
        } else {
715
                async.event = backend->enqueue_host_alloc(ainstr.get_size_bytes(), ainstr.get_alignment_bytes());
546✔
716
        }
717
        async.alloc_aid = ainstr.get_allocation_id(); // setting alloc_aid != null will make `retire_async_instruction` insert the result into `allocations`
1,097✔
718
}
1,097✔
719

720
void executor_impl::issue_async(const free_instruction& finstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async) {
1,097✔
721
        const auto it = allocations.find(finstr.get_allocation_id());
1,097✔
722
        assert(it != allocations.end());
1,097✔
723
        const auto ptr = it->second;
1,097✔
724
        allocations.erase(it);
1,097✔
725

726
        CELERITY_DETAIL_TRACE_INSTRUCTION(finstr, "free {}", finstr.get_allocation_id());
1,097✔
727

728
        if(assignment.device.has_value()) {
1,097✔
729
                async.event = backend->enqueue_device_free(*assignment.device, ptr);
551✔
730
        } else {
731
                async.event = backend->enqueue_host_free(ptr);
546✔
732
        }
733
}
1,097✔
734

735
void executor_impl::issue_async(const copy_instruction& cinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async) {
2,320✔
736
        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::issue_copy", Green4);
737

738
        assert(assignment.target == out_of_order_engine::target::host_queue || assignment.target == out_of_order_engine::target::device_queue);
2,320✔
739
        assert((assignment.target == out_of_order_engine::target::device_queue) == assignment.device.has_value());
2,320✔
740
        assert(assignment.lane.has_value());
2,320✔
741

742
        CELERITY_DETAIL_TRACE_INSTRUCTION(cinstr, "copy {} ({}) -> {} ({}); {}x{} bytes, {} bytes total", cinstr.get_source_allocation_id(),
2,320✔
743
            cinstr.get_source_layout(), cinstr.get_dest_allocation_id(), cinstr.get_dest_layout(), cinstr.get_copy_region(), cinstr.get_element_size(),
744
            cinstr.get_copy_region().get_area() * cinstr.get_element_size());
745

746
        const auto source_base = allocations.at(cinstr.get_source_allocation_id());
2,320✔
747
        const auto dest_base = allocations.at(cinstr.get_dest_allocation_id());
2,320✔
748

749
        if(assignment.device.has_value()) {
2,320✔
750
                async.event = backend->enqueue_device_copy(*assignment.device, *assignment.lane, source_base, dest_base, cinstr.get_source_layout(),
4,258✔
751
                    cinstr.get_dest_layout(), cinstr.get_copy_region(), cinstr.get_element_size());
2,129✔
752
        } else {
753
                async.event = backend->enqueue_host_copy(*assignment.lane, source_base, dest_base, cinstr.get_source_layout(), cinstr.get_dest_layout(),
382✔
754
                    cinstr.get_copy_region(), cinstr.get_element_size());
191✔
755
        }
756
}
2,320✔
757

758
std::string format_access_log(const buffer_access_allocation_map& map) {
3,483✔
759
        std::string acc_log;
3,483✔
760
        for(size_t i = 0; i < map.size(); ++i) {
5,943✔
761
                auto& aa = map[i];
2,460✔
762
                const auto accessed_box_in_allocation = box(aa.accessed_box_in_buffer.get_min() - aa.allocated_box_in_buffer.get_offset(),
7,380✔
763
                    aa.accessed_box_in_buffer.get_max() - aa.allocated_box_in_buffer.get_offset());
7,380✔
764
                fmt::format_to(std::back_inserter(acc_log), "{} {} {}", i == 0 ? "; accessing" : ",", aa.allocation_id, accessed_box_in_allocation);
4,920✔
765
        }
766
        return acc_log;
3,483✔
767
}
×
768

769
void executor_impl::issue_async(const device_kernel_instruction& dkinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async) {
853✔
770
        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::issue_device_kernel", Yellow2);
771

772
        assert(assignment.target == out_of_order_engine::target::device_queue);
853✔
773
        assert(assignment.device == dkinstr.get_device_id());
853✔
774
        assert(assignment.lane.has_value());
853✔
775

776
        CELERITY_DETAIL_TRACE_INSTRUCTION(dkinstr, "device kernel on D{}, {}{}; estimated global memory traffic: {:.2f}", dkinstr.get_device_id(),
853✔
777
            dkinstr.get_execution_range(), format_access_log(dkinstr.get_access_allocations()),
778
            as_decimal_size(dkinstr.get_estimated_global_memory_traffic_bytes()));
779

780
        auto accessor_infos = make_accessor_infos(dkinstr.get_access_allocations());
853✔
781
#if CELERITY_ACCESSOR_BOUNDARY_CHECK
782
        async.oob_info = attach_boundary_check_info(
1,706✔
783
            accessor_infos, dkinstr.get_access_allocations(), dkinstr.get_oob_task_type(), dkinstr.get_oob_task_id(), dkinstr.get_oob_task_name());
853✔
784
#endif
785

786
        const auto& reduction_allocs = dkinstr.get_reduction_allocations();
853✔
787
        std::vector<void*> reduction_ptrs(reduction_allocs.size());
2,559✔
788
        for(size_t i = 0; i < reduction_allocs.size(); ++i) {
958✔
789
                reduction_ptrs[i] = allocations.at(reduction_allocs[i].allocation_id);
105✔
790
        }
791

792
        async.event = backend->enqueue_device_kernel(
3,412✔
793
            dkinstr.get_device_id(), *assignment.lane, dkinstr.get_launcher(), std::move(accessor_infos), dkinstr.get_execution_range(), reduction_ptrs);
2,559✔
794
}
1,706✔
795

796
void executor_impl::issue_async(const host_task_instruction& htinstr, const out_of_order_engine::assignment& assignment, async_instruction_state& async) {
3,235✔
797
        assert(assignment.target == out_of_order_engine::target::host_queue);
3,235✔
798
        assert(!assignment.device.has_value());
3,235✔
799
        assert(assignment.lane.has_value());
3,235✔
800

801
        CELERITY_DETAIL_TRACE_INSTRUCTION(htinstr, "host task, {}{}", htinstr.get_execution_range(), format_access_log(htinstr.get_access_allocations()));
3,235✔
802

803
        auto accessor_infos = make_accessor_infos(htinstr.get_access_allocations());
3,235✔
804
#if CELERITY_ACCESSOR_BOUNDARY_CHECK
805
        async.oob_info = attach_boundary_check_info(
6,470✔
806
            accessor_infos, htinstr.get_access_allocations(), htinstr.get_oob_task_type(), htinstr.get_oob_task_id(), htinstr.get_oob_task_name());
3,235✔
807
#endif
808

809
        const auto& execution_range = htinstr.get_execution_range();
3,235✔
810
        const auto collective_comm =
811
            htinstr.get_collective_group_id() != non_collective_group_id ? cloned_communicators.at(htinstr.get_collective_group_id()).get() : nullptr;
3,235!
812

813
        async.event = backend->enqueue_host_task(*assignment.lane, htinstr.get_launcher(), std::move(accessor_infos), execution_range, collective_comm);
3,235✔
814
}
6,470✔
815

816
void executor_impl::issue_async(
662✔
817
    const send_instruction& sinstr, [[maybe_unused]] const out_of_order_engine::assignment& assignment, async_instruction_state& async) //
818
{
819
        assert(assignment.target == out_of_order_engine::target::immediate);
662✔
820

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

824
        const auto allocation_base = allocations.at(sinstr.get_source_allocation_id());
662✔
825
        const communicator::stride stride{
662✔
826
            sinstr.get_source_allocation_range(),
662✔
827
            subrange<3>{sinstr.get_offset_in_source_allocation(), sinstr.get_send_range()},
828
            sinstr.get_element_size(),
662✔
829
        };
662✔
830
        async.event = root_communicator->send_payload(sinstr.get_dest_node_id(), sinstr.get_message_id(), allocation_base, stride);
662✔
831
}
662✔
832

833
void executor_impl::issue_async(
370✔
834
    const receive_instruction& rinstr, [[maybe_unused]] const out_of_order_engine::assignment& assignment, async_instruction_state& async) //
835
{
836
        assert(assignment.target == out_of_order_engine::target::immediate);
370✔
837

838
        CELERITY_DETAIL_TRACE_INSTRUCTION(rinstr, "receive {} {}x{} bytes into {} ({})", rinstr.get_transfer_id(), rinstr.get_requested_region(),
370✔
839
            rinstr.get_element_size(), rinstr.get_dest_allocation_id(), rinstr.get_allocated_box());
840

841
        const auto allocation = allocations.at(rinstr.get_dest_allocation_id());
370✔
842
        async.event =
370✔
843
            recv_arbiter.receive(rinstr.get_transfer_id(), rinstr.get_requested_region(), allocation, rinstr.get_allocated_box(), rinstr.get_element_size());
370✔
844
}
370✔
845

846
void executor_impl::issue_async(
48✔
847
    const await_receive_instruction& arinstr, [[maybe_unused]] const out_of_order_engine::assignment& assignment, async_instruction_state& async) //
848
{
849
        assert(assignment.target == out_of_order_engine::target::immediate);
48✔
850

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

853
        async.event = recv_arbiter.await_split_receive_subregion(arinstr.get_transfer_id(), arinstr.get_received_region());
48✔
854
}
48✔
855

856
void executor_impl::issue_async(
18✔
857
    const gather_receive_instruction& grinstr, [[maybe_unused]] const out_of_order_engine::assignment& assignment, async_instruction_state& async) //
858
{
859
        assert(assignment.target == out_of_order_engine::target::immediate);
18✔
860

861
        CELERITY_DETAIL_TRACE_INSTRUCTION(
18✔
862
            grinstr, "gather receive {} into {}, {} bytes / node", grinstr.get_transfer_id(), grinstr.get_dest_allocation_id(), grinstr.get_node_chunk_size());
863

864
        const auto allocation = allocations.at(grinstr.get_dest_allocation_id());
18✔
865
        async.event = recv_arbiter.gather_receive(grinstr.get_transfer_id(), allocation, grinstr.get_node_chunk_size());
18✔
866
}
18✔
867

868
void executor_impl::collect(const instruction_garbage& garbage) {
1,638✔
869
        for(const auto rid : garbage.reductions) {
1,705✔
870
                assert(reducers.count(rid) != 0);
67✔
871
                reducers.erase(rid);
67✔
872
        }
873
        for(const auto aid : garbage.user_allocations) {
1,746✔
874
                assert(aid.get_memory_id() == user_memory_id);
108✔
875
                assert(allocations.count(aid) != 0);
108✔
876
                allocations.erase(aid);
108✔
877
        }
878
}
1,638✔
879

880
std::vector<closure_hydrator::accessor_info> executor_impl::make_accessor_infos(const buffer_access_allocation_map& amap) const {
4,088✔
881
        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::make_accessor_info", Magenta3);
882

883
        std::vector<closure_hydrator::accessor_info> accessor_infos(amap.size());
12,264✔
884
        for(size_t i = 0; i < amap.size(); ++i) {
7,654✔
885
                const auto ptr = allocations.at(amap[i].allocation_id);
3,566✔
886
                accessor_infos[i] = closure_hydrator::accessor_info{ptr, amap[i].allocated_box_in_buffer, amap[i].accessed_box_in_buffer};
3,566✔
887
        }
888
        return accessor_infos;
4,088✔
889
}
×
890

891
#if CELERITY_ACCESSOR_BOUNDARY_CHECK
892
std::unique_ptr<boundary_check_info> executor_impl::attach_boundary_check_info(std::vector<closure_hydrator::accessor_info>& accessor_infos,
4,088✔
893
    const buffer_access_allocation_map& amap, task_type tt, task_id tid, const std::string& task_name) const //
894
{
895
        if(amap.empty()) return nullptr;
4,088✔
896

897
        CELERITY_DETAIL_TRACY_ZONE_SCOPED("executor::oob_init", Red);
898
        auto oob_info = std::make_unique<boundary_check_info>(tt, tid, task_name);
2,960✔
899

900
        oob_info->illegal_access_bounding_boxes = static_cast<oob_bounding_box*>(backend->debug_alloc(amap.size() * sizeof(oob_bounding_box)));
2,960✔
901
        std::uninitialized_default_construct_n(oob_info->illegal_access_bounding_boxes, amap.size());
2,960✔
902

903
        oob_info->accessors.resize(amap.size());
2,960✔
904
        for(size_t i = 0; i < amap.size(); ++i) {
6,526✔
905
                oob_info->accessors[i] = boundary_check_info::accessor_info{amap[i].oob_buffer_id, amap[i].oob_buffer_name, amap[i].accessed_box_in_buffer};
3,566✔
906
                accessor_infos[i].out_of_bounds_indices = oob_info->illegal_access_bounding_boxes + i;
3,566✔
907
        }
908
        return oob_info;
2,960✔
909
}
2,960✔
910
#endif // CELERITY_ACCESSOR_BOUNDARY_CHECK
911

912
} // namespace celerity::detail::live_executor_detail
913

914
namespace celerity::detail {
915

916
live_executor::live_executor(std::unique_ptr<backend> backend, std::unique_ptr<communicator> root_comm, delegate* const dlg, const policy_set& policy)
233✔
917
    : m_root_comm(std::move(root_comm)), m_thread(&live_executor::thread_main, this, std::move(backend), dlg, policy) //
233✔
918
{
919
        set_thread_name(m_thread.native_handle(), "cy-executor");
699✔
920
}
233✔
921

922
live_executor::~live_executor() {
466✔
923
        m_thread.join(); // thread_main will exit only after executing shutdown epoch
233✔
924
}
466✔
925

926
void live_executor::track_user_allocation(const allocation_id aid, void* const ptr) {
108✔
927
        m_submission_queue.push(live_executor_detail::user_allocation_transfer{aid, ptr});
108✔
928
}
108✔
929

930
void live_executor::track_host_object_instance(const host_object_id hoid, std::unique_ptr<host_object_instance> instance) {
30✔
931
        assert(instance != nullptr);
30✔
932
        m_submission_queue.push(live_executor_detail::host_object_transfer{hoid, std::move(instance)});
30✔
933
}
30✔
934

935
void live_executor::track_reducer(const reduction_id rid, std::unique_ptr<reducer> reducer) {
68✔
936
        assert(reducer != nullptr);
68✔
937
        m_submission_queue.push(live_executor_detail::reducer_transfer{rid, std::move(reducer)});
68✔
938
}
68✔
939

940
void live_executor::submit(std::vector<const instruction*> instructions, std::vector<outbound_pilot> pilots) {
6,239✔
941
        m_submission_queue.push(live_executor_detail::instruction_pilot_batch{std::move(instructions), std::move(pilots)});
6,239!
942
}
6,239✔
943

944
void live_executor::thread_main(std::unique_ptr<backend> backend, delegate* const dlg, const policy_set& policy) {
233✔
945
        CELERITY_DETAIL_TRACY_SET_THREAD_NAME_AND_ORDER("cy-executor", tracy_detail::thread_order::executor);
946
        try {
947
                live_executor_detail::executor_impl(std::move(backend), m_root_comm.get(), m_submission_queue, dlg, policy).run();
233✔
948
        }
949
        // LCOV_EXCL_START
950
        catch(const std::exception& e) {
951
                CELERITY_CRITICAL("[executor] {}", e.what());
952
                std::abort();
953
        }
954
        // LCOV_EXCL_STOP
955
}
233✔
956

957
} // 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

© 2025 Coveralls, Inc