Skip to content

Commit 2a84c27

Browse files
authored
[CRL] Fix semaphore malloc issue (flagos-ai#337)
1 parent 5e32165 commit 2a84c27

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

flagcx/core/include/launch_kernel.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ struct flagcxSemaphore {
5454
// Host semaphore derived class
5555
struct flagcxHostSemaphore : public flagcxSemaphore {
5656
int counter; // total ops
57-
std::map<int, int> stepInfo; // opId -> singalId
57+
std::unordered_map<int, int> stepInfo; // opId -> sigalId
5858
std::vector<std::pair<int, int>> signals; // [curStep, nSteps]
5959
std::vector<flagcxEvent_t> events;
6060

6161
flagcxHostSemaphore() {
6262
counter = 0;
63+
stepInfo.reserve(FLAGCX_OPS_PER_SEMAPHORE);
6364
signals.reserve(FLAGCX_SIGNALS_PER_SEMAPHORE);
65+
events.reserve(FLAGCX_SIGNALS_PER_SEMAPHORE);
6466
}
6567
~flagcxHostSemaphore() override {
6668
for (auto event : events) {
@@ -105,15 +107,19 @@ struct flagcxHostSemaphore : public flagcxSemaphore {
105107
return (__atomic_load_n(&counter, __ATOMIC_ACQUIRE) == 0);
106108
}
107109
void wait() override {
108-
while (__atomic_load_n(&counter, __ATOMIC_ACQUIRE) > 0) {
110+
int nDone = 0;
111+
int nOps = __atomic_load_n(&counter, __ATOMIC_ACQUIRE);
112+
while (nDone < nOps) {
109113
for (auto it = stepInfo.begin(); it != stepInfo.end(); ++it) {
110-
if (signals[it->second].first == signals[it->second].second) {
111-
__atomic_fetch_sub(&counter, 1, __ATOMIC_RELEASE);
114+
if (__atomic_load_n(&signals[it->second].first, __ATOMIC_ACQUIRE) ==
115+
__atomic_load_n(&signals[it->second].second, __ATOMIC_ACQUIRE)) {
112116
__atomic_fetch_add(&signals[it->second].first, 1, __ATOMIC_RELEASE);
117+
nDone++;
113118
}
114119
}
115120
sched_yield();
116121
}
122+
__atomic_store_n(&counter, 0, __ATOMIC_RELEASE);
117123
}
118124
};
119125

0 commit comments

Comments
 (0)