rpc_device_api.cc 3.38 KB
Newer Older
1 2 3 4 5 6
/*!
 *  Copyright (c) 2017 by Contributors
 * \file rpc_device_api.cc
 */
#include <dmlc/logging.h>
#include <tvm/runtime/registry.h>
7
#include <tvm/runtime/device_api.h>
8
#include "rpc_session.h"
9 10 11 12 13 14 15 16 17 18 19 20 21 22

namespace tvm {
namespace runtime {

class RPCDeviceAPI final : public DeviceAPI {
 public:
  void SetDevice(TVMContext ctx) final {
    GetSess(ctx)->CallRemote(
        RPCCode::kDevSetDevice, ctx);
  }
  void GetAttr(TVMContext ctx, DeviceAttrKind kind, TVMRetValue* rv) final {
    *rv = GetSess(ctx)->CallRemote(
        RPCCode::kDevGetAttr, ctx, static_cast<int>(kind));
  }
23 24 25 26
  void* AllocDataSpace(TVMContext ctx,
                       size_t nbytes,
                       size_t alignment,
                       TVMType type_hint) final {
27 28
    auto sess = GetSess(ctx);
    void *data = sess->CallRemote(
29
            RPCCode::kDevAllocData, ctx, nbytes, alignment, type_hint);
30 31 32 33 34 35 36
    RemoteSpace* space = new RemoteSpace();
    space->data = data;
    space->sess = std::move(sess);
    return space;
  }
  void FreeDataSpace(TVMContext ctx, void* ptr) final {
    RemoteSpace* space = static_cast<RemoteSpace*>(ptr);
37 38 39 40 41 42
    try {
      GetSess(ctx)->CallRemote(
          RPCCode::kDevFreeData, ctx, space->data);
    } catch (const dmlc::Error& e) {
      // fault tolerance to remote close.
    }
43 44 45 46 47 48 49 50 51
    delete space;
  }
  void CopyDataFromTo(const void* from,
                      size_t from_offset,
                      void* to,
                      size_t to_offset,
                      size_t size,
                      TVMContext ctx_from,
                      TVMContext ctx_to,
52
                      TVMType type_hint,
53 54 55 56 57 58 59 60 61 62 63
                      TVMStreamHandle stream) final {
    int from_dev_type = ctx_from.device_type;
    int to_dev_type = ctx_to.device_type;
    if (from_dev_type > kRPCSessMask &&
        to_dev_type > kRPCSessMask) {
      CHECK(ctx_from.device_type == ctx_to.device_type)
          << "Cannot copy across two different remote session";
      GetSess(ctx_from)->CallRemote(
          RPCCode::kCopyAmongRemote,
          static_cast<const RemoteSpace*>(from)->data, from_offset,
          static_cast<const RemoteSpace*>(to)->data, to_offset,
64
          size,  ctx_from, ctx_to, type_hint, stream);
65
    } else if (from_dev_type > kRPCSessMask &&
66
               to_dev_type == kDLCPU) {
67 68
      GetSess(ctx_from)->CopyFromRemote(
          static_cast<const RemoteSpace*>(from)->data, from_offset,
69
          to, to_offset, size, ctx_from, type_hint);
70
    } else if (from_dev_type == kDLCPU &&
71 72 73 74
               to_dev_type > kRPCSessMask) {
      GetSess(ctx_to)->CopyToRemote(
          (void*)from, from_offset,  // NOLINT(*)
          static_cast<const RemoteSpace*>(to)->data, to_offset,
75
          size, ctx_to, type_hint);
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
    } else {
      LOG(FATAL) << "expect copy from/to remote or between remote";
    }
  }
  void StreamSync(TVMContext ctx, TVMStreamHandle stream) final {
    GetSess(ctx)->CallRemote(
        RPCCode::kDevStreamSync, ctx, stream);
  }

 private:
  std::shared_ptr<RPCSession> GetSess(TVMContext ctx) {
    int dev_type = ctx.device_type;
    CHECK_GE(dev_type, kRPCSessMask);
    int tbl_index = dev_type / kRPCSessMask -  1;
    return RPCSession::Get(tbl_index);
  }
};

TVM_REGISTER_GLOBAL("device_api.rpc")
.set_body([](TVMArgs args, TVMRetValue* rv) {
    static RPCDeviceAPI inst;
    DeviceAPI* ptr = &inst;
    *rv = static_cast<void*>(ptr);
  });
}  // namespace runtime
}  // namespace tvm