-
Notifications
You must be signed in to change notification settings - Fork 69
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
Clarify that capture by ref host_task requires caution #515
Clarify that capture by ref host_task requires caution #515
Conversation
must ensure that referred-to user defined classes use appropriate reference | ||
counting. Since the <<host_task>> callable may execute at an unspecified time | ||
between a command submission and a synchronization point, its execution may | ||
outlive the referred-to objects if reference counting is not used. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems too specific to a particular bug that you had, and the recommendation about reference counting is only one possible solution. I don't think we need to add any new formal requirements to the spec here. We already say that a host task's execution is deferred and that it executes asynchronously. A logical outcome of this is that you need to take care when using pointers or references inside the host task.
If we want to add something to the spec, I'd suggest a non-normative note like this:
[NOTE]
====
Care must be taken when using pointers or references inside a host task when the pointer
or reference refers to an object that is defined outside of the host task. Because the host
task is executed asynchronously, applications should ensure that the referenced object's
lifetime lasts at least until the host task completes.
====
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is reference counting even a solution? This is still a bug:
void submit_host_task(sycl::queue& q, std::shared_ptr<int> /* by value */ something) {
q.submit([&](sycl::handler& cgh) {
cgh.host_task([&]() {
printf("%d\n", *something); // oops, `something` went out of scope
});
});
}
A classic footgun is to capture host-task accessors by reference. Maybe it's worth pointing that out specifically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for comments. Have rephrased to advise caution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a normal caveat with any lambda not in an immediate invocation context.
61fa780
to
f88128c
Compare
@keryell I agree. I think it is worth reiterating here, but can close this PR if you think it doesn't belong in the spec |
Hi @hdelan, we have some new formatting requirements for the *.adoc files, and these are checked by our CI system. One of these checks failed for this PR. Can you do the following:
Thanks. |
f88128c
to
fd67962
Compare
Thanks @gmlueck have done that |
@gmlueck should I add my name to |
Yes, if you like. |
@gmlueck thanks, done |
If a C++ lambda is passed to a <<host-task>>, the lambda may capture by | ||
reference or by value. | ||
Since the <<host-task>> callable executes asynchronously, care must be taken to | ||
ensure that lifetimes of objects captured by reference by a <<host-task>> lambda |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even for objects captured by copy you still need to unsure that it works under the hood with the program logic.
The trivial example is a std::ref
or a pointer captured by copy. ;-)
WG approved |
Clarify that capture by ref host_task requires caution
Clarify that capture by ref host_task requires caution (cherry picked from commit bfe3330)
Clarify that capture by ref host_task requires caution (cherry picked from commit bfe3330)
C++ lambdas if capturing by ref need to be aware of the lifetime of the referred-to objects. This adds this information here as well.
Motivation:
oneapi-src/oneDNN#1767