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

Host/Device accessors for mdspan #3686

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b115039
first draft
fbusato Feb 5, 2025
0ea452d
split implemenation
fbusato Feb 5, 2025
9ae4953
add host/device pointer checks
fbusato Feb 5, 2025
96f8419
add pointer checks
fbusato Feb 6, 2025
52b12a6
add tests
fbusato Feb 6, 2025
cab748e
fix device/host memory accessibility detection
fbusato Feb 6, 2025
31b84c7
fix typo
fbusato Feb 6, 2025
430e46d
add recursive traits
fbusato Feb 6, 2025
3c76d32
prevent ADL
fbusato Feb 6, 2025
d63980d
size_t namespace
fbusato Feb 6, 2025
e9ce8a4
is_pointer namespace
fbusato Feb 6, 2025
de4264c
fix __self
fbusato Feb 7, 2025
c0d12da
add noexcept
fbusato Feb 7, 2025
572447d
Update libcudacxx/include/cuda/__mdspan/host_device_accessor.h
fbusato Feb 8, 2025
0eb90ef
Update libcudacxx/include/cuda/__mdspan/host_device_accessor.h
fbusato Feb 8, 2025
71a9161
expose accessor types
fbusato Feb 8, 2025
921273a
remove default, copy constructors
fbusato Feb 8, 2025
aef9602
fix default and copy constructors
fbusato Feb 10, 2025
0647703
add inheritance aliases
fbusato Feb 10, 2025
c30482e
Merge branch 'main' into host-device-mdspan-accessors
fbusato Feb 10, 2025
fc82009
add default_accessor constructor and conversion
fbusato Feb 10, 2025
21e2feb
guard cuda_runtime_api.h
fbusato Feb 14, 2025
22fecf3
Merge branch 'main' into host-device-mdspan-accessors
fbusato Feb 14, 2025
ca47b44
Update libcudacxx/include/cuda/__mdspan/host_device_accessor.h
fbusato Feb 20, 2025
9a7e393
add detectably_invalid
fbusato Feb 20, 2025
a0af8f4
add __managed_accessor conversions from/to default_accessors
fbusato Feb 21, 2025
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
148 changes: 148 additions & 0 deletions libcudacxx/include/cuda/__mdspan/host_device_accessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//===----------------------------------------------------------------------===//
//
// Part of libcu++, the C++ Standard Library for your entire system,
// 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) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#ifndef _CUDA___MDSPAN_HOST_DEVICE_ACCESSOR
#define _CUDA___MDSPAN_HOST_DEVICE_ACCESSOR

#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

_LIBCUDACXX_BEGIN_NAMESPACE_CUDA

template <typename _Accessor>
struct host_accessor;

template <typename _Accessor>
struct device_accessor;

template <typename _Accessor>
struct managed_accessor;

/***********************************************************************************************************************
* Host/Device/Managed Accessor Traits
**********************************************************************************************************************/

template <typename T>
inline constexpr bool is_host_accessor_v = false;

template <typename T>
inline constexpr bool is_device_accessor_v = false;

template <typename T>
inline constexpr bool is_managed_accessor_v = false;

template <typename _Accessor>
inline constexpr bool is_host_accessor_v<host_accessor<_Accessor>> = true;

template <typename _Accessor>
inline constexpr bool is_device_accessor_v<device_accessor<_Accessor>> = true;

template <typename _Accessor>
inline constexpr bool is_managed_accessor_v<managed_accessor<_Accessor>> = true;

template <typename T>
inline constexpr bool is_host_device_managed_accessor_v =
is_host_accessor_v<T> || is_device_accessor_v<T> || is_managed_accessor_v<T>;

/***********************************************************************************************************************
* Host Accessor
**********************************************************************************************************************/

template <typename _Accessor>
struct host_accessor : public _Accessor
{
private:
static constexpr bool __is_ctor_noexcept = noexcept(_Accessor{});
static constexpr bool __is_copy_ctor_noexcept = noexcept(_Accessor{});

static_assert(!is_host_device_managed_accessor_v<_Accessor>, "Host/Device/Managed accessor cannot be nested");

public:
using offset_policy = host_accessor;

constexpr host_accessor() noexcept(__is_ctor_noexcept) = default;

constexpr host_accessor(const host_accessor&) noexcept(__is_copy_ctor_noexcept) = default;
};

/***********************************************************************************************************************
* Device Accessor
**********************************************************************************************************************/

