|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of CUDA Experimental in CUDA C++ Core Libraries, |
| 4 | +// under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. |
| 8 | +// |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#ifndef __CUDAX___EXECUTION_ENV_CUH |
| 12 | +#define __CUDAX___EXECUTION_ENV_CUH |
| 13 | + |
| 14 | +#include <cuda/std/detail/__config> |
| 15 | + |
| 16 | +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) |
| 17 | +# pragma GCC system_header |
| 18 | +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) |
| 19 | +# pragma clang system_header |
| 20 | +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) |
| 21 | +# pragma system_header |
| 22 | +#endif // no system header |
| 23 | + |
| 24 | +#include <cuda/__memory_resource/properties.h> |
| 25 | +#include <cuda/std/__type_traits/is_same.h> |
| 26 | +#include <cuda/std/__utility/forward.h> |
| 27 | +#include <cuda/std/__utility/move.h> |
| 28 | + |
| 29 | +#include <cuda/experimental/__async/sender/queries.cuh> |
| 30 | +#include <cuda/experimental/__execution/policy.cuh> |
| 31 | +#include <cuda/experimental/__memory_resource/any_resource.cuh> |
| 32 | +#include <cuda/experimental/__memory_resource/device_memory_resource.cuh> |
| 33 | +#include <cuda/experimental/__memory_resource/get_memory_resource.cuh> |
| 34 | +#include <cuda/experimental/__stream/get_stream.cuh> |
| 35 | +#include <cuda/experimental/__stream/stream_ref.cuh> |
| 36 | + |
| 37 | +namespace cuda::experimental |
| 38 | +{ |
| 39 | + |
| 40 | +template <class... _Properties> |
| 41 | +class env_t |
| 42 | +{ |
| 43 | +private: |
| 44 | + using __resource = any_async_resource<_Properties...>; |
| 45 | + using __stream_ref = stream_ref; |
| 46 | + |
| 47 | + __resource __mr_ = device_memory_resource{}; |
| 48 | + __stream_ref __stream_ = detail::__invalid_stream; |
| 49 | + execution::execution_policy __policy_ = execution::execution_policy::invalid_execution_policy; |
| 50 | + |
| 51 | +public: |
| 52 | + //! @brief Default constructs an environment using ``device_memory_resource`` as the resource the default stream |
| 53 | + //! ``execution_policy::invalid_execution_policy`` as the execution policy |
| 54 | + _CCCL_HIDE_FROM_ABI env_t() = default; |
| 55 | + |
| 56 | + //! @brief Construct an env_t from an any_resource, a stream and a policy |
| 57 | + //! @param __mr The any_resource passed in |
| 58 | + //! @param __stream The stream_ref passed in |
| 59 | + //! @param __policy The execution_policy passed in |
| 60 | + _CCCL_HIDE_FROM_ABI |
| 61 | + env_t(__resource __mr, |
| 62 | + __stream_ref __stream = detail::__invalid_stream, |
| 63 | + execution::execution_policy __policy = execution::execution_policy::invalid_execution_policy) noexcept |
| 64 | + : __mr_(_CUDA_VSTD::move(__mr)) |
| 65 | + , __stream_(__stream) |
| 66 | + , __policy_(__policy) |
| 67 | + {} |
| 68 | + |
| 69 | + //! @brief Checks whether another env is compatible with this one. That requires it to have queries for the three |
| 70 | + //! properties we need |
| 71 | + template <class _Env> |
| 72 | + static constexpr bool __is_compatible_env = |
| 73 | + __async::__queryable<_Env, get_memory_resource_t> && __async::__queryable<_Env, get_stream_t> |
| 74 | + && __async::__queryable<_Env, execution::get_execution_policy_t>; |
| 75 | + |
| 76 | + //! @brief Construct from an environment that has the right queries |
| 77 | + //! @param __env The environment we are querying for the required information |
| 78 | + _CCCL_TEMPLATE(class _Env) |
| 79 | + _CCCL_REQUIRES((!_CCCL_TRAIT(_CUDA_VSTD::is_same, _Env, env_t)) _CCCL_AND __is_compatible_env<_Env>) |
| 80 | + _CCCL_HIDE_FROM_ABI env_t(const _Env& __env) noexcept |
| 81 | + : __mr_(__env.query(get_memory_resource)) |
| 82 | + , __stream_(__env.query(get_stream)) |
| 83 | + , __policy_(__env.query(execution::get_execution_policy)) |
| 84 | + {} |
| 85 | + |
| 86 | + _CCCL_NODISCARD _CCCL_HIDE_FROM_ABI const __resource& query(get_memory_resource_t) const noexcept |
| 87 | + { |
| 88 | + return __mr_; |
| 89 | + } |
| 90 | + |
| 91 | + _CCCL_NODISCARD _CCCL_HIDE_FROM_ABI __stream_ref query(get_stream_t) const noexcept |
| 92 | + { |
| 93 | + return __stream_; |
| 94 | + } |
| 95 | + |
| 96 | + _CCCL_NODISCARD _CCCL_HIDE_FROM_ABI execution::execution_policy query(execution::get_execution_policy_t) const noexcept |
| 97 | + { |
| 98 | + return __policy_; |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +} // namespace cuda::experimental |
| 103 | + |
| 104 | +#endif //__CUDAX___EXECUTION_ENV_CUH |
0 commit comments