Skip to content

Commit 75bfdb3

Browse files
committed
refine
1 parent fc4bcdd commit 75bfdb3

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

paddle/fluid/framework/executor.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ std::unique_ptr<ExecutorPrepareContext> Executor::Prepare(
279279
return std::unique_ptr<ExecutorPrepareContext>(ctx);
280280
}
281281

282-
std::vector<std::shared_ptr<ExecutorPrepareContext>> Prepare(
282+
std::vector<std::shared_ptr<ExecutorPrepareContext>> Executor::Prepare(
283283
const ProgramDesc& program, const std::vector<int>& block_ids) {
284284
std::vector<std::shared_ptr<ExecutorPrepareContext>> result;
285285
for (auto& bid : block_ids) {

paddle/fluid/operators/listen_and_serv_op.cc

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,24 @@ static void CreateTensorFromMessageType(framework::Variable *var,
5454
}
5555
}
5656

57-
static void ParallelExecuteBlocks(const std::vector<size_t> &parallel_blkids,
58-
framework::Executor *executor,
59-
framework::ProgramDesc *program,
60-
framework::Scope *scope) {
57+
static void ParallelExecuteBlocks(
58+
const std::vector<size_t> &parallel_blkids, framework::Executor *executor,
59+
const std::vector<std::shared_ptr<framework::ExecutorPrepareContext>>
60+
&prepared,
61+
framework::ProgramDesc *program, framework::Scope *scope) {
6162
std::vector<std::future<void>> fs;
6263
for (size_t idx : parallel_blkids) {
63-
fs.push_back(framework::Async([&executor, &program, &scope, idx]() {
64-
int run_block = idx; // thread local
65-
try {
66-
executor->Run(*program, scope, run_block, false, false);
67-
} catch (std::exception &e) {
68-
LOG(ERROR) << "run sub program error " << e.what();
69-
}
70-
}));
64+
fs.push_back(
65+
framework::Async([&executor, &prepared, &program, &scope, idx]() {
66+
int run_block = idx; // thread local
67+
try {
68+
// executor->Run(*program, scope, run_block, false, false);
69+
executor->RunPreparedContext(prepared[run_block].get(), scope,
70+
false, false);
71+
} catch (std::exception &e) {
72+
LOG(ERROR) << "run sub program error " << e.what();
73+
}
74+
}));
7175
}
7276
for (size_t i = 0; i < fs.size(); ++i) fs[i].wait();
7377
}
@@ -105,15 +109,18 @@ class ListenAndServOp : public framework::OperatorBase {
105109

106110
auto *block = Attr<framework::BlockDesc *>(kOptimizeBlock);
107111
auto *program = block->Program();
108-
int num_blocks = program->Size();
112+
size_t num_blocks = program->Size();
109113
PADDLE_ENFORCE_GE(num_blocks, 2,
110114
"server program should have at least 2 blocks");
111115

112116
framework::Executor executor(dev_place);
113117
std::vector<int> block_list;
114-
for (int blkid = 1; blkid < num_blocks; ++blkid)
118+
for (size_t blkid = 1; blkid < num_blocks; ++blkid)
115119
block_list.push_back(blkid);
116120
auto prepared = executor.Prepare(*program, block_list);
121+
prepared.insert(
122+
prepared.begin(),
123+
std::shared_ptr<framework::ExecutorPrepareContext>(nullptr));
117124

118125
// TODO(typhoonzero): change this to a while_op for every cluster-batch.
119126
bool exit_flag = false;
@@ -161,21 +168,22 @@ class ListenAndServOp : public framework::OperatorBase {
161168

162169
// The optimize blocks which have the same parent ID would run parallel
163170
// TODO(Yancey1989): need to use ParallelExecutor for future
164-
size_t last_parent_blkid = program->Block(1).Parent();
171+
int32_t last_parent_blkid = program->Block(1).Parent();
165172
std::vector<size_t> parallel_blkids;
166173
parallel_blkids.push_back(1);
167174
double ts = detail::GetTimestamp();
168175
for (size_t blkid = 2; blkid < num_blocks; ++blkid) {
169176
if (program->Block(blkid).Parent() != last_parent_blkid) {
170177
for (size_t idx : parallel_blkids) VLOG(3) << idx;
171-
ParallelExecuteBlocks(parallel_blkids, &executor, program,
178+
ParallelExecuteBlocks(parallel_blkids, &executor, prepared, program,
172179
&recv_scope);
173180
parallel_blkids.clear();
174181
last_parent_blkid = program->Block(blkid).Parent();
175182
}
176183
parallel_blkids.push_back(blkid);
177184
}
178-
ParallelExecuteBlocks(parallel_blkids, &executor, program, &recv_scope);
185+
ParallelExecuteBlocks(parallel_blkids, &executor, prepared, program,
186+
&recv_scope);
179187

180188
VLOG(2) << "run all blocks spent (ms) " << detail::GetTimestamp() - ts;
181189

0 commit comments

Comments
 (0)