ONE - On-device Neural Engine
Loading...
Searching...
No Matches
kuma.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "kuma.h"
18
19//
20// Greedy Allocation Algorithm
21//
22namespace kuma
23{
24
26{
27 uint32_t next = 0;
28
29 for (uint32_t n = 0; n < ctx->item_count(); ++n)
30 {
31 ctx->mem_offset(n, next);
32 next += ctx->item_size(n);
33 }
34
35 ctx->mem_total(next);
36};
37
38} // namespace kuma
39
40//
41// Linear Scan First Fit Algorithm
42//
43#include "IntervalSet.h"
44
45namespace kuma
46{
47
49{
50 using namespace kuma::details;
51
52 uint32_t upper_bound = 0;
53 std::map<ItemID, std::pair<uint32_t /* BEGIN */, uint32_t /* END */>> committed_items;
54
55 // Allocate items in linear order (from item 0, item 1, ...)
56 //
57 // The implementor of Context is responsible for item ordering.
58 for (uint32_t n = 0; n < ctx->item_count(); ++n)
59 {
60 IntervalSet intervals;
61
62 for (auto item_in_conflict : ctx->conflict_with(n))
63 {
64 auto it = committed_items.find(item_in_conflict);
65
66 // Skip if item_in_conflict is not committed yet
67 if (it == committed_items.end())
68 {
69 continue;
70 }
71
72 auto const alloc_s = it->second.first;
73 auto const alloc_e = it->second.second;
74 intervals.insert(mask(alloc_s, alloc_e));
75 }
76
77 uint32_t const item_size = ctx->item_size(n);
78 uint32_t const item_alloc_s = intervals.firstfit(item_size);
79 uint32_t const item_alloc_e = item_alloc_s + item_size;
80
81 // Notify "mem_offset"
82 ctx->mem_offset(n, item_alloc_s);
83
84 // Update "upper bound" and commit allocation
85 upper_bound = std::max(upper_bound, item_alloc_e);
86 committed_items[n] = std::make_pair(item_alloc_s, item_alloc_e);
87 }
88
89 // Notify "mem_total"
90 ctx->mem_total(upper_bound);
91}
92
93} // namespace kuma
void insert(const IntervalMask &)
uint32_t firstfit(uint32_t len) const
Definition kuma.h:24
uint32_t ItemID
Definition kuma.h:39
void solve(Context< Greedy > *)