nccl_context_test.cc 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//   Copyright (c) 2019 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 <thread>  // NOLINT

K
kuizhiqing 已提交
17 18
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/framework/variable.h"
19
#include "paddle/fluid/imperative/nccl_context.h"
20
#include "paddle/fluid/platform/gen_comm_id_helper.h"
W
wanghuancoder 已提交
21

22 23 24 25
#include "gtest/gtest.h"

namespace imperative = paddle::imperative;
namespace platform = paddle::platform;
K
kuizhiqing 已提交
26
namespace framework = paddle::framework;
27

28
int nrings = 2;
29
imperative::ParallelStrategy GetStrategy(int local_rank) {
30
  std::vector<std::string> eps = {"127.0.0.1:9866", "localhost:9867"};
31 32 33 34 35
  imperative::ParallelStrategy strategy;
  strategy.trainer_endpoints_ = eps;
  strategy.current_endpoint_ = eps[local_rank];
  strategy.nranks_ = 2;
  strategy.local_rank_ = local_rank;
36
  strategy.nrings_ = nrings;
37 38 39
  return strategy;
}

40
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
41
void BcastNCCLId(int local_rank, std::vector<ncclUniqueId>* nccl_ids) {
42
  auto strategy = GetStrategy(local_rank);
43 44
  int server_fd = platform::CreateListenSocket(strategy.current_endpoint_);

45 46
  platform::CUDAPlace gpu(local_rank);
  imperative::NCCLParallelContext ctx(strategy, gpu);
47 48 49
  ctx.BcastNCCLId(*nccl_ids, 0, server_fd);

  platform::CloseSocket(server_fd);
50 51 52
}

TEST(BcastNCCLId, Run) {
53 54 55 56 57
  std::vector<ncclUniqueId> nccl_ids;
  nccl_ids.resize(nrings);
  for (int i = 0; i < nrings; ++i) {
    platform::dynload::ncclGetUniqueId(&nccl_ids[i]);
  }
58

59 60 61 62 63 64 65 66
  std::thread t(BcastNCCLId, 0, &nccl_ids);

  std::vector<ncclUniqueId> recv_nccl_ids;
  recv_nccl_ids.resize(nrings);
  for (int i = 0; i < nrings; ++i) {
    platform::dynload::ncclGetUniqueId(&recv_nccl_ids[i]);
  }
  BcastNCCLId(1, &recv_nccl_ids);
67 68

  t.join();
69 70 71 72
  for (int i = 0; i < nrings; ++i) {
    EXPECT_EQ(0, std::memcmp(nccl_ids[i].internal, recv_nccl_ids[i].internal,
                             NCCL_UNIQUE_ID_BYTES));
  }
73
}
K
kuizhiqing 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

void Broadcast(int local_rank, int device_id) {
  int data_size = 4;
  float test_data = 7;
  const auto& place = platform::CUDAPlace(device_id);
  platform::CUDADeviceContext ctx(place);

  imperative::NCCLParallelContext npc(GetStrategy(local_rank), place);

  // init
  npc.Init();

  framework::Variable* src_dev_var(new framework::Variable());
  auto* src_dev_tensor = src_dev_var->GetMutable<framework::LoDTensor>();
  src_dev_tensor->mutable_data<float>(framework::make_ddim({data_size}), place);

  // fill data for rank 0 only
  std::vector<float> src_vec;
  if (local_rank == 0) {
    for (int i = 0; i < data_size; i++) {
      src_vec.push_back(test_data);
    }
    framework::TensorFromVector(src_vec, ctx, src_dev_tensor);
  }
  ctx.Wait();

  npc.Broadcast(src_dev_var, 0);
  std::this_thread::sleep_for(std::chrono::milliseconds(1000));

  // check result
  std::vector<float> dst_vec;
  framework::TensorToVector(*src_dev_tensor, ctx, &dst_vec);
  ctx.Wait();

  for (int i = 0; i < data_size; i++) {
    EXPECT_EQ(dst_vec[i], test_data);
  }
}

TEST(Broadcast, Run) {
R
ronnywang 已提交
114
  if (platform::GetGPUDeviceCount() >= 2) {
K
kuizhiqing 已提交
115 116 117 118 119 120
    std::thread t0(Broadcast, 0, 0);
    std::thread t1(Broadcast, 1, 1);
    t0.join();
    t1.join();
  }
}
121
#endif