cpu_device_api.cc 2.71 KB
Newer Older
1 2
/*!
 *  Copyright (c) 2016 by Contributors
3
 * \file cpu_device_api.cc
4 5
 */
#include <dmlc/logging.h>
6
#include <dmlc/thread_local.h>
7
#include <tvm/runtime/registry.h>
8
#include <tvm/runtime/device_api.h>
9 10
#include <cstdlib>
#include <cstring>
11
#include "./workspace_pool.h"
12 13 14

namespace tvm {
namespace runtime {
15
class CPUDeviceAPI final : public DeviceAPI {
16
 public:
17 18
  void SetDevice(TVMContext ctx) final {}
  void GetAttr(TVMContext ctx, DeviceAttrKind kind, TVMRetValue* rv) final {
19 20 21 22
    if (kind == kExist) {
      *rv = 1;
    }
  }
23 24 25 26
  void* AllocDataSpace(TVMContext ctx,
                       size_t nbytes,
                       size_t alignment,
                       TVMType type_hint) final {
27 28
    void* ptr;
#if _MSC_VER
29
    ptr = _aligned_malloc(nbytes, alignment);
30
    if (ptr == nullptr) throw std::bad_alloc();
nhynes committed
31 32 33
#elif defined(_LIBCPP_SGX_CONFIG)
    ptr = memalign(alignment, nbytes);
    if (ptr == nullptr) throw std::bad_alloc();
34
#else
35
    int ret = posix_memalign(&ptr, alignment, nbytes);
36 37 38 39 40 41 42 43 44 45 46 47 48 49
    if (ret != 0) throw std::bad_alloc();
#endif
    return ptr;
  }

  void FreeDataSpace(TVMContext ctx, void* ptr) final {
#if _MSC_VER
    _aligned_free(ptr);
#else
    free(ptr);
#endif
  }

  void CopyDataFromTo(const void* from,
50
                      size_t from_offset,
51
                      void* to,
52
                      size_t to_offset,
53 54 55 56
                      size_t size,
                      TVMContext ctx_from,
                      TVMContext ctx_to,
                      TVMStreamHandle stream) final {
57 58 59
    memcpy(static_cast<char*>(to) + to_offset,
           static_cast<const char*>(from) + from_offset,
           size);
60 61 62 63
  }

  void StreamSync(TVMContext ctx, TVMStreamHandle stream) final {
  }
64

65
  void* AllocWorkspace(TVMContext ctx, size_t size, TVMType type_hint) final;
66 67 68 69 70 71 72
  void FreeWorkspace(TVMContext ctx, void* data) final;

  static const std::shared_ptr<CPUDeviceAPI>& Global() {
    static std::shared_ptr<CPUDeviceAPI> inst =
        std::make_shared<CPUDeviceAPI>();
    return inst;
  }
73 74
};

75 76
struct CPUWorkspacePool : public WorkspacePool {
  CPUWorkspacePool() :
77
      WorkspacePool(kDLCPU, CPUDeviceAPI::Global()) {}
78 79
};

80 81 82
void* CPUDeviceAPI::AllocWorkspace(TVMContext ctx,
                                   size_t size,
                                   TVMType type_hint) {
83 84 85 86 87 88 89 90
  return dmlc::ThreadLocalStore<CPUWorkspacePool>::Get()
      ->AllocWorkspace(ctx, size);
}

void CPUDeviceAPI::FreeWorkspace(TVMContext ctx, void* data) {
  dmlc::ThreadLocalStore<CPUWorkspacePool>::Get()->FreeWorkspace(ctx, data);
}

91
TVM_REGISTER_GLOBAL("device_api.cpu")
92
.set_body([](TVMArgs args, TVMRetValue* rv) {
93
    DeviceAPI* ptr = CPUDeviceAPI::Global().get();
94 95 96 97
    *rv = static_cast<void*>(ptr);
  });
}  // namespace runtime
}  // namespace tvm