-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Spinlock #36030
Spinlock #36030
Conversation
Thanks for your contribution! |
static inline void CpuRelax() { | ||
#if defined(__WQ_x86__) | ||
_mm_pause(); | ||
#endif | ||
} | ||
|
||
class SpinLock { | ||
public: | ||
void lock() { | ||
for (;;) { | ||
if (!lock_.exchange(true, std::memory_order_acquire)) { | ||
break; | ||
} | ||
constexpr int kMaxLoop = 32; | ||
for (int loop = 1; lock_.load(std::memory_order_relaxed);) { | ||
if (loop <= kMaxLoop) { | ||
for (int i = 1; i <= loop; ++i) { | ||
CpuRelax(); | ||
} | ||
loop *= 2; | ||
} else { | ||
std::this_thread::yield(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void unlock() { lock_.store(false, std::memory_order_release); } | ||
|
||
private: | ||
std::atomic<bool> lock_{false}; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果测试发现,你这个版本的SpinLock更好,最好是去修改我之前写的SpinLock,而不是再写一个新的。Paddle里有1个SpinLock即可。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM,可否完善下PR标题。另外这个PR替换前后有数据对比么?
标题已完善,在PTB模型训练场景下不会有效果,因为竞争不够激烈,没有触发worker休眠。 |
PR types
Performance optimization
PR changes
Others
Describe
1)更新SpinLock实现
2)RunQueue使用spinlock替换mutex。防止竞争激烈时,worker线程休眠。