graph_brpc_client.h 5.0 KB
Newer Older
S
seemingwang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// 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

#include <ThreadPool.h>
#include <memory>
#include <string>
#include <vector>

#include <utility>
#include "ThreadPool.h"
#include "brpc/channel.h"
#include "brpc/controller.h"
#include "brpc/server.h"
27 28 29 30
#include "paddle/fluid/distributed/ps/service/brpc_ps_client.h"
#include "paddle/fluid/distributed/ps/service/graph_brpc_server.h"
#include "paddle/fluid/distributed/ps/service/ps_client.h"
#include "paddle/fluid/distributed/ps/table/table.h"
S
seemingwang 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/framework/tensor_util.h"

namespace paddle {
namespace distributed {

class GraphPsService_Stub : public PsService_Stub {
 public:
  GraphPsService_Stub(::google::protobuf::RpcChannel* channel,
                      ::google::protobuf::RpcChannel* local_channel = NULL,
                      GraphBrpcService* service = NULL, int thread_num = 1)
      : PsService_Stub(channel) {
    this->local_channel = local_channel;
    this->graph_service = service;
    task_pool.reset(new ::ThreadPool(thread_num));
  }
  virtual ~GraphPsService_Stub() {}

  // implements PsService ------------------------------------------
  GraphBrpcService* graph_service;
  std::shared_ptr<::ThreadPool> task_pool;
  ::google::protobuf::RpcChannel* local_channel;
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GraphPsService_Stub);
  void service(::google::protobuf::RpcController* controller,
               const ::paddle::distributed::PsRequestMessage* request,
               ::paddle::distributed::PsResponseMessage* response,
               ::google::protobuf::Closure* done);
};
class GraphBrpcClient : public BrpcPsClient {
 public:
  GraphBrpcClient() {}
  virtual ~GraphBrpcClient() {}
64 65
  // given a batch of nodes, sample graph_neighbors for each of them
  virtual std::future<int32_t> batch_sample_neighbors(
66 67
      uint32_t table_id, int idx, std::vector<int64_t> node_ids,
      int sample_size, std::vector<std::vector<int64_t>>& res,
68
      std::vector<std::vector<float>>& res_weight, bool need_weight,
S
seemingwang 已提交
69
      int server_index = -1);
S
seemingwang 已提交
70

71 72 73
  virtual std::future<int32_t> pull_graph_list(uint32_t table_id, int type_id,
                                               int idx, int server_index,
                                               int start, int size, int step,
S
seemingwang 已提交
74 75
                                               std::vector<FeatureNode>& res);
  virtual std::future<int32_t> random_sample_nodes(uint32_t table_id,
76
                                                   int type_id, int idx,
S
seemingwang 已提交
77 78
                                                   int server_index,
                                                   int sample_size,
79
                                                   std::vector<int64_t>& ids);
S
seemingwang 已提交
80
  virtual std::future<int32_t> get_node_feat(
81
      const uint32_t& table_id, int idx, const std::vector<int64_t>& node_ids,
S
seemingwang 已提交
82 83
      const std::vector<std::string>& feature_names,
      std::vector<std::vector<std::string>>& res);
84

S
seemingwang 已提交
85
  virtual std::future<int32_t> set_node_feat(
86
      const uint32_t& table_id, int idx, const std::vector<int64_t>& node_ids,
S
seemingwang 已提交
87 88 89
      const std::vector<std::string>& feature_names,
      const std::vector<std::vector<std::string>>& features);

90 91
  virtual std::future<int32_t> clear_nodes(uint32_t table_id, int type_id,
                                           int idx);
92
  virtual std::future<int32_t> add_graph_node(
93
      uint32_t table_id, int idx, std::vector<int64_t>& node_id_list,
94 95
      std::vector<bool>& is_weighted_list);
  virtual std::future<int32_t> remove_graph_node(
96
      uint32_t table_id, int idx_, std::vector<int64_t>& node_id_list);
Z
zhaocaibei123 已提交
97
  virtual int32_t Initialize();
S
seemingwang 已提交
98 99
  int get_shard_num() { return shard_num; }
  void set_shard_num(int shard_num) { this->shard_num = shard_num; }
100
  int get_server_index_by_id(int64_t id);
S
seemingwang 已提交
101
  void set_local_channel(int index) {
Z
zhaocaibei123 已提交
102
    this->local_channel = GetCmdChannel(index);
S
seemingwang 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  }
  void set_local_graph_service(GraphBrpcService* graph_service) {
    this->graph_service = graph_service;
  }
  GraphPsService_Stub getServiceStub(::google::protobuf::RpcChannel* channel,
                                     int thread_num = 1) {
    return GraphPsService_Stub(channel, local_channel, graph_service,
                               thread_num);
  }

 private:
  int shard_num;
  size_t server_size;
  ::google::protobuf::RpcChannel* local_channel;
  GraphBrpcService* graph_service;
};

}  // namespace distributed
}  // namespace paddle