cpu_device_api.cc 3.72 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

20 21
/*!
 *  Copyright (c) 2016 by Contributors
22
 * \file cpu_device_api.cc
23 24
 */
#include <dmlc/logging.h>
25
#include <dmlc/thread_local.h>
26
#include <tvm/runtime/registry.h>
27
#include <tvm/runtime/device_api.h>
28 29
#include <cstdlib>
#include <cstring>
30
#include "workspace_pool.h"
31

32 33 34 35
#ifdef __ANDROID__
#include <android/api-level.h>
#endif

36 37
namespace tvm {
namespace runtime {
38
class CPUDeviceAPI final : public DeviceAPI {
39
 public:
40 41
  void SetDevice(TVMContext ctx) final {}
  void GetAttr(TVMContext ctx, DeviceAttrKind kind, TVMRetValue* rv) final {
42 43 44 45
    if (kind == kExist) {
      *rv = 1;
    }
  }
46 47 48 49
  void* AllocDataSpace(TVMContext ctx,
                       size_t nbytes,
                       size_t alignment,
                       TVMType type_hint) final {
50 51
    void* ptr;
#if _MSC_VER
52
    ptr = _aligned_malloc(nbytes, alignment);
53
    if (ptr == nullptr) throw std::bad_alloc();
54
#elif defined(_LIBCPP_SGX_CONFIG) || (defined(__ANDROID__) && __ANDROID_API__ < 17)
nhynes committed
55 56
    ptr = memalign(alignment, nbytes);
    if (ptr == nullptr) throw std::bad_alloc();
57
#else
58
    // posix_memalign is available in android ndk since __ANDROID_API__ >= 17
59
    int ret = posix_memalign(&ptr, alignment, nbytes);
60 61 62 63 64 65 66 67 68 69 70 71 72 73
    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,
74
                      size_t from_offset,
75
                      void* to,
76
                      size_t to_offset,
77 78 79
                      size_t size,
                      TVMContext ctx_from,
                      TVMContext ctx_to,
80
                      TVMType type_hint,
81
                      TVMStreamHandle stream) final {
82 83 84
    memcpy(static_cast<char*>(to) + to_offset,
           static_cast<const char*>(from) + from_offset,
           size);
85 86 87 88
  }

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

90
  void* AllocWorkspace(TVMContext ctx, size_t size, TVMType type_hint) final;
91 92 93 94 95 96 97
  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;
  }
98 99
};

100 101
struct CPUWorkspacePool : public WorkspacePool {
  CPUWorkspacePool() :
102
      WorkspacePool(kDLCPU, CPUDeviceAPI::Global()) {}
103 104
};

105 106 107
void* CPUDeviceAPI::AllocWorkspace(TVMContext ctx,
                                   size_t size,
                                   TVMType type_hint) {
108 109 110 111 112 113 114 115
  return dmlc::ThreadLocalStore<CPUWorkspacePool>::Get()
      ->AllocWorkspace(ctx, size);
}

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

116
TVM_REGISTER_GLOBAL("device_api.cpu")
117
.set_body([](TVMArgs args, TVMRetValue* rv) {
118
    DeviceAPI* ptr = CPUDeviceAPI::Global().get();
119 120 121 122
    *rv = static_cast<void*>(ptr);
  });
}  // namespace runtime
}  // namespace tvm