Skip to content

[SYCL][Doc] USM allocation functions with properties support #6346

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

Merged
merged 16 commits into from
Feb 13, 2023

Conversation

jessicadavies-intel
Copy link
Contributor

This extension introduces USM memory allocation functions that take a sycl::ext::oneapi::experimental::properties argument instead of a sycl::property_list. They return a sycl::ext::oneapi::experimental::annotated_ptr object.

This PR replaces the initial draft #5656 opened by Sherry who is no longer working at Intel.

All properties that are valid for annotated_ptr must be supported by the USM memory allocation functions with `properties` support.
Unless otherwise specified, supported properties do not affect the behavior of the USM memory allocation functions with `properties` support.

[NOTE]
Copy link
Contributor

Choose a reason for hiding this comment

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

This note repeats the same information said immediately before it. You probably only need to say it once.

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 tried to reword this so it is less repetitive. The NOTE now just mentions that the motivation is for user convenience in only needing to specify the properties in one place.

The `usm_alloc` property is supported by annotated_ptr, and therefore by the USM memory allocation functions defined in this extension.
This property will always appear on the annotated_ptr returned by the USM memory allocation functions defined in this extension that do not have a `usm::alloc` parameter.
The `usm_alloc` property may also be passed to the USM memory allocation functions defined in this extension.
If the USM memory allocation kind specified by a function's `usm::alloc` parameter is different from the `usm_alloc` property, the result is undefined behavior.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is easy to diagnose at compile-time, right? If so, let's make it an "error, diagnostic required" instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The usm::alloc parameter's value is not known until runtime. I will change this from UB to an exception.

@pvchupin pvchupin requested a review from GarveyJoe October 12, 2022 23:54

// This section needs to be after the document title.
:doctype: book
:toc2:
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you intend to add a table of contents? I don't think I've seen it on any of the other extensions.

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 removed these lines. I don't want a table of contents.

@@ -0,0 +1,1468 @@

Copy link
Contributor

Choose a reason for hiding this comment

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

No title

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 added the title. Thanks.

== Examples

Runtime and compile-time constant properties can be passed to the USM memory allocation functions introduced by this extension.
Properties passed to an allocation function may or may not appear on the returned `annotated_ptr` object:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Properties passed to an allocation function may or may not appear on the returned `annotated_ptr` object:
Properties passed to an allocation function may or may not appear on the returned `annotated_ptr` object;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why isn't a colon appropriate here?

Copy link
Contributor

Choose a reason for hiding this comment

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

The part before the punctuation doesn't introduce the part after. Each part is a full sentence. You could use a period, but a semi-colon seems more appropriate since you want to show that they are related.

Copy link
Contributor

Choose a reason for hiding this comment

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

I changed my mind after looking at more style guides. I think the colon is fine here.


----

If the USM memory allocation kind specified by a parameter to the allocation function is different than the USM memory allocation kind specified by the `sycl::ext::oneapi::experimental::usm_kind` property, the USM memory allocation kind specified by the property takes precedence.
Copy link
Contributor

@GarveyJoe GarveyJoe Oct 27, 2022

Choose a reason for hiding this comment

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

I think you said you would make this throw an exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I have now updated this to throw an exception.

auto APtr11 = annotated_malloc_host<int>(N, q, P7); // Error
----

The following example uses the compile-time-constant property `alignment`, defined in the link:../proposed/sycl_ext_oneapi_annotated_ptr.asciidoc[sycl_ext_oneapi_annotated_ptr] extension.
Copy link
Contributor

Choose a reason for hiding this comment

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

link is broken

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The link will be broken until the annotated_ptr spec PR is merged. I will wait to merge the malloc extension until annotated_ptr is merged (and then I'll also double-check the links).


properties P9{alignment<1>};
// Allocate N integers.
// Alignment must be at least sizeof(int) bytes.
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this actually convey what you want to say? I worry that an alignment of 7 satisfies this requirement as written. Can we simply say that the resulting pointer has an alignment of sizeof(int) bytes? Isn't it already part of the definition of alignment that alignment of N can be satisfied by a pointer whose value is any multiple of N (and can therefore be a multiple of XN as well where X is a natural number)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"alignment must be at least sizeof(int) bytes" is a necessary condition but not sufficient.
I can change this to "alignment must be a positive non-zero integer multiple of sizeof(int)".

----

This extension also introduces USM memory allocation functions with `properties` support that allow alignment to be specified at runtime, using a separate parameter of type `size_t`.
If the compile-time constant `alignment` property is also passed in, it takes precedence over the alignment specified by the parameter of type `size_t`.
Copy link
Contributor

@GarveyJoe GarveyJoe Oct 27, 2022

Choose a reason for hiding this comment

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

This doesn't seem user friendly. I would expect the resulting pointer to have both specified alignments. I.e. the resulting pointer value should be multiple of the lowest common multiple of the two specified alignments. Assuming both specified alignments are powers of two, that would mean that the larger of the two should take precedence.

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 have made this change. I've removed all exceptions and errors that were caused by differing alignments.


[[table.usm.device.allocs]]
.USM Device Memory Allocation Functions with properties Support
[width="100%",options="header",separator="@",cols="65%,35%"]
Copy link
Contributor

Choose a reason for hiding this comment

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

This table doesn't render nicely for me. Is that just my setup or is there something wrong with it?

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 improved the formatting of the first column that contains the function declarations.


The following five tables list all functions introduced by this extension.

TODO: How is propertyListB going to be inferred? Do we need support for getting the type of a properties list with only the compile-time properties, and also adding a new compile-time property?
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think we want both of these.

@zjin-lcf
Copy link
Contributor

I am not clear how to view the rendered asciidoc file. Could you please explain how CUDA's constant memory is migrated to SYCL using USM ? Thanks.

device that is contained by that context, otherwise this function throws a
synchronous `exception` with the `errc::invalid` error code.

And error is reported if `propList` contains
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
And error is reported if `propList` contains
An error is reported if `propList` contains

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Done.

The `sycl::ext::oneapi::experimental::usm_kind` property is a compile-time constant property with a single non-type parameter. This parameter is a value belonging to the enumeration `sycl::usm::alloc`.
The `sycl::ext::oneapi::experimental::usm_kind` property is supported by `annotated_ptr` and the USM memory allocation functions defined in this extension.

=== Deallocation
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 add an overload of sycl::free that takes an annotated_ptr to make this easier?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

@keryell keryell left a comment

Choose a reason for hiding this comment

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

Good proposal which trusts the C++ type-system instead of fighting against it.

success. The allocation size is specified in bytes. This memory is not
accessible on the host. Memory allocated by `annotated_malloc_device`
must be deallocated with `sycl::free` to avoid memory leaks.
On failure, the raw pointer of the returned `annotated_ptr` will be `nullptr`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess it is not possible to return nullptr here which is a C++ keyword and obviously is of type https://en.cppreference.com/w/cpp/types/nullptr_t
Perhaps you could have a using nullptr_t = ... and constexpr static nullptr_v = ... inside your annotated pointer class for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are discussing adding a comparison operator to annotated_ptr, e.g.:

bool operator == (const nullptr_t &other) const { return this->ptr == nullptr; }
which would allow us to write

annotated_ptr x = malloc_device_annotated<int>(N, q); if (x == nullptr) {…}

Copy link
Contributor

Choose a reason for hiding this comment

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

Great idea.
You could also have a conversion-to-bool operator, if you do not have one already.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The annotated_ptr spec currently lists a conversion to bool operator:

explicit operator bool() const noexcept;
Returns false if the underlying pointer is null, returns true otherwise.

Copy link
Contributor

@gmlueck gmlueck left a comment

Choose a reason for hiding this comment

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

Some specific comments below, but I'd also like to bikeshed on the names. The current naming is like:

  • annotated_malloc_host

This seems weird to me, though. The function is primarily used for memory allocation, so shouldn't the "malloc" come first? How about:

  • malloc_host_annotated

It would be nice to drop the "annotated" entirely, but I don't see we can do that and still coexist with the sycl::malloc_host that is already in the spec.

…tr can now differ from those passed to the allocation function.
…l properties supported by annotated_ptr are supported by malloc. All compile-time and no runtime properties are propagated from malloc to the returned annotated_ptr.
…y on the annotated_ptr by default. Add support for specifying alignment and allocation kind at runtime.

Provide additional functions that require the usm_kind property.
@jessicadavies-intel
Copy link
Contributor Author

Some specific comments below, but I'd also like to bikeshed on the names. The current naming is like:

  • annotated_malloc_host

This seems weird to me, though. The function is primarily used for memory allocation, so shouldn't the "malloc" come first? How about:

  • malloc_host_annotated

I like malloc_host_annotated a lot better, so thanks for the suggestion. I made this change throughout the text.

@bader
Copy link
Contributor

bader commented Feb 9, 2023

@jessicadavies-intel, please, update PR title tags when PR is ready for merge.
E.g. [WIP] -> [SYCL][Doc].

@jessicadavies-intel jessicadavies-intel changed the title [WIP] USM allocation functions with properties support [SYCL][Doc] USM allocation functions with properties support Feb 13, 2023
@bader bader merged commit 3600f92 into intel:sycl Feb 13, 2023
@jessicadavies-intel jessicadavies-intel deleted the malloc_properties branch February 13, 2023 22:33
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.

6 participants