io_remote.cpp 2.1 KB
Newer Older
1
/**
M
Megvii Engine Team 已提交
2 3
 * \file imperative/src/test/io_remote.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
4
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6
 *
M
Megvii Engine Team 已提交
7 8 9
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 11 12
 */

#include "./helper.h"
13
#include "megbrain/imperative/ops/autogen.h"
14 15 16 17 18 19 20 21 22
#include "megbrain/opr/mm_handler.h"

using namespace mgb;
using namespace imperative;

TEST(TestImperative, IORemote) {
    REQUIRE_GPU(2);
    const char* server_addr = "127.0.0.1";
    uint32_t port = 4567;
23
    mgb_assert(opr::create_zmqrpc_server(server_addr, port) > 0);
24 25 26 27 28 29 30 31 32 33 34 35
    HostTensorGenerator<> gen;
    CompNode cn0 = CompNode::load("gpu0"), cn1 = CompNode::load("gpu1");

    size_t vector_size = 233;
    auto host_x = gen({vector_size}, cn0), host_y = gen({vector_size}, cn1);

    auto expect = gen({vector_size});
    for (size_t i = 0; i < vector_size; ++i) {
        expect->ptr<float>()[i] = host_x->ptr<float>()[i];
    }

    auto run_send = [&](std::shared_ptr<HostTensorND> hnd) {
36
        auto def = imperative::RemoteSend::make(
M
Megvii Engine Team 已提交
37
                "io_remote_test", server_addr, port, 1, "nccl");
38
        auto inp = Tensor::make(*hnd);
39
        auto oup = OpDef::apply_on_physical_tensor(*def, {inp});
40 41 42
    };

    auto run_recv = [&](std::shared_ptr<HostTensorND> hnd) {
43
        auto def = imperative::RemoteRecv::make(
M
Megvii Engine Team 已提交
44
                "io_remote_test", server_addr, port, 0, CompNode::load("gpu1"),
45
                std::vector<int32_t>{(int32_t)vector_size}, dtype::Float32(), "nccl");
46
        auto inp = Tensor::make(*hnd);
47
        auto oup = OpDef::apply_on_physical_tensor(*def, {inp});
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        HostTensorND host_v;
        host_v.copy_from(oup[0]->dev_tensor()).sync();
        MGB_ASSERT_TENSOR_NEAR(*expect, host_v, 1e-6);
    };

    std::thread t0(std::bind(run_send, host_x));
    std::thread t1(std::bind(run_recv, host_y));

    t0.join();
    t1.join();
}

// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

// ./imperative_test --gtest_filter TestIORemote