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

Adding public utility for users to check if usm_allocators are supported #1476

Conversation

danhoeflinger
Copy link
Contributor

@danhoeflinger danhoeflinger commented Apr 4, 2024

Support for USM allocators depends on the implementation details of the C++ standard library used after #1438, and after doc changes of #1477.

This PR adds a public utility (usm_allocated_vector_iterators_supported_v<ValueType>) which users can use at compile time to check if usm_allocators are available to them as efficient input to oneDPL algorithms.

  • If usm_allocated_vector_iterators_supported_v<ValueType> evaluates to true, std::vector<ValueType, usm_allocator>::iterator input types are passed directly to sycl kernels, and are an efficient way of using oneDPL.

  • If usm_allocated_vector_iterators_supported_v<ValueType> evaluates to false, std::vector<ValueType, usm_allocator>::iterator input types are treated as if they were host-side iterators, and wrapped in sycl::buffer before use in sycl kernels. This includes all that comes with that, an extra host-side copy in and out. Also these cannot then be used as the source iterator for permutation_iterator.

@danhoeflinger danhoeflinger changed the base branch from main to dev/dhoeflin/improve_usm_shared_alloc_vectors April 4, 2024 12:59
@danhoeflinger danhoeflinger force-pushed the dev/dhoeflin/add_public_util_usm_alloc branch from d994552 to 2e10abf Compare April 4, 2024 14:33
@danhoeflinger danhoeflinger changed the base branch from dev/dhoeflin/improve_usm_shared_alloc_vectors to dev/dhoeflin/usm_allocator_doc_changes April 4, 2024 14:34
@@ -233,6 +233,11 @@ __internal::sycl_iterator<access_mode::discard_read_write, T, Allocator> end(syc
return __internal::sycl_iterator<access_mode::discard_read_write, T, Allocator>{buf,
__dpl_sycl::__get_buffer_size(buf)};
}

template <typename ValueType>
constexpr static bool usm_allocated_vector_iterators_supported_v =
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. I guess the traits doesn't depend on ValueType.
  2. As an idea - to make such public trait more general, for example,
template <typename Container>
constexpr static bool is_usm_container_supported = ...

Copy link
Contributor Author

@danhoeflinger danhoeflinger Apr 5, 2024

Choose a reason for hiding this comment

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

Sadly, I believe it can depend on the ValueType, for example std::vector<bool>. GNU c++ standard library implementation normally includes allocator type in the iterator, but not for std::vector<bool> with the bitfield "optimization".

(This is the only instance I know of, but it shows that we cant guarantee that the implementation details are consistent across value type)


template <typename ValueType>
constexpr static bool usm_allocated_vector_iterators_supported_v =
oneapi::dpl::__internal::__vector_iter_distinguishes_by_allocator<ValueType*>::value;
Copy link
Contributor

Choose a reason for hiding this comment

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

BTW, what if ValueType is a pointer to the host data?
It is rather a kind of general question connected to "Pass Data to algorithms" topic.

Copy link
Contributor Author

@danhoeflinger danhoeflinger Apr 5, 2024

Choose a reason for hiding this comment

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

If I understand correctly, you suggest the following type:

std::vector<int*, usm_allocator<int*, ...>>

In theory, a vector of host pointers will be very similar to a vector of integers I think.

In principle, as long as they are not dereferencing the pointers on the device, there is nothing wrong with this.

@danhoeflinger danhoeflinger force-pushed the dev/dhoeflin/usm_allocator_doc_changes branch from b4022c0 to 07dffec Compare April 5, 2024 16:11
};
template <typename Iter>
struct __vector_iter_distinguishes_by_allocator<
Iter, std::enable_if_t<!std::is_same_v<__default_alloc_vec_iter<Iter>, __usm_shared_alloc_vec_iter<Iter>> &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we apply here std::decay_t for Iter template param?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, this is already merged code from #1438 . Working to rebase, to make this focus on just what is added by this PR.
If we need to go back and fix something here, it will needs its own PR I think.

//support std::vector::iterator with usm host / shared allocator as passed directly
template <typename Iter>
struct is_passed_directly<
Iter, std::enable_if_t<(std::is_same_v<Iter, oneapi::dpl::__internal::__usm_shared_alloc_vec_iter<Iter>> ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we apply here std::decay_t for Iter template param?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above, this is merged code from #1438. The duplicated conflict code has been fixed via rebase. Any changes here will need a new PR.

@danhoeflinger danhoeflinger force-pushed the dev/dhoeflin/add_public_util_usm_alloc branch from 2e10abf to 411ad2f Compare April 5, 2024 16:21
allocator is included in the ``std::vector::iterator`` type definition. You can check
if your C++ standard library implementation provides enough information by checking the
value of ``oneapi::dpl::usm_allocated_vector_iterators_supported_v<ValueType>``. This will
evaluate to ``true`` for C++ standard library implementations which allow oneDPL to support
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a comma between "implementations" and "which" and add an "s" to "allow". The final sentence should read:

This will evaluate to true for C++ standard library implementations, which allows oneDPL to support USM allocators with this ValueType and false otherwise.

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 think there was some confusion by my wording. I've expanded this some to hopefully make things more clear.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the update. My suggestions on the new content (ignore spacing; I broke things up for this review).

This evaluates to true when using a C++ standard library implementation, which allows oneDPL to detect and
support USM allocators for ValueType and false.

When oneapi::dpl::usm_allocated_vector_iterators_supported_v<ValueType> evaluates to false,
you should rely on USM pointers directly instead of using iterators to vectors with USM allocators.

@danhoeflinger danhoeflinger force-pushed the dev/dhoeflin/add_public_util_usm_alloc branch from 47b9858 to 8a67a8f Compare April 5, 2024 19:18
@danhoeflinger danhoeflinger force-pushed the dev/dhoeflin/usm_allocator_doc_changes branch from c748730 to 0d69123 Compare April 15, 2024 18:08
@danhoeflinger danhoeflinger force-pushed the dev/dhoeflin/add_public_util_usm_alloc branch from 8a67a8f to 45ebb8d Compare April 15, 2024 18:13
@danhoeflinger
Copy link
Contributor Author

I think that with the direction we are going with #1477, this does not make sense to continue with such a public API.
I prefer our decision to recommend against using USM allocated vector iterators in favor of USM pointers to data within them.
Exposing a public API like this is probably too "in the weeds" for the user about the details of the C++ standard library implementations.
Therefore, I'm closing this PR.

@danhoeflinger danhoeflinger deleted the dev/dhoeflin/add_public_util_usm_alloc branch May 6, 2024 16:16
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.

4 participants