-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathaicpu_executor.cpp
More file actions
1217 lines (1023 loc) · 51.1 KB
/
aicpu_executor.cpp
File metadata and controls
1217 lines (1023 loc) · 51.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) PyPTO Contributors.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
* -----------------------------------------------------------------------------------------------------------
*/
// NOLINTBEGIN
#include <atomic>
#include <cstdint>
#include <cstdio>
#include <mutex>
#include "aicpu/device_log.h" // NOLINT(clang-diagnostic-error)
#include "aicpu/device_time.h"
#include "aicpu/performance_collector_aicpu.h"
#include "aicpu/platform_regs.h"
#include "common/memory_barrier.h"
#include "common/perf_profiling.h"
#include "common/platform_config.h"
#include "common/unified_log.h"
#include "runtime.h"
#include "spin_hint.h"
constexpr int MAX_AICPU_THREADS = PLATFORM_MAX_AICPU_THREADS;
constexpr int MAX_CORES_PER_THREAD = PLATFORM_MAX_CORES_PER_THREAD;
constexpr int MAX_CORES = PLATFORM_MAX_CORES;
// Core information for discovery
struct CoreInfo {
int worker_id; // Index in runtime.workers[]
uint32_t physical_core_id; // Hardware physical core ID (from AICore)
uint64_t reg_addr; // Cached register address for fast access
CoreType core_type;
};
struct AicpuExecutor {
// ===== Thread management state =====
std::atomic<int> thread_idx_{0};
std::atomic<bool> initialized_{false};
std::atomic<bool> init_done_{false};
std::atomic<bool> init_failed_{false};
std::atomic<bool> finished_{false};
int thread_num_{0};
int cores_total_num_{0};
int thread_cores_num_[MAX_AICPU_THREADS]{}; // Total cores (AIC+AIV) assigned to each thread
int aic_per_thread_{0}; // Max AIC cores per thread (ceil), used as local queue cap
int aiv_per_thread_{0}; // Max AIV cores per thread (ceil), used as local queue cap
int core_assignments_[MAX_AICPU_THREADS][MAX_CORES_PER_THREAD];
// Core discovery arrays (space-time tradeoff: avoid sorting)
CoreInfo aic_cores_[MAX_CORES_PER_THREAD];
CoreInfo aiv_cores_[MAX_CORES_PER_THREAD];
int aic_count_{0};
int aiv_count_{0};
// Fast lookup: core_id -> reg_addr
uint64_t core_id_to_reg_addr_[MAX_CORES_PER_THREAD];
// Platform register base address array (set via get_platform_regs())
uint64_t regs_{0};
// volatile required to prevent compiler from caching in registers during polling loops
volatile int pending_task_ids_[MAX_CORES]; // Task waiting for ACK
volatile int running_task_ids_[MAX_CORES]; // Task executing after ACK
bool core_first_dispatch_[MAX_CORES];
// Per-thread local ready queues
int cur_ready_queue_aic_[MAX_AICPU_THREADS][MAX_CORES_PER_THREAD];
int cur_ready_queue_aiv_[MAX_AICPU_THREADS][MAX_CORES_PER_THREAD];
int cur_ready_queue_aic_head_[MAX_AICPU_THREADS];
int cur_ready_queue_aic_tail_[MAX_AICPU_THREADS];
int cur_ready_queue_aiv_head_[MAX_AICPU_THREADS];
int cur_ready_queue_aiv_tail_[MAX_AICPU_THREADS];
// ===== Task queue state =====
std::mutex ready_queue_aic_mutex_;
int ready_queue_aic_[RUNTIME_MAX_TASKS];
std::atomic<int> ready_count_aic_{0};
int ready_queue_aic_head_{0}; // Circular queue: read position (front)
int ready_queue_aic_tail_{0}; // Circular queue: write position (back)
std::mutex ready_queue_aiv_mutex_;
int ready_queue_aiv_[RUNTIME_MAX_TASKS];
std::atomic<int> ready_count_aiv_{0};
int ready_queue_aiv_head_{0}; // Circular queue: read position (front)
int ready_queue_aiv_tail_{0}; // Circular queue: write position (back)
// Task execution tracking
std::atomic<int> completed_tasks_{0};
std::atomic<int> total_tasks_{0};
std::atomic<int> finished_count_{0};
// ===== Performance profiling state =====
uint64_t dispatch_timestamps_[RUNTIME_MAX_WORKER]; // Per-core AICPU dispatch timestamp
uint32_t core_dispatch_counts_[RUNTIME_MAX_WORKER]; // Per-core total dispatched task counter
// ===== Methods =====
int init(Runtime *runtime);
int handshake_all_cores(Runtime *runtime);
void assign_cores_to_threads();
void classify_and_distribute_initial_tasks(Runtime *runtime);
int resolve_and_dispatch(Runtime &runtime, int thread_idx, const int *cur_thread_cores, int core_num);
int shutdown_aicore(Runtime *runtime, int thread_idx, const int *cur_thread_cores);
int run(Runtime *runtime);
void deinit(Runtime *runtime);
void emergency_shutdown(Runtime *runtime);
void
diagnose_stuck_state(Runtime &runtime, int thread_idx, const int *cur_thread_cores, int core_num, Handshake *hank);
// Helper functions (inline to avoid linker issues, not always_inline to preserve barriers)
inline void resolve_task_dependencies(
Task *task, Runtime &runtime, int *cur_ready_queue_aic, int &cur_aic_tail, int &cur_aic_ready_count,
int *cur_ready_queue_aiv, int &cur_aiv_tail, int &cur_aiv_ready_count
);
inline bool try_dispatch_task(
int core_id, uint64_t reg_addr, CoreType core_type, int thread_idx, int *local_queue, int &head,
int &ready_count, bool profiling_enabled, Runtime &runtime
);
};
static AicpuExecutor g_aicpu_executor;
// ===== Helper Function Implementations =====
// Resolve dependencies: decrement fanin and enqueue newly ready tasks
inline void AicpuExecutor::resolve_task_dependencies(
Task *task, Runtime &runtime, int *cur_ready_queue_aic, int &cur_aic_tail, int &cur_aic_ready_count,
int *cur_ready_queue_aiv, int &cur_aiv_tail, int &cur_aiv_ready_count
) {
for (int j = 0; j < task->fanout_count; j++) {
int dep_id = task->fanout[j];
Task *dep = runtime.get_task(dep_id);
int prev_fanin = dep->fanin.fetch_sub(1, std::memory_order_acq_rel);
if (prev_fanin == 1) {
if (dep->core_type == CoreType::AIC) {
if (cur_aic_ready_count < aic_per_thread_) {
cur_ready_queue_aic[cur_aic_tail] = dep_id;
cur_aic_tail = (cur_aic_tail + 1) % MAX_CORES_PER_THREAD;
cur_aic_ready_count++;
} else {
std::lock_guard<std::mutex> lock(ready_queue_aic_mutex_);
ready_queue_aic_[ready_queue_aic_tail_] = dep_id;
ready_queue_aic_tail_ = (ready_queue_aic_tail_ + 1) % RUNTIME_MAX_TASKS;
ready_count_aic_.fetch_add(1, std::memory_order_release);
}
} else {
if (cur_aiv_ready_count < aiv_per_thread_) {
cur_ready_queue_aiv[cur_aiv_tail] = dep_id;
cur_aiv_tail = (cur_aiv_tail + 1) % MAX_CORES_PER_THREAD;
cur_aiv_ready_count++;
} else {
std::lock_guard<std::mutex> lock(ready_queue_aiv_mutex_);
ready_queue_aiv_[ready_queue_aiv_tail_] = dep_id;
ready_queue_aiv_tail_ = (ready_queue_aiv_tail_ + 1) % RUNTIME_MAX_TASKS;
ready_count_aiv_.fetch_add(1, std::memory_order_release);
}
}
}
}
}
// Try to dispatch a task from thread-local queue to a core
inline bool AicpuExecutor::try_dispatch_task(
int core_id, uint64_t reg_addr, CoreType core_type, int thread_idx, int *local_queue, int &head, int &ready_count,
bool profiling_enabled, Runtime &runtime
) {
if (ready_count <= 0) {
return false;
}
// Dequeue task from thread-local queue
int task_id = local_queue[head];
head = (head + 1) % MAX_CORES_PER_THREAD;
ready_count--;
// Profiling: buffer switch check
if (profiling_enabled) {
core_dispatch_counts_[core_id]++;
if (core_dispatch_counts_[core_id] >= PLATFORM_PROF_BUFFER_SIZE - 1) {
perf_aicpu_switch_buffer(&runtime, core_id, thread_idx);
core_dispatch_counts_[core_id] = 0;
}
}
const char *core_type_str = (core_type == CoreType::AIC) ? "AIC" : "AIV";
LOG_INFO(
"Thread %d: Dispatching %s task %d to core %d (running_id=%d)", thread_idx, core_type_str, task_id, core_id,
running_task_ids_[core_id]
);
// Set state before writing register to avoid race with AICore ACK
pending_task_ids_[core_id] = task_id;
write_reg(reg_addr, RegId::DATA_MAIN_BASE, static_cast<uint64_t>(task_id));
return true;
}
// ===== AicpuExecutor Method Implementations =====
int AicpuExecutor::init(Runtime *runtime) {
bool expected = false;
if (!initialized_.compare_exchange_strong(expected, true, std::memory_order_acq_rel, std::memory_order_acquire)) {
return 0;
}
LOG_INFO("AicpuExecutor: Initializing");
if (runtime == nullptr) {
LOG_ERROR("runtime is nullptr");
init_failed_.store(true, std::memory_order_release);
return -1;
}
// Read execution parameters from runtime
thread_num_ = runtime->sche_cpu_num;
// Simplified defensive check
if (thread_num_ < 1 || thread_num_ > MAX_AICPU_THREADS) {
LOG_ERROR("Invalid thread_num: %d (valid range: 1-%d)", thread_num_, MAX_AICPU_THREADS);
init_failed_.store(true, std::memory_order_release);
return -1;
}
// Initialize core_id_to_reg_addr_ array to 0 before handshake
for (int i = 0; i < MAX_CORES_PER_THREAD; i++) {
core_id_to_reg_addr_[i] = 0;
}
// Perform core discovery: handshake with all cores and collect core type information
int rc = handshake_all_cores(runtime);
if (rc != 0) {
LOG_ERROR("Core discovery failed");
init_failed_.store(true, std::memory_order_release);
return -1;
}
LOG_INFO("Config: threads=%d, cores=%d", thread_num_, cores_total_num_);
for (int i = 0; i < cores_total_num_; i++) {
pending_task_ids_[i] = AICPU_TASK_INVALID;
running_task_ids_[i] = AICPU_TASK_INVALID;
core_first_dispatch_[i] = true;
}
assign_cores_to_threads();
classify_and_distribute_initial_tasks(runtime);
total_tasks_.store(runtime->get_task_count(), std::memory_order_release);
completed_tasks_.store(0, std::memory_order_release);
finished_count_.store(0, std::memory_order_release);
for (int i = 0; i < RUNTIME_MAX_WORKER; i++) {
dispatch_timestamps_[i] = 0;
core_dispatch_counts_[i] = 0;
}
if (runtime->enable_profiling) {
perf_aicpu_init_profiling(runtime);
}
init_done_.store(true, std::memory_order_release);
LOG_INFO("AicpuExecutor: Init complete");
return 0;
}
/**
* Handshake with all AICore workers and discover core types
*
* This function performs centralized handshaking with all cores and collects
* their type information. By doing this in a single thread, we avoid redundant
* handshakes and enable dynamic core assignment.
*
* Protocol:
* 1. Send aicpu_ready=1 to all cores
* 2. Wait for each core's aicore_done response
* 3. Read core_type reported by each core
* 4. Classify cores into aic_cores_[] and aiv_cores_[] arrays
*
* @param runtime Runtime pointer
* @return 0 on success, -1 on failure
*/
int AicpuExecutor::handshake_all_cores(Runtime *runtime) {
Handshake *all_handshakes = reinterpret_cast<Handshake *>(runtime->workers);
cores_total_num_ = runtime->worker_count;
// Validate cores_total_num_ before using as array index
if (cores_total_num_ == 0 || cores_total_num_ > MAX_CORES_PER_THREAD) {
LOG_ERROR("Invalid cores_total_num %d (expected 1-%d)", cores_total_num_, MAX_CORES_PER_THREAD);
return -1;
}
aic_count_ = 0;
aiv_count_ = 0;
LOG_INFO("Core Discovery: Handshaking with %d cores", cores_total_num_);
// Step 1: Send handshake signal to all cores
for (int i = 0; i < cores_total_num_; i++) {
all_handshakes[i].aicpu_ready = 1;
}
// Get platform physical cores count for validation
uint32_t max_physical_cores_count = platform_get_physical_cores_count();
// Step 2: Wait for all cores to respond and collect core type information
bool handshake_failed = false;
for (int i = 0; i < cores_total_num_; i++) {
Handshake *hank = &all_handshakes[i];
// Wait for aicore_regs_ready signal
while (hank->aicore_regs_ready == 0) {
// Busy wait for core response
}
uint32_t physical_core_id = hank->physical_core_id;
// Validate physical_core_id before using as array index
if (physical_core_id >= max_physical_cores_count) {
LOG_ERROR(
"Core %d reported invalid physical_core_id=%u (platform max=%u)", i, physical_core_id,
max_physical_cores_count
);
handshake_failed = true;
continue;
}
// Get register address using physical_core_id
uint64_t *regs = reinterpret_cast<uint64_t *>(regs_);
uint64_t reg_addr = regs[physical_core_id];
// Initialize AICore registers after discovery (first round)
platform_init_aicore_regs(reg_addr);
hank->aicpu_regs_ready = 1;
while (hank->aicore_done == 0) {}
CoreType type = hank->core_type;
if (type == CoreType::AIC) {
aic_cores_[aic_count_].worker_id = i;
aic_cores_[aic_count_].physical_core_id = physical_core_id;
aic_cores_[aic_count_].reg_addr = reg_addr;
aic_cores_[aic_count_].core_type = type;
aic_count_++;
} else if (type == CoreType::AIV) {
aiv_cores_[aiv_count_].worker_id = i;
aiv_cores_[aiv_count_].physical_core_id = physical_core_id;
aiv_cores_[aiv_count_].reg_addr = reg_addr;
aiv_cores_[aiv_count_].core_type = type;
aiv_count_++;
} else {
LOG_ERROR("Unknown core type from core %d", i);
handshake_failed = true;
}
core_id_to_reg_addr_[i] = reg_addr;
LOG_INFO(
" Core %d: type=%s, physical_id=%u, reg_addr=0x%lx", i, core_type_to_string(type), physical_core_id,
reg_addr
);
}
if (handshake_failed) {
emergency_shutdown(runtime);
return -1;
}
LOG_INFO("Discovery complete: AIC=%d, AIV=%d, Total=%d", aic_count_, aiv_count_, cores_total_num_);
return 0;
}
// Assign discovered cores to threads using round-robin
void AicpuExecutor::assign_cores_to_threads() {
// Round-robin: AIC core i → thread (i % thread_num_), AIV core i → thread (i % thread_num_).
// AIC and AIV are assigned independently; no cluster pairing is required.
// aic_per_thread_ / aiv_per_thread_ store the ceiling value and serve as local queue caps.
aic_per_thread_ = (aic_count_ + thread_num_ - 1) / thread_num_;
aiv_per_thread_ = (aiv_count_ + thread_num_ - 1) / thread_num_;
LOG_INFO(
"Core Assignment: %d AIC cores, %d AIV cores across %d threads (max %d AIC/thread, %d AIV/thread)", aic_count_,
aiv_count_, thread_num_, aic_per_thread_, aiv_per_thread_
);
for (int t = 0; t < thread_num_; t++) {
int core_idx = 0;
// Assign AIC cores: cores at indices t, t+thread_num_, t+2*thread_num_, ...
for (int i = t; i < aic_count_; i += thread_num_) {
core_assignments_[t][core_idx++] = aic_cores_[i].worker_id;
}
// Assign AIV cores after AIC cores
for (int i = t; i < aiv_count_; i += thread_num_) {
core_assignments_[t][core_idx++] = aiv_cores_[i].worker_id;
}
thread_cores_num_[t] = core_idx;
char log_buffer[256];
int offset = 0;
offset += snprintf(
log_buffer + offset, sizeof(log_buffer) - offset, "Thread %d: assigned %d cores - AIC[", t, core_idx
);
for (int k = 0, i = t; i < aic_count_; i += thread_num_, k++) {
if (k > 0) offset += snprintf(log_buffer + offset, sizeof(log_buffer) - offset, ",");
offset += snprintf(log_buffer + offset, sizeof(log_buffer) - offset, "%d", aic_cores_[i].worker_id);
}
offset += snprintf(log_buffer + offset, sizeof(log_buffer) - offset, "] AIV[");
for (int k = 0, i = t; i < aiv_count_; i += thread_num_, k++) {
if (k > 0) offset += snprintf(log_buffer + offset, sizeof(log_buffer) - offset, ",");
offset += snprintf(log_buffer + offset, sizeof(log_buffer) - offset, "%d", aiv_cores_[i].worker_id);
}
offset += snprintf(log_buffer + offset, sizeof(log_buffer) - offset, "]");
LOG_INFO("%s", log_buffer);
}
}
// Classify and distribute initial ready tasks to thread-local and shared queues
void AicpuExecutor::classify_and_distribute_initial_tasks(Runtime *runtime) {
ready_queue_aic_head_ = 0;
ready_queue_aic_tail_ = 0;
ready_queue_aiv_head_ = 0;
ready_queue_aiv_tail_ = 0;
for (int t = 0; t < MAX_AICPU_THREADS; t++) {
cur_ready_queue_aic_head_[t] = 0;
cur_ready_queue_aic_tail_[t] = 0;
cur_ready_queue_aiv_head_[t] = 0;
cur_ready_queue_aiv_tail_[t] = 0;
}
int initial_count = 0;
int initial_aic_count = 0;
int initial_aiv_count = 0;
int aic_shared_count = 0;
int aiv_shared_count = 0;
int next_aic_thread = 0;
int next_aiv_thread = 0;
auto enqueue_initial_task = [&](int task_id, CoreType core_type, int &next_thread_idx, int &shared_count) {
int thread_idx = next_thread_idx;
int *head_ptr = (core_type == CoreType::AIC) ? &cur_ready_queue_aic_head_[thread_idx] :
&cur_ready_queue_aiv_head_[thread_idx];
int *tail_ptr = (core_type == CoreType::AIC) ? &cur_ready_queue_aic_tail_[thread_idx] :
&cur_ready_queue_aiv_tail_[thread_idx];
int cur_size = (*tail_ptr - *head_ptr + MAX_CORES_PER_THREAD) % MAX_CORES_PER_THREAD;
int local_capacity = (core_type == CoreType::AIC) ? aic_per_thread_ : aiv_per_thread_;
if (cur_size < local_capacity) {
if (core_type == CoreType::AIC) {
cur_ready_queue_aic_[thread_idx][*tail_ptr] = task_id;
} else {
cur_ready_queue_aiv_[thread_idx][*tail_ptr] = task_id;
}
*tail_ptr = (*tail_ptr + 1) % MAX_CORES_PER_THREAD;
LOG_INFO(
"Init: %s task %d -> Thread %d local queue (size=%d)", core_type == CoreType::AIC ? "AIC" : "AIV",
task_id, thread_idx, cur_size + 1
);
} else if (core_type == CoreType::AIC) {
ready_queue_aic_[ready_queue_aic_tail_] = task_id;
ready_queue_aic_tail_ = (ready_queue_aic_tail_ + 1) % RUNTIME_MAX_TASKS;
shared_count++;
} else {
ready_queue_aiv_[ready_queue_aiv_tail_] = task_id;
ready_queue_aiv_tail_ = (ready_queue_aiv_tail_ + 1) % RUNTIME_MAX_TASKS;
shared_count++;
}
next_thread_idx = (thread_idx + 1) % thread_num_;
};
int task_count = runtime->get_task_count();
for (int task_id = 0; task_id < task_count; task_id++) {
Task *task = runtime->get_task(task_id);
if (task == nullptr || task->fanin.load(std::memory_order_acquire) != 0) {
continue;
}
initial_count++;
if (task->core_type == CoreType::AIC) {
initial_aic_count++;
enqueue_initial_task(task_id, CoreType::AIC, next_aic_thread, aic_shared_count);
} else {
initial_aiv_count++;
enqueue_initial_task(task_id, CoreType::AIV, next_aiv_thread, aiv_shared_count);
}
}
LOG_INFO("Init: Found %d initially ready tasks", initial_count);
LOG_INFO("Init: Initial ready tasks by type: AIC=%d, AIV=%d", initial_aic_count, initial_aiv_count);
ready_count_aic_.store(aic_shared_count, std::memory_order_release);
ready_count_aiv_.store(aiv_shared_count, std::memory_order_release);
LOG_INFO(
"Init: Task distribution complete - AIC: %d in local queues, %d in shared queue",
initial_aic_count - aic_shared_count, aic_shared_count
);
LOG_INFO(
"Init: Task distribution complete - AIV: %d in local queues, %d in shared queue",
initial_aiv_count - aiv_shared_count, aiv_shared_count
);
for (int t = 0; t < thread_num_; t++) {
int aic_size =
(cur_ready_queue_aic_tail_[t] - cur_ready_queue_aic_head_[t] + MAX_CORES_PER_THREAD) % MAX_CORES_PER_THREAD;
int aiv_size =
(cur_ready_queue_aiv_tail_[t] - cur_ready_queue_aiv_head_[t] + MAX_CORES_PER_THREAD) % MAX_CORES_PER_THREAD;
LOG_INFO("Init: Thread %d local queues - AIC: %d tasks, AIV: %d tasks", t, aic_size, aiv_size);
}
}
/**
* Shutdown AICore - Send quit signal to all AICore kernels
*/
int AicpuExecutor::shutdown_aicore(Runtime *runtime, int thread_idx, const int *cur_thread_cores) {
Handshake *all_handshakes = reinterpret_cast<Handshake *>(runtime->workers);
LOG_INFO("Thread %d: Shutting down %d cores", thread_idx, thread_cores_num_[thread_idx]);
for (int i = 0; i < thread_cores_num_[thread_idx]; i++) {
int core_id = cur_thread_cores[i];
Handshake *hank = &all_handshakes[core_id];
LOG_INFO("Thread %d: AICPU hank addr = 0x%lx", thread_idx, reinterpret_cast<uint64_t>(hank));
uint64_t reg_addr = core_id_to_reg_addr_[core_id];
if (reg_addr != 0) {
platform_deinit_aicore_regs(reg_addr);
} else {
LOG_ERROR("Thread %d: Core %d has invalid register address", thread_idx, core_id);
}
}
LOG_INFO("Thread %d: Shutdown complete", thread_idx);
return 0;
}
/**
* Resolve dependencies and dispatch tasks using fast-path scheduling
*/
int AicpuExecutor::resolve_and_dispatch(Runtime &runtime, int thread_idx, const int *cur_thread_cores, int core_num) {
Handshake *hank = reinterpret_cast<Handshake *>(runtime.workers);
LOG_INFO("Thread %d: Starting execution with %d cores", thread_idx, core_num);
int cur_thread_completed = 0;
int task_count = total_tasks_.load(std::memory_order_acquire);
// Timeout detection using idle iteration counting
int idle_iterations = 0;
const int MAX_IDLE_ITERATIONS = 50000000;
const int WARN_INTERVAL = 1000000;
bool made_progress = false;
int verification_warning_count = 0;
const int MAX_VERIFICATION_WARNINGS = 10;
bool profiling_enabled = runtime.enable_profiling;
// Extract array pointers as local variables for better readability and performance
int *cur_ready_queue_aic = cur_ready_queue_aic_[thread_idx];
int *cur_ready_queue_aiv = cur_ready_queue_aiv_[thread_idx];
// Initialize local circular queue pointers from member variables (set by init())
// After this point, only use local variables for lock-free performance
int cur_aic_head = cur_ready_queue_aic_head_[thread_idx];
int cur_aic_tail = cur_ready_queue_aic_tail_[thread_idx];
int cur_aiv_head = cur_ready_queue_aiv_head_[thread_idx];
int cur_aiv_tail = cur_ready_queue_aiv_tail_[thread_idx];
// Calculate initial queue sizes
int cur_aic_ready_count = (cur_aic_tail - cur_aic_head + MAX_CORES_PER_THREAD) % MAX_CORES_PER_THREAD;
int cur_aiv_ready_count = (cur_aiv_tail - cur_aiv_head + MAX_CORES_PER_THREAD) % MAX_CORES_PER_THREAD;
LOG_INFO(
"Thread %d: Initial state - local queue: %d AIC, %d AIV", thread_idx, cur_aic_ready_count, cur_aiv_ready_count
);
// Initialize dispatch timestamps for all cores
uint64_t dispatch_start_time = get_sys_cnt_aicpu();
for (int i = 0; i < core_num; i++) {
int core_id = cur_thread_cores[i];
dispatch_timestamps_[core_id] = dispatch_start_time;
}
// Main execution loop with unified scheduling
while (true) {
for (int i = 0; i < core_num; i++) {
int core_id = cur_thread_cores[i];
uint64_t reg_addr = core_id_to_reg_addr_[core_id];
Handshake *h = &hank[core_id];
uint64_t reg_val = poll_reg(reg_addr, RegId::COND);
int reg_task_id = EXTRACT_TASK_ID(reg_val);
int reg_state = EXTRACT_TASK_STATE(reg_val);
// Case 1: Pending task finished directly
if (reg_task_id == pending_task_ids_[core_id] && reg_state == TASK_FIN_STATE) {
poll_acquire_barrier();
LOG_INFO(
"Thread %d: Core %d completed task %d (running_id=%d)", thread_idx, core_id,
pending_task_ids_[core_id], running_task_ids_[core_id]
);
int completed_task_id = pending_task_ids_[core_id];
int prev_running_id = running_task_ids_[core_id];
// Profiling: when prev_running_id exists, its AICore record was
// written first (at records[count]), so complete it BEFORE the
// pending task's record to maintain buffer ordering.
if (profiling_enabled) {
uint64_t finish_ts = get_sys_cnt_aicpu();
PerfBuffer *perf_buf = reinterpret_cast<PerfBuffer *>(h->perf_records_addr);
if (prev_running_id != AICPU_TASK_INVALID) {
Task *prev_task = &runtime.tasks[prev_running_id];
uint64_t fanout_arr[RUNTIME_MAX_FANOUT];
for (int i = 0; i < prev_task->fanout_count; i++) {
fanout_arr[i] = static_cast<uint64_t>(prev_task->fanout[i]);
}
if (perf_aicpu_complete_record(
perf_buf, static_cast<uint32_t>(prev_running_id),
static_cast<uint64_t>(prev_running_id), prev_task->func_id, h->core_type,
dispatch_timestamps_[core_id], finish_ts, fanout_arr, prev_task->fanout_count
) != 0) {
DEV_ERROR(
"Core %d: perf_aicpu_complete_record failed for implicit task %d", core_id,
prev_running_id
);
}
dispatch_timestamps_[core_id] = get_sys_cnt_aicpu();
}
finish_ts = get_sys_cnt_aicpu();
Task *task = &runtime.tasks[completed_task_id];
uint64_t fanout_arr[RUNTIME_MAX_FANOUT];
for (int i = 0; i < task->fanout_count; i++) {
fanout_arr[i] = static_cast<uint64_t>(task->fanout[i]);
}
if (perf_aicpu_complete_record(
perf_buf, static_cast<uint32_t>(completed_task_id),
static_cast<uint64_t>(completed_task_id), task->func_id, h->core_type,
dispatch_timestamps_[core_id], finish_ts, fanout_arr, task->fanout_count
) != 0) {
DEV_ERROR("Core %d: perf_aicpu_complete_record failed for task %d", core_id, completed_task_id);
}
dispatch_timestamps_[core_id] = get_sys_cnt_aicpu();
}
cur_thread_completed++;
completed_tasks_.fetch_add(1, std::memory_order_release);
pending_task_ids_[core_id] = AICPU_TASK_INVALID;
running_task_ids_[core_id] = AICPU_TASK_INVALID;
// Try dispatch BEFORE resolve_dependencies
// This allows the core to start next task immediately
bool dispatched = false;
if (h->core_type == CoreType::AIC && cur_aic_ready_count > 0) {
dispatched = try_dispatch_task(
core_id, reg_addr, CoreType::AIC, thread_idx, cur_ready_queue_aic, cur_aic_head,
cur_aic_ready_count, profiling_enabled, runtime
);
} else if (h->core_type == CoreType::AIV && cur_aiv_ready_count > 0) {
dispatched = try_dispatch_task(
core_id, reg_addr, CoreType::AIV, thread_idx, cur_ready_queue_aiv, cur_aiv_head,
cur_aiv_ready_count, profiling_enabled, runtime
);
}
// Resolve old running task dependencies (if exists)
// When pending task FINs directly, the running task was implicitly
// completed (AICore overwrote COND before we could read its FIN).
// Count it here to avoid losing completion.
if (prev_running_id != AICPU_TASK_INVALID) {
cur_thread_completed++;
completed_tasks_.fetch_add(1, std::memory_order_release);
Task *prev_running_task = runtime.get_task(prev_running_id);
resolve_task_dependencies(
prev_running_task, runtime, cur_ready_queue_aic, cur_aic_tail, cur_aic_ready_count,
cur_ready_queue_aiv, cur_aiv_tail, cur_aiv_ready_count
);
LOG_INFO("Thread %d: Core %d resolved old running task %d", thread_idx, core_id, prev_running_id);
}
Task *task = runtime.get_task(completed_task_id);
resolve_task_dependencies(
task, runtime, cur_ready_queue_aic, cur_aic_tail, cur_aic_ready_count, cur_ready_queue_aiv,
cur_aiv_tail, cur_aiv_ready_count
);
made_progress = true;
// Update timestamp if didn't dispatch (try_dispatch_task updates it if dispatched)
if (!dispatched && profiling_enabled) {
dispatch_timestamps_[core_id] = get_sys_cnt_aicpu();
}
} else if (reg_task_id == pending_task_ids_[core_id] && reg_state == TASK_ACK_STATE) {
// Case 2: Pending task received ACK
poll_acquire_barrier();
LOG_INFO(
"Thread %d: Core %d ACKed task %d (running_id=%d)", thread_idx, core_id, pending_task_ids_[core_id],
running_task_ids_[core_id]
);
int prev_running_id = running_task_ids_[core_id];
// Move pending to running
running_task_ids_[core_id] = pending_task_ids_[core_id];
pending_task_ids_[core_id] = AICPU_TASK_INVALID;
made_progress = true;
// When pending task ACKs, the old running task was implicitly
// completed (AICore overwrote COND before we could read its FIN).
// Count it here to avoid losing completion.
if (prev_running_id != AICPU_TASK_INVALID) {
// Profiling: complete the implicit task's AICore record
if (profiling_enabled) {
uint64_t finish_ts = get_sys_cnt_aicpu();
PerfBuffer *perf_buf = reinterpret_cast<PerfBuffer *>(h->perf_records_addr);
Task *prev_task = &runtime.tasks[prev_running_id];
uint64_t fanout_arr[RUNTIME_MAX_FANOUT];
for (int i = 0; i < prev_task->fanout_count; i++) {
fanout_arr[i] = static_cast<uint64_t>(prev_task->fanout[i]);
}
if (perf_aicpu_complete_record(
perf_buf, static_cast<uint32_t>(prev_running_id),
static_cast<uint64_t>(prev_running_id), prev_task->func_id, h->core_type,
dispatch_timestamps_[core_id], finish_ts, fanout_arr, prev_task->fanout_count
) != 0) {
DEV_ERROR(
"Core %d: perf_aicpu_complete_record failed for implicit task %d", core_id,
prev_running_id
);
}
dispatch_timestamps_[core_id] = get_sys_cnt_aicpu();
}
cur_thread_completed++;
completed_tasks_.fetch_add(1, std::memory_order_release);
Task *prev_running_task = runtime.get_task(prev_running_id);
resolve_task_dependencies(
prev_running_task, runtime, cur_ready_queue_aic, cur_aic_tail, cur_aic_ready_count,
cur_ready_queue_aiv, cur_aiv_tail, cur_aiv_ready_count
);
LOG_INFO("Thread %d: Core %d resolved old running task %d", thread_idx, core_id, prev_running_id);
}
// Core can accept new task now (pipeline!)
// Continue to Case 4 to dispatch next task
} else if (reg_task_id == running_task_ids_[core_id] && reg_state == TASK_FIN_STATE) {
// Case 3: Running task finished
poll_acquire_barrier();
LOG_INFO(
"Thread %d: Core %d completed task %d (pending_id=%d)", thread_idx, core_id,
running_task_ids_[core_id], pending_task_ids_[core_id]
);
int completed_task_id = running_task_ids_[core_id];
if (profiling_enabled) {
uint64_t finish_ts = get_sys_cnt_aicpu();
PerfBuffer *perf_buf = reinterpret_cast<PerfBuffer *>(h->perf_records_addr);
Task *task = &runtime.tasks[completed_task_id];
uint64_t fanout_arr[RUNTIME_MAX_FANOUT];
for (int i = 0; i < task->fanout_count; i++) {
fanout_arr[i] = static_cast<uint64_t>(task->fanout[i]);
}
if (perf_aicpu_complete_record(
perf_buf, static_cast<uint32_t>(completed_task_id),
static_cast<uint64_t>(completed_task_id), task->func_id, h->core_type,
dispatch_timestamps_[core_id], finish_ts, fanout_arr, task->fanout_count
) != 0) {
DEV_ERROR("Core %d: perf_aicpu_complete_record failed for task %d", core_id, completed_task_id);
}
dispatch_timestamps_[core_id] = get_sys_cnt_aicpu();
}
cur_thread_completed++;
completed_tasks_.fetch_add(1, std::memory_order_release);
running_task_ids_[core_id] = AICPU_TASK_INVALID;
bool dispatched = false;
if (pending_task_ids_[core_id] == AICPU_TASK_INVALID) {
if (h->core_type == CoreType::AIC && cur_aic_ready_count > 0) {
dispatched = try_dispatch_task(
core_id, reg_addr, CoreType::AIC, thread_idx, cur_ready_queue_aic, cur_aic_head,
cur_aic_ready_count, profiling_enabled, runtime
);
} else if (h->core_type == CoreType::AIV && cur_aiv_ready_count > 0) {
dispatched = try_dispatch_task(
core_id, reg_addr, CoreType::AIV, thread_idx, cur_ready_queue_aiv, cur_aiv_head,
cur_aiv_ready_count, profiling_enabled, runtime
);
}
}
Task *task = runtime.get_task(completed_task_id);
resolve_task_dependencies(
task, runtime, cur_ready_queue_aic, cur_aic_tail, cur_aic_ready_count, cur_ready_queue_aiv,
cur_aiv_tail, cur_aiv_ready_count
);
made_progress = true;
// Update timestamp if didn't dispatch (try_dispatch_task updates it if dispatched)
if (!dispatched && profiling_enabled) {
dispatch_timestamps_[core_id] = get_sys_cnt_aicpu();
}
}
// Case 4: Dispatch new task if pending slot is available
if (pending_task_ids_[core_id] == AICPU_TASK_INVALID) {
if (h->core_type == CoreType::AIC && cur_aic_ready_count > 0) {
if (try_dispatch_task(
core_id, reg_addr, CoreType::AIC, thread_idx, cur_ready_queue_aic, cur_aic_head,
cur_aic_ready_count, profiling_enabled, runtime
)) {
made_progress = true;
}
} else if (h->core_type == CoreType::AIV && cur_aiv_ready_count > 0) {
if (try_dispatch_task(
core_id, reg_addr, CoreType::AIV, thread_idx, cur_ready_queue_aiv, cur_aiv_head,
cur_aiv_ready_count, profiling_enabled, runtime
)) {
made_progress = true;
}
}
}
}
// Refill local queues from shared queues
if (cur_aic_ready_count == 0) {
if (ready_count_aic_.load(std::memory_order_acquire) > 0) {
std::lock_guard<std::mutex> lock(ready_queue_aic_mutex_);
int available = ready_count_aic_.load(std::memory_order_relaxed);
int to_grab = (available < aic_per_thread_) ? available : aic_per_thread_;
for (int i = 0; i < to_grab; i++) {
int task_id = ready_queue_aic_[ready_queue_aic_head_];
ready_queue_aic_head_ = (ready_queue_aic_head_ + 1) % RUNTIME_MAX_TASKS;
cur_ready_queue_aic[cur_aic_tail] = task_id;
cur_aic_tail = (cur_aic_tail + 1) % MAX_CORES_PER_THREAD;
}
ready_count_aic_.fetch_sub(to_grab, std::memory_order_release);
cur_aic_ready_count += to_grab;
LOG_INFO(
"Thread %d: Grabbed %d AIC tasks from shared queue (available=%d)", thread_idx, to_grab, available
);
}
}
if (cur_aiv_ready_count == 0) {
if (ready_count_aiv_.load(std::memory_order_acquire) > 0) {
std::lock_guard<std::mutex> lock(ready_queue_aiv_mutex_);
int available = ready_count_aiv_.load(std::memory_order_relaxed);
int to_grab = (available < aiv_per_thread_) ? available : aiv_per_thread_;
for (int i = 0; i < to_grab; i++) {
int task_id = ready_queue_aiv_[ready_queue_aiv_head_];
ready_queue_aiv_head_ = (ready_queue_aiv_head_ + 1) % RUNTIME_MAX_TASKS;
cur_ready_queue_aiv[cur_aiv_tail] = task_id;
cur_aiv_tail = (cur_aiv_tail + 1) % MAX_CORES_PER_THREAD;
}
ready_count_aiv_.fetch_sub(to_grab, std::memory_order_release);
cur_aiv_ready_count += to_grab;
LOG_INFO(
"Thread %d: Grabbed %d AIV tasks from shared queue (available=%d)", thread_idx, to_grab, available
);
}
}
// Check completion
if (completed_tasks_.load(std::memory_order_acquire) >= task_count) {
bool all_cores_idle = true;
for (int i = 0; i < core_num; i++) {
int core_id = cur_thread_cores[i];
if (pending_task_ids_[core_id] != AICPU_TASK_INVALID ||
running_task_ids_[core_id] != AICPU_TASK_INVALID) {
all_cores_idle = false;
if (verification_warning_count == 0) {
uint64_t reg_addr = core_id_to_reg_addr_[core_id];
uint64_t reg_val = read_reg(reg_addr, RegId::COND);
LOG_WARN(
"Thread %d: Counter reached %d/%d but core %d still has work (COND=0x%lx, pending_id=%d, "
"running_id=%d)",
thread_idx, completed_tasks_.load(std::memory_order_acquire), task_count, core_id, reg_val,
pending_task_ids_[core_id], running_task_ids_[core_id]
);
}
break;
}
}
if (all_cores_idle) {
// Truly complete: counter reached and all cores idle
int aic_remaining = ready_count_aic_.load(std::memory_order_acquire);
int aiv_remaining = ready_count_aiv_.load(std::memory_order_acquire);
if (aic_remaining > 0 || aiv_remaining > 0) {
LOG_WARN(
"Thread %d: Queues not empty after completion! AIC=%d, AIV=%d", thread_idx, aic_remaining,
aiv_remaining
);
}
break; // Exit main loop
}
verification_warning_count++;
if (verification_warning_count > MAX_VERIFICATION_WARNINGS) {
LOG_ERROR(
"Thread %d: Counter reached but cores still working after %d checks!", thread_idx,
verification_warning_count
);
diagnose_stuck_state(runtime, thread_idx, cur_thread_cores, core_num, hank);
return -1;
}
}
// Timeout detection
if (!made_progress) {
idle_iterations++;
if (idle_iterations % WARN_INTERVAL == 0) {
int current = completed_tasks_.load(std::memory_order_acquire);
LOG_WARN(
"Thread %d: %d idle iterations, progress %d/%d tasks", thread_idx, idle_iterations, current,
task_count
);
}
if (idle_iterations > MAX_IDLE_ITERATIONS) {
LOG_ERROR("Thread %d: Timeout after %d idle iterations!", thread_idx, idle_iterations);
diagnose_stuck_state(runtime, thread_idx, cur_thread_cores, core_num, hank);
return -1;
} else {
SPIN_WAIT_HINT();
}
} else {
idle_iterations = 0;
}
made_progress = false;
}
LOG_INFO("Thread %d: Execution complete, completed %d tasks", thread_idx, cur_thread_completed);
return cur_thread_completed;
}
int AicpuExecutor::run(Runtime *runtime) {
int thread_idx = thread_idx_++;
LOG_INFO("Thread %d: Start", thread_idx);
const int *cur_thread_cores = core_assignments_[thread_idx];
LOG_INFO("Thread %d: Runtime has %d tasks", thread_idx, runtime->get_task_count());
int completed = resolve_and_dispatch(*runtime, thread_idx, cur_thread_cores, thread_cores_num_[thread_idx]);
LOG_INFO("Thread %d: Executed %d tasks from runtime", thread_idx, completed);
int rc = shutdown_aicore(runtime, thread_idx, cur_thread_cores);
if (rc != 0) {
return rc;
}
// Flush performance buffers for cores managed by this thread
if (runtime->enable_profiling) {
perf_aicpu_flush_buffers(runtime, thread_idx, cur_thread_cores, thread_cores_num_[thread_idx]);
}
LOG_INFO("Thread %d: Completed", thread_idx);
int prev_finished = finished_count_.fetch_add(1, std::memory_order_acq_rel);
if (prev_finished + 1 == thread_num_) {
finished_.store(true, std::memory_order_release);
LOG_INFO("Thread %d: Last thread, marking executor finished", thread_idx);
}