brpc_ps_client.cc 74.9 KB
Newer Older
T
tangwei12 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

15 16
#include "paddle/fluid/distributed/ps/service/brpc_ps_client.h"

T
tangwei12 已提交
17
#include <memory>
18
#include <sstream>
T
tangwei12 已提交
19
#include <string>
T
tangwei12 已提交
20

T
tangwei12 已提交
21 22
#include "paddle/fluid/framework/archive.h"

Z
zhaocaibei123 已提交
23
static const int max_port = 65535;
T
tangwei12 已提交
24

25 26
DEFINE_int32(pserver_push_dense_merge_limit,
             12,
T
tangwei12 已提交
27 28
             "limit max push_dense local merge requests");

29 30
DEFINE_int32(pserver_push_sparse_merge_limit,
             12,
T
tangwei12 已提交
31 32
             "limit max push_sparse local merge requests");

33 34
DEFINE_int32(pserver_pull_dense_limit,
             12,
T
tangwei12 已提交
35 36
             "limit max push_sparse local merge requests");

37 38
DEFINE_int32(pserver_async_push_dense_interval_ms,
             10,
T
tangwei12 已提交
39 40
             "async push_dense to server interval");

41 42
DEFINE_int32(pserver_async_push_sparse_interval_ms,
             10,
T
tangwei12 已提交
43 44
             "async push_sparse to server interval");

45 46
DEFINE_bool(pserver_scale_gradient_by_merge,
            false,
T
tangwei12 已提交
47 48
            "scale dense gradient when merged");

49 50
DEFINE_int32(pserver_communicate_compress_type,
             0,
T
tangwei12 已提交
51 52
             "none:0 snappy:1 gzip:2 zlib:3 lz4:4");

53 54
DEFINE_int32(pserver_max_async_call_num,
             13,
T
tangwei12 已提交
55 56 57 58
             "max task num in async_call_server");

DEFINE_int32(pserver_timeout_ms, 500000, "pserver request server timeout_ms");

59 60
DEFINE_int32(pserver_connect_timeout_ms,
             10000,
T
tangwei12 已提交
61 62 63 64
             "pserver connect server timeout_ms");

DEFINE_int32(pserver_sparse_merge_thread, 1, "pserver sparse merge thread num");

65 66
DEFINE_int32(pserver_sparse_table_shard_num,
             1000,
Z
zhaocaibei123 已提交
67 68
             "sparse table shard for save & load");

69 70 71 72 73 74 75
namespace paddle {
namespace framework {
class Scope;
class Variable;
}  // namespace framework
}  // namespace paddle

