test_send_nccl_id.cc 3.0 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 24 25 26
/* 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"
#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 已提交
27
#include "paddle/fluid/platform/nccl_helper.h"
T
typhoonzero 已提交
28 29 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;

std::unique_ptr<detail::AsyncGRPCServer> rpc_service;

Y
yi.wu 已提交
40
void StartServer(std::atomic<bool>* initialized) {
T
typhoonzero 已提交
41 42
  f::Scope scope;
  p::CPUPlace place;
T
typhoonzero 已提交
43
  scope.Var(NCCL_ID_VARNAME);
T
typhoonzero 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57
  p::DeviceContextPool& pool = p::DeviceContextPool::Instance();
  auto& dev_ctx = *pool.Get(p::CPUPlace());

  rpc_service.reset(new detail::AsyncGRPCServer("127.0.0.1:0", true));

  f::ProgramDesc empty_program;
  f::Executor executor(dev_ctx.GetPlace());
  rpc_service->SetScope(&scope);
  rpc_service->SetDevCtx(&dev_ctx);
  rpc_service->SetProgram(&empty_program);
  rpc_service->SetExecutor(&executor);

  std::thread server_thread(
      std::bind(&detail::AsyncGRPCServer::RunSyncUpdate, rpc_service.get()));
Y
yi.wu 已提交
58
  *initialized = true;
T
typhoonzero 已提交
59 60 61 62 63 64 65
  rpc_service->SetCond(0);
  auto recv = rpc_service->Get();
  LOG(INFO) << "got nccl id and stop server...";
  rpc_service->ShutDown();
  server_thread.join();
}

Y
yuyang18 已提交
66
TEST(SendNcclId, Normal) {
Y
yi.wu 已提交
67 68 69 70
  std::atomic<bool> initialized{false};
  std::thread server_thread(StartServer, &initialized);
  while (!initialized) {
  }
T
typhoonzero 已提交
71
  // wait server to start
Y
yi.wu 已提交
72 73
  // sleep(2);
  rpc_service->WaitServerReady();
T
typhoonzero 已提交
74 75 76 77 78 79

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

T
typhoonzero 已提交
80
  auto var = scope.Var(NCCL_ID_VARNAME);
T
typhoonzero 已提交
81 82 83 84 85 86 87 88
  // var->SetType(f::proto::VarType_Type_RAW);
  auto id = var->GetMutable<ncclUniqueId>();
  p::dynload::ncclGetUniqueId(id);

  int port = rpc_service->GetSelectedPort();
  std::string ep = string::Sprintf("127.0.0.1:%d", port);
  detail::RPCClient client;

T
typhoonzero 已提交
89
  client.AsyncSendVariable(ep, dev_ctx, scope, NCCL_ID_VARNAME);
T
typhoonzero 已提交
90 91 92 93 94
  client.Wait();
  server_thread.join();
  auto* ptr = rpc_service.release();
  delete ptr;
}