cpu_device_api.cc 2.39 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  void* AllocDataSpace(TVMContext ctx, size_t size, size_t alignment) final {
    void* ptr;
#if _MSC_VER
    ptr = _aligned_malloc(size, alignment);
    if (ptr == nullptr) throw std::bad_alloc();
#else
    int ret = posix_memalign(&ptr, alignment, size);
    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,
44
                      size_t from_offset,
45
                      void* to,
46
                      size_t to_offset,
47 48 49 50
                      size_t size,
                      TVMContext ctx_from,
                      TVMContext ctx_to,
                      TVMStreamHandle stream) final {
51 52 53
    memcpy(static_cast<char*>(to) + to_offset,
           static_cast<const char*>(from) + from_offset,
           size);
54 55 56 57
  }

  void StreamSync(TVMContext ctx, TVMStreamHandle stream) final {
  }
58 59 60 61 62 63 64 65 66

  void* AllocWorkspace(TVMContext ctx, size_t size) final;
  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;
  }
67 68
};

69 70
struct CPUWorkspacePool : public WorkspacePool {
  CPUWorkspacePool() :
71
      WorkspacePool(kDLCPU, CPUDeviceAPI::Global()) {}
72 73 74 75 76 77 78 79 80 81 82
};

void* CPUDeviceAPI::AllocWorkspace(TVMContext ctx, size_t size) {
  return dmlc::ThreadLocalStore<CPUWorkspacePool>::Get()
      ->AllocWorkspace(ctx, size);
}

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

83
TVM_REGISTER_GLOBAL("device_api.cpu")
84
.set_body([](TVMArgs args, TVMRetValue* rv) {
85
    DeviceAPI* ptr = CPUDeviceAPI::Global().get();
86 87 88 89
    *rv = static_cast<void*>(ptr);
  });
}  // namespace runtime
}  // namespace tvm