T
tangwei12 已提交
76 77 78
namespace paddle {
namespace distributed {

79 80
inline size_t get_sparse_shard(uint32_t shard_num,
                               uint32_t server_num,
T
tangwei12 已提交
81 82 83 84 85 86 87 88 89
                               uint64_t key) {
  size_t remind = shard_num % server_num;
  size_t local_shard_num =
      remind == 0 ? shard_num / server_num : shard_num / server_num + 1;
  return (key % shard_num) / local_shard_num;
}

void DownpourPsClientService::service(
    ::google::protobuf::RpcController *controller,
90 91
    const PsRequestMessage *request,
    PsResponseMessage *response,
T
tangwei12 已提交
92
    ::google::protobuf::Closure *done) {
T
tangwei12 已提交
93
  brpc::ClosureGuard done_guard(done);
Z
zhaocaibei123 已提交
94
  int ret = _client->HandleClient2ClientMsg(
T
tangwei12 已提交
95 96 97 98 99 100 101 102 103 104
      request->cmd_id(), request->client_id(), request->data());
  response->set_err_code(0);
  response->set_err_msg("");
  if (ret != 0) {
    response->set_err_code(-1);
    response->set_err_msg("handle_client2client_msg failed");
  }
}

// 启动client端RpcService 用于数据互发等操作
Z
zhaocaibei123 已提交
105 106
int32_t BrpcPsClient::StartClientService() {
  if (_service.Configure(this, _client_id) != 0) {
T
tangwei12 已提交
107 108 109 110 111 112 113 114 115
    LOG(ERROR)
        << "service initialize failed, service_name:DownpourPsClientService";
    return -1;
  }
  _server.AddService(&_service, brpc::SERVER_DOESNT_OWN_SERVICE);
  brpc::ServerOptions options;
  int start_port = 8500;
  options.num_threads = 24;

116 117
  if (_server.Start(butil::my_ip_cstr(),
                    brpc::PortRange(start_port, max_port),
T
tangwei12 已提交
118 119 120 121
                    &options) != 0) {
    LOG(ERROR) << "BrpcPsServer start failed";
    return -1;
  }
Z
zhaocaibei123 已提交
122
  _server_started = true;
123 124
  _env->RegistePsClient(
      butil::my_ip_cstr(), _server.listen_address().port, _client_id);
T
tangwei12 已提交
125 126 127
  return 0;
}

Z
zhaocaibei123 已提交
128
int32_t BrpcPsClient::CreateClient2ClientConnection(
T
tangwei12 已提交
129 130 131 132 133 134 135 136
    int pserver_timeout_ms, int pserver_connect_timeout_ms, int max_retry) {
  brpc::ChannelOptions options;
  options.protocol = "baidu_std";
  options.timeout_ms = pserver_timeout_ms;
  options.connection_type = "pooled";
  options.connect_timeout_ms = pserver_connect_timeout_ms;
  options.max_retry = max_retry;

Z
zhaocaibei123 已提交
137
  std::vector<PSHost> client_list = _env->GetPsClients();
Z
zhaocaibei123 已提交
138 139 140 141
  VLOG(1) << "BrpcPsClient::create_c2c_connection client_list size: "
          << client_list.size();
  for (auto cc : client_list) {
    VLOG(1) << "BrpcPsClient::create_c2c_connection client_list: "
Z
zhaocaibei123 已提交
142
            << cc.ToString();
Z
zhaocaibei123 已提交
143
  }
T
tangwei12 已提交
144 145 146 147 148 149 150 151
  _client_channels.resize(client_list.size());
  std::ostringstream os;
  std::string server_ip_port;
  for (size_t i = 0; i < client_list.size(); ++i) {
    server_ip_port.assign(client_list[i].ip.c_str());
    server_ip_port.append(":");
    server_ip_port.append(std::to_string(client_list[i].port));
    _client_channels[i].reset(new brpc::Channel());
Z
zhangchunle 已提交
152
    if (_client_channels[i]->Init(server_ip_port.c_str(), "", &options)) {
153 154 155 156 157 158 159 160 161
      VLOG(0) << "BrpcPSClient connect to Client:" << server_ip_port
              << " Failed! Try again.";
      std::string int_ip_port =
          GetIntTypeEndpoint(client_list[i].ip, client_list[i].port);
      if (_client_channels[i]->Init(int_ip_port.c_str(), "", &options) != 0) {
        LOG(ERROR) << "BrpcPSClient connect to Client:" << int_ip_port
                   << " Failed!";
        return -1;
      }
T
tangwei12 已提交
162 163 164 165 166 167 168
    }
    os << server_ip_port << ",";
  }
  LOG(INFO) << "Client connect success:" << os.str();
  return 0;
}

Z
zhaocaibei123 已提交
169
int32_t BrpcPsClient::Initialize() {
T
tangwei12 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183
  _async_call_num = 0;

  brpc::ChannelOptions options;
  options.protocol = "baidu_std";
  options.timeout_ms = FLAGS_pserver_timeout_ms;
  options.connection_type = "pooled";
  options.connect_timeout_ms = FLAGS_pserver_connect_timeout_ms;
  options.max_retry = 3;

  std::ostringstream os;
  std::string server_ip_port;
  std::string client_ip(butil::my_ip_cstr());

  // 获取server列表,并连接
Z
zhaocaibei123 已提交
184
  std::vector<PSHost> server_list = _env->GetPsServers();
T
tangwei12 已提交
185 186 187 188 189 190 191 192 193
  _server_channels.resize(server_list.size());
  for (size_t i = 0; i < server_list.size(); ++i) {
    server_ip_port.assign(server_list[i].ip.c_str());
    server_ip_port.append(":");
    server_ip_port.append(std::to_string(server_list[i].port));
    for (size_t j = 0; j < _server_channels[i].size(); ++j) {
      _server_channels[i][j].reset(new brpc::Channel());
      if (_server_channels[i][j]->Init(server_ip_port.c_str(), "", &options) !=
          0) {
194 195 196 197 198 199 200 201 202 203
        VLOG(0) << "BrpcPSclient connect to Server:" << server_ip_port
                << " Failed! Try again.";
        std::string int_ip_port =
            GetIntTypeEndpoint(server_list[i].ip, server_list[i].port);
        if (_server_channels[i][j]->Init(int_ip_port.c_str(), "", &options) !=
            0) {
          LOG(ERROR) << "BrpcPSclient connect to Server:" << int_ip_port
                     << " Failed!";
          return -1;
        }
T
tangwei12 已提交
204 205 206 207 208
      }
    }
    os << server_ip_port << ",";
  }
  // 启动client探听接口, 并相互建立连接
Z
zhaocaibei123 已提交
209
  StartClientService();
T
tangwei12 已提交
210

Z
zhaocaibei123 已提交
211 212
  // 异步push 请求队列初始化
  const auto &worker_param = _config.worker_param().downpour_worker_param();
213
  for (int i = 0; i < worker_param.downpour_table_param_size(); ++i) {
Z
zhaocaibei123 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226
    auto type = worker_param.downpour_table_param(i).type();
    auto table_id = worker_param.downpour_table_param(i).table_id();
    if (type == PS_DENSE_TABLE) {
      _push_dense_task_queue_map[table_id] =
          paddle::framework::MakeChannel<DenseAsyncTask *>();
    }
    if (type == PS_SPARSE_TABLE) {
      _push_sparse_task_queue_map[table_id] =
          paddle::framework::MakeChannel<SparseAsyncTask *>();
      _push_sparse_merge_count_map[table_id] = 0;
    }
  }

227 228 229
  auto &profiler = CostProfiler::instance();
  profiler.register_profiler("pserver_client_pull_dense");
  profiler.register_profiler("pserver_client_pull_sparse");
Z
zhaocaibei123 已提交
230
  profiler.register_profiler("pserver_client_pull_sparse_param");
231 232 233 234 235 236 237 238 239 240 241 242 243 244
  profiler.register_profiler("pserver_client_pull_sparse_local");
  profiler.register_profiler("pserver_client_push_sparse");
  profiler.register_profiler("pserver_client_push_sparse_parse");
  profiler.register_profiler("client_push_sparse_put");
  profiler.register_profiler("pserver_client_push_sparse");
  profiler.register_profiler("pserver_client_push_sparse_merge");
  profiler.register_profiler("pserver_client_push_sparse_rpc");
  profiler.register_profiler("pserver_client_push_dense");
  profiler.register_profiler("pserver_client_push_dense_parse");
  profiler.register_profiler("push_dense_put");
  profiler.register_profiler("pserver_client_push_dense_merge");
  profiler.register_profiler("pserver_client_push_dense_rpc");
  profiler.register_profiler("pserver_client_push_dense_send");

T
tangwei12 已提交
245 246
  _running = true;
  _flushing = false;
Z
zhaocaibei123 已提交
247 248
  // 启动异步push线程
  _async_push_sparse_thread =
Z
zhaocaibei123 已提交
249
      std::thread(std::bind(&BrpcPsClient::PushSparseTaskConsume, this));
Z
zhaocaibei123 已提交
250 251
  // _async_push_sparse_thread.detach();
  _async_push_dense_thread =
Z
zhaocaibei123 已提交
252
      std::thread(std::bind(&BrpcPsClient::PushDenseTaskConsume, this));
Z
zhaocaibei123 已提交
253 254
  // for debug
  // _print_thread =
Z
zhaocaibei123 已提交
255
  //    std::thread(std::bind(&BrpcPsClient::PrintQueueSizeThread, this));
Z
zhaocaibei123 已提交
256

T
tangwei12 已提交
257 258 259 260 261
  return 0;
}

int DownpourBrpcClosure::check_response(size_t request_idx, int cmd_id) {
  if (_cntls[request_idx]->Failed()) {
262 263 264
    LOG(ERROR) << "resquest cmd_id:" << cmd_id
               << " failed, "
                  "err:"
T
tangwei12 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278
               << _cntls[request_idx]->ErrorText();
    return -1;
  }
  if (_responses[request_idx].err_code() != 0) {
    LOG(ERROR) << "response ret bad, server_idx:" << request_idx
               << "cmd_id:" << cmd_id
               << " err_code:" << _responses[request_idx].err_code()
               << " err_msg:" << _responses[request_idx].err_msg();
    return -1;
  }
  return 0;
}

int DownpourBrpcClosure::check_save_response(size_t request_idx, int cmd_id) {
Z
zhaocaibei123 已提交
279
  int32_t feasign_size = 0;
T
tangwei12 已提交
280
  if (_cntls[request_idx]->Failed()) {
281 282 283
    LOG(ERROR) << "resquest cmd_id:" << cmd_id
               << " failed, "
                  "err:"
T
tangwei12 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
               << _cntls[request_idx]->ErrorText();
    return -1;
  }
  feasign_size = _responses[request_idx].err_code();
  if (feasign_size < 0) {
    LOG(ERROR) << "response ret bad, server_idx:" << request_idx
               << "cmd_id:" << cmd_id
               << " err_code:" << _responses[request_idx].err_code()
               << " err_msg:" << _responses[request_idx].err_msg();
    return -1;
  }
  return feasign_size;
}

std::string DownpourBrpcClosure::get_response(size_t request_idx, int cmd_id) {
  std::string data = _responses[request_idx].data();
  return data;
}

Z
zhaocaibei123 已提交
303
std::future<int32_t> BrpcPsClient::PrintTableStat(uint32_t table_id) {
T
tangwei12 已提交
304 305 306 307 308 309 310
  size_t request_call_num = _server_channels.size();
  DownpourBrpcClosure *closure = new DownpourBrpcClosure(
      request_call_num, [request_call_num, table_id](void *done) {
        int ret = 0;
        uint64_t feasign_size = 0;
        uint64_t mf_size = 0;
        paddle::framework::BinaryArchive ar;
Z
zhaocaibei123 已提交
311
        auto *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
T
tangwei12 已提交
312 313 314 315 316 317
        for (size_t i = 0; i < request_call_num; ++i) {
          if (closure->check_response(i, PS_PRINT_TABLE_STAT) != 0) {
            ret = -1;
            break;
          }
          std::string resp = closure->get_response(i, PS_PRINT_TABLE_STAT);
318 319
          ar.SetReadBuffer(
              const_cast<char *>(resp.c_str()), resp.length(), nullptr);
T
tangwei12 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

          feasign_size += ar.Get<uint64_t>();
          mf_size += ar.Get<uint64_t>();
        }
        closure->set_promise_value(ret);
        std::cout << "table id: " << table_id
                  << ", feasign size: " << feasign_size
                  << ", mf size: " << mf_size << std::endl;
      });
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();
  for (size_t i = 0; i < request_call_num; ++i) {
    closure->request(i)->set_cmd_id(PS_PRINT_TABLE_STAT);
    closure->request(i)->set_table_id(table_id);
    closure->request(i)->set_client_id(_client_id);
Z
zhaocaibei123 已提交
336
    PsService_Stub rpc_stub(GetCmdChannel(i));
T
tangwei12 已提交
337 338
    closure->cntl(i)->set_timeout_ms(
        10800000);  // cmd msg don't limit timeout for save/load
339 340
    rpc_stub.service(
        closure->cntl(i), closure->request(i), closure->response(i), closure);
T
tangwei12 已提交
341 342 343
  }
  return fut;
}
Z
zhaocaibei123 已提交
344
std::future<int32_t> BrpcPsClient::SendCmd(
T
tangwei12 已提交
345 346 347 348 349
    uint32_t table_id, int cmd_id, const std::vector<std::string> &params) {
  size_t request_call_num = _server_channels.size();
  DownpourBrpcClosure *closure = new DownpourBrpcClosure(
      request_call_num, [request_call_num, cmd_id](void *done) {
        int ret = 0;
Z
zhaocaibei123 已提交
350
        auto *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
T
tangwei12 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
        for (size_t i = 0; i < request_call_num; ++i) {
          if (closure->check_response(i, cmd_id) != 0) {
            ret = -1;
            break;
          }
        }
        closure->set_promise_value(ret);
      });
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();
  for (size_t i = 0; i < request_call_num; ++i) {
    closure->request(i)->set_cmd_id(cmd_id);
    closure->request(i)->set_table_id(table_id);
    closure->request(i)->set_client_id(_client_id);
    for (const auto &param : params) {
      closure->request(i)->add_params(param);
    }
Z
zhaocaibei123 已提交
369
    PsService_Stub rpc_stub(GetCmdChannel(i));
T
tangwei12 已提交
370
    closure->cntl(i)->set_timeout_ms(
Z
zhaocaibei123 已提交
371
        10800000 * 2);  // cmd msg don't limit timeout for save/load
372 373
    rpc_stub.service(
        closure->cntl(i), closure->request(i), closure->response(i), closure);
T
tangwei12 已提交
374 375 376 377
  }
  return fut;
}

Z
zhaocaibei123 已提交
378
std::future<int32_t> BrpcPsClient::SendSaveCmd(
T
tangwei12 已提交
379 380 381 382 383 384
    uint32_t table_id, int cmd_id, const std::vector<std::string> &params) {
  size_t request_call_num = _server_channels.size();
  DownpourBrpcClosure *closure = new DownpourBrpcClosure(
      request_call_num, [request_call_num, cmd_id](void *done) {
        int ret = 0;
        uint32_t feasign_size = 0;
Z
zhaocaibei123 已提交
385
        auto *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
T
tangwei12 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
        for (size_t i = 0; i < request_call_num; ++i) {
          if (closure->check_save_response(i, cmd_id) < 0) {
            ret = -1;
            break;
          }
          feasign_size += closure->check_save_response(i, cmd_id);
        }
        if (ret == 0) {
          closure->set_promise_value(feasign_size);
        } else {
          closure->set_promise_value(ret);
        }
      });
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();
  for (size_t i = 0; i < request_call_num; ++i) {
    closure->request(i)->set_cmd_id(cmd_id);
    closure->request(i)->set_table_id(table_id);
    closure->request(i)->set_client_id(_client_id);
    for (const auto &param : params) {
      closure->request(i)->add_params(param);
    }
Z
zhaocaibei123 已提交
409
    PsService_Stub rpc_stub(GetCmdChannel(i));
T
tangwei12 已提交
410 411
    closure->cntl(i)->set_timeout_ms(
        10800000);  // cmd msg don't limit timeout for save/load
412 413
    rpc_stub.service(
        closure->cntl(i), closure->request(i), closure->response(i), closure);
T
tangwei12 已提交
414 415 416 417
  }
  return fut;
}

Z
zhaocaibei123 已提交
418
std::future<int32_t> BrpcPsClient::Shrink(uint32_t table_id,
419
                                          const std::string threshold) {
Z
zhaocaibei123 已提交
420
  return SendCmd(table_id, PS_SHRINK_TABLE, {threshold});
T
tangwei12 已提交
421 422
}

Z
zhaocaibei123 已提交
423
std::future<int32_t> BrpcPsClient::Load(const std::string &epoch,
T
tangwei12 已提交
424
                                        const std::string &mode) {
Z
zhaocaibei123 已提交
425
  return SendCmd(-1, PS_LOAD_ALL_TABLE, {epoch, mode});
T
tangwei12 已提交
426
}
Z
zhaocaibei123 已提交
427
std::future<int32_t> BrpcPsClient::Load(uint32_t table_id,
T
tangwei12 已提交
428 429
                                        const std::string &epoch,
                                        const std::string &mode) {
Z
zhaocaibei123 已提交
430
  return SendCmd(table_id, PS_LOAD_ONE_TABLE, {epoch, mode});
T
tangwei12 已提交
431 432
}

Z
zhaocaibei123 已提交
433
std::future<int32_t> BrpcPsClient::Save(const std::string &epoch,
T
tangwei12 已提交
434
                                        const std::string &mode) {
Z
zhaocaibei123 已提交
435
  VLOG(1) << "BrpcPsClient::save path " << epoch;
Z
zhaocaibei123 已提交
436
  return SendSaveCmd(-1, PS_SAVE_ALL_TABLE, {epoch, mode});
T
tangwei12 已提交
437
}
Z
zhaocaibei123 已提交
438
std::future<int32_t> BrpcPsClient::Save(uint32_t table_id,
T
tangwei12 已提交
439 440
                                        const std::string &epoch,
                                        const std::string &mode) {
Z
zhaocaibei123 已提交
441 442
  VLOG(1) << "BrpcPsClient::save one table path " << epoch << " table_id "
          << table_id;
Z
zhaocaibei123 已提交
443
  return SendSaveCmd(table_id, PS_SAVE_ONE_TABLE, {epoch, mode});
T
tangwei12 已提交
444 445
}

Z
zhaocaibei123 已提交
446
std::future<int32_t> BrpcPsClient::CacheShuffle(
447 448 449
    uint32_t table_id,
    const std::string &path,
    const std::string &mode,
Z
zhaocaibei123 已提交
450 451 452 453 454 455
    const std::string &cache_threshold) {
  VLOG(1) << "BrpcPsClient send cmd for cache shuffle";
  return SendSaveCmd(table_id, PS_CACHE_SHUFFLE, {path, mode, cache_threshold});
}

std::future<int32_t> BrpcPsClient::CacheShuffleMultiTable(
456 457 458
    std::vector<int> tables,
    const std::string &path,
    const std::string &mode,
Z
zhaocaibei123 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    const std::string &cache_threshold) {
  VLOG(1) << "BrpcPsClient send cmd for cache shuffle multi table one path";
  std::vector<std::string> param;
  param.push_back(path);
  param.push_back(mode);
  param.push_back(cache_threshold);
  for (size_t i = 0; i < tables.size(); i++) {
    param.push_back(std::to_string(tables[i]));
  }
  return SendSaveCmd(0, PS_CACHE_SHUFFLE, param);
}

std::future<int32_t> BrpcPsClient::SaveCache(uint32_t table_id,
                                             const std::string &path,
                                             const std::string &mode) {
  return SendSaveCmd(table_id, PS_SAVE_ONE_CACHE_TABLE, {path, mode});
}

std::future<int32_t> BrpcPsClient::GetCacheThreshold(uint32_t table_id,
                                                     double &cache_threshold) {
  int cmd_id = PS_GET_CACHE_THRESHOLD;
  size_t request_call_num = _server_channels.size();
  DownpourBrpcClosure *closure = new DownpourBrpcClosure(
      request_call_num,
      [request_call_num, cmd_id, &cache_threshold](void *done) {
        int ret = 0;
Z
zhaocaibei123 已提交
485
        auto *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
Z
zhaocaibei123 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
        std::vector<double> cache_thresholds(request_call_num, 0);
        for (size_t i = 0; i < request_call_num; ++i) {
          if (closure->check_response(i, cmd_id) != 0) {
            ret = -1;
            break;
          }
          std::string cur_res = closure->get_response(i, cmd_id);
          cache_thresholds[i] = std::stod(cur_res);
        }
        double sum_threshold = 0.0;
        int count = 0;
        for (auto t : cache_thresholds) {
          if (t >= 0) {
            sum_threshold += t;
            ++count;
          }
        }
        if (count == 0) {
          cache_threshold = 0;
        } else {
          cache_threshold = sum_threshold / count;
        }
        VLOG(1) << "client get cache threshold: " << cache_threshold;
        closure->set_promise_value(ret);
      });
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();
  for (size_t i = 0; i < request_call_num; ++i) {
    closure->request(i)->set_cmd_id(cmd_id);
    closure->request(i)->set_table_id(table_id);
    closure->request(i)->set_client_id(_client_id);
    PsService_Stub rpc_stub(GetCmdChannel(i));
    closure->cntl(i)->set_timeout_ms(10800000);
520 521
    rpc_stub.service(
        closure->cntl(i), closure->request(i), closure->response(i), closure);
Z
zhaocaibei123 已提交
522 523 524 525
  }
  return fut;
}

Z
zhaocaibei123 已提交
526 527
std::future<int32_t> BrpcPsClient::Clear() {
  return SendCmd(-1, PS_CLEAR_ALL_TABLE, {});
T
tangwei12 已提交
528
}
Z
zhaocaibei123 已提交
529 530
std::future<int32_t> BrpcPsClient::Clear(uint32_t table_id) {
  return SendCmd(table_id, PS_CLEAR_ONE_TABLE, {});
T
tangwei12 已提交
531 532
}

Z
zhaocaibei123 已提交
533 534 535 536 537 538 539 540
std::future<int32_t> BrpcPsClient::Revert() {
  return SendCmd(-1, PS_REVERT, {});
}

std::future<int32_t> BrpcPsClient::CheckSavePrePatchDone() {
  return SendCmd(-1, PS_CHECK_SAVE_PRE_PATCH_DONE, {});
}

Z
zhaocaibei123 已提交
541
std::future<int32_t> BrpcPsClient::Flush() {
Z
zhaocaibei123 已提交
542
  VLOG(0) << "BrpcPsClient::flush begin";
T
tangwei12 已提交
543 544 545 546 547 548 549
  _flushing = true;
  std::promise<int> promise;
  std::future<int32_t> fut = promise.get_future();
  do {
    VLOG(3) << "wait _async_call_num:" << _async_call_num;
    usleep(100000);  // sleep 100ms wait async end
  } while (_async_call_num > 0);
Z
zhaocaibei123 已提交
550
  VLOG(1) << "flush _async_call_num = 0";
T
tangwei12 已提交
551 552
  promise.set_value(0);
  _flushing = false;
Z
zhaocaibei123 已提交
553
  VLOG(0) << "BrpcPsClient::flush done";
Z
zhaocaibei123 已提交
554
  PrintQueueSize();
T
tangwei12 已提交
555 556 557
  return fut;
}

Z
zhaocaibei123 已提交
558
void BrpcPsClient::PrintQueueSize() {
Z
zhaocaibei123 已提交
559 560 561
  for (auto &push_sparse_task_itr : _push_sparse_task_queue_map) {
    auto table_id = push_sparse_task_itr.first;
    auto queue_size = push_sparse_task_itr.second->Size();
Z
zhaocaibei123 已提交
562
    VLOG(0) << "BrpcPsClient::PrintQueueSize: table " << table_id
Z
zhaocaibei123 已提交
563 564 565 566 567 568
            << " size: " << queue_size;
  }

  for (auto &task_queue_itr : _push_dense_task_queue_map) {
    auto table_id = task_queue_itr.first;
    auto queue_size = task_queue_itr.second->Size();
Z
zhaocaibei123 已提交
569
    VLOG(0) << "BrpcPsClient::PrintQueueSize: table " << table_id
Z
zhaocaibei123 已提交
570 571 572 573
            << " size: " << queue_size;
  }
}

Z
zhaocaibei123 已提交
574
void BrpcPsClient::PrintQueueSizeThread() {
Z
zhaocaibei123 已提交
575 576
  while (_running) {
    usleep(1000000 * 60 * 2);
Z
zhaocaibei123 已提交
577
    PrintQueueSize();
Z
zhaocaibei123 已提交
578 579 580
  }
}

Z
zhaocaibei123 已提交
581 582 583
void BrpcPsClient::FinalizeWorker() {
  Flush();
  VLOG(0) << "BrpcPsClient::FinalizeWorker begin join thread";
T
tangwei12 已提交
584
  _running = false;
Z
zhaocaibei123 已提交
585 586 587
  _async_push_dense_thread.join();
  _async_push_sparse_thread.join();
  // _print_thread.join();
Z
zhaocaibei123 已提交
588
  VLOG(0) << "BrpcPsClient::FinalizeWorker begin join server";
T
tangwei12 已提交
589 590
  _server.Stop(1000);
  _server.Join();
Z
zhaocaibei123 已提交
591
  _server_started = false;
Z
zhaocaibei123 已提交
592
  VLOG(0) << "BrpcPsClient::FinalizeWorker done";
T
tangwei12 已提交
593 594
}

Z
zhaocaibei123 已提交
595 596
std::future<int32_t> BrpcPsClient::StopServer() {
  return SendCmd(-1, PS_STOP_SERVER, {});
T
tangwei12 已提交
597 598
}

Z
zhaocaibei123 已提交
599 600
std::future<int32_t> BrpcPsClient::StartProfiler() {
  return SendCmd(-1, PS_START_PROFILER, {});
T
tangwei12 已提交
601 602
}

Z
zhaocaibei123 已提交
603 604
std::future<int32_t> BrpcPsClient::StopProfiler() {
  return SendCmd(-1, PS_STOP_PROFILER, {});
T
tangwei12 已提交
605 606
}

Z
zhaocaibei123 已提交
607
std::future<int32_t> BrpcPsClient::Barrier(size_t table_id,
T
tangwei12 已提交
608
                                           uint32_t barrier_type) {
Z
zhaocaibei123 已提交
609
  return SendCmd(table_id, PS_BARRIER, {std::to_string(barrier_type)});
Y
yaoxuefeng 已提交
610 611
}

Z
zhaocaibei123 已提交
612 613 614 615 616
std::future<int32_t> BrpcPsClient::PullGeoParam(size_t table_id,
                                                std::vector<float> *values,
                                                std::vector<uint64_t> *keys,
                                                int pserver_idx) {
  auto *accessor = GetTableAccessor(table_id);
T
tangwei12 已提交
617 618 619
  DownpourBrpcClosure *closure =
      new DownpourBrpcClosure(1, [keys, values, accessor](void *done) {
        int ret = 0;
Z
zhaocaibei123 已提交
620
        auto *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
T
tangwei12 已提交
621 622 623 624 625 626
        uint32_t shard_nums;
        if (closure->check_response(0, PS_PULL_GEO_PARAM) != 0) {
          ret = -1;
        }
        auto &res_io_buffer = closure->cntl(0)->response_attachment();
        butil::IOBufBytesIterator io_buffer_itr(res_io_buffer);
Z
zhaocaibei123 已提交
627 628
        io_buffer_itr.copy_and_forward(reinterpret_cast<void *>(&shard_nums),
                                       sizeof(uint32_t));
T
tangwei12 已提交
629
        keys->resize(shard_nums);
630
        values->resize(shard_nums * accessor->GetAccessorInfo().update_dim);
Z
zhaocaibei123 已提交
631
        io_buffer_itr.copy_and_forward((void *)(keys->data()),  // NOLINT
T
tangwei12 已提交
632
                                       sizeof(uint64_t) * shard_nums);
633 634
        io_buffer_itr.copy_and_forward(
            (void *)(values->data()),  // NOLINT
635
            shard_nums * accessor->GetAccessorInfo().update_size);
T
tangwei12 已提交
636 637 638 639 640 641 642 643 644
        closure->set_promise_value(ret);
      });
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();

  closure->request(0)->set_cmd_id(PS_PULL_GEO_PARAM);
  closure->request(0)->set_table_id(table_id);
  closure->request(0)->set_client_id(_client_id);
Z
zhaocaibei123 已提交
645
  PsService_Stub rpc_stub(GetCmdChannel(pserver_idx));
T
tangwei12 已提交
646
  closure->cntl(0)->set_log_id(butil::gettimeofday_ms());
647 648
  rpc_stub.service(
      closure->cntl(0), closure->request(0), closure->response(0), closure);
T
tangwei12 已提交
649 650 651
  return fut;
}

Z
zhaocaibei123 已提交
652
// for GEO
Z
zhaocaibei123 已提交
653 654 655
std::future<int32_t> BrpcPsClient::PushSparseParam(size_t table_id,
                                                   const uint64_t *keys,
                                                   const float **update_values,
656 657
                                                   size_t num,
                                                   void *done) {
Z
zhaocaibei123 已提交
658
  auto *accessor = GetTableAccessor(table_id);
T
tangwei12 已提交
659 660 661 662 663 664 665 666 667 668
  // 发送RPC请求
  DownpourBrpcClosure *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();
  size_t request_call_num = _server_channels.size();
  std::vector<std::vector<uint64_t>> ids;
  std::vector<std::vector<const float *>> value_ptrs;
  ids.resize(request_call_num);
  value_ptrs.resize(request_call_num);
Z
zhaocaibei123 已提交
669

T
tangwei12 已提交
670
  for (size_t i = 0; i < num; ++i) {
Z
zhaocaibei123 已提交
671
    size_t pserver_idx = keys[i] % request_call_num;
T
tangwei12 已提交
672 673 674 675 676 677 678
    ids[pserver_idx].push_back(keys[i]);
    value_ptrs[pserver_idx].push_back(update_values[i]);
  }
  for (size_t shard_idx = 0; shard_idx < request_call_num; ++shard_idx) {
    auto kvs = ids[shard_idx];
    auto value_ptr = value_ptrs[shard_idx];
    size_t kv_size = kvs.size();
679
    uint32_t value_size = accessor->GetAccessorInfo().update_size;
T
tangwei12 已提交
680 681 682 683 684
    // 发送RPC请求
    auto *push_request = closure->request(shard_idx);
    push_request->set_cmd_id(PS_PUSH_SPARSE_PARAM);
    push_request->set_table_id(table_id);
    push_request->set_client_id(_client_id);
Z
zhaocaibei123 已提交
685
    push_request->add_params((char *)&kv_size, sizeof(uint32_t));  // NOLINT
T
tangwei12 已提交
686
    auto *push_data = push_request->mutable_data();
687
    push_data->resize(kv_size * (sizeof(uint64_t) + value_size));
T
tangwei12 已提交
688 689 690
    char *push_data_ptr = const_cast<char *>(push_data->data());
    memcpy(push_data_ptr, kvs.data(), kv_size * sizeof(uint64_t));
    push_data_ptr += kv_size * sizeof(uint64_t);
691
    for (size_t i = 0; i < kv_size; ++i) {
692 693
      memcpy(push_data_ptr, value_ptr[i], value_size);
      push_data_ptr += value_size;
T
tangwei12 已提交
694
    }
Z
zhaocaibei123 已提交
695
    PsService_Stub rpc_stub(GetSparseChannel(shard_idx));
T
tangwei12 已提交
696 697
    closure->cntl(shard_idx)->set_request_compress_type(
        (brpc::CompressType)FLAGS_pserver_communicate_compress_type);
698 699 700 701
    rpc_stub.service(closure->cntl(shard_idx),
                     closure->request(shard_idx),
                     closure->response(shard_idx),
                     closure);
T
tangwei12 已提交
702 703 704 705
  }
  return fut;
}

706 707
std::future<int32_t> BrpcPsClient::PullDense(Region *regions,
                                             size_t region_num,
Z
zhaocaibei123 已提交
708
                                             size_t table_id) {
709
  auto timer = std::make_shared<CostTimer>("pserver_client_pull_dense");
Z
zhaocaibei123 已提交
710
  auto *accessor = GetTableAccessor(table_id);
711
  auto fea_dim = accessor->GetAccessorInfo().fea_dim;
T
tangwei12 已提交
712
  size_t request_call_num = _server_channels.size();
713
  uint32_t num_per_shard = DenseDimPerShard(fea_dim, request_call_num);
T
tangwei12 已提交
714 715
  // callback 将各shard结果,顺序填入region
  DownpourBrpcClosure *closure = new DownpourBrpcClosure(
716 717 718
      request_call_num,
      [request_call_num, num_per_shard, regions, region_num, accessor](
          void *done) {
T
tangwei12 已提交
719 720 721
        int ret = 0;
        size_t region_idx = 0;       // 当前填充的region偏移
        size_t region_data_idx = 0;  // 当前填充的region内data偏移
Z
zhaocaibei123 已提交
722
        auto *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
723
        size_t shard_data_size =
724
            num_per_shard * accessor->GetAccessorInfo().select_size;
T
tangwei12 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
        for (size_t i = 0; i < request_call_num; ++i) {
          if (closure->check_response(i, PS_PULL_DENSE_TABLE) != 0) {
            ret = -1;
            break;
          }
          auto &res_io_buffer = closure->cntl(i)->response_attachment();

          butil::IOBufBytesIterator io_buffer_itr(res_io_buffer);
          size_t shard_buffer_remain = res_io_buffer.size();
          if (shard_buffer_remain != shard_data_size) {
            LOG(ERROR) << "expect res_size:" << shard_data_size
                       << ", but size:" << shard_buffer_remain
                       << ", ignore this response";
            ret = -1;
            break;
          }
          while (shard_buffer_remain > 0 && region_idx < region_num) {
            auto &region = regions[region_idx];
            if (region.size - region_data_idx >= shard_buffer_remain) {
              // region待填充空间 >= 分片buffer数据, 直接拷贝置入
              io_buffer_itr.copy_and_forward(
Z
zhaocaibei123 已提交
746 747
                  reinterpret_cast<void *>(region.data + region_data_idx),
                  shard_buffer_remain);
T
tangwei12 已提交
748 749 750 751 752 753 754 755 756
              region_data_idx += shard_buffer_remain;
              shard_buffer_remain = 0;
            } else if (region.size - region_data_idx == 0) {
              // region填满,切换到下一个region
              ++region_idx;
              region_data_idx = 0;
            } else {
              // region不足以容纳所有数据,则能放多少 拷贝多少
              io_buffer_itr.copy_and_forward(
Z
zhaocaibei123 已提交
757
                  reinterpret_cast<void *>(region.data + region_data_idx),
T
tangwei12 已提交
758 759 760 761 762 763 764 765 766
                  region.size - region_data_idx);
              shard_buffer_remain -= (region.size - region_data_idx);
              ++region_idx;
              region_data_idx = 0;
            }
          }
        }
        closure->set_promise_value(ret);
      });
767
  closure->add_timer(timer);
T
tangwei12 已提交
768 769 770 771 772 773 774
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();
  for (size_t i = 0; i < request_call_num; ++i) {
    closure->request(i)->set_cmd_id(PS_PULL_DENSE_TABLE);
    closure->request(i)->set_table_id(table_id);
    closure->request(i)->set_client_id(_client_id);
Z
zhaocaibei123 已提交
775
    closure->request(i)->add_params((char *)&num_per_shard,  // NOLINT
T
tangwei12 已提交
776
                                    sizeof(num_per_shard));
Z
zhaocaibei123 已提交
777
    PsService_Stub rpc_stub(GetDenseChannel(i));
778 779
    rpc_stub.service(
        closure->cntl(i), closure->request(i), closure->response(i), closure);
T
tangwei12 已提交
780 781 782 783
  }
  return fut;
}

Z
zhaocaibei123 已提交
784 785 786 787
std::future<int32_t> BrpcPsClient::PushDenseParam(const Region *regions,
                                                  size_t region_num,
                                                  size_t table_id) {
  auto *accessor = GetTableAccessor(table_id);
788
  auto accessor_info = accessor->GetAccessorInfo();
T
tangwei12 已提交
789 790 791 792
  size_t request_call_num = _server_channels.size();
  // 1.拆分Region数据到shard中,后续多shard并行拷贝数据
  std::vector<std::vector<Region>> regions_partition(request_call_num);
  uint32_t num_per_shard =
793 794
      DenseDimPerShard(accessor_info.fea_dim, request_call_num);
  size_t shard_data_size = num_per_shard * accessor_info.update_size;
T
tangwei12 已提交
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
  size_t current_region_idx = 0;
  size_t current_region_data_idx = 0;
  for (size_t i = 0; i < request_call_num; ++i) {
    size_t shard_data_remain_size = shard_data_size;
    while (shard_data_remain_size > 0 && current_region_idx < region_num) {
      const auto &region = regions[current_region_idx];
      size_t region_remain_size = region.size - current_region_data_idx;
      if (shard_data_remain_size >= region_remain_size) {
        regions_partition[i].push_back(
            Region(region.data + current_region_data_idx, region_remain_size));
        ++current_region_idx;
        current_region_data_idx = 0;
        shard_data_remain_size -= region_remain_size;
      } else {
        regions_partition[i].push_back(Region(
            region.data + current_region_data_idx, shard_data_remain_size));
        current_region_data_idx += shard_data_remain_size;
        shard_data_remain_size = 0;
      }
    }
  }

  DownpourBrpcClosure *closure =
      new DownpourBrpcClosure(request_call_num, [request_call_num](void *done) {
        int ret = 0;
Z
zhaocaibei123 已提交
820
        auto *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
T
tangwei12 已提交
821 822 823 824 825 826 827 828 829 830 831 832
        for (size_t i = 0; i < request_call_num; ++i) {
          if (closure->check_response(i, PS_PUSH_DENSE_PARAM) != 0) {
            ret = -1;
            break;
          }
        }
        closure->set_promise_value(ret);
      });
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();
  static const int REGION_ASSIGN_BUFFER_SIZE = 1024 * 10;
Z
zhaocaibei123 已提交
833 834
  static char region_assign_buffer[REGION_ASSIGN_BUFFER_SIZE];  // 用于数据补齐
  // 开始多shard并行拷贝&请求
T
tangwei12 已提交
835 836 837 838 839
  for (size_t i = 0; i < request_call_num; ++i) {
    closure->request(i)->set_cmd_id(PS_PUSH_DENSE_PARAM);
    closure->request(i)->set_table_id(table_id);
    closure->request(i)->set_client_id(_client_id);
    auto &request_buffer = closure->cntl(i)->request_attachment();
Z
zhaocaibei123 已提交
840 841
    request_buffer.append(reinterpret_cast<void *>(&num_per_shard),
                          sizeof(uint32_t));
T
tangwei12 已提交
842 843 844 845
    auto &region_list = regions_partition[i];
    size_t fill_remain_size = shard_data_size;
    for (auto &region : region_list) {
      fill_remain_size -= region.size;
Z
zhaocaibei123 已提交
846
      request_buffer.append(reinterpret_cast<void *>(region.data), region.size);
T
tangwei12 已提交
847
    }
Z
zhaocaibei123 已提交
848
    // 保证各分片数据对齐
T
tangwei12 已提交
849 850 851 852
    while (fill_remain_size > 0) {
      size_t fill_num = fill_remain_size > REGION_ASSIGN_BUFFER_SIZE
                            ? REGION_ASSIGN_BUFFER_SIZE
                            : fill_remain_size;
Z
zhaocaibei123 已提交
853 854
      request_buffer.append(reinterpret_cast<void *>(region_assign_buffer),
                            fill_num);
T
tangwei12 已提交
855 856
      fill_remain_size -= fill_num;
    }
Z
zhaocaibei123 已提交
857
    PsService_Stub rpc_stub(GetDenseChannel(i));
858 859
    rpc_stub.service(
        closure->cntl(i), closure->request(i), closure->response(i), closure);
T
tangwei12 已提交
860 861 862 863
  }
  return fut;
}

Z
zhaocaibei123 已提交
864
std::future<int32_t> BrpcPsClient::PushSparseRawGradient(
865 866 867 868 869
    size_t table_id,
    const uint64_t *keys,
    const float **update_values,
    size_t num,
    void *done) {
Z
zhaocaibei123 已提交
870
  auto *accessor = GetTableAccessor(table_id);
Z
zhaocaibei123 已提交
871
  // 发送RPC请求
T
tangwei12 已提交
872 873 874 875 876 877 878 879 880 881 882
  DownpourBrpcClosure *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();

  size_t request_call_num = _server_channels.size();
  std::vector<std::vector<uint64_t>> ids;
  std::vector<std::vector<const float *>> value_ptrs;
  ids.resize(request_call_num);
  value_ptrs.resize(request_call_num);

Z
zhaocaibei123 已提交
883 884 885 886 887 888 889 890 891 892
  const auto &server_param = _config.server_param().downpour_server_param();
  uint64_t shard_num = FLAGS_pserver_sparse_table_shard_num;
  for (int i = 0; i < server_param.downpour_table_param_size(); ++i) {
    const auto &table_param = server_param.downpour_table_param(i);
    if (table_param.table_id() == table_id) {
      shard_num = table_param.shard_num();
      break;
    }
  }

T
tangwei12 已提交
893
  for (size_t i = 0; i < num; ++i) {
Z
zhaocaibei123 已提交
894
    size_t pserver_idx = get_sparse_shard(shard_num, request_call_num, keys[i]);
T
tangwei12 已提交
895 896 897 898 899 900 901 902 903
    ids[pserver_idx].push_back(keys[i]);
    value_ptrs[pserver_idx].push_back(update_values[i]);
  }

  for (size_t shard_idx = 0; shard_idx < request_call_num; ++shard_idx) {
    auto kvs = ids[shard_idx];
    auto value_ptr = value_ptrs[shard_idx];

    size_t kv_size = kvs.size();
904
    uint32_t value_size = accessor->GetAccessorInfo().update_size;
T
tangwei12 已提交
905 906 907 908 909 910

    // 发送RPC请求
    auto *push_request = closure->request(shard_idx);
    push_request->set_cmd_id(PS_PUSH_SPARSE_TABLE);
    push_request->set_table_id(table_id);
    push_request->set_client_id(_client_id);
Z
zhaocaibei123 已提交
911
    push_request->add_params((char *)&kv_size, sizeof(uint32_t));  // NOLINT
T
tangwei12 已提交
912
    auto *push_data = push_request->mutable_data();
913
    push_data->resize(kv_size * (sizeof(uint64_t) + value_size));
T
tangwei12 已提交
914 915 916 917
    char *push_data_ptr = const_cast<char *>(push_data->data());
    memcpy(push_data_ptr, kvs.data(), kv_size * sizeof(uint64_t));
    push_data_ptr += kv_size * sizeof(uint64_t);

918
    for (size_t i = 0; i < kv_size; ++i) {
919 920
      memcpy(push_data_ptr, value_ptr[i], value_size);
      push_data_ptr += value_size;
T
tangwei12 已提交
921
    }
Z
zhaocaibei123 已提交
922
    PsService_Stub rpc_stub(GetSparseChannel(shard_idx));
T
tangwei12 已提交
923 924
    closure->cntl(shard_idx)->set_request_compress_type(
        (brpc::CompressType)FLAGS_pserver_communicate_compress_type);
925 926 927 928
    rpc_stub.service(closure->cntl(shard_idx),
                     closure->request(shard_idx),
                     closure->response(shard_idx),
                     closure);
T
tangwei12 已提交
929 930 931 932
  }
  return fut;
}

Z
zhaocaibei123 已提交
933
std::future<int32_t> BrpcPsClient::PushDenseRawGradient(
934 935 936
    int table_id,
    float *total_send_data,
    size_t total_send_data_size,
T
tangwei12 已提交
937 938 939 940 941 942
    void *done) {
  size_t request_call_num = _server_channels.size();
  DownpourBrpcClosure *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();
Z
zhaocaibei123 已提交
943
  auto *accessor = GetTableAccessor(table_id);
T
tangwei12 已提交
944
  uint32_t num_per_shard =
945
      DenseDimPerShard(accessor->GetAccessorInfo().fea_dim, request_call_num);
T
tangwei12 已提交
946 947 948 949 950 951 952 953 954 955
  for (size_t i = 0; i < request_call_num; ++i) {
    closure->request(i)->set_cmd_id(PS_PUSH_DENSE_TABLE);
    closure->request(i)->set_table_id(table_id);
    closure->request(i)->set_client_id(_client_id);
    auto *push_data = closure->request(i)->mutable_data();
    push_data->clear();
    push_data->resize(sizeof(uint32_t) + num_per_shard * sizeof(float));
    char *push_data_ptr = const_cast<char *>(push_data->data());
    memcpy(push_data_ptr, &num_per_shard, sizeof(uint32_t));
    memcpy(push_data_ptr + sizeof(uint32_t),
956 957
           total_send_data + i * num_per_shard,
           num_per_shard * sizeof(float));
T
tangwei12 已提交
958 959
    // closure->cntl(i)->set_request_compress_type(
    //     (brpc::CompressType)FLAGS_pserver_communicate_compress_type);
Z
zhaocaibei123 已提交
960
    PsService_Stub rpc_stub(GetDenseChannel(i));
961 962
    rpc_stub.service(
        closure->cntl(i), closure->request(i), closure->response(i), closure);
T
tangwei12 已提交
963 964 965 966
  }
  return fut;
}

Z
zhaocaibei123 已提交
967 968 969
std::future<int32_t> BrpcPsClient::PushGlobalStep(int table_id,
                                                  int64_t *total_send_data,
                                                  void *done) {
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
  size_t request_call_num = _server_channels.size();
  DownpourBrpcClosure *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();
  for (size_t i = 0; i < request_call_num; ++i) {
    closure->request(i)->set_cmd_id(PS_PUSH_GLOBAL_STEP);
    closure->request(i)->set_table_id(table_id);
    closure->request(i)->set_client_id(_client_id);
    auto *push_data = closure->request(i)->mutable_data();
    push_data->clear();
    int32_t num_per_shard = 1;
    push_data->resize(sizeof(uint32_t) + num_per_shard * sizeof(int64_t));
    char *push_data_ptr = const_cast<char *>(push_data->data());
    memcpy(push_data_ptr, &num_per_shard, sizeof(uint32_t));
985 986
    memcpy(push_data_ptr + sizeof(uint32_t),
           total_send_data,
987 988
           num_per_shard * sizeof(int64_t));

Z
zhaocaibei123 已提交
989
    PsService_Stub rpc_stub(GetDenseChannel(i));
990 991
    rpc_stub.service(
        closure->cntl(i), closure->request(i), closure->response(i), closure);
992 993 994 995
  }
  return fut;
}

Z
zhaocaibei123 已提交
996 997
std::future<int32_t> BrpcPsClient::PullSparse(float **select_values,
                                              size_t table_id,
998 999
                                              const uint64_t *keys,
                                              size_t num,
Z
zhaocaibei123 已提交
1000
                                              bool is_training) {
1001 1002 1003
  auto timer = std::make_shared<CostTimer>("pserver_client_pull_sparse");
  auto local_timer =
      std::make_shared<CostTimer>("pserver_client_pull_sparse_local");
T
tangwei12 已提交
1004 1005 1006 1007 1008 1009
  size_t request_call_num = _server_channels.size();

  auto shard_sorted_kvs = std::make_shared<
      std::vector<std::vector<std::pair<uint64_t, float *>>>>();
  shard_sorted_kvs->resize(request_call_num);

Z
zhaocaibei123 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
  const auto &server_param = _config.server_param().downpour_server_param();
  uint64_t shard_num = FLAGS_pserver_sparse_table_shard_num;
  for (int i = 0; i < server_param.downpour_table_param_size(); ++i) {
    const auto &table_param = server_param.downpour_table_param(i);
    if (table_param.table_id() == table_id) {
      shard_num = table_param.shard_num();
      break;
    }
  }

T
tangwei12 已提交
1020
  for (size_t i = 0; i < num; ++i) {
Z
zhaocaibei123 已提交
1021
    size_t shard_id = get_sparse_shard(shard_num, request_call_num, keys[i]);
T
tangwei12 已提交
1022 1023 1024
    shard_sorted_kvs->at(shard_id).push_back({keys[i], select_values[i]});
  }

Z
zhaocaibei123 已提交
1025
  auto *accessor = GetTableAccessor(table_id);
1026

1027
  size_t value_size = accessor->GetAccessorInfo().select_size;
T
tangwei12 已提交
1028 1029 1030 1031

  DownpourBrpcClosure *closure = new DownpourBrpcClosure(
      request_call_num, [shard_sorted_kvs, value_size](void *done) {
        int ret = 0;
Z
zhaocaibei123 已提交
1032
        auto *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
T
tangwei12 已提交
1033
        for (size_t i = 0; i < shard_sorted_kvs->size(); ++i) {
T
tangwei12 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
          if (closure->check_response(i, PS_PULL_SPARSE_TABLE) != 0) {
            ret = -1;
            break;
          }

          auto &request_kvs = shard_sorted_kvs->at(i);
          auto &res_io_buffer = closure->cntl(i)->response_attachment();
          butil::IOBufBytesIterator io_buffer_itr(res_io_buffer);
          uint64_t last_key = UINT64_MAX;
          float *last_value_data = NULL;

          for (size_t kv_idx = 0; kv_idx < request_kvs.size(); ++kv_idx) {
            auto *kv_pair = &(request_kvs[kv_idx]);
            if (kv_pair->first == last_key) {
Z
zhaocaibei123 已提交
1048
              memcpy(reinterpret_cast<void *>(kv_pair->second),
1049 1050
                     reinterpret_cast<void *>(last_value_data),
                     value_size);
T
tangwei12 已提交
1051 1052 1053 1054
            } else {
              last_key = kv_pair->first;
              last_value_data = kv_pair->second;
              if (value_size !=
Z
zhaocaibei123 已提交
1055 1056
                  io_buffer_itr.copy_and_forward(
                      reinterpret_cast<void *>(last_value_data), value_size)) {
T
tangwei12 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065
                LOG(WARNING) << "res data is lack or not in format";
                ret = -1;
                break;
              }
            }
          }
        }
        closure->set_promise_value(ret);
      });
1066
  closure->add_timer(timer);
T
tangwei12 已提交
1067 1068 1069 1070 1071 1072
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();

  for (size_t i = 0; i < request_call_num; ++i) {
    auto &sorted_kvs = shard_sorted_kvs->at(i);
1073 1074
    std::sort(sorted_kvs.begin(),
              sorted_kvs.end(),
T
tangwei12 已提交
1075 1076 1077 1078 1079 1080 1081 1082 1083
              [](const std::pair<uint64_t, float *> &k1,
                 const std::pair<uint64_t, float *> &k2) {
                return k1.first < k2.first;
              });

    uint64_t last_key = UINT64_MAX;
    uint32_t kv_request_count = 0;
    size_t sorted_kv_size = sorted_kvs.size();
    auto &request_buffer = closure->cntl(i)->request_attachment();
1084

Z
zhaocaibei123 已提交
1085
    request_buffer.append(reinterpret_cast<void *>(&is_training), sizeof(bool));
1086 1087 1088
    std::vector<uint32_t> keys_counter;
    keys_counter.reserve(sorted_kv_size);

T
tangwei12 已提交
1089 1090
    for (size_t kv_idx = 0; kv_idx < sorted_kv_size; ++kv_idx) {
      ++kv_request_count;
1091
      uint32_t keys = 1;
T
tangwei12 已提交
1092
      last_key = sorted_kvs[kv_idx].first;
Z
zhaocaibei123 已提交
1093 1094
      request_buffer.append(reinterpret_cast<void *>(&last_key),
                            sizeof(uint64_t));
T
tangwei12 已提交
1095 1096 1097
      while (kv_idx < sorted_kv_size - 1 &&
             last_key == sorted_kvs[kv_idx + 1].first) {
        ++kv_idx;
1098
        ++keys;
T
tangwei12 已提交
1099
      }
1100
      keys_counter.push_back(keys);
T
tangwei12 已提交
1101 1102
    }

Z
zhaocaibei123 已提交
1103
    request_buffer.append(reinterpret_cast<void *>(keys_counter.data()),
1104 1105
                          sizeof(uint32_t) * keys_counter.size());

T
tangwei12 已提交
1106 1107 1108 1109 1110 1111
    if (kv_request_count == 0) {
      closure->Run();
    } else {
      closure->request(i)->set_cmd_id(PS_PULL_SPARSE_TABLE);
      closure->request(i)->set_table_id(table_id);
      closure->request(i)->set_client_id(_client_id);
Z
zhaocaibei123 已提交
1112
      closure->request(i)->add_params((char *)&kv_request_count,  // NOLINT
T
tangwei12 已提交
1113
                                      sizeof(uint32_t));
Z
zhaocaibei123 已提交
1114
      PsService_Stub rpc_stub(GetCmdChannel(i));
T
tangwei12 已提交
1115
      closure->cntl(i)->set_log_id(butil::gettimeofday_ms());
1116 1117
      rpc_stub.service(
          closure->cntl(i), closure->request(i), closure->response(i), closure);
T
tangwei12 已提交
1118 1119 1120 1121 1122
    }
  }
  return fut;
}

Z
zhaocaibei123 已提交
1123
// for GEO
Z
zhaocaibei123 已提交
1124 1125 1126 1127 1128
std::future<int32_t> BrpcPsClient::PullSparseParam(float **select_values,
                                                   size_t table_id,
                                                   const uint64_t *keys,
                                                   size_t num,
                                                   bool is_training) {
Z
zhaocaibei123 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
  auto timer = std::make_shared<CostTimer>("pserver_client_pull_sparse_param");
  size_t request_call_num = _server_channels.size();

  auto shard_sorted_kvs = std::make_shared<
      std::vector<std::vector<std::pair<uint64_t, float *>>>>();
  shard_sorted_kvs->resize(request_call_num);

  for (size_t i = 0; i < num; ++i) {
    size_t shard_id = keys[i] % request_call_num;
    shard_sorted_kvs->at(shard_id).push_back({keys[i], select_values[i]});
  }

Z
zhaocaibei123 已提交
1141
  auto *accessor = GetTableAccessor(table_id);
1142
  size_t value_size = accessor->GetAccessorInfo().select_size;
Z
zhaocaibei123 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
  DownpourBrpcClosure *closure = new DownpourBrpcClosure(
      request_call_num, [shard_sorted_kvs, value_size](void *done) {
        int ret = 0;
        auto *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
        for (size_t i = 0; i < shard_sorted_kvs->size(); ++i) {
          if (closure->check_response(i, PS_PULL_SPARSE_TABLE) != 0) {
            ret = -1;
            break;
          }

          auto &request_kvs = shard_sorted_kvs->at(i);
          auto &res_io_buffer = closure->cntl(i)->response_attachment();
          butil::IOBufBytesIterator io_buffer_itr(res_io_buffer);
          uint64_t last_key = UINT64_MAX;
          float *last_value_data = NULL;

          // can remove sort&unique
          for (size_t kv_idx = 0; kv_idx < request_kvs.size(); ++kv_idx) {
            auto *kv_pair = &(request_kvs[kv_idx]);
            if (kv_pair->first == last_key) {
              memcpy(reinterpret_cast<void *>(kv_pair->second),
1164 1165
                     reinterpret_cast<void *>(last_value_data),
                     value_size);
Z
zhaocaibei123 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
            } else {
              last_key = kv_pair->first;
              last_value_data = kv_pair->second;
              if (value_size !=
                  io_buffer_itr.copy_and_forward(
                      reinterpret_cast<void *>(last_value_data), value_size)) {
                LOG(WARNING) << "res data is lack or not in format";
                ret = -1;
                break;
              }
            }
          }
        }
        closure->set_promise_value(ret);
      });
Z
zhaocaibei123 已提交
1181

Z
zhaocaibei123 已提交
1182 1183 1184 1185 1186 1187 1188
  closure->add_timer(timer);
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();

  for (size_t i = 0; i < request_call_num; ++i) {
    auto &sorted_kvs = shard_sorted_kvs->at(i);
1189 1190
    std::sort(sorted_kvs.begin(),
              sorted_kvs.end(),
Z
zhaocaibei123 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
              [](const std::pair<uint64_t, float *> &k1,
                 const std::pair<uint64_t, float *> &k2) {
                return k1.first < k2.first;
              });

    uint64_t last_key = UINT64_MAX;
    uint32_t kv_request_count = 0;
    size_t sorted_kv_size = sorted_kvs.size();
    auto &request_buffer = closure->cntl(i)->request_attachment();

    request_buffer.append(reinterpret_cast<void *>(&is_training), sizeof(bool));
    std::vector<uint32_t> keys_counter;
    keys_counter.reserve(sorted_kv_size);

    for (size_t kv_idx = 0; kv_idx < sorted_kv_size; ++kv_idx) {
      ++kv_request_count;
      uint32_t keys = 1;
      last_key = sorted_kvs[kv_idx].first;
      request_buffer.append(reinterpret_cast<void *>(&last_key),
                            sizeof(uint64_t));
      while (kv_idx < sorted_kv_size - 1 &&
             last_key == sorted_kvs[kv_idx + 1].first) {
        ++kv_idx;
        ++keys;
      }
      keys_counter.push_back(keys);
    }

    request_buffer.append(reinterpret_cast<void *>(keys_counter.data()),
                          sizeof(uint32_t) * keys_counter.size());

    if (kv_request_count == 0) {
      closure->Run();
    } else {
      closure->request(i)->set_cmd_id(PS_PULL_SPARSE_TABLE);
      closure->request(i)->set_table_id(table_id);
      closure->request(i)->set_client_id(_client_id);
      closure->request(i)->add_params((char *)&kv_request_count,  // NOLINT
                                      sizeof(uint32_t));
Z
zhaocaibei123 已提交
1230
      PsService_Stub rpc_stub(GetCmdChannel(i));
Z
zhaocaibei123 已提交
1231
      closure->cntl(i)->set_log_id(butil::gettimeofday_ms());
1232 1233
      rpc_stub.service(
          closure->cntl(i), closure->request(i), closure->response(i), closure);
Z
zhaocaibei123 已提交
1234 1235 1236 1237 1238
    }
  }
  return fut;
}

Z
zhaocaibei123 已提交
1239
std::future<int32_t> BrpcPsClient::SendClient2ClientMsg(
T
tangwei12 已提交
1240 1241 1242
    int msg_type, int to_client_id, const std::string &msg) {
  auto promise = std::make_shared<std::promise<int32_t>>();
  std::future<int> fut = promise->get_future();
Z
zhangchunle 已提交
1243 1244
  if (to_client_id >= 0 &&
      static_cast<size_t>(to_client_id) >= _client_channels.size()) {
T
Thunderbrook 已提交
1245 1246
    VLOG(0) << "to_client_id is out of range clients, which size is "
            << _client_channels.size();
T
tangwei12 已提交
1247 1248 1249 1250
    promise->set_value(-1);
    return fut;
  }
  auto *closure = new DownpourBrpcClosure(1, [msg_type](void *done) {
Z
zhaocaibei123 已提交
1251
    auto *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
T
tangwei12 已提交
1252 1253 1254 1255 1256 1257 1258 1259
    int32_t ret = closure->check_response(0, msg_type + 1000);
    closure->set_promise_value(ret);
  });
  closure->add_promise(promise);
  closure->request(0)->set_cmd_id(msg_type);
  closure->request(0)->set_client_id(_client_id);
  closure->request(0)->set_data(msg);
  PsService_Stub rpc_stub(_client_channels[to_client_id].get());
1260 1261
  rpc_stub.service(
      closure->cntl(0), closure->request(0), closure->response(0), closure);
T
tangwei12 已提交
1262 1263 1264
  return fut;
}

Z
zhaocaibei123 已提交
1265
std::future<int32_t> BrpcPsClient::PushSparseRawGradientPartial(
1266 1267 1268 1269 1270 1271
    size_t table_id,
    const uint64_t *keys,
    const float **update_values,
    uint32_t num,
    void *done,
    int pserver_idx) {
Z
zhaocaibei123 已提交
1272
  auto *accessor = GetTableAccessor(table_id);
1273
  size_t value_size = accessor->GetAccessorInfo().update_size;
T
tangwei12 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
  DownpourBrpcClosure *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
  auto promise = std::make_shared<std::promise<int32_t>>();
  closure->add_promise(promise);
  std::future<int> fut = promise->get_future();

  // 发送RPC请求
  auto *push_request = closure->request(0);
  push_request->set_cmd_id(PS_PUSH_SPARSE_TABLE);
  push_request->set_table_id(table_id);
  push_request->set_client_id(_client_id);
Z
zhaocaibei123 已提交
1284
  push_request->add_params((char *)&num, sizeof(uint32_t));  // NOLINT
T
tangwei12 已提交
1285 1286 1287 1288 1289
  auto *push_data = push_request->mutable_data();
  push_data->resize(num * (sizeof(uint64_t) + value_size));
  char *push_data_ptr = const_cast<char *>(push_data->data());
  memcpy(push_data_ptr, keys, num * sizeof(uint64_t));
  push_data_ptr += num * sizeof(uint64_t);
1290
  for (uint32_t i = 0; i < num; ++i) {
T
tangwei12 已提交
1291 1292 1293
    memcpy(push_data_ptr, update_values[i], value_size);
    push_data_ptr += value_size;
  }
Z
zhaocaibei123 已提交
1294
  PsService_Stub rpc_stub(GetSparseChannel(pserver_idx));
T
tangwei12 已提交
1295 1296
  closure->cntl(0)->set_request_compress_type(
      (brpc::CompressType)FLAGS_pserver_communicate_compress_type);
1297 1298
  rpc_stub.service(
      closure->cntl(0), closure->request(0), closure->response(0), closure);
T
tangwei12 已提交
1299 1300 1301
  return fut;
}

Z
zhaocaibei123 已提交
1302 1303
int32_t BrpcPsClient::RecvAndSaveTable(const uint64_t table_id,
                                       const std::string &path) {
1304 1305 1306 1307
  // get var information
  std::string var_name = "";
  int64_t var_num = 0;
  int64_t var_shape = 0;
Z
zhaocaibei123 已提交
1308
  std::string table_class;
1309
  const auto &worker_param = _config.worker_param().downpour_worker_param();
1310
  for (int i = 0; i < worker_param.downpour_table_param_size(); ++i) {
1311 1312
    if (worker_param.downpour_table_param(i).table_id() == table_id) {
      var_name = worker_param.downpour_table_param(i).common().table_name();
1313 1314
      var_num = worker_param.downpour_table_param(i).common().table_num();
      var_shape = worker_param.downpour_table_param(i).common().table_dim();
Z
zhaocaibei123 已提交
1315
      table_class = worker_param.downpour_table_param(i).table_class();
1316 1317 1318 1319 1320
      break;
    }
  }

  PADDLE_ENFORCE_NE(
1321 1322
      var_name,
      "",
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
      platform::errors::InvalidArgument(
          "Cannot find table id %d to save variables.", table_id));

  std::string var_store = string::Sprintf("%s", path);
  MkDirRecursively(var_store.c_str());

  // pull sparse from server
  std::vector<float> save_huge_vec(var_num * var_shape);
  std::vector<uint64_t> save_key(var_num);
  std::vector<float *> save_vec;
  for (size_t i = 0; i < save_key.size(); ++i) {
    save_key[i] = i;
    save_vec.push_back(save_huge_vec.data() + i * var_shape);
  }

Z
zhaocaibei123 已提交
1338
  VLOG(2) << "RecvAndSaveTable: table_class: " << table_class;
Z
zhaocaibei123 已提交
1339
  // TODO(zhaocaibei123): new GeoBrpcPSClient, move this to its
Z
zhaocaibei123 已提交
1340
  // RecvAndSaveTable
Z
zhaocaibei123 已提交
1341
  if (table_class == "MemorySparseGeoTable") {
1342 1343 1344 1345 1346
    auto status = PullSparseParam(reinterpret_cast<float **>(save_vec.data()),
                                  table_id,
                                  save_key.data(),
                                  save_key.size(),
                                  true);
Z
zhaocaibei123 已提交
1347 1348
    status.wait();
  } else {
Z
zhaocaibei123 已提交
1349
    auto status = PullSparse(reinterpret_cast<float **>(save_vec.data()),
1350 1351 1352 1353
                             table_id,
                             save_key.data(),
                             save_key.size(),
                             true);
Z
zhaocaibei123 已提交
1354 1355
    status.wait();
  }
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367

  // create lod tensor
  std::shared_ptr<framework::Scope> scope;
  scope.reset(new framework::Scope());
  auto place = platform::CPUPlace();
  platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
  auto &dev_ctx = *pool.Get(place);

  framework::Variable *var = scope->Var(var_name);
  framework::LoDTensor *var_tensor = var->GetMutable<framework::LoDTensor>();

  std::vector<int64_t> vec_dim = {var_num, var_shape};
1368
  var_tensor->Resize(phi::make_ddim(vec_dim));
1369 1370 1371

  // copy and save
  float *tensor_data = var_tensor->mutable_data<float>(place);
1372 1373
  memcpy(
      tensor_data, save_huge_vec.data(), var_num * var_shape * sizeof(float));
1374 1375 1376

  std::string file_name = string::Sprintf("%s/%s", var_store, var_name);
  std::ofstream fout(file_name, std::ios::binary);
1377 1378
  PADDLE_ENFORCE_EQ(static_cast<bool>(fout),
                    true,
1379 1380 1381 1382 1383 1384 1385 1386 1387
                    platform::errors::Unavailable(
                        "Cannot open %s to save variables.", file_name));

  framework::SerializeToStream(fout, *var_tensor, dev_ctx);
  fout.close();

  return 0;
}

Z
zhaocaibei123 已提交
1388 1389 1390 1391
std::future<int32_t> BrpcPsClient::PushSparse(size_t table_id,
                                              const uint64_t *keys,
                                              const float **update_values,
                                              size_t num) {
1392 1393
  auto push_timer = std::make_shared<CostTimer>("pserver_client_push_sparse");
  CostTimer parse_timer("pserver_client_push_sparse_parse");
Z
zhaocaibei123 已提交
1394 1395
  int push_sparse_async_num = _push_sparse_task_queue_map[table_id]->Size();
  while (push_sparse_async_num > FLAGS_pserver_max_async_call_num) {
Z
zhaocaibei123 已提交
1396
    //    LOG(INFO) << "PushSparse Waiting for async_call_num comsume,
1397 1398 1399
    //    task_num:"
    //              << push_sparse_async_num
    //              << ", max_task_limit:" << FLAGS_pserver_max_async_call_num;
Z
zhaocaibei123 已提交
1400 1401 1402
    usleep(5000);  // 5ms
    push_sparse_async_num = _push_sparse_task_queue_map[table_id]->Size();
  }
1403
  auto put_timer = std::make_shared<CostTimer>("client_push_sparse_put");
Z
zhaocaibei123 已提交
1404 1405
  thread_local std::vector<std::vector<std::pair<uint64_t, const float *>>>
      shard_sorted_kv_list;
Z
zhaocaibei123 已提交
1406
  auto *accessor = GetTableAccessor(table_id);
Z
zhaocaibei123 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
  size_t request_call_num = _server_channels.size();
  shard_sorted_kv_list.resize(request_call_num);
  for (auto &x : shard_sorted_kv_list) {
    x.clear();
  }
  const auto &server_param = _config.server_param().downpour_server_param();
  uint64_t shard_num = FLAGS_pserver_sparse_table_shard_num;
  for (int i = 0; i < server_param.downpour_table_param_size(); ++i) {
    const auto &table_param = server_param.downpour_table_param(i);
    if (table_param.table_id() == table_id) {
      shard_num = table_param.shard_num();
      break;
    }
  }
  for (size_t i = 0; i < num; ++i) {
    size_t shard_id = get_sparse_shard(shard_num, request_call_num, keys[i]);
    shard_sorted_kv_list[shard_id].push_back({keys[i], update_values[i]});
  }
  auto sparse_task_data = _sparse_task_pool.get();
  sparse_task_data->shared_data.resize(request_call_num);
  auto async_task = new SparseAsyncTask(sparse_task_data, table_id, push_timer);

  for (size_t i = 0; i < request_call_num; ++i) {
    auto &sorted_kv_list = shard_sorted_kv_list[i];
    size_t sorted_kv_size = sorted_kv_list.size();
    auto &shard_kv_data = async_task->data()->shared_data[i];
    shard_kv_data.key_list.resize(sorted_kv_size);
    shard_kv_data.value_list.resize(sorted_kv_size);

    if (sorted_kv_size == 0) {
      shard_kv_data.kv_num = 0;
      continue;
    }
1440
    uint32_t value_size = accessor->GetAccessorInfo().update_size;
Z
zhaocaibei123 已提交
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
    for (size_t kv_idx = 0; kv_idx < sorted_kv_size; ++kv_idx) {
      shard_kv_data.key_list[kv_idx] = sorted_kv_list[kv_idx].first;
      shard_kv_data.value_list[kv_idx].assign(
          (const char *)sorted_kv_list[kv_idx].second, value_size);
    }
    shard_kv_data.kv_num = sorted_kv_size;
  }

  std::future<int> fut = async_task->get_future();
  _push_sparse_task_queue_map[table_id]->Put(std::move(async_task));
  return fut;
}

Z
zhaocaibei123 已提交
1454
void BrpcPsClient::PushSparseTaskConsume() {
Z
zhaocaibei123 已提交
1455 1456 1457 1458 1459 1460
  uint64_t merge_size = FLAGS_pserver_push_sparse_merge_limit;
  std::vector<std::shared_ptr<SparseAsyncTask>> task_list;
  size_t request_call_num = _server_channels.size();
  ::ThreadPool async_push_sparse_shard_threads(
      FLAGS_pserver_sparse_merge_thread);
  while (_running) {
1461
    auto async_start_time_ms = butil::gettimeofday_ms();
Z
zhaocaibei123 已提交
1462 1463 1464
    // 所有sparseTable的pushTask 进行处理
    for (auto &push_sparse_task_itr : _push_sparse_task_queue_map) {
      auto table_id = push_sparse_task_itr.first;
Z
zhaocaibei123 已提交
1465
      auto *accessor = GetTableAccessor(table_id);
Z
zhaocaibei123 已提交
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
      auto &task_queue = push_sparse_task_itr.second;
      auto queue_size = task_queue->Size();
      if (queue_size == 0) {
        continue;
      }
      if (merge_size > 0 && (queue_size <= 1 && _flushing == false)) {
        continue;
      }
      ++_async_call_num;

      int merge_count = 0;
      for (size_t i = 0; i < task_list.size(); ++i) {
        if (task_list[i]->data()) {
          _sparse_task_pool.push(task_list[i]->data());
        }
      }
      auto sparse_task_data = _sparse_task_pool.get();

      task_list.clear();
      int cur_meger_size = task_queue->Size();

      // task_list[0] 为一个空SparseAsyncTask, 分shard异步merge结果存入此结构。
      sparse_task_data->shared_data.resize(request_call_num);
      auto push_timer =
          std::make_shared<CostTimer>("pserver_client_push_sparse");

      auto async_task =
          new SparseAsyncTask(sparse_task_data, table_id, push_timer);

      task_list.reserve(cur_meger_size + 1);

      task_list.push_back(
          std::move(std::shared_ptr<SparseAsyncTask>(async_task)));

      while (!task_queue->Empty() && merge_count < cur_meger_size) {
        ++merge_count;
        SparseAsyncTask *task;
        task_queue->Get(task);
        task_list.push_back(std::shared_ptr<SparseAsyncTask>(task));
      }

      _push_sparse_merge_count_map[table_id] += merge_count;

      // 达到或大于 merge_size发送, 发送过程中
      std::vector<int> request_kv_num(request_call_num, 0);

      if (_push_sparse_merge_count_map[table_id] >= merge_size ||
          _flushing == true) {
        DownpourBrpcClosure *closure = new DownpourBrpcClosure(
            request_call_num, [this, request_call_num](void *done) {
              int ret = 0;
              auto *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
              for (size_t i = 0; i < request_call_num; ++i) {
                if (closure->check_response(i, PS_PUSH_SPARSE_TABLE) != 0) {
                  ret = -1;
                  break;
                }
              }
              closure->set_promise_value(ret);
              --_async_call_num;
            });

1528 1529 1530 1531
        for_each(task_list.begin() + 1,
                 task_list.end(),
                 [&request_kv_num, request_call_num, closure](
                     std::shared_ptr<SparseAsyncTask> &task) {
1532
                   closure->add_timer(task->timer());
Z
zhaocaibei123 已提交
1533 1534 1535
                   closure->add_promise(task->promise());
                 });

1536 1537 1538 1539
        CostTimer merge_timer("pserver_client_push_sparse_merge");
        auto rpc_timer =
            std::make_shared<CostTimer>("pserver_client_push_sparse_rpc");
        closure->add_timer(rpc_timer);
Z
zhaocaibei123 已提交
1540 1541

        std::vector<std::future<int>> merge_status(request_call_num);
1542
        for (size_t shard_idx = 0; shard_idx < request_call_num; ++shard_idx) {
1543 1544 1545 1546 1547 1548 1549 1550 1551
          merge_status[shard_idx] = async_push_sparse_shard_threads.enqueue(
              std::bind(&BrpcPsClient::PushSparseAsyncShardPush,
                        this,
                        task_list,
                        request_kv_num,
                        table_id,
                        shard_idx,
                        closure,
                        accessor));
Z
zhaocaibei123 已提交
1552
        }
1553
        for (size_t shard_idx = 0; shard_idx < request_call_num; ++shard_idx) {
Z
zhaocaibei123 已提交
1554 1555 1556 1557 1558 1559 1560
          merge_status[shard_idx].wait();
        }
        merge_status.clear();
        std::vector<std::future<int>>().swap(merge_status);
        _push_sparse_merge_count_map[table_id] = 0;
      } else {  // 未达到阈值 只做多路归并
        std::vector<std::future<int>> merge_status(request_call_num);
1561
        for (size_t shard_idx = 0; shard_idx < request_call_num; ++shard_idx) {
1562 1563 1564 1565 1566 1567 1568 1569
          merge_status[shard_idx] = async_push_sparse_shard_threads.enqueue(
              std::bind(&BrpcPsClient::PushSparseAsyncShardMerge,
                        this,
                        task_list,
                        request_kv_num,
                        table_id,
                        shard_idx,
                        accessor));
Z
zhaocaibei123 已提交
1570
        }
1571
        for (size_t shard_idx = 0; shard_idx < request_call_num; ++shard_idx) {
Z
zhaocaibei123 已提交
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
          merge_status[shard_idx].wait();
        }

        // meger到task_list[0]
        auto async_task = new SparseAsyncTask(*(task_list[0].get()));

        task_queue->Put(std::move(async_task));
        --_async_call_num;
        merge_status.clear();
        std::vector<std::future<int>>().swap(merge_status);
      }
    }
1584 1585
    auto wait_ms = FLAGS_pserver_async_push_sparse_interval_ms -
                   (butil::gettimeofday_ms() - async_start_time_ms);
Z
zhaocaibei123 已提交
1586 1587 1588 1589 1590 1591
    if (wait_ms > 0) {
      usleep(wait_ms * 1000);
    }
  }
}

1592 1593
void sparse_local_merge(ValueAccessor *accessor,
                        float *merge_data,
Z
zhaocaibei123 已提交
1594
                        const float *another_data) {
1595
  size_t col_num = accessor->GetAccessorInfo().update_dim;
Z
zhaocaibei123 已提交
1596 1597
  float *merge_data_shell[col_num];
  const float *another_data_shell[col_num];
1598
  for (size_t i = 0; i < col_num; ++i) {
Z
zhaocaibei123 已提交
1599 1600 1601
    merge_data_shell[i] = merge_data + i;
    another_data_shell[i] = another_data + i;
  }
1602
  accessor->Merge(merge_data_shell, another_data_shell, 1);
Z
zhaocaibei123 已提交
1603 1604
}

Z
zhaocaibei123 已提交
1605
int BrpcPsClient::PushSparseAsyncShardMerge(
Z
zhaocaibei123 已提交
1606
    std::vector<std::shared_ptr<SparseAsyncTask>> &task_list,
1607 1608 1609
    std::vector<int> &request_kv_num,
    int table_id,
    int shard_idx,
Z
zhaocaibei123 已提交
1610 1611
    ValueAccessor *accessor) {
  size_t merged_kv_count = 0;
1612
  uint32_t value_size = accessor->GetAccessorInfo().update_size;
Z
zhaocaibei123 已提交
1613 1614 1615

  thread_local std::vector<std::pair<uint64_t, const float *>> sorted_kv_list;
  sorted_kv_list.clear();
1616
  for (size_t i = 1; i < task_list.size(); ++i) {
Z
zhaocaibei123 已提交
1617 1618 1619 1620
    size_t kv_num = task_list[i]->data()->shared_data[shard_idx].kv_num;
    auto &key_list = task_list[i]->data()->shared_data[shard_idx].key_list;
    auto &value_list = task_list[i]->data()->shared_data[shard_idx].value_list;

1621
    for (size_t j = 0; j < kv_num; ++j) {
Z
zhaocaibei123 已提交
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
      if (value_list[j].size() < value_size) {
        LOG(WARNING) << "value_list[" << j << "]: " << value_list[j].c_str()
                     << "is invalid.";
        continue;
      }
      char *task_data_ptr = const_cast<char *>(value_list[j].data());
      sorted_kv_list.push_back(
          {key_list[j], reinterpret_cast<float *>(task_data_ptr)});
    }
  }

  // 按key排序&去重
1634 1635
  std::sort(sorted_kv_list.begin(),
            sorted_kv_list.end(),
Z
zhaocaibei123 已提交
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
            [](const std::pair<uint64_t, const float *> &k1,
               const std::pair<uint64_t, const float *> &k2) {
              return k1.first < k2.first;
            });

  auto &async_task = task_list[0];
  size_t sorted_kv_size = sorted_kv_list.size();
  auto &shard_kv_data = async_task->data()->shared_data[shard_idx];
  shard_kv_data.key_list.resize(sorted_kv_size);
  shard_kv_data.value_list.resize(sorted_kv_size);

  // 将去重后数据写入分shard包
  if (sorted_kv_size == 0) {
    shard_kv_data.kv_num = 0;
    return 0;
  } else if (sorted_kv_size == 1) {
    shard_kv_data.kv_num = 1;
    shard_kv_data.key_list[0] = sorted_kv_list[0].first;
    shard_kv_data.value_list[0].assign((const char *)(sorted_kv_list[0].second),
                                       value_size);
    return 0;
  }

  // 去重 本地merge
  uint64_t last_key = sorted_kv_list[0].first;
  const float *last_value_data = sorted_kv_list[0].second;
  float *last_merge_data = NULL;
  std::shared_ptr<char> merger_buffer(new char[value_size],
                                      array_deleter<char>());
  for (size_t kv_idx = 1; kv_idx < sorted_kv_size; ++kv_idx) {
    while (kv_idx < sorted_kv_size &&
           last_key == sorted_kv_list[kv_idx].first) {
      if (last_merge_data == NULL) {
        last_merge_data = reinterpret_cast<float *>(merger_buffer.get());
        memcpy(last_merge_data, last_value_data, value_size);
      }
1672 1673
      sparse_local_merge(
          accessor, last_merge_data, sorted_kv_list[kv_idx].second);
Z
zhaocaibei123 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
      ++kv_idx;
    }
    if (last_merge_data != NULL) {
      shard_kv_data.value_list[merged_kv_count].assign(
          (const char *)last_merge_data, value_size);
      last_merge_data = NULL;
    } else {
      shard_kv_data.value_list[merged_kv_count].assign(
          (const char *)sorted_kv_list[kv_idx - 1].second, value_size);
    }
    shard_kv_data.key_list[merged_kv_count++] = last_key;
    if (kv_idx < sorted_kv_size) {
      last_key = sorted_kv_list[kv_idx].first;
      last_value_data = sorted_kv_list[kv_idx].second;
    }
    if (kv_idx == sorted_kv_size - 1) {
      shard_kv_data.value_list[merged_kv_count].assign(
          (const char *)last_value_data, value_size);
      shard_kv_data.key_list[merged_kv_count++] = last_key;
    }
  }
  shard_kv_data.kv_num = merged_kv_count;
  return 0;
}

Z
zhaocaibei123 已提交
1699
int BrpcPsClient::PushSparseAsyncShardPush(
Z
zhaocaibei123 已提交
1700
    std::vector<std::shared_ptr<SparseAsyncTask>> &task_list,
1701 1702 1703 1704 1705 1706 1707
    std::vector<int> &request_kv_num,
    int table_id,
    int shard_idx,
    DownpourBrpcClosure *closure,
    ValueAccessor *accessor) {
  PushSparseAsyncShardMerge(
      task_list, request_kv_num, table_id, shard_idx, accessor);
Z
zhaocaibei123 已提交
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
  size_t merged_kv_count = task_list[0]->data()->shared_data[shard_idx].kv_num;

  auto &merged_key_list = task_list[0]->data()->shared_data[shard_idx].key_list;
  auto &merged_value_list =
      task_list[0]->data()->shared_data[shard_idx].value_list;

  // 发送RPC请求
  auto *push_request = closure->request(shard_idx);
  push_request->set_cmd_id(PS_PUSH_SPARSE_TABLE);
  push_request->set_table_id(table_id);
  push_request->set_client_id(_client_id);
  push_request->add_params(reinterpret_cast<char *>(&merged_kv_count),
                           sizeof(uint32_t));  // NOLINT
  auto *push_data = push_request->mutable_data();
1722 1723
  int update_size = accessor->GetAccessorInfo().update_size;
  push_data->resize(merged_kv_count * (sizeof(uint64_t) + update_size));
Z
zhaocaibei123 已提交
1724
  char *push_data_ptr = const_cast<char *>(push_data->data());
1725 1726
  memcpy(push_data_ptr,
         merged_key_list.data(),
Z
zhaocaibei123 已提交
1727 1728
         merged_kv_count * sizeof(uint64_t));
  push_data_ptr += merged_kv_count * sizeof(uint64_t);
1729
  for (size_t i = 0; i < merged_kv_count; ++i) {
Z
zhaocaibei123 已提交
1730 1731
    const char *task_data_ptr = merged_value_list[i].data();

1732 1733
    memcpy(push_data_ptr,
           (float *)(task_data_ptr),  // NOLINT
1734 1735
           update_size);
    push_data_ptr += update_size;
Z
zhaocaibei123 已提交
1736
  }
Z
zhaocaibei123 已提交
1737
  PsService_Stub rpc_stub(GetSparseChannel(shard_idx));
Z
zhaocaibei123 已提交
1738 1739
  closure->cntl(shard_idx)->set_request_compress_type(
      (brpc::CompressType)FLAGS_pserver_communicate_compress_type);
1740 1741 1742 1743
  rpc_stub.service(closure->cntl(shard_idx),
                   closure->request(shard_idx),
                   closure->response(shard_idx),
                   closure);
Z
zhaocaibei123 已提交
1744 1745 1746 1747
  _push_sparse_merge_count_map[table_id] = 0;
  return 0;
}

Z
zhaocaibei123 已提交
1748 1749 1750 1751
std::future<int32_t> BrpcPsClient::PushDense(const Region *regions,
                                             size_t region_num,
                                             size_t table_id) {
  auto *accessor = GetTableAccessor(table_id);
1752 1753
  int fea_dim = accessor->GetAccessorInfo().fea_dim;
  int update_dim = accessor->GetAccessorInfo().update_dim;
Z
zhaocaibei123 已提交
1754 1755 1756 1757 1758
  auto push_timer = std::make_shared<CostTimer>("pserver_client_push_dense");
  auto parse_timer =
      std::make_shared<CostTimer>("pserver_client_push_dense_parse");
  int push_dense_async_num = _push_dense_task_queue_map[table_id]->Size();
  while (push_dense_async_num > FLAGS_pserver_max_async_call_num) {
Z
zhaocaibei123 已提交
1759
    //    LOG(INFO) << "PushDense Waiting for async_call_num comsume,
1760 1761 1762
    //    task_num:"
    //              << push_dense_async_num
    //              << ", max_task_limit:" << FLAGS_pserver_max_async_call_num;
Z
zhaocaibei123 已提交
1763 1764 1765
    usleep(5000);  // 5ms
    push_dense_async_num = _push_dense_task_queue_map[table_id]->Size();
  }
1766
  auto push_dense_timer = std::make_shared<CostTimer>("push_dense_put");
Z
zhaocaibei123 已提交
1767 1768 1769 1770
  // auto dense_data = _dense_matrix_obj_pool.get();
  auto dense_data = std::make_shared<std::vector<float>>();
  auto async_task = new DenseAsyncTask(dense_data, table_id, push_timer);
  size_t request_call_num = _server_channels.size();
1771
  uint32_t num_per_shard = DenseDimPerShard(fea_dim, request_call_num);
Z
zhaocaibei123 已提交
1772
  // 将region数据拷贝到转置矩阵中
1773
  async_task->data()->resize(num_per_shard * request_call_num * update_dim);
Z
zhaocaibei123 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
  float *data = async_task->data()->data();
  size_t data_size = async_task->data()->size();
  uint32_t pos = 0;
  for (size_t i = 0; i < region_num; ++i) {
    uint32_t data_num = regions[i].size / sizeof(float);
    CHECK(pos + data_num <= data_size)
        << "invalid dense size, cur pos[" << pos << "]"
        << " data_num[" << data_num << "] size[" << data_size << "]";
    const float *region_data = (const float *)(regions[i].data);
    memcpy(data + pos, region_data, regions[i].size);
    pos += data_num;
  }
  std::future<int> fut = async_task->get_future();
  _push_dense_task_queue_map[table_id]->Put(std::move(async_task));
  return fut;
}

Z
zhaocaibei123 已提交
1791
void BrpcPsClient::PushDenseTaskConsume() {
Z
zhaocaibei123 已提交
1792 1793 1794 1795
  uint64_t merge_size = FLAGS_pserver_push_dense_merge_limit;
  static bool scale_gradient = FLAGS_pserver_scale_gradient_by_merge;
  ::ThreadPool async_merge_dense_threads(10);
  while (_running) {
1796
    auto async_start_time_ms = butil::gettimeofday_ms();
Z
zhaocaibei123 已提交
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
    for (auto &task_queue_itr : _push_dense_task_queue_map) {
      auto &task_queue = task_queue_itr.second;
      auto queue_size = task_queue->Size();
      if (queue_size == 0) {
        continue;
      }
      if (queue_size <= merge_size && _flushing == false) {
        continue;
      }
      ++_async_call_num;
      DenseAsyncTask *task;
      task_queue->Get(task);
Z
zhaocaibei123 已提交
1809
      auto *accessor = GetTableAccessor(task->table_id());
Z
zhaocaibei123 已提交
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
      // 设置请求回调
      size_t request_call_num = _server_channels.size();

      DownpourBrpcClosure *closure = new DownpourBrpcClosure(
          request_call_num, [this, request_call_num](void *done) {
            int ret = 0;
            auto *closure = reinterpret_cast<DownpourBrpcClosure *>(done);
            for (size_t i = 0; i < request_call_num; ++i) {
              if (closure->check_response(i, PS_PUSH_DENSE_TABLE) != 0) {
                ret = -1;
                break;
              }
            }
            closure->set_promise_value(ret);
            --_async_call_num;
          });

      auto &total_send_data_vec = *(task->data());
      float *total_send_data =
          reinterpret_cast<float *>(total_send_data_vec.data());
      size_t total_send_data_size = total_send_data_vec.size();
      {
        CostTimer merge_timer("pserver_client_push_dense_merge");
        uint32_t merge_count = 0;
        std::vector<std::future<int>> merge_status(merge_size);
        while (!task_queue->Empty() && merge_count < merge_size) {
          auto *async_task = new DenseAsyncTask();
          task_queue->Get(async_task);
          closure->add_timer(async_task->timer());
          closure->add_promise(async_task->promise());
1840 1841 1842 1843 1844 1845
          merge_status[merge_count] =
              async_merge_dense_threads.enqueue([closure,
                                                 accessor,
                                                 &total_send_data,
                                                 total_send_data_size,
                                                 async_task]() -> int {
Z
zhaocaibei123 已提交
1846 1847
                auto &tmp_task_vec = *(async_task->data());
                const float *merge_data = tmp_task_vec.data();
1848 1849
                accessor->Merge(
                    &total_send_data, &merge_data, total_send_data_size);
Z
zhaocaibei123 已提交
1850 1851 1852 1853 1854 1855 1856
#pragma optimize("", off)
                delete async_task;
#pragma optimize("", on)
                return 0;
              });
          ++merge_count;
        }
Z
zhangchunle 已提交
1857
        for (size_t i = 0; i < merge_count; ++i) {
Z
zhaocaibei123 已提交
1858 1859 1860
          merge_status[i].wait();
        }

Z
zhaocaibei123 已提交
1861
        VLOG(3) << "BrpcPsClient::PushDenseTaskConsume before merge "
Z
zhaocaibei123 已提交
1862 1863 1864 1865 1866
                   "total_send_data[0]"
                << total_send_data[0] << " total_send_data[-2]"
                << total_send_data[total_send_data_size - 2]
                << total_send_data[0] << " total_send_data[-1]"
                << total_send_data[total_send_data_size - 1];
1867

Z
zhaocaibei123 已提交
1868
        if (scale_gradient && merge_count > 1) {
1869 1870
          Eigen::Map<Eigen::MatrixXf> mat(
              total_send_data, 1, total_send_data_size);
Z
zhaocaibei123 已提交
1871 1872 1873
          mat *= (1.0 / (merge_count + 1));
        }

Z
zhaocaibei123 已提交
1874
        VLOG(3) << "BrpcPsClient::PushDenseTaskConsume after merge "
Z
zhaocaibei123 已提交
1875 1876 1877 1878 1879 1880 1881 1882
                   "total_send_data[0]"
                << total_send_data[0] << " total_send_data[-2]"
                << total_send_data[total_send_data_size - 2]
                << " total_send_data[-1]"
                << total_send_data[total_send_data_size - 1] << " merge_count "
                << merge_count;
      }
      std::shared_ptr<DenseAsyncTask> task_ptr(task);
1883 1884
      PushDenseRawGradient(
          task_ptr, total_send_data, total_send_data_size, closure);
Z
zhaocaibei123 已提交
1885
    }
1886 1887
    auto wait_ms = FLAGS_pserver_async_push_dense_interval_ms -
                   (butil::gettimeofday_ms() - async_start_time_ms);
Z
zhaocaibei123 已提交
1888 1889 1890 1891 1892 1893
    if (wait_ms > 0) {
      usleep(wait_ms * 1000);
    }
  }
}

Z
zhaocaibei123 已提交
1894 1895 1896 1897 1898
void BrpcPsClient::PushDenseRawGradient(std::shared_ptr<DenseAsyncTask> &task,
                                        float *total_send_data,
                                        size_t total_send_data_size,
                                        DownpourBrpcClosure *closure) {
  auto *accessor = GetTableAccessor(task->table_id());
Z
zhaocaibei123 已提交
1899 1900 1901 1902 1903
  size_t request_call_num = _server_channels.size();
  // 将数据拷贝到请求buffer区
  auto timer = std::make_shared<CostTimer>("pserver_client_push_dense_rpc");
  closure->add_timer(timer);
  uint32_t num_per_shard =
1904
      DenseDimPerShard(accessor->GetAccessorInfo().fea_dim, request_call_num);
1905 1906
  auto send_timer =
      std::make_shared<CostTimer>("pserver_client_push_dense_send");
Z
zhaocaibei123 已提交
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
  for (size_t i = 0; i < request_call_num; ++i) {
    closure->request(i)->set_cmd_id(PS_PUSH_DENSE_TABLE);
    closure->request(i)->set_table_id(task->table_id());
    closure->request(i)->set_client_id(_client_id);
    auto *push_data = closure->request(i)->mutable_data();
    push_data->clear();
    push_data->resize(sizeof(uint32_t) + num_per_shard * sizeof(float));
    char *push_data_ptr = const_cast<char *>(push_data->data());
    memcpy(push_data_ptr, &num_per_shard, sizeof(uint32_t));
    memcpy(push_data_ptr + sizeof(uint32_t),
1917 1918
           total_send_data + i * num_per_shard,
           num_per_shard * sizeof(float));
Z
zhaocaibei123 已提交
1919 1920
    closure->cntl(i)->set_request_compress_type(
        (brpc::CompressType)FLAGS_pserver_communicate_compress_type);
Z
zhaocaibei123 已提交
1921
    PsService_Stub rpc_stub(GetDenseChannel(i));
1922 1923
    rpc_stub.service(
        closure->cntl(i), closure->request(i), closure->response(i), closure);
Z
zhaocaibei123 已提交
1924 1925 1926
  }
}

T
tangwei12 已提交
1927
}  // namespace distributed
T
Thunderbrook 已提交
1928
}  // namespace paddle