MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
LocalRegion.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#include "LocalRegion.hpp"
6#include <algorithm>
7#include <numeric>
8#include "Exception.hpp"
9
11{
12
13 LocalRegion LocalRegion::sub(std::uint64_t offset, std::size_t length) const
14 {
15 if (offset + length > len)
16 {
17 throw Exception::invalidState("Tried to access out-of-bounds sub region of LocalRegion");
18 }
19
20 return LocalRegion{
21 .addr = addr + offset,
22 .len = length,
23 .desc = desc,
24 };
25 }
26
27 ::iovec LocalRegion::toIovec() const noexcept
28 {
29 return ::iovec{.iov_base = reinterpret_cast<void*>(addr), .iov_len = len}; // NOLINT(performance-no-int-to-ptr): No way to avoid this
30 }
31
32 ::iovec const* LocalRegionGroup::asIovec() const noexcept
33 {
34 return _iovs.data();
35 }
36
37 void* const* LocalRegionGroup::desc() const noexcept
38 {
39 return _descs.data();
40 }
41
42 std::vector<::iovec> LocalRegionGroup::iovFromGroup(std::vector<LocalRegion> group) noexcept
43 {
44 std::vector<::iovec> iovs;
45 std::ranges::transform(group, std::back_inserter(iovs), [](LocalRegion const& reg) { return reg.toIovec(); });
46 return iovs;
47 }
48
49 std::vector<void*> LocalRegionGroup::descFromGroup(std::vector<LocalRegion> group) noexcept
50 {
51 std::vector<void*> descs;
52 std::ranges::transform(group, std::back_inserter(descs), [](LocalRegion& reg) { return reg.desc; });
53 return descs;
54 }
55
56 LocalRegionGroupSpan LocalRegionGroup::span(std::size_t begin, std::size_t end) const
57 {
58 if (end < begin)
59 {
60 throw Exception::invalidArgument("end {} is smaller than begin {}", end, begin);
61 }
62
63 auto const spanLength = end - begin;
64 if (spanLength > _inner.size())
65 {
67 "requested span size {} will be bigger than the actual size of the full vector {}", spanLength, _inner.size());
68 }
69
71 std::span{_inner.cbegin() + begin, spanLength},
72 std::span{_iovs.cbegin() + begin, spanLength},
73 std::span{_descs.cbegin() + begin, spanLength},
74 };
75 }
76
77 LocalRegionGroupSpan::LocalRegionGroupSpan(std::span<LocalRegion const> region, std::span<::iovec const> iovec, std::span<void* const> desc)
78 : _inner(region)
79 , _iovec(iovec)
80 , _descs(desc)
81 {}
82
83 std::size_t LocalRegionGroupSpan::byteSize() const noexcept
84 {
85 return std::accumulate(_inner.begin(), _inner.end(), std::size_t{0}, [](std::size_t sum, auto const region) { return sum + region.len; });
86 }
87} // namespace mxl::lib::fabrics::ofi
static Exception invalidArgument(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_ARG exception.
Definition Exception.hpp:43
static Exception invalidState(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_STATE exception.
Definition Exception.hpp:59
Represent a span of a LocalRegionGroup.
void *const * desc() const noexcept
std::span< LocalRegion const > _inner
LocalRegionGroupSpan(std::span< LocalRegion const >, std::span<::iovec const >, std::span< void *const >)
std::size_t byteSize() const noexcept
Returns the sum of each region size in the span.
::iovec const * asIovec() const noexcept
Return the underlying array of iovec structures representing the memory region group.
void *const * desc() const noexcept
Return the underlying array of descriptors representing the memory region group.
LocalRegionGroupSpan span(std::size_t beginIndex, std::size_t endIndex) const
Return a span representing a contiguous subset of the LocalRegionGroup.
static std::vector< void * > descFromGroup(std::vector< LocalRegion > group) noexcept
Generate descriptor array from a group of LocalRegion.
static std::vector<::iovec > iovFromGroup(std::vector< LocalRegion > group) noexcept
Generate iovec array from a group of LocalRegion.
Represent a source memory region used for for data transfer.
LocalRegion sub(std::uint64_t offset, std::size_t length) const
Create a sub-region of this LocalRegion.
::iovec toIovec() const noexcept
Convert this LocalRegion to a struct iovec used by libfabric transfer functions.