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

[SYCL] Return nullptr when allocation size is zero in usm allocator #12765

Merged
merged 4 commits into from
Feb 21, 2024

Conversation

uditagarwal97
Copy link
Contributor

@uditagarwal97 uditagarwal97 commented Feb 20, 2024

Currently, usm allocator throws when the allocation size is zero. However, this behavior is not aligned with that of std::allocator.
Refer KhronosGroup/SYCL-Docs#355 for discussion regarding this. The spec says that the allocation functions must succeed when the size is zero. The value returned in this case is unspecified (it can either be a NULL pointer or a non-NULL pointer)

This PR makes USM allocator return a null pointer when the allocation size is zero.

@uditagarwal97 uditagarwal97 marked this pull request as ready for review February 20, 2024 17:06
@uditagarwal97 uditagarwal97 requested a review from a team as a code owner February 20, 2024 17:06
auto ctxt = q.get_context();

if (dev.get_info<info::device::usm_host_allocations>()) {
sycl::usm_allocator<int, sycl::usm::alloc::host> ua{ctxt, dev};
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we have another overload that accepts queue here? If not, I think the common spelling in our sources is ctx.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

auto dev = q.get_device();
auto ctxt = q.get_context();

if (dev.get_info<info::device::usm_host_allocations>()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we only test "host" here?

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'm not sure how adding more allocation types will increase the test coverage of this change. Nevertheless, I've expanded the test to also check for zero-sized shared allocations. usm::alloc::device is not supported by usm_allocator so, I've skipped that.

Comment on lines 18 to 19
assert(!p && "usm_allocator should return a null pointer when allocation "
"size is zero.");
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd reword as "our implementation is expected to ...".

#include <sycl/sycl.hpp>

using namespace sycl;
#define ALLOC_SIZE 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Just inline it...


assert(!p && "Our implementation of usm_allocator is expected to return a "
"null pointer when allocation "
"size is zero.");
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 this is fine, but another option is to simply remove the assert. From an API perspective, the important thing is that you can call allocate with a zero size and that you can then call deallocate on the returned pointer, all without any exception being raised. It's not important whether or not the returned pointer is null.

@aelovikov-intel aelovikov-intel merged commit 7f51d92 into intel:sycl Feb 21, 2024
11 checks passed
@uditagarwal97 uditagarwal97 deleted the usm_alloc_null branch March 21, 2024 19:58
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.

3 participants