Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvement on device syncer #231

Merged
merged 5 commits into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions include/mscclpp/concurrency_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef MSCCLPP_CONCURRENCY_DEVICE_HPP_
#define MSCCLPP_CONCURRENCY_DEVICE_HPP_

#include "atomic_device.hpp"
#include "poll_device.hpp"

namespace mscclpp {
Expand All @@ -29,19 +30,13 @@ struct DeviceSyncer {
if (threadIdx.x == 0) {
// Need a `__threadfence()` before to flip `flag`.
__threadfence();
int tmp = isIncFlag_ ^ 1;
if (tmp) {
if (atomicInc(&count_, maxOldCnt) == maxOldCnt) {
flag_ = 1;
}
POLL_MAYBE_JAILBREAK(!flag_, maxSpinCount);
unsigned int tmp = preFlag_ ^ 1;
if (atomicInc(&count_, maxOldCnt) == maxOldCnt) {
atomicStore(&flag_, tmp, memoryOrderRelaxed);
} else {
if (atomicInc(&count_, maxOldCnt) == maxOldCnt) {
flag_ = 0;
}
POLL_MAYBE_JAILBREAK(flag_, maxSpinCount);
POLL_MAYBE_JAILBREAK((atomicLoad(&flag_, memoryOrderRelaxed) != tmp), maxSpinCount);
}
isIncFlag_ = tmp;
preFlag_ = tmp;
}
// We need sync here because only a single thread is checking whether
// the flag is flipped.
Expand All @@ -51,11 +46,11 @@ struct DeviceSyncer {

private:
/// The flag to indicate whether the barrier is reached by the latest thread.
volatile int flag_;
unsigned int flag_;
/// The counter of synchronized blocks.
unsigned int count_;
/// The flag to indicate whether to increase or decrease @ref flag_.
int isIncFlag_;
unsigned int preFlag_;
};

} // namespace mscclpp
Expand Down
Loading