| 6 | using namespace imperative; |
| 7 | |
| 8 | TEST(TestImperative, AllReduceBasic) { |
| 9 | REQUIRE_GPU(2); |
| 10 | const char* server_addr = "127.0.0.1"; |
| 11 | uint32_t port = 3456; |
| 12 | mgb_assert(opr::create_zmqrpc_server(server_addr, port) > 0); |
| 13 | HostTensorGenerator<> gen; |
| 14 | CompNode cn0 = CompNode::load("gpu0"), cn1 = CompNode::load("gpu1"); |
| 15 | |
| 16 | auto host_x = gen({233}, cn0), host_y = gen({233}, cn1); |
| 17 | auto expect = gen({233}); |
| 18 | for (size_t i = 0; i < 233; ++i) { |
| 19 | expect->ptr<float>()[i] = host_x->ptr<float>()[i] + host_y->ptr<float>()[i]; |
| 20 | } |
| 21 | |
| 22 | auto run = [&](std::shared_ptr<HostTensorND> hnd, uint32_t idx) { |
| 23 | auto def = imperative::CollectiveComm::make( |
| 24 | megdnn::param::CollectiveComm::Mode::ALL_REDUCE_SUM, "all_reduce", 2, |
| 25 | idx, idx == 0, false, server_addr, port, dtype::Float32(), "nccl", ""); |
| 26 | auto inp = Tensor::make(*hnd); |
| 27 | SmallVector<LogicalTensorDesc> output_descs; |
| 28 | auto oup = OpDef::apply_on_physical_tensor(*def, {inp}, output_descs, false); |
| 29 | HostTensorND host_v; |
| 30 | host_v.copy_from(oup[0]->dev_tensor()).sync(); |
| 31 | MGB_ASSERT_TENSOR_NEAR(*expect, host_v, 1e-6); |
| 32 | }; |
| 33 | |
| 34 | std::thread t0(std::bind(run, host_x, 0)); |
| 35 | std::thread t1(std::bind(run, host_y, 1)); |
| 36 | |
| 37 | t0.join(); |
| 38 | t1.join(); |
| 39 | } |
| 40 | |
| 41 | // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}} |
nothing calls this directly
no test coverage detected