|
1 | 1 | #include <stdexec/execution.hpp> |
2 | 2 | #include <test_common/catch2.hpp> |
3 | 3 |
|
| 4 | +#include "common.cuh" |
4 | 5 | #include "nvexec/stream_context.cuh" |
5 | 6 |
|
| 7 | +#include <memory_resource> |
| 8 | + |
6 | 9 | namespace |
7 | 10 | { |
| 11 | + class pinned_memory_resource_t : public std::pmr::memory_resource |
| 12 | + { |
| 13 | + void* do_allocate(std::size_t bytes, std::size_t) override |
| 14 | + { |
| 15 | + void* storage{}; |
| 16 | + STDEXEC_TRY_CUDA_API(cudaMallocHost(&storage, bytes)); |
| 17 | + return storage; |
| 18 | + } |
| 19 | + |
| 20 | + void do_deallocate(void* storage, std::size_t, std::size_t) override |
| 21 | + { |
| 22 | + STDEXEC_ASSERT_CUDA_API(cudaFreeHost(storage)); |
| 23 | + } |
| 24 | + |
| 25 | + auto do_is_equal(std::pmr::memory_resource const & other) const noexcept -> bool override |
| 26 | + { |
| 27 | + return this == &other; |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + class destruction_probe_t |
| 32 | + { |
| 33 | + flags_storage_t<>::flags_t flags_; |
| 34 | + bool owns_{true}; |
| 35 | + |
| 36 | + public: |
| 37 | + destruction_probe_t() = delete; |
| 38 | + destruction_probe_t(destruction_probe_t const &) = delete; |
| 39 | + auto operator=(destruction_probe_t const &) -> destruction_probe_t& = delete; |
| 40 | + auto operator=(destruction_probe_t&&) -> destruction_probe_t& = delete; |
| 41 | + |
| 42 | + __host__ __device__ explicit destruction_probe_t(flags_storage_t<>::flags_t flags) |
| 43 | + : flags_(flags) |
| 44 | + {} |
| 45 | + |
| 46 | + __host__ __device__ destruction_probe_t(destruction_probe_t&& other) |
| 47 | + : flags_(other.flags_) |
| 48 | + , owns_(other.owns_) |
| 49 | + { |
| 50 | + other.owns_ = false; |
| 51 | + } |
| 52 | + |
| 53 | + __host__ __device__ ~destruction_probe_t() |
| 54 | + { |
| 55 | + if (owns_) |
| 56 | + { |
| 57 | + flags_.set(); |
| 58 | + } |
| 59 | + } |
| 60 | + }; |
| 61 | + |
8 | 62 | TEST_CASE("continues on after just", "[cuda][stream][adaptors][continues_on]") |
9 | 63 | { |
10 | 64 | nvexec::stream_context ctx; |
@@ -43,4 +97,32 @@ namespace |
43 | 97 |
|
44 | 98 | REQUIRE(result.has_value()); |
45 | 99 | } |
| 100 | + |
| 101 | + TEST_CASE("continues_on destroys host-constructed storage after a CUDA error", |
| 102 | + "[cuda][stream][adaptors][continues_on]") |
| 103 | + { |
| 104 | + int device{}; |
| 105 | + STDEXEC_TRY_CUDA_API(cudaGetDevice(&device)); |
| 106 | + |
| 107 | + int concurrent_managed_access{}; |
| 108 | + STDEXEC_TRY_CUDA_API(cudaDeviceGetAttribute(&concurrent_managed_access, |
| 109 | + cudaDevAttrConcurrentManagedAccess, |
| 110 | + device)); |
| 111 | + if (!concurrent_managed_access) |
| 112 | + { |
| 113 | + SKIP("device does not support concurrent managed access"); |
| 114 | + } |
| 115 | + |
| 116 | + pinned_memory_resource_t pinned_memory; |
| 117 | + nvexec::stream_context ctx; |
| 118 | + auto scheduler = ctx.get_scheduler(); |
| 119 | + scheduler.ctx_.managed_resource_ = &pinned_memory; |
| 120 | + |
| 121 | + flags_storage_t<> destructions{}; |
| 122 | + auto sndr = STDEXEC::just(destruction_probe_t{destructions.get()}) |
| 123 | + | STDEXEC::continues_on(scheduler); |
| 124 | + |
| 125 | + REQUIRE_THROWS(STDEXEC::sync_wait(std::move(sndr))); |
| 126 | + REQUIRE(destructions.all_set_once()); |
| 127 | + } |
46 | 128 | } // namespace |
0 commit comments