提交 1ab11814 编写于 作者: H helinwang 提交者: Yancey

aysnc send/recv, seriliaze/deserialize using threadpool. (#7705)

* aysnc send/recv, seriliaze/deserialize using threadpool

* implement paralell deserialization correctly
上级 77a7bba8
...@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and ...@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "grpc_client.h" #include "grpc_client.h"
#include "paddle/framework/threadpool.h"
namespace paddle { namespace paddle {
namespace operators { namespace operators {
namespace detail { namespace detail {
...@@ -22,25 +23,32 @@ bool RPCClient::AsyncSendVariable(const std::string& ep, ...@@ -22,25 +23,32 @@ bool RPCClient::AsyncSendVariable(const std::string& ep,
const framework::Scope& scope, const framework::Scope& scope,
const std::string& var_name, const std::string& var_name,
int64_t time_out) { int64_t time_out) {
sendrecv::VariableMessage req; const platform::DeviceContext* p_ctx = &ctx;
auto* var = scope.FindVar(var_name); const std::string ep_val = ep;
SerializeToMessage(var_name, var, ctx, &req); const std::string var_name_val = var_name;
const framework::Scope* p_scope = &scope;
// varhandle const auto ch = GetChannel(ep_val);
VarHandle var_h;
var_h.ep = ep; framework::Async([var_name_val, p_ctx, ep_val, p_scope, time_out, ch, this] {
var_h.scope = &scope; auto* var = p_scope->FindVar(var_name_val);
var_h.name = var_name; sendrecv::VariableMessage req;
var_h.ctx = &ctx; SerializeToMessage(var_name_val, var, *p_ctx, &req);
// stub context // varhandle
auto ch = GetChannel(ep); VarHandle var_h;
SendProcessor* s = new SendProcessor(ch); var_h.ep = ep_val;
s->Prepare(var_h, time_out); var_h.scope = p_scope;
s->response_call_back_ = NULL; var_h.name = var_name_val;
var_h.ctx = p_ctx;
auto rpc = s->stub_->AsyncSendVariable(s->context_.get(), req, &cq_);
rpc->Finish(&s->reply_, &s->status_, (void*)s); // stub context
SendProcessor* s = new SendProcessor(ch);
s->Prepare(var_h, time_out);
s->response_call_back_ = NULL;
auto rpc = s->stub_->AsyncSendVariable(s->context_.get(), req, &cq_);
rpc->Finish(&s->reply_, &s->status_, (void*)s);
});
req_count_++; req_count_++;
...@@ -50,8 +58,6 @@ bool RPCClient::AsyncSendVariable(const std::string& ep, ...@@ -50,8 +58,6 @@ bool RPCClient::AsyncSendVariable(const std::string& ep,
void ProcGetResponse(const VarHandle& var_h, void ProcGetResponse(const VarHandle& var_h,
const sendrecv::VariableMessage& ret_msg) { const sendrecv::VariableMessage& ret_msg) {
auto* outvar = var_h.scope->FindVar(var_h.name); auto* outvar = var_h.scope->FindVar(var_h.name);
std::istringstream iss(ret_msg.serialized());
DeserializeFromMessage(ret_msg, *var_h.ctx, outvar); DeserializeFromMessage(ret_msg, *var_h.ctx, outvar);
} }
...@@ -60,24 +66,31 @@ bool RPCClient::AsyncGetVariable(const std::string& ep, ...@@ -60,24 +66,31 @@ bool RPCClient::AsyncGetVariable(const std::string& ep,
const framework::Scope& scope, const framework::Scope& scope,
const std::string& var_name, const std::string& var_name,
int64_t time_out) { int64_t time_out) {
sendrecv::VariableMessage req; const platform::DeviceContext* p_ctx = &ctx;
req.set_varname(var_name); const std::string ep_val = ep;
const std::string var_name_val = var_name;
// varhandle const framework::Scope* p_scope = &scope;
VarHandle var_h; const auto ch = GetChannel(ep_val);
var_h.ep = ep;
var_h.scope = &scope; framework::Async([var_name_val, ep_val, p_scope, p_ctx, time_out, ch, this] {
var_h.name = var_name; sendrecv::VariableMessage req;
var_h.ctx = &ctx; req.set_varname(var_name_val);
// stub context // varhandle
auto ch = GetChannel(ep); VarHandle var_h;
GetProcessor* s = new GetProcessor(ch); var_h.ep = ep_val;
s->Prepare(var_h, time_out); var_h.scope = p_scope;
s->response_call_back_ = ProcGetResponse; var_h.name = var_name_val;
var_h.ctx = p_ctx;
auto rpc = s->stub_->AsyncGetVariable(s->context_.get(), req, &cq_);
rpc->Finish(&s->reply_, &s->status_, (void*)s); // stub context
GetProcessor* s = new GetProcessor(ch);
s->Prepare(var_h, time_out);
s->response_call_back_ = ProcGetResponse;
auto rpc = s->stub_->AsyncGetVariable(s->context_.get(), req, &cq_);
rpc->Finish(&s->reply_, &s->status_, (void*)s);
});
req_count_++; req_count_++;
...@@ -85,19 +98,31 @@ bool RPCClient::AsyncGetVariable(const std::string& ep, ...@@ -85,19 +98,31 @@ bool RPCClient::AsyncGetVariable(const std::string& ep,
} }
bool RPCClient::Wait() { bool RPCClient::Wait() {
bool ok = true; if (req_count_ <= 0) {
return true;
}
while (true) { std::vector<bool> a(req_count_);
if (req_count_ <= 0) { std::vector<std::future<void>> waits(req_count_);
break;
}
if (!Proceed()) { for (int i = 0; i < req_count_; i++) {
waits[i] = framework::Async([i, &a, this] { a[i] = Proceed(); });
}
for (int i = 0; i < req_count_; i++) {
waits[i].wait();
}
int last_req_count = req_count_;
req_count_ = 0;
for (int i = 0; i < last_req_count; i++) {
if (!a[i]) {
return false; return false;
} }
} }
return ok; return true;
} }
bool RPCClient::Proceed() { bool RPCClient::Proceed() {
...@@ -124,7 +149,6 @@ bool RPCClient::Proceed() { ...@@ -124,7 +149,6 @@ bool RPCClient::Proceed() {
c->Process(); c->Process();
delete c; delete c;
req_count_--;
return true; return true;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册