Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
a9ea41c5
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
a9ea41c5
编写于
9月 29, 2021
作者:
L
liutiexing
提交者:
GitHub
9月 29, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Spinlock (#36030)
* add align for WorkQueue * add spinlock * merge spinlock
上级
79bd5f90
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
36 addition
and
22 deletion
+36
-22
paddle/fluid/framework/new_executor/run_queue.h
paddle/fluid/framework/new_executor/run_queue.h
+6
-4
paddle/fluid/framework/new_executor/workqueue.cc
paddle/fluid/framework/new_executor/workqueue.cc
+2
-2
paddle/fluid/framework/new_executor/workqueue_utils.h
paddle/fluid/framework/new_executor/workqueue_utils.h
+1
-0
paddle/fluid/memory/allocation/spin_lock.h
paddle/fluid/memory/allocation/spin_lock.h
+27
-16
未找到文件。
paddle/fluid/framework/new_executor/run_queue.h
浏览文件 @
a9ea41c5
...
...
@@ -37,6 +37,8 @@
#include <cstdint>
#include <mutex>
#include <vector>
#include "paddle/fluid/framework/new_executor/workqueue_utils.h"
#include "paddle/fluid/memory/allocation/spin_lock.h"
namespace
paddle
{
namespace
framework
{
...
...
@@ -101,7 +103,7 @@ class RunQueue {
// PushBack adds w at the end of the queue.
// If queue is full returns w, otherwise returns default-constructed Work.
Work
PushBack
(
Work
w
)
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
std
::
unique_lock
<
paddle
::
memory
::
SpinLock
>
lock
(
mutex_
);
unsigned
back
=
back_
.
load
(
std
::
memory_order_relaxed
);
Elem
*
e
=
&
array_
[(
back
-
1
)
&
kMask
];
uint8_t
s
=
e
->
state
.
load
(
std
::
memory_order_relaxed
);
...
...
@@ -123,7 +125,7 @@ class RunQueue {
return
Work
();
}
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
std
::
unique_lock
<
paddle
::
memory
::
SpinLock
>
lock
(
mutex_
);
unsigned
back
=
back_
.
load
(
std
::
memory_order_relaxed
);
Elem
*
e
=
&
array_
[
back
&
kMask
];
uint8_t
s
=
e
->
state
.
load
(
std
::
memory_order_relaxed
);
...
...
@@ -145,7 +147,7 @@ class RunQueue {
return
0
;
}
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
std
::
unique_lock
<
paddle
::
memory
::
SpinLock
>
lock
(
mutex_
);
unsigned
back
=
back_
.
load
(
std
::
memory_order_relaxed
);
unsigned
size
=
Size
();
unsigned
mid
=
back
;
...
...
@@ -213,7 +215,7 @@ class RunQueue {
// modification counters.
alignas
(
64
)
std
::
atomic
<
unsigned
>
front_
;
alignas
(
64
)
std
::
atomic
<
unsigned
>
back_
;
std
::
mutex
mutex_
;
paddle
::
memory
::
SpinLock
mutex_
;
Elem
array_
[
kSize
];
// SizeOrNotEmpty returns current queue size; if NeedSizeEstimate is false,
...
...
paddle/fluid/framework/new_executor/workqueue.cc
浏览文件 @
a9ea41c5
...
...
@@ -166,7 +166,7 @@ std::unique_ptr<WorkQueue> CreateMultiThreadedWorkQueue(
"WorkQueueOptions.num_threads must be "
"greater than 1."
));
std
::
unique_ptr
<
WorkQueue
>
ptr
(
new
WorkQueueImpl
(
options
));
return
ptr
;
return
std
::
move
(
ptr
)
;
}
std
::
unique_ptr
<
WorkQueueGroup
>
CreateWorkQueueGroup
(
...
...
@@ -176,7 +176,7 @@ std::unique_ptr<WorkQueueGroup> CreateWorkQueueGroup(
"For a WorkQueueGroup, the number of WorkQueueOptions "
"must be greater than 1."
));
std
::
unique_ptr
<
WorkQueueGroup
>
ptr
(
new
WorkQueueGroupImpl
(
queues_options
));
return
ptr
;
return
std
::
move
(
ptr
)
;
}
}
// namespace framework
...
...
paddle/fluid/framework/new_executor/workqueue_utils.h
浏览文件 @
a9ea41c5
...
...
@@ -14,6 +14,7 @@
#pragma once
#include <atomic>
#include <cassert>
#include <cstddef>
#include <cstdlib>
...
...
paddle/fluid/memory/allocation/spin_lock.h
浏览文件 @
a9ea41c5
...
...
@@ -15,37 +15,48 @@
#pragma once
#include <atomic>
#if !defined(_WIN32)
#include <sched.h>
#else
#include <windows.h>
#endif // !_WIN32
#if defined(_M_X64) || defined(__x86_64__) || defined(_M_IX86) || \
defined(__i386__)
#define __PADDLE_x86__
#include <immintrin.h>
#endif
#include <thread>
#include "paddle/fluid/platform/macros.h"
namespace
paddle
{
namespace
memory
{
static
inline
void
CpuRelax
()
{
#if defined(__PADDLE_x86__)
_mm_pause
();
#endif
}
class
SpinLock
{
public:
SpinLock
()
:
mlock_
(
false
)
{}
void
lock
()
{
bool
expect
=
false
;
uint64_t
spin_cnt
=
0
;
while
(
!
mlock_
.
compare_exchange_weak
(
expect
,
true
))
{
expect
=
false
;
if
((
++
spin_cnt
&
0xFF
)
==
0
)
{
#if defined(_WIN32)
SleepEx
(
50
,
FALSE
);
#else
sched_yield
();
#endif
for
(;;)
{
if
(
!
mlock_
.
exchange
(
true
,
std
::
memory_order_acquire
))
{
break
;
}
constexpr
int
kMaxLoop
=
32
;
for
(
int
loop
=
1
;
mlock_
.
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
()
{
mlock_
.
store
(
false
);
}
void
unlock
()
{
mlock_
.
store
(
false
,
std
::
memory_order_release
);
}
DISABLE_COPY_AND_ASSIGN
(
SpinLock
);
private:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录