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 detection idiom from the C++ standard library extension version 2 #3980

Merged
merged 9 commits into from
May 5, 2021

Conversation

dalg24
Copy link
Member

@dalg24 dalg24 commented Apr 28, 2021

Following up on #3863 (comment)

The goal of this PR is only to add support and replace in a couple places to demonstrate usage. We will incrementally adopt and replace in the rest of Kokkos in upcoming PRs. Fixing it all up at once would be a daunting task.

Copy link
Member

@Rombur Rombur left a comment

Choose a reason for hiding this comment

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

This looks good but do I understand correctly that the goal of the PR is to change the current code closer to the standard?

core/unit_test/TestDetectionIdiom.cpp Show resolved Hide resolved
#include <type_traits>

// NOTE This header implements the detection idiom from Version 2 of the C++
// Extensions for Library Fundamentals, ISO/IEC TS 19568:2017
Copy link
Member

Choose a reason for hiding this comment

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

Is the plan to remove this file in the future once it's part of std?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think so but we don't know for sure that it will actually be added to the standard library.

core/unit_test/CMakeLists.txt Outdated Show resolved Hide resolved
Comment on lines +53 to +54
// I deliberately omitted detected_or which does not fit well with the rest
// of the specification. In my opinion, it should be removed from the TS.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there are multiple occasions where we want to define a type alias if it exists and a custom default fallback type otherwise. I would just add it (in particular if you still provide detected_or_t).

Copy link
Member Author

Choose a reason for hiding this comment

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

I think there are multiple occasions where we want to define a type alias if it exists and a custom default fallback type otherwise.

This is what detected_or_t provide

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I know. The *_t are normally just abbreviations for typename *::type and that's what I was referring to in the last sentence. Anyway, I don't feel strongly about it.

using is_detected_convertible =
std::is_convertible<detected_t<Op, Args...>, To>;

#ifdef KOKKOS_ENABLE_CXX17
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to only provide this conditionally apart from the STL only providing *_v abbreviations for C++17?

Copy link
Member Author

Choose a reason for hiding this comment

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

No other reason that consistency but I would be ok adding them unconditionally if reviewers disagree with my choice.

Copy link
Contributor

@nliber nliber Apr 29, 2021

Choose a reason for hiding this comment

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

They should be inline constexpr bool, which does require C++17. If they aren't inline, I think we may need (but haven't tested) to do something to avoid separate definitions for the same global in different translation units, such as putting them in an anonymous namespace in the header.

Copy link
Contributor

@nliber nliber Apr 29, 2021

Choose a reason for hiding this comment

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

Okay, so I tried the following simple test from two translation units:

template<class T>
constexpr bool beta = std::is_trivially_copyable<T>::value;

    std::cout << &beta<int> << '\n';

In C++14 mode, gcc10 gives me two different addresses, which clang11 gives me the same address. I think this is an ODR-violation.

Putting them in an anonymous namespace (or declaring them static) should fix the ODR-violation.

core/src/Kokkos_Parallel.hpp Show resolved Hide resolved
core/src/Kokkos_DetectionIdiom.hpp Outdated Show resolved Hide resolved
// NOTE This header implements the detection idiom from Version 2 of the C++
// Extensions for Library Fundamentals, ISO/IEC TS 19568:2017

// I deliberately omitted detected_or which does not fit well with the rest
Copy link
Contributor

Choose a reason for hiding this comment

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

Update the comment to also say that detected_or_t is superior to use over detected_t in all cases as the reason for removing it (it threw me off a little to see detected_or_t there until I thought about it).

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that updating the comment here might help with clarity.

dalg24 and others added 2 commits April 29, 2021 14:18
Take advantage of DefaultExecutionSpace::execution_space in FunctorPolicyExecutionSpace

Co-Authored-By: Daniel Arndt <arndtd@ornl.gov>
// NOTE This header implements the detection idiom from Version 2 of the C++
// Extensions for Library Fundamentals, ISO/IEC TS 19568:2017

// I deliberately omitted detected_or which does not fit well with the rest
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that updating the comment here might help with clarity.

void operator=(nonesuch const&) = delete;
};

template <template <class...> class Op, class... Args>
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know how Kokkos documentation works.. but shouldn't we have some doxygen here to explain what these things do?

Copy link
Contributor

Choose a reason for hiding this comment

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

We don't use doxygen but rather rely on the Wiki for user-facing documentation.

@masterleinad
Copy link
Contributor

You wanted to remove the last commit, isn't it?

@dalg24
Copy link
Member Author

dalg24 commented May 5, 2021

You wanted to remove the last commit, isn't it?

I was waiting on feedback from @crtrott but I will go ahead and revert

@crtrott crtrott merged commit 6d1b566 into kokkos:develop May 5, 2021
@dalg24 dalg24 deleted the detection_idiom branch May 20, 2021 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants