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

Add size function to alpaka::Vec #2416

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions include/alpaka/vec/Vec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ namespace alpaka
return m_data[Dim::value - 4];
}

//! Function returns the size of the static array
//! \return The size
ALPAKA_FN_HOST_ACC static constexpr decltype(auto) size()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please put an explicit return type.
I understand that the compiler can guess what it is, but the users shouldn't have to try and guess.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please copy the function signature from my PR #2415
This is the state of the art definition for C++20.

{
return Dim::value;
}

//! @}

//! Value reference accessor at the given non-unsigned integer index.
Expand Down
23 changes: 23 additions & 0 deletions test/unit/vec/src/VecTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,29 @@ TEST_CASE("basicVecTraits", "[vec]")
STATIC_REQUIRE(Vec{1, 2, 3}.front() == 1); // non-const overload
STATIC_REQUIRE(Vec{1, 2, 3}.back() == 3); // non-const overload
}
{
constexpr alpaka::Vec<Dim, Idx> vec4(static_cast<Idx>(47u), static_cast<Idx>(8u), static_cast<Idx>(3u));
// compile time tests
STATIC_REQUIRE(vec4.size() == 3);
STATIC_REQUIRE((alpaka::Vec{4, 8, 3}).size() == 3);
STATIC_REQUIRE((alpaka::Vec{4, 8}).size() == 2);
STATIC_REQUIRE((alpaka::Vec{4}).size() == 1);
STATIC_REQUIRE(decltype(vec4)::size() == 3);
STATIC_REQUIRE(decltype(alpaka::Vec{4, 8, 3})::size() == 3);

using Vec3DType = alpaka::Vec<Dim, Idx>;
STATIC_REQUIRE(Vec3DType::size() == 3);

constexpr alpaka::Vec vec5{8, 3};
STATIC_REQUIRE(vec5.size() == 2);

// runtime tests
REQUIRE(vec4.size() == 3);
REQUIRE((alpaka::Vec{4, 8, 3}).size() == 3);
REQUIRE((alpaka::Vec{4, 8}).size() == 2);
REQUIRE((alpaka::Vec{4}).size() == 1);
REQUIRE(vec5.size() == 2);
}
}

template<typename TDim, typename TIdx>
Expand Down
Loading