|
| 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 | + |
0 commit comments