Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
8
Issues
8
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
NiuTrans
NiuTrans.Tensor
Commits
c8da72b4
Commit
c8da72b4
authored
Mar 12, 2021
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updates of locks
parent
7c53210c
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
132 行增加
和
7 行删除
+132
-7
source/train/XLeader.cpp
+81
-1
source/train/XLeader.h
+6
-0
source/train/XWorker.cpp
+19
-1
source/train/XWorker.h
+6
-0
source/train/XWorkerBroadcast.cpp
+9
-2
source/train/XWorkerCollect.cpp
+2
-1
source/train/XWorkerUpdate.cpp
+9
-2
没有找到文件。
source/train/XLeader.cpp
查看文件 @
c8da72b4
...
...
@@ -125,6 +125,78 @@ void XLeader::InitForRun()
}
}
/* mark the workers as LOCKED */
void
XLeader
::
LockWorkers
()
{
int
paramNum
=
serverModel
.
paramNum
;
for
(
int
i
=
0
;
i
<
jworkers
.
count
;
i
++
)
{
XWorkerJob
*
worker
=
(
XWorkerJob
*
)
jworkers
[
i
];
worker
->
ResetJobCount
();
worker
->
LockCount
();
}
for
(
int
i
=
0
;
i
<
cworkers
.
count
;
i
++
)
{
XWorkerCollect
*
worker
=
(
XWorkerCollect
*
)
cworkers
[
i
];
worker
->
ResetJobCount
();
worker
->
LockCount
();
}
for
(
int
i
=
0
;
i
<
uworkers
.
count
;
i
++
)
{
XWorkerUpdate
*
worker
=
(
XWorkerUpdate
*
)
uworkers
[
i
];
worker
->
ResetJobCount
(
paramNum
);
worker
->
LockCount
();
}
for
(
int
i
=
0
;
i
<
bworkers
.
count
;
i
++
)
{
XWorkerBroadcast
*
worker
=
(
XWorkerBroadcast
*
)
bworkers
[
i
];
worker
->
ResetJobCount
(
paramNum
);
worker
->
LockCount
();
}
}
/*
wait for unlocked workers (i.e., all workers finish their jobs).
It is used as a response to LockWorkers(). It tries to lock the mutex
in each worker. Because the mutex has been locked in LockWorkers(),
WaitForUnlockedWorkers() must wait until the worker finishes its job and
unlock the mutex. In this way, WaitForUnlockedWorkers() would return only
if all workers finish their jobs.
>> activeJobWorkers - flags that indicat whether a job worker is active
*/
void
XLeader
::
WaitForUnlockedWorkers
(
const
int
*
activeJobWorkers
)
{
int
activeNum
=
0
;
for
(
int
i
=
0
;
i
<
jworkers
.
count
;
i
++
)
{
XWorkerJob
*
worker
=
(
XWorkerJob
*
)
jworkers
[
i
];
if
(
activeJobWorkers
[
i
]
>
0
)
{
worker
->
LockCount
();
CheckNTErrors
(
worker
->
IsFinished
(),
"Something is wrong with the worker!"
);
worker
->
UnlockCount
();
activeNum
++
;
}
}
XList
workers
;
workers
.
AddList
(
&
cworkers
);
workers
.
AddList
(
&
uworkers
);
workers
.
AddList
(
&
bworkers
);
for
(
int
i
=
0
;
i
<
workers
.
count
;
i
++
)
{
XWorker
*
worker
=
(
XWorker
*
)
workers
[
i
];
if
(
activeNum
>
0
)
{
worker
->
LockCount
();
CheckNTErrors
(
worker
->
IsFinished
(),
"Something is wrong with the worker!"
);
worker
->
UnlockCount
();
}
else
{
worker
->
UnlockCount
();
}
}
}
/* get loss */
float
XLeader
::
GetLoss
()
{
...
...
@@ -278,6 +350,8 @@ bool XLeader::Run(XConfig * config, DataDistributeBase * dataDistributor,
InitForRun
();
LockWorkers
();
for
(
int
i
=
0
;
i
<
jworkers
.
count
;
i
++
)
active
[
i
]
=
0
;
...
...
@@ -306,6 +380,9 @@ bool XLeader::Run(XConfig * config, DataDistributeBase * dataDistributor,
/* job in queue 1: make a record of the run */
worker
->
AddJobRecord
();
/* job in quene 1: finish the job */
worker
->
AddJobCountFinished
();
active
[
i
]
=
1
;
activeJobCount
++
;
}
...
...
@@ -338,6 +415,7 @@ bool XLeader::Run(XConfig * config, DataDistributeBase * dataDistributor,
collecter
->
AddJobUpdateAll
(
&
members
,
&
membersAll
,
&
serverModel
,
optimizer
,
updater
,
broadcaster
);
collecter
->
AddJobCollectOther
(
&
memberRecords
,
&
serverRecord
);
collecter
->
AddJobCountFinished
();
/* jobs in queue 2: collect the (gradient) data and other stuff. This
is a reduce process. */
...
...
@@ -352,9 +430,11 @@ bool XLeader::Run(XConfig * config, DataDistributeBase * dataDistributor,
not involved in this run. */
//broadcaster->AddJobBroadcast(&serverModel, &membersAll);
WaitForFinishing
();
//
WaitForFinishing();
}
WaitForUnlockedWorkers
(
active
);
for
(
int
i
=
0
;
i
<
jworkers
.
count
;
i
++
)
{
XWorkerJob
*
worker
=
(
XWorkerJob
*
)
jworkers
[
i
];
worker
->
Clear
();
...
...
source/train/XLeader.h
查看文件 @
c8da72b4
...
...
@@ -112,6 +112,12 @@ public:
/* initialize the models for running them */
void
InitForRun
();
/* mark the workers as LOCKED */
void
LockWorkers
();
/* wait for unlocked workers (i.e., all workers finish their jobs) */
void
WaitForUnlockedWorkers
(
const
int
*
activeJobWorkers
);
/* get loss */
float
GetLoss
();
...
...
source/train/XWorker.cpp
查看文件 @
c8da72b4
...
...
@@ -130,13 +130,31 @@ void XWorker::LockCount()
{
MUTEX_LOCK
(
countMutex
);
}
/* unlock the count mutex */
void
XWorker
::
UnlockCount
()
{
MUTEX_UNLOCK
(
countMutex
);
}
/* check if the worker finishes its job */
bool
XWorker
::
IsFinished
()
{
if
(
jobCountExpected
<=
0
)
return
true
;
if
(
jobCountExpected
==
jobCountFinished
)
return
true
;
return
false
;
}
/* count a finished job */
void
XWorker
::
CountFinishedJob
()
{
jobCountFinished
++
;
if
(
jobCountExpected
<
0
||
jobCountFinished
>
jobCountExpected
){
if
(
jobCountExpected
<
0
||
jobCountFinished
>
=
jobCountExpected
){
MUTEX_UNLOCK
(
countMutex
);
}
else
{
...
...
source/train/XWorker.h
查看文件 @
c8da72b4
...
...
@@ -115,6 +115,12 @@ public:
/* lock the count mutex */
void
LockCount
();
/* unlock the count mutex */
void
UnlockCount
();
/* check if the worker finishes its job */
bool
IsFinished
();
/* count a finished job */
void
CountFinishedJob
();
...
...
source/train/XWorkerBroadcast.cpp
查看文件 @
c8da72b4
...
...
@@ -191,10 +191,17 @@ bool XWorkerBroadcast::AddJobBroadcastSingle(XModel * source, XList * targetList
args
.
AddList
(
targetList
);
args
.
AddInt
(
pid
);
if
(
isInstantRun
)
XList
argsNULL
;
argsNULL
.
Add
(
this
);
if
(
isInstantRun
)
{
XWorkerBroadcast
::
BroadcastSingle
(
&
args
);
else
XWorkerBroadcast
::
CountFinished
(
&
argsNULL
);
}
else
{
queue
.
EnqueueJob
((
void
*
)(
char
*
)
XWorkerBroadcast
::
BroadcastSingle
,
&
args
);
queue
.
EnqueueJob
((
void
*
)(
char
*
)
XWorkerBroadcast
::
CountFinished
,
&
argsNULL
);
}
return
true
;
}
...
...
source/train/XWorkerCollect.cpp
查看文件 @
c8da72b4
...
...
@@ -124,8 +124,9 @@ void XWorkerCollect::UpdateDataAll(XList * memberActive, XList * memberAll, XMod
(in another thread) */
if
(
finishedCount
[
j
]
==
memberActive
->
count
)
{
paramServer
.
flag
=
PARAM_STATE_COLLECTED
;
if
(
updater
!=
NULL
)
if
(
updater
!=
NULL
)
{
updater
->
AddJobUpdateSingle
(
server
,
memberAll
,
j
,
optimizer
,
broadcaster
);
}
}
else
if
(
finishedCount
[
j
]
>
memberActive
->
count
)
{
ShowNTErrors
(
"Something is wrong with finishedCount!"
);
...
...
source/train/XWorkerUpdate.cpp
查看文件 @
c8da72b4
...
...
@@ -189,10 +189,17 @@ bool XWorkerUpdate::AddJobUpdateSingle(XModel * model, XList * members, int pid,
args
.
Add
(
optimizer
);
args
.
Add
(
broadcaster
);
if
(
isInstantRun
)
XList
argsNULL
;
argsNULL
.
Add
(
this
);
if
(
isInstantRun
)
{
XWorkerUpdate
::
UpdateSingle
(
&
args
);
else
XWorkerUpdate
::
CountFinished
(
&
argsNULL
);
}
else
{
queue
.
EnqueueJob
((
void
*
)(
char
*
)
XWorkerUpdate
::
UpdateSingle
,
&
args
);
queue
.
EnqueueJob
((
void
*
)(
char
*
)
XWorkerUpdate
::
CountFinished
,
&
argsNULL
);
}
return
true
;
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论