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 likely and unlikely attribute from C++20 to ref counting in views #6730

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

rgayatri23
Copy link
Contributor

@rgayatri23 rgayatri23 commented Jan 19, 2024

The PR is to address issue #6508.

The PR adds the following:

  • Macros to mark branches with likely and unlikely attribute from C++20 for reference counting of views.
  • A benchmark to understand the performance implications when using the attributes inside and outside a kernel.

@rgayatri23 rgayatri23 added Enhancement Improve existing capability; will potentially require voting InDevelop Enhancement, fix, etc. has been merged into the develop branch; labels Jan 19, 2024
@rgayatri23 rgayatri23 self-assigned this Jan 19, 2024
@masterleinad masterleinad marked this pull request as draft January 22, 2024 14:26
@cdaley
Copy link

cdaley commented Jan 23, 2024

Thank you for adding this draft PR. Is there a reason for the specific KOKKOS_COMPILER_CLANG macro around the attribute? The likelihood attributes are legal C++20 and should be available in any compliant compiler, so the C++ version check should suffice.

@rgayatri23
Copy link
Contributor Author

Thank you for adding this draft PR. Is there a reason for the specific KOKKOS_COMPILER_CLANG macro around the attribute? The likelihood attributes are legal C++20 and should be available in any compliant compiler, so the C++ version check should suffice.

That's right. Fixed it.

#if defined(KOKKOS_IMPL_REF_COUNT_BRANCH_LIKELY)
#define KOKKOS_BRANCH_PROB [[likely]]
#elif defined(KOKKOS_IMPL_REF_COUNT_BRANCH_UNLIKELY)
#define KOKKOS_BRANCH_PROB [[unlikely]]
Copy link

Choose a reason for hiding this comment

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

I also noticed that you need another else branch. This was not needed in the original code because it did not have a KOKKOS_ENABLE_CXX20 branch.

#else
#define KOKKOS_BRANCH_PROB

#define KOKKOS_BRANCH_PROB [[unlikely]]
#endif
#else
#define KOKKOS_BRANCH_PROB
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do have an else here. Do you mean something else @cdaley ?

@rgayatri23 rgayatri23 marked this pull request as ready for review January 29, 2024 07:39
Comment on lines 29 to 35
#if defined(KOKKOS_ENABLE_CXX20)
#if defined(KOKKOS_IMPL_REF_COUNT_BRANCH_LIKELY)
#define KOKKOS_BRANCH_PROB [[likely]]
#elif defined(KOKKOS_IMPL_REF_COUNT_BRANCH_UNLIKELY)
#define KOKKOS_BRANCH_PROB [[unlikely]]
#endif
#endif
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason why not just do this:

Suggested change
#if defined(KOKKOS_ENABLE_CXX20)
#if defined(KOKKOS_IMPL_REF_COUNT_BRANCH_LIKELY)
#define KOKKOS_BRANCH_PROB [[likely]]
#elif defined(KOKKOS_IMPL_REF_COUNT_BRANCH_UNLIKELY)
#define KOKKOS_BRANCH_PROB [[unlikely]]
#endif
#endif
#if defined(KOKKOS_ENABLE_CXX20)
#define KOKKOS_BRANCH_PROB [[unlikely]]
#endif

@crtrott
Copy link
Member

crtrott commented Feb 1, 2024

Basically either we do it or we don't. We could have an IMPL option to turn it off in case we get complaints, but there is no reason for us to switch back and forth. This will just cause issues down the road. Right now by default this is not on ever.

Copy link
Member

@crtrott crtrott left a comment

Choose a reason for hiding this comment

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

I don't think we should do this this way. When we merge this by default the unlikely argument should be used. And MAYBE we have an impl option to turn it off. But if so there needs to be a cmake option.

@rgayatri23
Copy link
Contributor Author

Followed @crtrott suggestion. Using [unlikely] attribute by default unless specified by a cake IMPL macro.

Comment on lines 29 to 34
#if defined(KOKKOS_ENABLE_IMPL_REF_COUNT_BRANCH_LIKELY) && \
defined(KOKKOS_ENABLE_CXX20)
#define KOKKOS_BRANCH_PROB [[likely]]
#else
#define KOKKOS_BRANCH_PROB [[unlikely]]
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

This won't work. It should be

Suggested change
#if defined(KOKKOS_ENABLE_IMPL_REF_COUNT_BRANCH_LIKELY) && \
defined(KOKKOS_ENABLE_CXX20)
#define KOKKOS_BRANCH_PROB [[likely]]
#else
#define KOKKOS_BRANCH_PROB [[unlikely]]
#endif
#if defined(KOKKOS_ENABLE_CXX20)
#define KOKKOS_BRANCH_PROB [[unlikely]]
#else
#define KOKKOS_BRANCH_PROB
#endif

to conform to @crtrott's suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So we don't need a user provided option where they can opt for [likely] attribute?

Copy link
Contributor

Choose a reason for hiding this comment

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

Right. We are only considering adding [[unlikely]].

CXX = clang++
EXE = copy_view_performance.exe

CXXFLAGS ?= -Ofast -DKOKKOS_IMPL_REF_COUNT_BRANCH_UNLIKELY
Copy link

Choose a reason for hiding this comment

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

This macro should now be "KOKKOS_ENABLE_IMPL_REF_COUNT_BRANCH_UNLIKELY" to be the same as the macro in core/src/impl/Kokkos_SharedAlloc.hpp

@@ -48,6 +48,7 @@ KOKKOS_ENABLE_OPTION(CUDA_LAMBDA ${CUDA_LAMBDA_DEFAULT} "Whether to allow lambda
# resolved but we keep the option around a bit longer to be safe.
KOKKOS_ENABLE_OPTION(IMPL_CUDA_MALLOC_ASYNC ON "Whether to enable CudaMallocAsync (requires CUDA Toolkit 11.2)")
KOKKOS_ENABLE_OPTION(IMPL_NVHPC_AS_DEVICE_COMPILER OFF "Whether to allow nvc++ as Cuda device compiler")
KOKKOS_ENABLE_OPTION(IMPL_REF_COUNT_BRANCH_UNLIKELY ON "Turn on `likely` attribute from C++20 when reference counting view copies")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
KOKKOS_ENABLE_OPTION(IMPL_REF_COUNT_BRANCH_UNLIKELY ON "Turn on `likely` attribute from C++20 when reference counting view copies")
KOKKOS_ENABLE_OPTION(IMPL_REF_COUNT_BRANCH_UNLIKELY ON "Turn on `unlikely` attribute from C++20 when reference counting view copies")

@@ -24,6 +24,15 @@
#include <cstdint>
#include <string>

// Use likely, unlikely attributes from C++20 to improve performance of
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Use likely, unlikely attributes from C++20 to improve performance of
// Use unlikely attribute from C++20 to improve performance of

@masterleinad masterleinad removed the InDevelop Enhancement, fix, etc. has been merged into the develop branch; label Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Improve existing capability; will potentially require voting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants