alpaka-group / alpaka

Abstraction Library for Parallel Kernel Acceleration :llama:
https://alpaka.readthedocs.io
Mozilla Public License 2.0
356 stars 74 forks source link

implement alpaka::concepts::Tag #2403

Closed SimeonEhrig closed 1 month ago

SimeonEhrig commented 1 month ago

First alpaka C++20 concept :-) I think there are several points to discuss.

Implement alpaka::concept::Tag which checks the requirements for an alpaka tag. Remove support Clang 10, 11 and 12 because they are not fully supports C++20 concepts.

SimeonEhrig commented 1 month ago

@alpaka-group/alpaka-maintainers I was thinking about naming concepts. Here it is easy because alpaka::Tag is unique and not used yet. Maybe the name looks like a base class where the actual tags inherintant, but I think this fine.

I the case of alpaka::Queue this is different. We have already the class alpaka::Queue: https://github.com/alpaka-group/alpaka/blob/e20236d86be9a8093fa2efae44eaf2db79dc38db/example/helloWorld/src/helloWorld.cpp#L74

In the case of template class deduction this can confuse:

// use the class alpaka::Queue, deducts the argument from the function call
alpaka::Queue queue = create_queue();

// use the concept alpaka::Queue
alpaka::Queue auto queue2 = create_queue();

The C++ standard makes clear, where classes and where concepts are used. But without the context, like in the documentation, it is hard to distinguish.

Possible solutions:

What do you think?

fwyzard commented 1 month ago

What is the syntax for using concepts ?

SimeonEhrig commented 1 month ago

What is the syntax for using concepts ?

Have a look in this commit: https://github.com/alpaka-group/alpaka/pull/2403/commits/37c62ed8b2e7f977da7025e2fda6e12cd8466e5e

Also interesting is, what is the error message. Try to call the functions specialize_tag<NoTag1>() and specialize_tag<NoTag2>() in the AccTagTest.cpp and see what the compiler is telling you.

SimeonEhrig commented 1 month ago

You can also write something like this:

template<typename T>
   requires std::integral<T>
T gcd(T a, T b) {
   if( b == 0 ) return a;
   else return gcd(b, a % b);
}

But I prefer the version, which I used because it uses less boilerplate code and looks more intuitive.

psychocoderHPC commented 1 month ago

Possible solutions:

  • let it as it
  • add a prefix like for templates: alpaka::CTag, alpaka::CAcc, alpaka::CQueue
  • used the alpaka namespace alpaka::concept::Tag, alpaka::concept::Acc, alpaka::concept::Queue

What do you think?

I am a friend of namespaces and not mangling all into names. C* is also very often used for classes. This is what I learned long time ago. Namespaces will clearly separate implementations from concepts.

fwyzard commented 1 month ago

You can also write something like this:

template<typename T>
   requires std::integral<T>

No, I agree that

template<std::integral T>

looks cleaner.

fwyzard commented 1 month ago

used the alpaka namespace alpaka::concept::Tag, alpaka::concept::Acc, alpaka::concept::Queue

👍🏻

fwyzard commented 1 month ago

To avoid confusion, we could rename the current namespace and classes called "Concept" to "Interface" ?

SimeonEhrig commented 1 month ago

You can also write something like this:

template<typename T>
   requires std::integral<T>

No, I agree that

template<std::integral T>

looks cleaner.

I agree with you. And I was thinking about, when you need this requires in general. The answer is, if you have more than one template argument in the concept.

template<typename T, typename U>
concept foo = requires { requires std::is_same_v<T,U>; };

template<typename T, typename U>
void bar() requires foo<T,U> {}

But if we can do it, we should use the first syntax.

SimeonEhrig commented 1 month ago

To avoid confusion, we could rename the current namespace and classes called "Concept" to "Interface" ?

I agree with the idea. So let's rename the current alpaka::concepts to alpaka::inteface and reuse the namespace alpaka::concepts for C++20 concepts.

By the way. All our namespaces are singular. But we cannot use alpaka::concept because concept is a key word.