io_remote.cpp 3.3 KB
Newer Older
1
/**
M
Megvii Engine Team 已提交
2 3
 * \file imperative/src/impl/ops/io_remote.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
4
 *
M
Megvii Engine Team 已提交
5
 * Copyright (c) 2014-2020 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
 */
M
Megvii Engine Team 已提交
11

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "megbrain_build_config.h"

#if MGB_ENABLE_OPR_MM
#include "../op_trait.h"
#include "../proxy_graph_detail.h"
#include "megbrain/opr/io_remote.h"
#include "megbrain/opr/mm_handler.h"
#endif // MGB_ENABLE_OPR_MM

#include "megbrain/imperative/ops/io_remote.h"

namespace mgb {
namespace imperative {

#if MGB_ENABLE_OPR_MM
namespace {
cg::OperatorNodeBase* apply_on_var_node_remote_send(
        const OpDef& def, const VarNodeArray& inputs) {
    auto&& send = def.cast_final_safe<RemoteSend>();
    auto group_client = std::make_shared<GroupClientProxy>(
            ssprintf("%s:%d", send.addr.data(), send.port));
    auto&& graph = inputs[0]->owner_graph();

    cg::OperatorNodeConfig config;
    cg::OperatorNodeBase* opr =
            graph->insert_opr(std::make_unique<mgb::opr::RemoteSend>(
                    send.key, inputs[0], group_client, true, config));
    return opr;
}

cg::OperatorNodeBase* apply_on_var_node_remote_recv(
        const OpDef& def, const VarNodeArray& inputs) {
    auto&& recv = def.cast_final_safe<RemoteRecv>();
    auto group_client = std::make_shared<GroupClientProxy>(
            ssprintf("%s:%d", recv.addr.data(), recv.port));
    auto&& graph = inputs[0]->owner_graph();
    return graph->insert_opr(std::make_unique<mgb::opr::RemoteRecv>(
49
            recv.key, inputs[0], *graph, group_client, OperatorNodeConfig{recv.cn},
50 51 52 53 54 55 56 57 58 59 60 61 62
            recv.shape, recv.dtype));
}

OP_TRAIT_REG(RemoteSend, RemoteSend, mgb::opr::RemoteSend)
        .apply_on_var_node(apply_on_var_node_remote_send)
        .fallback();

OP_TRAIT_REG(RemoteRecv, RemoteRecv, mgb::opr::RemoteRecv)
        .apply_on_var_node(apply_on_var_node_remote_recv)
        .fallback();
}  // anonymous namespace
#endif // MGB_ENABLE_OPR_MM

63 64 65 66 67 68 69 70 71 72 73 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
bool RemoteSend::is_same_st(const Hashable& another) const{
    return as_tuple() == another.cast_final<RemoteSend>().as_tuple();
}

size_t RemoteSend::hash() const{
    XXHash xxhash;
    auto append = [&xxhash](auto field){
        auto hash_val = HashTrait<decltype(field)>::eval(field);
        xxhash.update(reinterpret_cast<void*>(&hash_val), sizeof(hash_val));
    };
    append(key); 
    append(addr);
    append(port);
    append(rank_to);
    return xxhash.digest();
}

bool RemoteRecv::is_same_st(const Hashable& another) const{
    return as_tuple() == another.cast_final<RemoteRecv>().as_tuple();
}

size_t RemoteRecv::hash() const{
    XXHash xxhash;
    auto append = [&xxhash](auto field){
        auto hash_val = HashTrait<decltype(field)>::eval(field);
        xxhash.update(reinterpret_cast<void*>(&hash_val), sizeof(hash_val));
    };
    append(key); 
    append(addr);
    append(port);
    append(rank_from);
    append(cn.to_string());
    append(dtype.handle());
    append(shape.to_string());
    return xxhash.digest();
}

100 101 102 103 104
MGB_DYN_TYPE_OBJ_FINAL_IMPL(RemoteSend);
MGB_DYN_TYPE_OBJ_FINAL_IMPL(RemoteRecv);

}  // namespace imperative
}  // namespace mgb