Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
项目经理老王
Mace
提交
0531fadb
Mace
项目概览
项目经理老王
/
Mace
与 Fork 源项目一致
Fork自
Xiaomi / Mace
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Mace
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0531fadb
编写于
6月 19, 2019
作者:
B
Bin Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix CPU AFFINITY_NONE
上级
55f1e541
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
48 addition
and
40 deletion
+48
-40
mace/core/runtime/cpu/cpu_runtime.cc
mace/core/runtime/cpu/cpu_runtime.cc
+1
-6
mace/utils/thread_pool.cc
mace/utils/thread_pool.cc
+44
-32
mace/utils/thread_pool.h
mace/utils/thread_pool.h
+3
-2
未找到文件。
mace/core/runtime/cpu/cpu_runtime.cc
浏览文件 @
0531fadb
...
...
@@ -104,12 +104,7 @@ MaceStatus CPURuntime::SetOpenMPThreadsAndAffinityPolicy(
std
::
vector
<
size_t
>
cores_to_use
;
MACE_RETURN_IF_ERROR
(
mace
::
utils
::
GetCPUCoresToUse
(
cpu_max_freqs
,
policy
,
num_threads_hint
,
&
cores_to_use
));
int
cpu_count
=
static_cast
<
int
>
(
cores_to_use
.
size
());
if
(
num_threads_hint
<=
0
||
num_threads_hint
>
cpu_count
)
{
num_threads_hint
=
cpu_count
;
}
cpu_max_freqs
,
policy
,
&
num_threads_hint
,
&
cores_to_use
));
if
(
policy
==
CPUAffinityPolicy
::
AFFINITY_NONE
)
{
#ifdef MACE_ENABLE_QUANTIZE
...
...
mace/utils/thread_pool.cc
浏览文件 @
0531fadb
...
...
@@ -46,20 +46,22 @@ struct CPUFreq {
float
freq
;
};
size_t
GetCpuCoresForPerfomance
(
const
std
::
vector
<
CPUFreq
>
&
cpu_freqs
)
{
int
GetCpuCoresForPerfomance
(
const
std
::
vector
<
CPUFreq
>
&
cpu_freqs
,
const
std
::
function
<
bool
(
const
float
&
x
,
const
float
&
y
)
>
&
comp
)
{
float
total_freq
=
std
::
accumulate
(
cpu_freqs
.
begin
(),
cpu_freqs
.
end
(),
0
,
[](
float
accum
,
CPUFreq
cpu_freq
)
{
return
accum
+
cpu_freq
.
freq
;
});
size
_t
valid_cpu_nums
=
std
::
count_if
(
cpu_freqs
.
begin
(),
cpu_freqs
.
end
(),
int64
_t
valid_cpu_nums
=
std
::
count_if
(
cpu_freqs
.
begin
(),
cpu_freqs
.
end
(),
[](
CPUFreq
cpu_freq
)
{
return
cpu_freq
.
freq
!=
0
;
});
float
avg_freq
=
total_freq
/
valid_cpu_nums
;
size_
t
cores_to_use
=
0
;
in
t
cores_to_use
=
0
;
for
(
auto
cpu_info
:
cpu_freqs
)
{
if
((
c
pu_info
.
freq
>
avg_freq
if
((
c
omp
(
cpu_info
.
freq
,
avg_freq
)
&&
cores_to_use
<
kMaxCpuCoresForPerformance
)
||
cores_to_use
<
kMinCpuCoresForPerformance
)
{
++
cores_to_use
;
...
...
@@ -73,16 +75,17 @@ size_t GetCpuCoresForPerfomance(const std::vector<CPUFreq> &cpu_freqs) {
MaceStatus
GetCPUCoresToUse
(
const
std
::
vector
<
float
>
&
cpu_max_freqs
,
const
CPUAffinityPolicy
policy
,
const
size_t
thread_count_hi
nt
,
int
*
thread_cou
nt
,
std
::
vector
<
size_t
>
*
cores
)
{
if
(
cpu_max_freqs
.
empty
())
{
*
thread_count
=
1
;
LOG
(
ERROR
)
<<
"CPU core is empty"
;
return
MaceStatus
::
MACE_RUNTIME_ERROR
;
}
size_t
thread_count
=
thread_count_hint
;
const
size_t
cpu_count
=
cpu_max_freqs
.
size
(
);
if
(
thread_count
==
0
||
thread_count
>
cpu_count
)
{
thread_count
=
cpu_count
;
*
thread_count
=
std
::
max
(
*
thread_count
,
0
)
;
const
int
cpu_count
=
static_cast
<
int
>
(
cpu_max_freqs
.
size
()
);
if
(
*
thread_count
==
0
||
*
thread_count
>
cpu_count
)
{
*
thread_count
=
cpu_count
;
}
if
(
policy
!=
CPUAffinityPolicy
::
AFFINITY_NONE
)
{
...
...
@@ -108,69 +111,78 @@ MaceStatus GetCPUCoresToUse(const std::vector<float> &cpu_max_freqs,
}
// decide num of cores to use
size_t
cores_to_use
=
0
;
if
(
policy
==
CPUAffinityPolicy
::
AFFINITY_BIG_ONLY
||
policy
==
CPUAffinityPolicy
::
AFFINITY_LITTLE_ONLY
)
{
cores_to_use
=
GetCpuCoresForPerfomance
(
cpu_freq
);
int
cores_to_use
=
0
;
if
(
policy
==
CPUAffinityPolicy
::
AFFINITY_BIG_ONLY
)
{
cores_to_use
=
GetCpuCoresForPerfomance
(
cpu_freq
,
std
::
greater_equal
<
float
>
());
}
else
if
(
policy
==
CPUAffinityPolicy
::
AFFINITY_LITTLE_ONLY
)
{
cores_to_use
=
GetCpuCoresForPerfomance
(
cpu_freq
,
std
::
less_equal
<
float
>
());
}
else
{
cores_to_use
=
thread_count
;
cores_to_use
=
*
thread_count
;
}
MACE_CHECK
(
cores_to_use
>
0
,
"number of cores to use should > 0"
);
cores
->
resize
(
cores_to_use
);
for
(
size_
t
i
=
0
;
i
<
cores_to_use
;
++
i
)
{
cores
->
resize
(
static_cast
<
size_t
>
(
cores_to_use
)
);
for
(
in
t
i
=
0
;
i
<
cores_to_use
;
++
i
)
{
VLOG
(
2
)
<<
"Bind thread to core: "
<<
cpu_freq
[
i
].
core_id
<<
" with freq "
<<
cpu_freq
[
i
].
freq
;
(
*
cores
)[
i
]
=
static_cast
<
int
>
(
cpu_freq
[
i
].
core_id
);
}
if
(
*
thread_count
==
0
||
*
thread_count
>
cores_to_use
)
{
*
thread_count
=
cores_to_use
;
}
}
return
MaceStatus
::
MACE_SUCCESS
;
}
ThreadPool
::
ThreadPool
(
const
size_
t
thread_count_hint
,
ThreadPool
::
ThreadPool
(
const
in
t
thread_count_hint
,
const
CPUAffinityPolicy
policy
)
:
event_
(
kThreadPoolNone
),
count_down_latch_
(
kThreadPoolSpinWaitTime
)
{
size_
t
thread_count
=
thread_count_hint
;
in
t
thread_count
=
thread_count_hint
;
std
::
vector
<
float
>
cpu_max_freqs
;
if
(
port
::
Env
::
Default
()
->
GetCPUMaxFreq
(
&
cpu_max_freqs
)
if
(
port
::
Env
::
Default
()
->
GetCPUMaxFreq
(
&
cpu_max_freqs_
)
!=
MaceStatus
::
MACE_SUCCESS
)
{
LOG
(
ERROR
)
<<
"Fail to get cpu max frequencies"
;
}
thread_count
=
std
::
max
(
static_cast
<
size_t
>
(
1
),
std
::
min
(
thread_count
,
cpu_max_freqs
.
size
()));
std
::
vector
<
size_t
>
cores_to_use
;
GetCPUCoresToUse
(
cpu_max_freqs
,
policy
,
thread_count
,
&
cores_to_use
);
GetCPUCoresToUse
(
cpu_max_freqs_
,
policy
,
&
thread_count
,
&
cores_to_use
);
MACE_CHECK
(
thread_count
>
0
);
VLOG
(
2
)
<<
"Use "
<<
thread_count
<<
" threads"
;
if
(
!
cores_to_use
.
empty
())
{
if
(
port
::
Env
::
Default
()
->
SchedSetAffinity
(
cores_to_use
)
!=
MaceStatus
::
MACE_SUCCESS
)
{
LOG
(
ERROR
)
<<
"Failed to sched_set_affinity"
;
}
}
if
(
!
cores_to_use
.
empty
()
&&
thread_count
>
cores_to_use
.
size
())
{
thread_count
=
cores_to_use
.
size
();
}
VLOG
(
2
)
<<
"Use "
<<
thread_count
<<
" threads"
;
default_tile_count_
=
thread_count
;
if
(
cores_to_use
.
size
()
>=
2
&&
cpu_max_freqs
[
cores_to_use
[
0
]]
!=
cpu_max_freqs
[
cores_to_use
.
back
()])
{
if
(
thread_count
>
1
)
{
default_tile_count_
=
thread_count
*
kTileCountPerThread
;
}
MACE_CHECK
(
default_tile_count_
>
0
,
"default tile count should > 0"
);
threads_
=
std
::
vector
<
std
::
thread
>
(
thread_count
);
thread_infos_
=
std
::
vector
<
ThreadInfo
>
(
thread_count
);
threads_
=
std
::
vector
<
std
::
thread
>
(
static_cast
<
size_t
>
(
thread_count
)
);
thread_infos_
=
std
::
vector
<
ThreadInfo
>
(
static_cast
<
size_t
>
(
thread_count
)
);
for
(
auto
&
thread_info
:
thread_infos_
)
{
thread_info
.
cpu_cores
=
cores_to_use
;
}
}
ThreadPool
::~
ThreadPool
()
{
// Clear affinity of main thread
if
(
!
cpu_max_freqs_
.
empty
())
{
std
::
vector
<
size_t
>
cores
(
cpu_max_freqs_
.
size
());
for
(
size_t
i
=
0
;
i
<
cores
.
size
();
++
i
)
{
cores
[
i
]
=
i
;
}
port
::
Env
::
Default
()
->
SchedSetAffinity
(
cores
);
}
Destroy
();
}
...
...
mace/utils/thread_pool.h
浏览文件 @
0531fadb
...
...
@@ -31,12 +31,12 @@ namespace utils {
MaceStatus
GetCPUCoresToUse
(
const
std
::
vector
<
float
>
&
cpu_max_freqs
,
const
CPUAffinityPolicy
policy
,
const
size_t
thread_count_hint
,
int
*
thread_count_hint
,
std
::
vector
<
size_t
>
*
cores
);
class
ThreadPool
{
public:
ThreadPool
(
const
size_
t
thread_count
,
ThreadPool
(
const
in
t
thread_count
,
const
CPUAffinityPolicy
affinity_policy
);
~
ThreadPool
();
...
...
@@ -114,6 +114,7 @@ class ThreadPool {
};
std
::
vector
<
ThreadInfo
>
thread_infos_
;
std
::
vector
<
std
::
thread
>
threads_
;
std
::
vector
<
float
>
cpu_max_freqs_
;
int64_t
default_tile_count_
;
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录