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

Spinlock #36030

Merged
merged 6 commits into from
Sep 29, 2021
Merged

Spinlock #36030

merged 6 commits into from
Sep 29, 2021

Conversation

liutiexing
Copy link
Contributor

@liutiexing liutiexing commented Sep 23, 2021

PR types

Performance optimization

PR changes

Others

Describe

1)更新SpinLock实现
2)RunQueue使用spinlock替换mutex。防止竞争激烈时,worker线程休眠。

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

Comment on lines 73 to 105
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};
};

Copy link
Contributor

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即可。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

Copy link
Contributor

@Aurelius84 Aurelius84 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM,可否完善下PR标题。另外这个PR替换前后有数据对比么?

@liutiexing
Copy link
Contributor Author

LGTM,可否完善下PR标题。另外这个PR替换前后有数据对比么?

标题已完善,在PTB模型训练场景下不会有效果,因为竞争不够激烈,没有触发worker休眠。

@wanghuancoder wanghuancoder merged commit a9ea41c into PaddlePaddle:develop Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants