Skip to content

Commit 8337aaf

Browse files
committed
Remove free_all_async and add fixed_order_resource.
Signed-off-by: Michał Zientkiewicz <mzient@gmail.com>
1 parent 7317547 commit 8337aaf

File tree

2 files changed

+73
-30
lines changed

2 files changed

+73
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef DALI_CORE_MM_FIXED_ORDER_RESOURCE_H_
16+
#define DALI_CORE_MM_FIXED_ORDER_RESOURCE_H_
17+
18+
#include <dali/core/mm/memory_resource.h>
19+
#include <dali/core/access_order.h>
20+
21+
namespace dali {
22+
namespace mm {
23+
24+
/**
25+
* @brief A proxy resource that performs all allocations and deallocation in a fixed order
26+
* (stream or host) and exposes a non-stream allocation API.
27+
*
28+
* NOTE: This class cannot be used by an arbitrary consumer of memory resources if the
29+
* allocation order is not AccessOrder::host.
30+
*
31+
* NOTE: If this resource is used as an upstream for another resource, then the pool
32+
* inherits the order of allocation and deallocation if:
33+
* - the allocaton and deallocation order matches
34+
* - it does not attempt to access the memory in order other than specified
35+
*/
36+
template <typename MemoryKind, typename Upstream = async_memory_resource<MemoryKind> >
37+
class fixed_order_resource final : public memory_resource<MemoryKind> {
38+
public:
39+
fixed_order_resource() = default;
40+
41+
fixed_order_resource(Upstream *upstream, AccessOrder order)
42+
: upstream_(upstream), alloc_order_(order), dealloc_order_(order) {}
43+
44+
fixed_order_resource(Upstream *upstream, AccessOrder alloc_order, AccessOrder dealloc_order)
45+
: upstream_(upstream), alloc_order_(alloc_order), dealloc_order_(dealloc_order) {}
46+
47+
AccessOrder alloc_order() const { return alloc_order_; }
48+
AccessOrder dealloc_order() const { return dealloc_order_; }
49+
50+
private:
51+
void *do_allocate(size_t size, size_t alignment) final {
52+
if (alloc_order_.is_device())
53+
return upstream_->allocate_async(size, alignment, alloc_order_.stream());
54+
else
55+
return upstream_->allocate(size, alignment);
56+
}
57+
58+
void do_deallocate(void *ptr, size_t size, size_t alignment) final {
59+
if (dealloc_order_.is_device())
60+
upstream_->deallocate_async(ptr, size, alignment, dealloc_order_.stream());
61+
else
62+
upstream_->deallocate(ptr, size, alignment);
63+
}
64+
65+
Upstream *upstream_ = nullptr;
66+
AccessOrder alloc_order_, dealloc_order_;
67+
};
68+
69+
} // namespace mm
70+
} // namespace dali
71+
72+
#endif // DALI_CORE_MM_FIXED_ORDER_RESOURCE_H_
73+

include/dali/core/mm/monotonic_resource.h

-30
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,6 @@ class monotonic_memory_resource<Kind, Upstream, true>
138138
next_block_size_ = first_block_size_;
139139
}
140140

141-
142-
/**
143-
* @brief Releases, in stream order, all memory blocks that were taken from upstream
144-
*/
145-
void free_all_async(stream_view stream) {
146-
while (curr_block_) {
147-
assert(curr_block_->sentinel == sentinel_value && "Memory corruption detected");
148-
auto *prev = curr_block_->prev;
149-
auto *base = reinterpret_cast<char*>(curr_block_) - curr_block_->usable_size;
150-
size_t alloc_size = curr_block_->usable_size + sizeof(block_info);
151-
auto &up = dynamic_cast<async_memory_resource<Kind>&>(*upstream_);
152-
up.deallocate_async(base, alloc_size, curr_block_->alignment, stream);
153-
curr_block_ = prev;
154-
}
155-
next_block_size_ = first_block_size_;
156-
}
157-
158141
private:
159142
void *do_allocate(size_t bytes, size_t alignment) override {
160143
char *ret = detail::align_ptr(curr_, alignment);
@@ -258,19 +241,6 @@ class monotonic_memory_resource<Kind, Upstream, false>
258241
next_block_size_ = first_block_size_;
259242
}
260243

261-
/**
262-
* @brief Releases, in stream order, all memory blocks that were taken from upstream
263-
*/
264-
void free_all_async(stream_view stream) {
265-
for (int i = blocks_.size() - 1; i >= 0; i--) {
266-
auto &blk = blocks_[i];
267-
auto &up = dynamic_cast<async_memory_resource<Kind>&>(*upstream_);
268-
up.deallocate_async(blk.base, blk.size, blk.alignment, stream);
269-
}
270-
blocks_.clear();
271-
next_block_size_ = first_block_size_;
272-
}
273-
274244
Upstream *get_upstream() const { return upstream_; }
275245

276246

0 commit comments

Comments
 (0)