template <typename _Accessor>
struct device_accessor : public _Accessor
{
private:
static constexpr bool __is_ctor_noexcept = noexcept(_Accessor{});
static constexpr bool __is_copy_ctor_noexcept = noexcept(_Accessor{});

static_assert(!is_host_device_managed_accessor_v<_Accessor>, "Host/Device/Managed accessor cannot be nested");

public:
using offset_policy = device_accessor;

constexpr device_accessor() noexcept(__is_ctor_noexcept) = default;

constexpr device_accessor(const device_accessor&) noexcept(__is_copy_ctor_noexcept) = default;
};

/***********************************************************************************************************************
* Managed Accessor
**********************************************************************************************************************/

template <typename _Accessor>
struct managed_accessor : public _Accessor
{
private:
static constexpr bool __is_ctor_noexcept = noexcept(_Accessor{});
static constexpr bool __is_copy_ctor_noexcept = noexcept(_Accessor{});

static_assert(!is_host_device_managed_accessor_v<_Accessor>, "Host/Device/Managed accessor cannot be nested");

public:
using offset_policy = managed_accessor;

constexpr managed_accessor() noexcept(__is_ctor_noexcept) = default;

constexpr managed_accessor(const managed_accessor&) noexcept(__is_copy_ctor_noexcept) = default;
};

/***********************************************************************************************************************
* Accessibility Traits
**********************************************************************************************************************/

template <typename T>
inline constexpr bool is_host_accessible_v = false;

template <typename T>
inline constexpr bool is_device_accessible_v = false;

template <typename _Accessor>
inline constexpr bool is_host_accessible_v<host_accessor<_Accessor>> = true;

template <typename _Accessor>
inline constexpr bool is_host_accessible_v<managed_accessor<_Accessor>> = true;

template <typename _Accessor>
inline constexpr bool is_device_accessible_v<device_accessor<_Accessor>> = true;

template <typename _Accessor>
inline constexpr bool is_device_accessible_v<managed_accessor<_Accessor>> = true;

_LIBCUDACXX_END_NAMESPACE_CUDA

#endif // _CUDA___MDSPAN_HOST_DEVICE_ACCESSOR
59 changes: 59 additions & 0 deletions libcudacxx/include/cuda/__mdspan/host_device_mdspan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// Part of libcu++, the C++ Standard Library for your entire system,
// 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) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#ifndef _CUDA___MDSPAN_HOST_DEVICE_MDSPAN
#define _CUDA___MDSPAN_HOST_DEVICE_MDSPAN

#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

#include <cuda/__mdspan/host_device_accessor.h>
#include <cuda/std/mdspan>

_LIBCUDACXX_BEGIN_NAMESPACE_CUDA

template <typename _ElementType,
typename _Extents,
typename _LayoutPolicy = _CUDA_VSTD::layout_right,
typename _AccessorPolicy = _CUDA_VSTD::default_accessor<_ElementType>>
using host_mdspan = _CUDA_VSTD::mdspan<_ElementType, _Extents, _LayoutPolicy, host_accessor<_AccessorPolicy>>;

template <typename _ElementType,
typename _Extents,
typename _LayoutPolicy = _CUDA_VSTD::layout_right,
typename _AccessorPolicy = _CUDA_VSTD::default_accessor<_ElementType>>
using device_mdspan = _CUDA_VSTD::mdspan<_ElementType, _Extents, _LayoutPolicy, device_accessor<_AccessorPolicy>>;

template <typename _ElementType,
typename _Extents,
typename _LayoutPolicy = _CUDA_VSTD::layout_right,
typename _AccessorPolicy = _CUDA_VSTD::default_accessor<_ElementType>>
using managed_mdspan = _CUDA_VSTD::mdspan<_ElementType, _Extents, _LayoutPolicy, managed_accessor<_AccessorPolicy>>;

/***********************************************************************************************************************
* Accessibility Traits
**********************************************************************************************************************/

template <typename _T, typename _E, typename _L, typename _A>
inline constexpr bool is_host_accessible_v<_CUDA_VSTD::mdspan<_T, _E, _L, _A>> = is_host_accessible_v<_A>;

template <typename _T, typename _E, typename _L, typename _A>
inline constexpr bool is_device_accessible_v<_CUDA_VSTD::mdspan<_T, _E, _L, _A>> = is_device_accessible_v<_A>;

_LIBCUDACXX_END_NAMESPACE_CUDA

#endif // _CUDA___MDSPAN_HOST_DEVICE_MDSPAN
Loading