-
Notifications
You must be signed in to change notification settings - Fork 205
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
Add dimensions description functionality to CUDA Experimental library #1743
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7809e23
Add dimensions description implementation back
pciolkosz a2103be
Enable cudax CI matrix
pciolkosz f7d5243
Merge branch 'NVIDIA:main' into hierarchy_dimensions
pciolkosz 380f39f
Update library name in license header
pciolkosz ca307fb
Fix extra spaces in the CI matrix
pciolkosz 36de036
[pre-commit.ci] auto code formatting
pre-commit-ci[bot] b9f7ed5
Fix unused variable warning in the examples test
pciolkosz 84e9a83
Fix MSVC issue with local variable hiding a global
pciolkosz f6e09b0
Apply review feedback
pciolkosz 4c1963e
Move local lambda to a function object
pciolkosz 357cb01
Rename detail directory to __hierarchy
pciolkosz 93eed09
Correct unreachable in a wrong place
pciolkosz 12e1096
Fix incomplete class member access error in clang
pciolkosz 38a9b8c
Fix copy paste issue in doxygen
pciolkosz c0011d1
Remove extended lambda from tests
pciolkosz 821f793
Proper casts on extents ops and tests improvements
pciolkosz 33701a9
Merge branch 'main' into pr/pciolkosz/1743
miscco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
cudax/include/cuda/experimental/__hierarchy/dimensions.cuh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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__HIERARCHY_DIMENSIONS | ||
#define _CUDAX__HIERARCHY_DIMENSIONS | ||
|
||
#include <cuda/std/mdspan> | ||
|
||
#if _CCCL_STD_VER >= 2017 | ||
namespace cuda::experimental | ||
{ | ||
|
||
template <typename T, size_t... Extents> | ||
using dimensions = ::cuda::std::extents<T, Extents...>; | ||
|
||
// not unsigned because of a bug in ::cuda::std::extents | ||
using dimensions_index_type = int; | ||
|
||
/** | ||
* @brief Type representing a result of a multi-dimensional hierarchy query. | ||
* | ||
* Returned from extents and index queries. | ||
* | ||
* @par Snippet | ||
* @code | ||
* #include <cudax/hierarchy_dimensions.cuh> | ||
* | ||
* template <typename Dimensions> | ||
* __global__ void kernel(Dimensions dims) | ||
* { | ||
* auto ext = dims.extents(); | ||
* | ||
* // Can be accessed like cuda::std::extents or like dim3 | ||
* assert(ext.extent(0) == expected); | ||
* assert(ext.x == expected); | ||
* | ||
* // Can be converted to dim3 | ||
* dim3 dimensions = ext; | ||
* } | ||
* @endcode | ||
* @par | ||
* | ||
* @tparam T | ||
* Type of the result for each dimension | ||
* | ||
* @tparam Extents | ||
* Extents of the result | ||
*/ | ||
template <typename T, size_t... Extents> | ||
struct hierarchy_query_result : public dimensions<T, Extents...> | ||
{ | ||
using Dims = dimensions<T, Extents...>; | ||
using Dims::Dims; | ||
_CCCL_HOST_DEVICE explicit constexpr hierarchy_query_result(const Dims& dims) | ||
: Dims(dims) | ||
{} | ||
static_assert(Dims::rank() > 0 && Dims::rank() <= 3); | ||
|
||
const T x = Dims::extent(0); | ||
const T y = Dims::rank() > 1 ? Dims::extent(1) : 1; | ||
const T z = Dims::rank() > 2 ? Dims::extent(2) : 1; | ||
|
||
_CCCL_HOST_DEVICE constexpr operator dim3() const | ||
{ | ||
return dim3(static_cast<uint32_t>(x), static_cast<uint32_t>(y), static_cast<uint32_t>(z)); | ||
} | ||
}; | ||
|
||
namespace detail | ||
{ | ||
template <typename OpType> | ||
_CCCL_NODISCARD _CCCL_HOST_DEVICE constexpr size_t merge_extents(size_t e1, size_t e2) | ||
{ | ||
if (e1 == ::cuda::std::dynamic_extent || e2 == ::cuda::std::dynamic_extent) | ||
{ | ||
return ::cuda::std::dynamic_extent; | ||
} | ||
else | ||
{ | ||
OpType op; | ||
return op(e1, e2); | ||
} | ||
} | ||
|
||
template <typename DstType, typename OpType, typename T1, size_t... Extents1, typename T2, size_t... Extents2> | ||
_CCCL_NODISCARD _CCCL_HOST_DEVICE constexpr auto | ||
dims_op(const OpType& op, const dimensions<T1, Extents1...>& h1, const dimensions<T2, Extents2...>& h2) noexcept | ||
{ | ||
// For now target only 3 dim extents | ||
static_assert(sizeof...(Extents1) == sizeof...(Extents2)); | ||
static_assert(sizeof...(Extents1) == 3); | ||
|
||
return dimensions<DstType, merge_extents<OpType>(Extents1, Extents2)...>( | ||
op(static_cast<DstType>(h1.extent(0)), h2.extent(0)), | ||
op(static_cast<DstType>(h1.extent(1)), h2.extent(1)), | ||
op(static_cast<DstType>(h1.extent(2)), h2.extent(2))); | ||
} | ||
|
||
template <typename DstType, typename T1, size_t... Extents1, typename T2, size_t... Extents2> | ||
_CCCL_NODISCARD _CCCL_HOST_DEVICE constexpr auto | ||
dims_product(const dimensions<T1, Extents1...>& h1, const dimensions<T2, Extents2...>& h2) noexcept | ||
{ | ||
return dims_op<DstType>(::cuda::std::multiplies(), h1, h2); | ||
} | ||
|
||
template <typename DstType, typename T1, size_t... Extents1, typename T2, size_t... Extents2> | ||
_CCCL_NODISCARD _CCCL_HOST_DEVICE constexpr auto | ||
dims_sum(const dimensions<T1, Extents1...>& h1, const dimensions<T2, Extents2...>& h2) noexcept | ||
{ | ||
return dims_op<DstType>(::cuda::std::plus(), h1, h2); | ||
} | ||
|
||
template <typename T, size_t... Extents> | ||
_CCCL_NODISCARD _CCCL_HOST_DEVICE constexpr auto convert_to_query_result(const dimensions<T, Extents...>& result) | ||
{ | ||
return hierarchy_query_result<T, Extents...>(result); | ||
} | ||
|
||
_CCCL_NODISCARD _CCCL_HOST_DEVICE constexpr auto dim3_to_dims(const dim3& dims) | ||
{ | ||
return dimensions<dimensions_index_type, | ||
::cuda::std::dynamic_extent, | ||
::cuda::std::dynamic_extent, | ||
::cuda::std::dynamic_extent>(dims.x, dims.y, dims.z); | ||
} | ||
|
||
template <typename TyTrunc, typename Index, typename Dims> | ||
_CCCL_NODISCARD _CCCL_HOST_DEVICE constexpr auto index_to_linear(const Index& index, const Dims& dims) | ||
{ | ||
static_assert(Dims::rank() == 3); | ||
|
||
return (static_cast<TyTrunc>(index.extent(2)) * dims.extent(1) + index.extent(1)) * dims.extent(0) + index.extent(0); | ||
} | ||
|
||
} // namespace detail | ||
} // namespace cuda::experimental | ||
#endif // _CCCL_STD_VER >= 2017 | ||
#endif // _CUDAX__HIERARCHY_DIMENSIONS |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that still valid? I would guess that
std: 'all'
will also test C++11 / C++14There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this seems wrong, but then why are there only c++17/20 jobs running for this PR?