29concept TensorAccessState = std::move_constructible<State> && std::is_nothrow_move_constructible_v<State> &&
30 requires(State& state) {
31 { state.release() }
noexcept;
37struct immediate_mdspan_access_state
39 void release() noexcept {}
43template <ImmediateTensorView Tensor>
class borrowed_read_tensor_lease {
45 using tensor_type = std::remove_cvref_t<Tensor>;
46 using mdspan_type = std::remove_cvref_t<decltype(std::declval<tensor_type const&>().mdspan())>;
47 using storage_policy = tensor_storage_policy_t<tensor_type>;
48 using extents_type =
typename mdspan_type::extents_type;
49 using index_type =
typename mdspan_type::index_type;
51 explicit borrowed_read_tensor_lease(tensor_type
const& tensor) noexcept : tensor_(&tensor) {}
53 borrowed_read_tensor_lease(borrowed_read_tensor_lease
const&) =
delete;
54 borrowed_read_tensor_lease& operator=(borrowed_read_tensor_lease
const&) =
delete;
56 borrowed_read_tensor_lease(borrowed_read_tensor_lease&& other) noexcept
57 : tensor_(std::exchange(other.tensor_,
nullptr))
60 borrowed_read_tensor_lease& operator=(borrowed_read_tensor_lease&& other)
noexcept
62 if (
this != &other) tensor_ = std::exchange(other.tensor_,
nullptr);
67 [[nodiscard]]
decltype(
auto) mdspan()
const& {
return this->source().mdspan(); }
69 void mdspan() && =
delete;
72 [[nodiscard]]
decltype(
auto) backend_selector()
const {
return this->source().backend_selector(); }
75 [[nodiscard]]
decltype(
auto) storage()
const
76 requires requires(tensor_type
const& tensor) { tensor.storage(); }
78 return this->source().storage();
82 [[nodiscard]]
decltype(
auto) extents()
const {
return this->source().extents(); }
85 [[nodiscard]]
index_type extent(std::size_t axis)
const {
return this->source().extent(axis); }
88 [[nodiscard]]
static constexpr std::size_t rank() noexcept {
return mdspan_type::rank(); }
92 void release() noexcept { tensor_ =
nullptr; }
95 [[nodiscard]] tensor_type
const& source()
const
97 CHECK(tensor_ !=
nullptr,
"cannot use a released borrowed tensor lease");
101 tensor_type
const* tensor_ =
nullptr;
105template <MutableImmediateTensorView Tensor>
class borrowed_write_tensor_lease {
107 using tensor_type = std::remove_cvref_t<Tensor>;
108 using mdspan_type = std::remove_cvref_t<decltype(std::declval<tensor_type&>().mdspan())>;
109 using const_mdspan_type = std::remove_cvref_t<decltype(std::declval<tensor_type const&>().mdspan())>;
110 using storage_policy = tensor_storage_policy_t<tensor_type>;
111 using extents_type =
typename mdspan_type::extents_type;
112 using index_type =
typename mdspan_type::index_type;
114 explicit borrowed_write_tensor_lease(tensor_type& tensor) noexcept : tensor_(&tensor) {}
116 borrowed_write_tensor_lease(borrowed_write_tensor_lease
const&) =
delete;
117 borrowed_write_tensor_lease& operator=(borrowed_write_tensor_lease
const&) =
delete;
119 borrowed_write_tensor_lease(borrowed_write_tensor_lease&& other) noexcept
120 : tensor_(std::exchange(other.tensor_,
nullptr))
123 borrowed_write_tensor_lease& operator=(borrowed_write_tensor_lease&& other)
noexcept
125 if (
this != &other) tensor_ = std::exchange(other.tensor_,
nullptr);
130 [[nodiscard]]
decltype(
auto) mdspan() & {
return this->source().mdspan(); }
133 [[nodiscard]]
decltype(
auto) mdspan()
const& {
return std::as_const(this->source()).mdspan(); }
135 void mdspan() && =
delete;
138 [[nodiscard]]
decltype(
auto) backend_selector()
const {
return this->source().backend_selector(); }
141 [[nodiscard]]
decltype(
auto) extents()
const {
return this->source().extents(); }
144 [[nodiscard]]
index_type extent(std::size_t axis)
const {
return this->source().extent(axis); }
147 [[nodiscard]]
static constexpr std::size_t rank() noexcept {
return mdspan_type::rank(); }
151 void release() noexcept { tensor_ =
nullptr; }
154 [[nodiscard]] tensor_type& source()
const
156 CHECK(tensor_ !=
nullptr,
"cannot use a released borrowed tensor lease");
160 tensor_type* tensor_ =
nullptr;
171template <MdspanLike Mdspan, TensorAccessState AccessState>
172 requires std::is_const_v<typename Mdspan::element_type> && std::is_nothrow_move_constructible_v<Mdspan>
180 : state_(std::move(state)), mdspan_(std::move(
mdspan))
187 : state_(std::move(other.state_)), mdspan_(std::move(other.mdspan_))
189 other.mdspan_.reset();
194 requires(std::is_nothrow_move_assignable_v<access_state_type> && std::is_nothrow_move_assignable_v<mdspan_type>)
199 state_ = std::move(other.state_);
200 mdspan_ = std::move(other.mdspan_);
201 other.mdspan_.reset();
211 CHECK(mdspan_.has_value(),
"cannot use an inactive mdspan lease");
218 CHECK(mdspan_.has_value(),
"cannot use an inactive mdspan lease");
228 if (!mdspan_)
return;
235 std::optional<mdspan_type> mdspan_;
244template <MutableMdspanLike Mdspan, TensorAccessState AccessState>
245 requires std::is_nothrow_move_constructible_v<Mdspan>
253 : state_(std::move(state)), mdspan_(std::move(
mdspan))
260 : state_(std::move(other.state_)), mdspan_(std::move(other.mdspan_))
262 other.mdspan_.reset();
267 requires(std::is_nothrow_move_assignable_v<access_state_type> && std::is_nothrow_move_assignable_v<mdspan_type>)
272 state_ = std::move(other.state_);
273 mdspan_ = std::move(other.mdspan_);
274 other.mdspan_.reset();
284 CHECK(mdspan_.has_value(),
"cannot use an inactive mdspan lease");
291 CHECK(mdspan_.has_value(),
"cannot use an inactive mdspan lease");
301 if (!mdspan_)
return;
308 std::optional<mdspan_type> mdspan_;
320template <MdspanLike Mdspan, TensorAccessState AccessState,
class BackendSelector,
class StoragePolicy>
321 requires std::is_const_v<typename Mdspan::element_type> && std::is_nothrow_move_constructible_v<Mdspan> &&
322 std::is_nothrow_move_constructible_v<BackendSelector>
334 : state_(std::move(state)), mdspan_(std::move(
mdspan)), selector_(std::move(selector))
341 : state_(std::move(other.state_)), mdspan_(std::move(other.mdspan_)), selector_(std::move(other.selector_))
343 other.mdspan_.reset();
348 requires(std::is_nothrow_move_assignable_v<access_state_type> && std::is_nothrow_move_assignable_v<mdspan_type> &&
349 std::is_nothrow_move_assignable_v<backend_selector_type>)
354 state_ = std::move(other.state_);
355 mdspan_ = std::move(other.mdspan_);
356 selector_ = std::move(other.selector_);
357 other.mdspan_.reset();
367 CHECK(mdspan_.has_value(),
"cannot resolve mdspan through an inactive tensor lease");
374 CHECK(mdspan_.has_value(),
"cannot resolve mdspan through an inactive tensor lease");
387 return state_.storage();
397 [[nodiscard]]
static constexpr std::size_t
rank() noexcept {
return mdspan_type::rank(); }
404 if (!mdspan_)
return;
411 std::optional<mdspan_type> mdspan_;
424template <MutableMdspanLike MutableMdspan, MdspanLike ConstMdspan, TensorAccessState AccessState,
class BackendSelector,
426 requires std::is_const_v<typename ConstMdspan::element_type> &&
427 std::is_nothrow_move_constructible_v<MutableMdspan> &&
428 std::is_nothrow_move_constructible_v<ConstMdspan> &&
429 std::is_nothrow_move_constructible_v<BackendSelector>
443 : state_(std::move(state)), mdspan_(std::move(
mdspan)), const_mdspan_(std::move(const_mdspan)),
444 selector_(std::move(selector))
451 : state_(std::move(other.state_)), mdspan_(std::move(other.mdspan_)),
452 const_mdspan_(std::move(other.const_mdspan_)), selector_(std::move(other.selector_))
454 other.const_mdspan_.reset();
455 other.mdspan_.reset();
460 requires(std::is_nothrow_move_assignable_v<access_state_type> && std::is_nothrow_move_assignable_v<mdspan_type> &&
461 std::is_nothrow_move_assignable_v<const_mdspan_type> &&
462 std::is_nothrow_move_assignable_v<backend_selector_type>)
467 state_ = std::move(other.state_);
468 mdspan_ = std::move(other.mdspan_);
469 const_mdspan_ = std::move(other.const_mdspan_);
470 selector_ = std::move(other.selector_);
471 other.const_mdspan_.reset();
472 other.mdspan_.reset();
482 CHECK(mdspan_.has_value(),
"cannot resolve mdspan through an inactive tensor lease");
489 CHECK(const_mdspan_.has_value(),
"cannot resolve mdspan through an inactive tensor lease");
490 return *const_mdspan_;
505 [[nodiscard]]
static constexpr std::size_t
rank() noexcept {
return mdspan_type::rank(); }
512 if (!mdspan_)
return;
513 const_mdspan_.reset();
520 std::optional<mdspan_type> mdspan_;
521 std::optional<const_mdspan_type> const_mdspan_;
526template <
class Lease>
528 std::move_constructible<Lease> && (!std::copy_constructible<Lease>) &&
requires(Lease& lease) {
529 requires MdspanLike<
decltype(lease.mdspan())>;
530 requires std::is_const_v<
typename std::remove_cvref_t<
decltype(lease.mdspan())>::element_type>;
531 { lease.release() }
noexcept;
535template <
class Lease>
537 std::move_constructible<Lease> && (!std::copy_constructible<Lease>) &&
requires(Lease& lease) {
539 { lease.release() }
noexcept;
543template <
class Lease>
547template <
class Lease>
552template <
class Lease>
556template <
class Lease>
563template <
class Lease>
565 (!std::copy_constructible<Lease>) &&
requires(Lease& lease) {
566 { lease.release() }
noexcept;
570template <
class Lease>
574template <
class Lease>
578template <
class Lease>
583template <
class Lease>
587template <
class Lease>
614template <HostAccessibleMdspan Mdspan>
615 requires std::is_const_v<typename std::remove_cvref_t<Mdspan>::element_type>
618 using mdspan_type = std::remove_cvref_t<Mdspan>;
624template <MutableMdspanLike Mdspan>
625 requires HostAccessibleMdspan<Mdspan>
628 using mdspan_type = std::remove_cvref_t<Mdspan>;
634template <ImmediateTensorView Tensor>
635 requires HostAccessibleMdspan<immediate_tensor_mdspan_t<Tensor>>
638 using tensor_type = std::remove_cvref_t<Tensor>;
639 return detail::borrowed_read_tensor_lease<tensor_type>{tensor};
643template <MutableImmediateTensorView Tensor>
644 requires HostAccessibleMdspan<mutable_immediate_tensor_mdspan_t<Tensor>>
647 using tensor_type = std::remove_cvref_t<Tensor>;
648 return detail::borrowed_write_tensor_lease<tensor_type>{tensor};
652template <ImmediateTensorView Tensor>
653 requires HostAccessibleMdspan<immediate_tensor_mdspan_t<Tensor>>
660template <MutableImmediateTensorView Tensor>
661 requires HostAccessibleMdspan<mutable_immediate_tensor_mdspan_t<Tensor>>
669template <
class Mdspec>
672template <
class Mdspec>
677template <
class Mdspec>
679 std::remove_cvref_t<
decltype(std::declval<detail::host_read_mdspan_lease_t<Mdspec>&>().mdspan())>;
682template <
class Mdspec>
684 std::remove_cvref_t<
decltype(std::declval<detail::host_write_mdspan_lease_t<Mdspec>&>().mdspan())>;
687template <
class Mdspec>
693template <
class Mdspec>
700template <
class Tensor>
703template <
class Tensor>
708template <
class Tensor>
710 std::remove_cvref_t<
decltype(std::declval<detail::host_read_tensor_lease_t<Tensor>&>().mdspan())>;
713template <
class Tensor>
715 std::remove_cvref_t<
decltype(std::declval<detail::host_write_tensor_lease_t<Tensor>&>().mdspan())>;
718template <
class Tensor>
724template <
class Tensor>
General-purpose owning tensor with immediate or deferred mdspan metadata.
Definition basic_tensor.hpp:109
Move-only RAII lease exposing one resolved read-only mdspan.
Definition access.hpp:173
mdspan_type mdspan() &&=delete
Mdspan mdspan_type
Definition access.hpp:175
mdspan_type & mdspan() &
Return the resolved read-only mdspan.
Definition access.hpp:209
read_mdspan_lease(access_state_type state, mdspan_type mdspan) noexcept
Construct an active read lease.
Definition access.hpp:179
read_mdspan_lease(read_mdspan_lease const &)=delete
read_mdspan_lease & operator=(read_mdspan_lease &&other) noexcept
Definition access.hpp:193
~read_mdspan_lease()
Definition access.hpp:206
read_mdspan_lease & operator=(read_mdspan_lease const &)=delete
mdspan_type const & mdspan() const &
Return the resolved read-only mdspan.
Definition access.hpp:216
void release() noexcept
End the access scope.
Definition access.hpp:226
AccessState access_state_type
Definition access.hpp:176
read_mdspan_lease(read_mdspan_lease &&other) noexcept
Definition access.hpp:186
Move-only RAII lease exposing a read-only ImmediateTensorView.
Definition access.hpp:323
~read_tensor_lease()
Definition access.hpp:362
extents_type const & extents() const
Return the resolved tensor extents.
Definition access.hpp:391
mdspan_type & mdspan() &
Return the resolved read-only mdspan.
Definition access.hpp:365
read_tensor_lease & operator=(read_tensor_lease const &)=delete
index_type extent(std::size_t axis) const
Return one resolved tensor extent.
Definition access.hpp:394
read_tensor_lease(read_tensor_lease &&other) noexcept
Definition access.hpp:340
Mdspan mdspan_type
Definition access.hpp:325
read_tensor_lease & operator=(read_tensor_lease &&other) noexcept
Definition access.hpp:347
mdspan_type const & mdspan() const &
Return the resolved read-only mdspan.
Definition access.hpp:372
backend_selector_type const & backend_selector() const noexcept
Return the backend selector retained from the source tensor view.
Definition access.hpp:381
static constexpr std::size_t rank() noexcept
Return the static tensor rank.
Definition access.hpp:397
mdspan_type mdspan() &&=delete
typename mdspan_type::extents_type extents_type
Definition access.hpp:329
AccessState access_state_type
Definition access.hpp:326
typename mdspan_type::index_type index_type
Definition access.hpp:330
void release() noexcept
End the access scope and make this lease inactive.
Definition access.hpp:402
read_tensor_lease(read_tensor_lease const &)=delete
StoragePolicy storage_policy
Definition access.hpp:328
BackendSelector backend_selector_type
Definition access.hpp:327
read_tensor_lease(access_state_type state, mdspan_type mdspan, backend_selector_type selector) noexcept
Construct an active read lease.
Definition access.hpp:333
decltype(auto) storage() const
Return the storage retained by access state when it exposes one.
Definition access.hpp:384
Always-ready awaitable that transfers one access lease.
Definition access.hpp:592
ready_access(ready_access &&)=default
ready_access & operator=(ready_access const &)=delete
bool await_ready() const noexcept
Definition access.hpp:603
ready_access(lease_type lease)
Definition access.hpp:596
ready_access(ready_access const &)=delete
ready_access & operator=(ready_access &&)=default
void await_suspend(Task &&) const noexcept
Definition access.hpp:605
lease_type await_resume()
Definition access.hpp:607
Lease lease_type
Definition access.hpp:594
Move-only RAII lease exposing one resolved writable mdspan.
Definition access.hpp:246
write_mdspan_lease(write_mdspan_lease &&other) noexcept
Definition access.hpp:259
write_mdspan_lease(access_state_type state, mdspan_type mdspan) noexcept
Construct an active write lease.
Definition access.hpp:252
mdspan_type & mdspan() &
Return the resolved writable mdspan.
Definition access.hpp:282
mdspan_type mdspan() &&=delete
write_mdspan_lease(write_mdspan_lease const &)=delete
~write_mdspan_lease()
Definition access.hpp:279
void release() noexcept
End the access scope.
Definition access.hpp:299
mdspan_type const & mdspan() const &
Return the resolved writable mdspan without changing lease ownership.
Definition access.hpp:289
Mdspan mdspan_type
Definition access.hpp:248
write_mdspan_lease & operator=(write_mdspan_lease &&other) noexcept
Definition access.hpp:266
AccessState access_state_type
Definition access.hpp:249
write_mdspan_lease & operator=(write_mdspan_lease const &)=delete
Move-only RAII lease exposing mutable and const immediate TensorViews.
Definition access.hpp:430
static constexpr std::size_t rank() noexcept
Return the static tensor rank.
Definition access.hpp:505
BackendSelector backend_selector_type
Definition access.hpp:435
MutableMdspan mdspan_type
Definition access.hpp:432
write_tensor_lease & operator=(write_tensor_lease const &)=delete
write_tensor_lease(write_tensor_lease &&other) noexcept
Definition access.hpp:450
extents_type const & extents() const
Return the resolved tensor extents.
Definition access.hpp:499
mdspan_type mdspan() &&=delete
const_mdspan_type const & mdspan() const &
Return the resolved read-only mdspan.
Definition access.hpp:487
ConstMdspan const_mdspan_type
Definition access.hpp:433
index_type extent(std::size_t axis) const
Return one resolved tensor extent.
Definition access.hpp:502
AccessState access_state_type
Definition access.hpp:434
write_tensor_lease(write_tensor_lease const &)=delete
typename mdspan_type::extents_type extents_type
Definition access.hpp:437
backend_selector_type const & backend_selector() const noexcept
Return the backend selector retained from the source tensor view.
Definition access.hpp:496
write_tensor_lease & operator=(write_tensor_lease &&other) noexcept
Definition access.hpp:459
void release() noexcept
End the access scope and make this lease inactive.
Definition access.hpp:510
StoragePolicy storage_policy
Definition access.hpp:436
write_tensor_lease(access_state_type state, mdspan_type mdspan, const_mdspan_type const_mdspan, backend_selector_type selector) noexcept
Construct an active write lease.
Definition access.hpp:441
typename mdspan_type::index_type index_type
Definition access.hpp:438
mdspan_type & mdspan() &
Return the resolved writable mdspan.
Definition access.hpp:480
~write_tensor_lease()
Definition access.hpp:477
Mdspan whose accessor may be evaluated directly by CUDA device code.
Definition mdspan/concepts.hpp:317
Read mdspan lease whose resolved accessor is valid in CUDA code.
Definition access.hpp:553
Read lease whose resolved mdspan is directly CUDA-accessible.
Definition access.hpp:584
Write mdspan lease whose resolved accessor is valid in CUDA code.
Definition access.hpp:557
Write lease whose resolved mutable mdspan is directly CUDA-accessible.
Definition access.hpp:588
Mdspan whose accessor may be evaluated directly by host code.
Definition mdspan/concepts.hpp:311
Read mdspan lease whose resolved accessor is valid in host code.
Definition access.hpp:544
Read lease whose resolved mdspan is directly host-accessible.
Definition access.hpp:575
Mdspec metadata supporting host read acquisition.
Definition access.hpp:688
Tensor view supporting host read acquisition.
Definition access.hpp:719
Mutable mdspec metadata supporting host write acquisition.
Definition access.hpp:694
Mutable tensor view supporting host write acquisition.
Definition access.hpp:725
Write mdspan lease whose resolved accessor is valid in host code.
Definition access.hpp:548
Write lease whose resolved mutable mdspan is directly host-accessible.
Definition access.hpp:579
A readable type implementing the mdspan observers and multidimensional subscript contract.
Definition mdspan/concepts.hpp:291
Mdspan metadata whose data handle is immediate or available through a data descriptor.
Definition mdspan/concepts.hpp:304
MdspanLike types whose accessor supports indexed assignment.
Definition mdspan/concepts.hpp:387
MdspecLike types whose eventual storage supports writes.
Definition mdspan/concepts.hpp:398
TensorView whose writable mdspec supports eventual element assignment.
Definition tensor/concepts.hpp:133
Move-only lease exposing a resolved read-only mdspan.
Definition access.hpp:527
ImmediateTensorView that owns a move-only read access lifetime.
Definition access.hpp:564
Movable state retaining one acquired tensor data-handle lifetime.
Definition access.hpp:29
Tensor-level object exposing readable multidimensional metadata and a backend selector.
Definition tensor/concepts.hpp:82
Move-only lease exposing a resolved writable mdspan.
Definition access.hpp:536
MutableImmediateTensorView that owns a move-only write access lifetime.
Definition access.hpp:571
ReadBuffer< T > && release(ReadBuffer< T > &in)
Adaptor for transferring ReadBuffer ownership into an await expression.
Definition buffers.hpp:227
Definition async/async.hpp:26
auto acquire_host_read_access_async(Tensor &tensor)
Return an immediately-ready host read acquisition.
Definition access.hpp:654
std::remove_cvref_t< decltype(std::declval< detail::host_read_mdspan_lease_t< Mdspec > & >().mdspan())> host_read_mdspan_t
Read-only mdspan type resolved by host descriptor acquisition.
Definition access.hpp:679
std::remove_cvref_t< decltype(std::declval< detail::host_read_tensor_lease_t< Tensor > & >().mdspan())> host_read_tensor_mdspan_t
Read-only mdspan type resolved by host tensor acquisition.
Definition access.hpp:710
auto acquire_host_read_access_sync(Mdspan const &mdspan)
Acquire a directly host-accessible read-only mdspan.
Definition access.hpp:616
std::remove_cvref_t< decltype(std::declval< detail::host_write_mdspan_lease_t< Mdspec > & >().mdspan())> host_write_mdspan_t
Writable mdspan type resolved by host descriptor acquisition.
Definition access.hpp:684
std::ptrdiff_t index_type
Definition types.hpp:19
Tensor(InputTensor const &) -> Tensor< tensor_element_t< InputTensor >, tensor_mdspec_t< InputTensor >::rank(), HostStorage, detail::materialized_layout_t< void, tensor_mdspec_t< InputTensor > >, DefaultAccessorFactory >
Deduce a runtime-extents host tensor that materializes a tensor view.
std::remove_cvref_t< decltype(std::declval< detail::host_write_tensor_lease_t< Tensor > & >().mdspan())> host_write_tensor_mdspan_t
Writable mdspan type resolved by host tensor acquisition.
Definition access.hpp:715
auto acquire_host_write_access_sync(Mdspan &mdspan)
Acquire a directly host-accessible writable mdspan.
Definition access.hpp:626
auto acquire_host_write_access_async(Tensor &tensor)
Return an immediately-ready host write acquisition.
Definition access.hpp:662
Concepts for tensor-level objects that resolve mdspan kernel operands.
#define CHECK(cond,...)
Definition trace.hpp:299