"git@developer.sourcefind.cn:change/sglang.git" did not exist on "6bfdb4031dec751d51b6e68c09c768bd41922291"
cpu_device_api.cc 2.79 KB
Newer Older
Minjie Wang's avatar
Minjie Wang committed
1
2
3
4
5
6
7
8
9
10
11
12
/*!
 *  Copyright (c) 2016 by Contributors
 * \file cpu_device_api.cc
 */
#include <dmlc/logging.h>
#include <dmlc/thread_local.h>
#include <dgl/runtime/registry.h>
#include <dgl/runtime/device_api.h>
#include <cstdlib>
#include <cstring>
#include "workspace_pool.h"

13
namespace dgl {
Minjie Wang's avatar
Minjie Wang committed
14
15
16
namespace runtime {
class CPUDeviceAPI final : public DeviceAPI {
 public:
17
18
  void SetDevice(DGLContext ctx) final {}
  void GetAttr(DGLContext ctx, DeviceAttrKind kind, DGLRetValue* rv) final {
Minjie Wang's avatar
Minjie Wang committed
19
20
21
22
    if (kind == kExist) {
      *rv = 1;
    }
  }
23
  void* AllocDataSpace(DGLContext ctx,
Minjie Wang's avatar
Minjie Wang committed
24
25
                       size_t nbytes,
                       size_t alignment,
26
                       DGLType type_hint) final {
Minjie Wang's avatar
Minjie Wang committed
27
    void* ptr;
28
#if _MSC_VER || defined(__MINGW32__)
Minjie Wang's avatar
Minjie Wang committed
29
30
31
32
33
34
35
36
37
38
39
40
    ptr = _aligned_malloc(nbytes, alignment);
    if (ptr == nullptr) throw std::bad_alloc();
#elif defined(_LIBCPP_SGX_CONFIG)
    ptr = memalign(alignment, nbytes);
    if (ptr == nullptr) throw std::bad_alloc();
#else
    int ret = posix_memalign(&ptr, alignment, nbytes);
    if (ret != 0) throw std::bad_alloc();
#endif
    return ptr;
  }

41
  void FreeDataSpace(DGLContext ctx, void* ptr) final {
42
#if _MSC_VER || defined(__MINGW32__)
Minjie Wang's avatar
Minjie Wang committed
43
44
45
46
47
48
49
50
51
52
53
    _aligned_free(ptr);
#else
    free(ptr);
#endif
  }

  void CopyDataFromTo(const void* from,
                      size_t from_offset,
                      void* to,
                      size_t to_offset,
                      size_t size,
54
55
56
57
                      DGLContext ctx_from,
                      DGLContext ctx_to,
                      DGLType type_hint,
                      DGLStreamHandle stream) final {
Minjie Wang's avatar
Minjie Wang committed
58
59
60
61
62
    memcpy(static_cast<char*>(to) + to_offset,
           static_cast<const char*>(from) + from_offset,
           size);
  }

63
  void StreamSync(DGLContext ctx, DGLStreamHandle stream) final {
Minjie Wang's avatar
Minjie Wang committed
64
65
  }

66
67
  void* AllocWorkspace(DGLContext ctx, size_t size, DGLType type_hint) final;
  void FreeWorkspace(DGLContext ctx, void* data) final;
Minjie Wang's avatar
Minjie Wang committed
68
69
70
71
72
73
74
75
76
77
78
79
80

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

struct CPUWorkspacePool : public WorkspacePool {
  CPUWorkspacePool() :
      WorkspacePool(kDLCPU, CPUDeviceAPI::Global()) {}
};

81
void* CPUDeviceAPI::AllocWorkspace(DGLContext ctx,
Minjie Wang's avatar
Minjie Wang committed
82
                                   size_t size,
83
                                   DGLType type_hint) {
Minjie Wang's avatar
Minjie Wang committed
84
85
86
87
  return dmlc::ThreadLocalStore<CPUWorkspacePool>::Get()
      ->AllocWorkspace(ctx, size);
}

88
void CPUDeviceAPI::FreeWorkspace(DGLContext ctx, void* data) {
Minjie Wang's avatar
Minjie Wang committed
89
90
91
  dmlc::ThreadLocalStore<CPUWorkspacePool>::Get()->FreeWorkspace(ctx, data);
}

92
93
DGL_REGISTER_GLOBAL("device_api.cpu")
.set_body([](DGLArgs args, DGLRetValue* rv) {
Minjie Wang's avatar
Minjie Wang committed
94
95
96
97
    DeviceAPI* ptr = CPUDeviceAPI::Global().get();
    *rv = static_cast<void*>(ptr);
  });
}  // namespace runtime
98
}  // namespace dgl