1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*!
21 * \file src/runtime/naive_allocator.h
22 */
23#ifndef TVM_RUNTIME_VM_NAIVE_ALLOCATOR_H_
24#define TVM_RUNTIME_VM_NAIVE_ALLOCATOR_H_
25
26#include <tvm/runtime/device_api.h>
27#include <tvm/runtime/vm/memory_manager.h>
28
29#include <atomic>
30
31namespace tvm {
32namespace runtime {
33namespace vm {
34
35class NaiveAllocator final : public Allocator {
36 public:
37 explicit NaiveAllocator(Device dev) : Allocator(kNaive), used_memory_(0), device_(dev) {}
38
39 Buffer Alloc(size_t nbytes, size_t alignment, DLDataType type_hint) override {
40 Buffer buf;
41 buf.device = device_;
42 buf.size = nbytes;
43 buf.data = DeviceAPI::Get(device_)->AllocDataSpace(device_, nbytes, alignment, type_hint);
44 used_memory_.fetch_add(nbytes, std::memory_order_relaxed);
45 DLOG(INFO) << "allocate " << nbytes << " B, used memory " << used_memory_ << " B";
46 return buf;
47 }
48
49 void Free(const Buffer& buffer) override {
50 DeviceAPI::Get(device_)->FreeDataSpace(buffer.device, buffer.data);
51 used_memory_.fetch_sub(buffer.size, std::memory_order_relaxed);
52 DLOG(INFO) << "free " << buffer.size << " B, used memory " << used_memory_ << " B";
53 }
54
55 size_t UsedMemory() const override { return used_memory_.load(std::memory_order_relaxed); }
56
57 private:
58 std::atomic<size_t> used_memory_;
59 Device device_;
60};
61
62} // namespace vm
63} // namespace runtime
64} // namespace tvm
65
66#endif // TVM_RUNTIME_VM_NAIVE_ALLOCATOR_H_
67