test_send_nccl_id.cc 3.4 KB
Newer Older
T
typhoonzero 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* Copyright (c) 2016 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. */

#include <unistd.h>
#include <string>
#include <thread>  // NOLINT

#include "gtest/gtest.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/operators/detail/grpc_client.h"
24 25
#include "paddle/fluid/operators/detail/grpc_server.h"
#include "paddle/fluid/operators/detail/request_handler_impl.h"
T
typhoonzero 已提交
26 27 28
#include "paddle/fluid/operators/listen_and_serv_op.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/operators/math/selected_rows_functor.h"
Y
yi.wu 已提交
29
#include "paddle/fluid/platform/nccl_helper.h"
T
typhoonzero 已提交
30 31 32 33 34 35 36 37 38 39
#include "paddle/fluid/string/printf.h"

USE_NO_KERNEL_OP(listen_and_serv);

namespace f = paddle::framework;
namespace p = paddle::platform;
namespace m = paddle::operators::math;
namespace detail = paddle::operators::detail;
namespace string = paddle::string;

40 41
std::unique_ptr<detail::AsyncGRPCServer> g_rpc_service;
std::unique_ptr<detail::RequestHandler> g_req_handler;
T
typhoonzero 已提交
42

43
void StartServer() {
T
typhoonzero 已提交
44 45
  f::Scope scope;
  p::CPUPlace place;
T
typhoonzero 已提交
46
  scope.Var(NCCL_ID_VARNAME);
T
typhoonzero 已提交
47 48 49 50 51
  p::DeviceContextPool& pool = p::DeviceContextPool::Instance();
  auto& dev_ctx = *pool.Get(p::CPUPlace());

  f::ProgramDesc empty_program;
  f::Executor executor(dev_ctx.GetPlace());
52 53 54 55 56 57 58
  g_req_handler->SetScope(&scope);
  g_req_handler->SetDevCtx(&dev_ctx);
  g_req_handler->SetProgram(&empty_program);
  g_req_handler->SetExecutor(&executor);

  g_rpc_service->RegisterRPC(detail::kRequestSend, g_req_handler.get());
  g_req_handler->SetRPCServer(g_rpc_service.get());
T
typhoonzero 已提交
59 60

  std::thread server_thread(
61 62 63 64 65 66
      std::bind(&detail::AsyncGRPCServer::StartServer, g_rpc_service.get()));

  g_rpc_service->SetCond(detail::kRequestSend);
  std::cout << "before WaitFanInOfSend" << std::endl;
  g_rpc_service->WaitBarrier(detail::kRequestSend);

T
typhoonzero 已提交
67
  LOG(INFO) << "got nccl id and stop server...";
68
  g_rpc_service->ShutDown();
T
typhoonzero 已提交
69 70 71
  server_thread.join();
}

72 73 74 75 76 77
TEST(SendNcclId, GrpcServer) {
  g_req_handler.reset(new detail::RequestSendHandler(true));
  g_rpc_service.reset(new detail::AsyncGRPCServer("127.0.0.1:0", 1));

  std::thread server_thread(StartServer);
  g_rpc_service->WaitServerReady();
T
typhoonzero 已提交
78 79 80 81 82 83

  f::Scope scope;
  p::CPUPlace place;
  p::DeviceContextPool& pool = p::DeviceContextPool::Instance();
  auto& dev_ctx = *pool.Get(p::CPUPlace());

T
typhoonzero 已提交
84
  auto var = scope.Var(NCCL_ID_VARNAME);
T
typhoonzero 已提交
85 86 87
  auto id = var->GetMutable<ncclUniqueId>();
  p::dynload::ncclGetUniqueId(id);

88 89
  int port = g_rpc_service->GetSelectedPort();

T
typhoonzero 已提交
90 91
  std::string ep = string::Sprintf("127.0.0.1:%d", port);
  detail::RPCClient client;
92
  LOG(INFO) << "connect to server" << ep;
T
typhoonzero 已提交
93
  client.AsyncSendVariable(ep, dev_ctx, scope, NCCL_ID_VARNAME);
T
typhoonzero 已提交
94
  client.Wait();
95 96 97
  client.AsyncSendBatchBarrier(ep);
  client.Wait();

T
typhoonzero 已提交
98
  server_thread.join();
99 100
  g_rpc_service.reset(nullptr);
  g_req_handler.reset(nullptr);
T
typhoonzero 已提交
101
}