You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.8 KiB
90 lines
2.8 KiB
7 years ago
|
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
|
||
7 years ago
|
|
||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
you may not use this file except in compliance with the License.
|
||
|
You may obtain a copy of the License at
|
||
|
|
||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
||
|
Unless required by applicable law or agreed to in writing, software
|
||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
See the License for the specific language governing permissions and
|
||
|
limitations under the License. */
|
||
|
|
||
|
#pragma once
|
||
|
|
||
7 years ago
|
#include <map>
|
||
|
#include <set>
|
||
7 years ago
|
#include <string>
|
||
|
#include <thread> // NOLINT
|
||
|
#include <utility>
|
||
7 years ago
|
#include <vector>
|
||
7 years ago
|
|
||
7 years ago
|
#include "grpc++/grpc++.h"
|
||
7 years ago
|
#include "paddle/fluid/framework/blocking_queue.h"
|
||
7 years ago
|
#include "paddle/fluid/framework/executor.h"
|
||
7 years ago
|
#include "paddle/fluid/framework/lod_tensor.h"
|
||
7 years ago
|
#include "paddle/fluid/framework/program_desc.h"
|
||
7 years ago
|
#include "paddle/fluid/framework/scope.h"
|
||
|
#include "paddle/fluid/framework/selected_rows.h"
|
||
|
#include "paddle/fluid/framework/var_type.h"
|
||
7 years ago
|
#include "paddle/fluid/operators/distributed/grpc_service.h"
|
||
|
#include "paddle/fluid/operators/distributed/request_handler.h"
|
||
|
#include "paddle/fluid/operators/distributed/rpc_server.h"
|
||
|
#include "paddle/fluid/operators/distributed/send_recv.grpc.pb.h"
|
||
|
#include "paddle/fluid/operators/distributed/send_recv.pb.h"
|
||
|
#include "paddle/fluid/operators/distributed/sendrecvop_utils.h"
|
||
7 years ago
|
#include "paddle/fluid/platform/profiler.h"
|
||
7 years ago
|
|
||
|
namespace paddle {
|
||
|
namespace operators {
|
||
7 years ago
|
namespace distributed {
|
||
7 years ago
|
|
||
|
class RequestBase;
|
||
|
|
||
7 years ago
|
class AsyncGRPCServer final : public RPCServer {
|
||
7 years ago
|
public:
|
||
7 years ago
|
explicit AsyncGRPCServer(const std::string& address, int client_num)
|
||
|
: RPCServer(address, client_num), ready_(0) {}
|
||
7 years ago
|
|
||
7 years ago
|
virtual ~AsyncGRPCServer() {}
|
||
|
void WaitServerReady() override;
|
||
|
void StartServer() override;
|
||
7 years ago
|
|
||
7 years ago
|
private:
|
||
7 years ago
|
// HandleRequest needs to be thread-safe.
|
||
7 years ago
|
void HandleRequest(
|
||
|
::grpc::ServerCompletionQueue* cq, const std::string& rpc_name,
|
||
|
std::function<void(const std::string&, int)> TryToRegisterNewOne);
|
||
7 years ago
|
|
||
7 years ago
|
void TryToRegisterNewOne(const std::string& rpc_name, int req_id);
|
||
7 years ago
|
void ShutdownQueue();
|
||
7 years ago
|
void ShutDownImpl() override;
|
||
7 years ago
|
|
||
|
private:
|
||
7 years ago
|
static const int kRequestBufSize = 100;
|
||
7 years ago
|
|
||
7 years ago
|
std::mutex cq_mutex_;
|
||
|
volatile bool is_shut_down_ = false;
|
||
7 years ago
|
|
||
7 years ago
|
GrpcService::AsyncService service_;
|
||
|
std::unique_ptr<::grpc::Server> server_;
|
||
7 years ago
|
|
||
|
// condition of the sub program
|
||
7 years ago
|
std::condition_variable barrier_condition_;
|
||
7 years ago
|
|
||
7 years ago
|
std::mutex mutex_ready_;
|
||
7 years ago
|
std::condition_variable condition_ready_;
|
||
7 years ago
|
|
||
7 years ago
|
int ready_;
|
||
7 years ago
|
|
||
|
std::map<std::string, std::unique_ptr<::grpc::ServerCompletionQueue>> rpc_cq_;
|
||
|
std::map<std::string, std::vector<std::unique_ptr<std::thread>>> rpc_threads_;
|
||
|
std::map<std::string, std::vector<RequestBase*>> rpc_reqs_;
|
||
7 years ago
|
};
|
||
|
|
||
7 years ago
|
}; // namespace distributed
|
||
7 years ago
|
}; // namespace operators
|
||
|
}; // namespace paddle
|