Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new type-erased memory resources #2824

Open
wants to merge 59 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
06715ac
`basic_any`: a utility for defining type-erasing wrappers in terms of…
ericniebler Oct 17, 2024
a278c34
[WIP] re-re-reimplement type-erased memory resources
ericniebler Nov 15, 2024
486897e
simplify `__property_result_t` alias for older compilers
ericniebler Nov 15, 2024
308d6ac
disambiguate `basic_any<I&>` and `basic_any<__ireference<I>>` for old…
ericniebler Nov 15, 2024
53696f3
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 15, 2024
7ce5ea5
mark function arguments as potentially unused
ericniebler Nov 15, 2024
59c9fb0
fix friend declaration in C++20+ code
ericniebler Nov 15, 2024
e61bc58
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 15, 2024
ae04ac1
work around weird nvcc concepts bug
ericniebler Nov 16, 2024
0f453ab
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 16, 2024
20798d2
Merge remote-tracking branch 'origin/main' into cudax-any-resource-redux
ericniebler Nov 16, 2024
425f7cf
work around concepts bug in nvcc
ericniebler Nov 17, 2024
5d22307
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 17, 2024
4efe709
help nvc++ with void return after `if constexpr`
ericniebler Nov 17, 2024
8f8b0f3
workarounds for nvc++ and msvc bugs
ericniebler Nov 17, 2024
81fa967
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 17, 2024
72bd4e7
break `basic_any` into separate files, grouped logically
ericniebler Nov 17, 2024
6c8e651
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 17, 2024
36fd535
add missing header for `uint8_t`
ericniebler Nov 17, 2024
a2b32d0
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 17, 2024
201948c
add some missing includes
ericniebler Nov 17, 2024
fb8bc39
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 17, 2024
73ff1fe
use existing work-around for known nvc++ bug with constexpr function …
ericniebler Nov 18, 2024
2d6315f
IWYU export all headers from `basic_any.cuh`
ericniebler Nov 18, 2024
8c58244
naming and doxygen comments
ericniebler Nov 18, 2024
6ab3361
more Doxygen comments
ericniebler Nov 18, 2024
3a51c21
Merge remote-tracking branch 'origin/main' into cudax-any-resource-redux
ericniebler Nov 18, 2024
55af1ee
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 18, 2024
5fc39dc
replace `cudax::any_resource` and provide `cudax::resource_ref`
ericniebler Nov 18, 2024
bf65ccd
make `basic_any` host-only for now
ericniebler Nov 19, 2024
60e09ff
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 19, 2024
5e442f2
fixing some bugs
ericniebler Nov 19, 2024
4a33f34
fix another bug
ericniebler Nov 19, 2024
2cc7d2b
explicitly initialize basic_any ireference base
ericniebler Nov 19, 2024
2ca835b
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 19, 2024
1225795
bug fixing
ericniebler Nov 20, 2024
f17ea03
uncomment necessary requires clause
ericniebler Nov 20, 2024
569dea3
molify gcc
ericniebler Nov 20, 2024
0da06e6
turn on MSVC empty bases for `resource_ref`
ericniebler Nov 20, 2024
de83907
`interface` is a macro on windows (wtf?)
ericniebler Nov 21, 2024
748cd35
Merge remote-tracking branch 'origin/main' into cudax-any-resource-redux
ericniebler Nov 21, 2024
466e8be
Merge remote-tracking branch 'origin/main' into cudax-basic-any
ericniebler Nov 21, 2024
db5b797
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 21, 2024
85c002b
undef `interface`
ericniebler Nov 21, 2024
23fda7d
Merge remote-tracking branch 'origin/main' into cudax-basic-any
ericniebler Nov 21, 2024
0dad283
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 21, 2024
4e2282e
Merge remote-tracking branch 'origin/main' into cudax-basic-any
ericniebler Nov 22, 2024
874f344
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 22, 2024
dae30fa
Merge remote-tracking branch 'origin/main' into cudax-any-resource-redux
ericniebler Nov 25, 2024
8443c6f
more doxygen comments
ericniebler Nov 25, 2024
f9bb949
add missing `noexcept` to some member functions
ericniebler Nov 25, 2024
ab130e0
Merge remote-tracking branch 'origin/main' into cudax-basic-any
ericniebler Nov 26, 2024
98d5e9c
use trailing return types
ericniebler Nov 26, 2024
4b2aba4
change doxygen comment type
ericniebler Nov 26, 2024
c47e037
include public cstddef and cstdint headers directly
ericniebler Nov 26, 2024
c1f014b
comments on #else and #endif lines
ericniebler Nov 26, 2024
1dfa55b
Merge branch 'cudax-basic-any' into cudax-any-resource-redux
ericniebler Nov 26, 2024
82865b0
make `cudax::any_async_resource` convertible to `cuda::mr::async_reso…
ericniebler Nov 26, 2024
ac29d83
fix bad doc comment that was breaking sphinx
ericniebler Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cudax/include/cuda/experimental/__detail/config.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

namespace cuda::experimental
{
namespace __cudax = ::cuda::experimental; // NOLINT: misc-unused-alias-decls
}

#endif // __CUDAX__DETAIL_CONFIG_CUH
17 changes: 15 additions & 2 deletions cudax/include/cuda/experimental/__detail/utility.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
# pragma system_header
#endif // no system header

#include <cuda/std/__concepts/__concept_macros.h>

namespace cuda::experimental
{
namespace detail
Expand All @@ -30,8 +32,8 @@ struct [[maybe_unused]] __ignore
{
__ignore() = default;

template <typename _Arg>
_CCCL_HOST_DEVICE constexpr __ignore(_Arg&&) noexcept
template <typename... _Args>
_CCCL_HOST_DEVICE constexpr __ignore(_Args&&...) noexcept
{}
};

Expand All @@ -42,6 +44,17 @@ struct __immovable
__immovable(__immovable&&) = delete;
__immovable& operator=(__immovable&&) = delete;
};

template <class... _Types>
struct _LIBCUDACXX_DECLSPEC_EMPTY_BASES __inherit : _Types...
{};

template <class _Type, template <class...> class _Template>
inline constexpr bool __is_specialization_of = false;

template <template <class...> class _Template, class... _Args>
inline constexpr bool __is_specialization_of<_Template<_Args...>, _Template> = true;

} // namespace detail

struct uninit_t
Expand Down
188 changes: 188 additions & 0 deletions cudax/include/cuda/experimental/__memory_resource/any_resource2.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
//===----------------------------------------------------------------------===//
//
// Part of CUDA Experimental in CUDA C++ Core Libraries,
// under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#ifndef _CUDAX__MEMORY_RESOURCE_ANY_RESOURCE2_H
#define _CUDAX__MEMORY_RESOURCE_ANY_RESOURCE2_H

#include <cuda/std/detail/__config>

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header

// If the memory resource header was included without the experimental flag,
// tell the user to define the experimental flag.
#if defined(_CUDA_MEMORY_RESOURCE) && !defined(LIBCUDACXX_ENABLE_EXPERIMENTAL_MEMORY_RESOURCE)
# error "To use the experimental memory resource, define LIBCUDACXX_ENABLE_EXPERIMENTAL_MEMORY_RESOURCE"
#endif

// cuda::mr is unavable on MSVC 2017
#if defined(_CCCL_COMPILER_MSVC_2017)
# error "The any_resource header is not supported on MSVC 2017"
#endif

#if !defined(LIBCUDACXX_ENABLE_EXPERIMENTAL_MEMORY_RESOURCE)
# define LIBCUDACXX_ENABLE_EXPERIMENTAL_MEMORY_RESOURCE
#endif

#include <cuda/__memory_resource/get_property.h>
#include <cuda/__memory_resource/properties.h>
#include <cuda/__memory_resource/resource.h>
#include <cuda/std/__concepts/__concept_macros.h>
#include <cuda/std/__utility/forward.h>
#include <cuda/std/optional>

#include <cuda/experimental/__utility/basic_any.cuh>

namespace cuda::experimental::mr
{
using __property_value_or_void _CCCL_NODEBUG_ALIAS = //
_CUDA_VSTD::__type_try_catch< //
_CUDA_VSTD::__type_indirect_quote<__property_value_t>, // try to get the property value
_CUDA_VSTD::__type_always<void>>; // if it fails, return void

template <class _Property>
using __property_result_t _CCCL_NODEBUG_ALIAS = _CUDA_VSTD::__type_call1<__property_value_or_void, _Property>;

template <class _Property>
struct __with_property
{
template <class _Ty>
_CUDAX_PUBLIC_API static auto __get_property(const _Ty& __obj, _Property __prop) noexcept //
-> __property_result_t<_Property>
{
static_assert(noexcept(get_property(__obj, __prop)));
if constexpr (!_CUDA_VSTD::is_same_v<__property_result_t<_Property>, void>)
{
return get_property(__obj, __prop);
}
ericniebler marked this conversation as resolved.
Show resolved Hide resolved
}

template <class...>
struct __iproperty : interface<__iproperty>
{
_CUDAX_API friend auto get_property(const __iproperty& __obj, _Property __prop) noexcept //
-> __property_result_t<_Property>
{
if constexpr (!_CUDA_VSTD::is_same_v<__property_result_t<_Property>, void>)
{
return __cudax::virtcall<&__get_property<__iproperty>>(&__obj, __prop);
}
ericniebler marked this conversation as resolved.
Show resolved Hide resolved
}

template <class _Ty>
using overrides _CCCL_NODEBUG_ALIAS = overrides_for<_Ty, &__get_property<_Ty>>;
};
};

template <class _Property>
using __iproperty = typename __with_property<_Property>::template __iproperty<>;

template <class... _Properties>
using __iproperty_set = iset<__iproperty<_Properties>...>;

template <class...>
struct __ibasic_resource : interface<__ibasic_resource, extends<icopyable<>, iequality_comparable<>>>
ericniebler marked this conversation as resolved.
Show resolved Hide resolved
{
_CUDAX_PUBLIC_API void* allocate(size_t __bytes, size_t __alignment)
{
return __cudax::virtcall<&__ibasic_resource::allocate>(this, __bytes, __alignment);
}

_CUDAX_PUBLIC_API void deallocate(void* __pv, size_t __bytes, size_t __alignment)
{
return __cudax::virtcall<&__ibasic_resource::deallocate>(this, __pv, __bytes, __alignment);
}

_CUDAX_PUBLIC_API void* allocate_async(size_t __bytes, size_t __alignment, ::cuda::stream_ref __stream)
{
return __cudax::virtcall<&__ibasic_resource::allocate_async>(this, __bytes, __alignment, __stream);
}

_CUDAX_PUBLIC_API void deallocate_async(void* __pv, size_t __bytes, size_t __alignment, ::cuda::stream_ref __stream)
{
return __cudax::virtcall<&__ibasic_resource::deallocate_async>(this, __pv, __bytes, __alignment, __stream);
}

template <class _Ty>
using overrides _CCCL_NODEBUG_ALIAS =
overrides_for<_Ty, &_Ty::allocate, &_Ty::deallocate, &_Ty::allocate_async, &_Ty::deallocate_async>;
};

template <class... _Properties>
using __iresource _CCCL_NODEBUG_ALIAS = iset<__ibasic_resource<>, __iproperty_set<_Properties...>>;

template <class _Property>
using __try_property_result_t =
_CUDA_VSTD::conditional_t<!_CUDA_VSTD::is_same_v<__property_result_t<_Property>, void>, //
_CUDA_VSTD::optional<__property_result_t<_Property>>, //
bool>;

template <class _Derived>
struct __with_try_get_property
{
template <class _Property>
_CUDAX_HOST_API _CCCL_NODISCARD_FRIEND auto
try_get_property(const _Derived& __self, _Property) noexcept -> __try_property_result_t<_Property>
{
auto __prop = __cudax::dynamic_any_cast<const __iproperty<_Property>*>(&__self);
if constexpr (_CUDA_VSTD::is_same_v<__property_result_t<_Property>, void>)
{
return __prop != nullptr;
}
else if (__prop)
{
return get_property(*__prop, _Property{});
}
else
{
return _CUDA_VSTD::nullopt;
}
}
};

template <class... _Properties>
struct _LIBCUDACXX_DECLSPEC_EMPTY_BASES any_resource2
: basic_any<__iresource<_Properties...>>
, __with_try_get_property<any_resource2<_Properties...>>
{
using any_resource2::basic_any::basic_any;

private:
using any_resource2::basic_any::interface;
};

template <class... _Properties>
struct _LIBCUDACXX_DECLSPEC_EMPTY_BASES resource_ref2
: basic_any<__iresource<_Properties...>&>
, __with_try_get_property<resource_ref2<_Properties...>>
{
using resource_ref2::basic_any::basic_any;

private:
using resource_ref2::basic_any::interface;
};

template <class _Resource, class... _Properties, class... _Args>
auto make_any_resource2(_Args&&... __args) -> any_resource2<_Properties...>
{
static_assert(_CUDA_VMR::resource<_Resource>, "_Resource does not satisfy the cuda::mr::resource concept");
static_assert(_CUDA_VMR::resource_with<_Resource, _Properties...>,
"Resource does not satisfy the required properties");
return any_resource2<_Properties...>{_CUDA_VSTD::in_place_type<_Resource>, _CUDA_VSTD::forward<_Args>(__args)...};
}

} // namespace cuda::experimental::mr

#endif // _CUDAX__MEMORY_RESOURCE_ANY_RESOURCE2_H
Loading
Loading