-
Notifications
You must be signed in to change notification settings - Fork 75
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
std::is_trivially_copyable too strong as a constraint #2195
Comments
hi @krzikalla
could you provide an example of such a type ? |
include/codi/expressions/activeType.hpp is an example. For some template types |
OK - for this specific case, could you change the destructor from CODI_INLINE ~ActiveType() {} to CODI_INLINE ~ActiveType() = default; ? This should make it a trivially copyable type :-) In general, one the concerns with non-trivially copyable types related to destructors is - if the type has a non-trivial destructor, should it be called inside the kernel, when the objects go out of scope ? |
Even though the template ActiveType looks complicated, it sometimes results in a trivially copyable type, e.g. Its state only consist of two doubles, which can be copied using memcpy. |
I had a quick look at the CoDiPack project and defaulting the destructor does not make the type trivially copyable, because it is still not trivially destructible. The reason is that I think alpaka is right in rejecting to byte-wise copy such a type to an accelerator, since it would break the guarantees of the type system. Now I also understand your desire to hack the type system, in which case you could e.g. bluntly cast away the type before passing a value to a kernel, e.g. casting it to |
@krzikalla IMO if you are 100% sure that the type is allowed to be copied via |
@psychocoderHPC : forbidden by the standard. |
Ohh missed that in the standard. :-( |
According to CppReference (I don't have the standard at hand) it's not "forbidden", it's "undefined behaviour":
@psychocoderHPC, @bernhardmgruber:
Technically true, but it's definitely cumbersome as a user interface. The request to have something like namespace alpaka {
template<typename T>
struct is_trivially_copyable : public std::false_type {};
} that users can namespace alpaka {
template<>
struct is_trivially_copyable<MyType> : public std::true_type {};
} seems reasonable enough... |
Which is arguably worse, UB means "behavior for which this document imposes no requirements", see current draft. So the program can literally do anything. I want to add that my suggestion of erasing the type to bypass the test is equally UB.
I may be on board if we name it differently so it communicates intent and consequences. Something like: namespace alpaka {
template <typename T>
inline constexpr bool allowMemcpyToDeviceAndIWillLiveWithTheConsequences = ...;
} Anyway, this is not only my decision. |
... including the actually intended behaviour :-)
Agreed. |
What is if we change our check and use our own trait to check for the required condition which can be specialized by the user? So I suggest |
Fine. Please go ahead and provide a PR :) |
Just some additional background information: @janbac discussed this topic with me yesterday and I did some tests. It would be possible to implement a CoDiPack type that is trivially copyable. Here we would need to define default versions for the constructor, destructor and the copy operator (operator=) in the classes
My personal opinion on that, is that the formulation is wrong. I think the authors wanted to say: If a type is not trivially copyable and a specialization of std::is_trivially_copyable changes this. then the behavior is undefined. |
Special casing through the preprocessor is also bad. But I also get that it may be difficult to solve in pure C++. @psychocoderHPC is preparing a PR to supply you the escape hatch you need.
Inheritance should work perfectly fine with trivial/defaulted implementations for construction, copy and move operations. And mixins in C++ are just a different form of inheritance, often coupled with CRTP. But as I said, if it's too difficult, you can use the escape hatch.
Well, the wording of the C++26 working draft says that: "Unless otherwise specified, the behavior of a program that adds specializations for any of the templates specified in [type.traits] is undefined. See here. And |
Thanks for the link to the standard draft. That clears things up. Do you know of a document that explains the reasoning behind this choice? |
I don't, but you may be lucky posting to the std-discussion mailing list, or asking on reddit. |
fix alpaka-group#2195 Provide an alpaka specific trait to validate kernel argument conditions which can be specialized by the user for their own types and at their own risk.
I added the trait that gives the user the possibility to relax the kernel argument conditions at their own risk in #2198 |
fix alpaka-group#2195 Provide an alpaka specific trait to validate kernel argument conditions which can be specialized by the user for their own types and at their own risk.
fix alpaka-group#2195 Provide an alpaka specific trait to validate kernel argument conditions which can be specialized by the user for their own types and at their own risk.
fix alpaka-group#2195 Provide an alpaka specific trait to validate kernel argument conditions which can be specialized by the user for their own types and at their own risk.
fix alpaka-group#2195 Provide an alpaka specific trait to validate kernel argument conditions which can be specialized by the user for their own types and at their own risk.
fix #2195 Provide an alpaka specific trait to validate kernel argument conditions which can be specialized by the user for their own types and at their own risk.
While this change clears the issue with the check on kernel arguments being trivially copyable, unfortunately I am running into a comparable issue with the check done on the kernel itself. |
The line
alpaka/include/alpaka/kernel/Traits.hpp
Line 227 in 4c981f0
constrains types to copyable ones by just using
std::is_trivially_copyable
. Our users sometimes have types, which are trivially copyable, but don't fulfill the requirements ofstd::is_trivially_copyable
. It is not allowed to specializestd::is_trivially_copyable
itself.Is it possible to get a trait for this property, so that a user is able to specify trivial copyability for particular types?
The text was updated successfully, but these errors were encountered: