nccl_op_test.cu 2.7 KB
Newer Older
D
Dong Zhihong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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 "paddle/operators/nccl_op.h"

#include "glog/logging.h"
#include "gtest/gtest.h"

D
Dong Zhihong 已提交
19 20 21 22 23
#include "paddle/framework/block_desc.h"
#include "paddle/framework/op_desc.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/program_desc.h"
#include "paddle/framework/var_desc.h"
D
Dong Zhihong 已提交
24 25 26 27 28 29 30 31 32 33
#include "paddle/platform/device_context.h"
#include "paddle/platform/enforce.h"
#include "paddle/platform/gpu_info.h"

#include <thrust/device_vector.h>
#include <memory>
#include <vector>

static std::vector<int> gpu_list;

D
Dong Zhihong 已提交
34 35
namespace f = paddle::framework;
namespace ops = paddle::operators;
D
Dong Zhihong 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

void AddOp(const std::string &type, const f::VariableNameMap &inputs,
           const f::VariableNameMap &outputs, f::AttributeMap attrs,
           paddle::framework::BlockDescBind *block) {
  for (auto kv : outputs) {
    for (auto v : kv.second) {
      auto var = block->Var(v);
      var->SetDataType(paddle::framework::DataType::FP32);
    }
  }

  auto op = block->AppendOp();
  op->SetType(type);
  for (auto &kv : inputs) {
    op->SetInput(kv.first, kv.second);
  }
  for (auto &kv : outputs) {
    op->SetOutput(kv.first, kv.second);
  }
  op->SetAttrMap(attrs);
}

D
Dong Zhihong 已提交
58
TEST(NCCL, ncclInit) {
D
Dong Zhihong 已提交
59 60
  f::ProgramDescBind program;
  f::BlockDescBind *block = program.Block(0);
D
Dong Zhihong 已提交
61 62 63 64 65 66 67 68
  f::OpDescBind *op = block->AppendOp();

  paddle::platform::Communicator comm;
  op->SetType("ncclInit");
  op->SetOutput("Communicator", )

      AddOp("ncclInit", {}, {{"Communicator", {comm}}}, {{"gpus", {gpu_list}}},
            block);
D
Dong Zhihong 已提交
69 70
}

D
Dong Zhihong 已提交
71 72 73 74 75 76 77 78 79
// TEST(NCCL, ncclAllReduce) {
//   f::ProgramDescBind program;
//   f::BlockDescBind *block = program.Block(0);

//   paddle::platform::Communicator comm;
//   AddOp("ncclInit", {}, {{"Communicator", {comm}}, {"gpus", {gpu_list}}},
//   block);
// }

D
Dong Zhihong 已提交
80
int main(int argc, char **argv) {
D
Dong Zhihong 已提交
81
  static int dev_count = paddle::platform::GetCUDADeviceCount();
D
Dong Zhihong 已提交
82 83 84 85 86 87
  if (dev_count <= 1) {
    LOG(WARNING)
        << "Cannot test multi-gpu nccl, because the CUDA device count is "
        << dev_count;
    return 0;
  }
D
Dong Zhihong 已提交
88 89 90 91

  for (int i = 0; i < dev_count; ++i) {
    gpu_list.emplace_back(i);
  }
D
Dong Zhihong 已提交
92 93 94
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}