-
Notifications
You must be signed in to change notification settings - Fork 17
Add proposal for default-constructible buffers #97
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
+97
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
default-constructed-buffers/default-constructed-buffers.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Default-constructed Buffers | ||
|
|
||
| Proposal ID | CP021 | ||
| ---------------- | --------------------------- | ||
| Name | Default-constructed Buffers | ||
| Date of Creation | 2019-08-20 | ||
| Target | SYCL 1.2.1 | ||
| Status | _Draft_ | ||
| Author | Duncan McBain [Duncan McBain](mailto:duncan@codeplay.com) | ||
| Contributors | Duncan McBain, [Gordon Brown](mailto:gordon@codeplay.com) | ||
|
|
||
| ## Description | ||
|
|
||
| In SYCL, there are no buffer constructors that take no arguments, which means | ||
| that `cl::sycl::buffer` cannot be default-constructed. However, this limits the | ||
| use of buffers in some interfaces. Consider a library function that has an | ||
| optional user-provided allocation for temporary data. The library can avoid | ||
| allocations if the library user so desires by providing a buffer here, but how | ||
| should the user indicate that allocation is allowed? A natural way would be to | ||
| provide a default argument in the API function: | ||
|
|
||
| ```c++ | ||
| template <typename Allocation_t> | ||
| void foo(Allocation_t input_a, | ||
| Allocation_t input_b, | ||
| Allocation_t temp = Allocation_t{}); | ||
| ``` | ||
|
|
||
| For `Allocation_t` types that are pointer-like, the library can check for | ||
| `nullptr` and allocate if this condition is true. A similar mechanism that | ||
| allows `cl::sycl::buffer` to be constructed and checked for usability would | ||
| aid their use in this style of generic interface. | ||
|
|
||
| ## Proposal | ||
|
|
||
| The `cl::sycl::buffer` class should be augmented with an additional constructor | ||
| that takes no arguments, which initialises the buffer with a zero-size range. | ||
| ```c++ | ||
| namespace cl { | ||
| namespace sycl { | ||
| template <typename T, int dimensions = 1, | ||
| typename AllocatorT = cl::sycl::buffer_allocator> | ||
| class buffer { | ||
| buffer(); | ||
|
|
||
| bool is_valid() const noexcept; | ||
|
|
||
| explicit operator bool() const noexcept; | ||
| }; | ||
| } // namespace sycl | ||
| } // namespace cl | ||
| ``` | ||
| The template arguments should remain the same, so that the argument can be | ||
| rebound to a new `buffer` instance later using the copy constructor. | ||
|
|
||
| The `is_valid()` call would allow the programmer to query whether or not | ||
| the buffer can be used, i.e. whether or not it was constructed with a range of | ||
| size zero. The explicit conversion operator would call this same function but | ||
| allow its use in `if` statements. | ||
|
|
||
| Requesting access from a default-constructed buffer should throw an exception. | ||
| It is not meaningful to use a zero-sized allocation on-device. Since there is | ||
| no allocation associated with the `buffer`, `cl::sycl::buffer::set_final_data` | ||
| and `cl::sycl::buffer::set_write_back` should behave as if the `buffer` had a | ||
| final pointer of `nullptr` at all times. The other functions in the `buffer` | ||
| API should behave as though the buffer were constructed with a `range` of size | ||
| zero and otherwise behave normally. | ||
|
|
||
| ## Sample code | ||
|
|
||
| ```c++ | ||
| #include <CL/sycl.hpp> | ||
| using namespace sycl = cl::sycl; | ||
| using namespace access = sycl::access; | ||
|
|
||
| class name; | ||
|
|
||
| void api_func(sycl::queue& q, | ||
| sycl::buffer<float, 1> input, sycl::buffer<float, 1> output, | ||
| sycl::buffer<float, 1> workspace = sycl::buffer<float, 1>{}) { | ||
| if (!workspace) { | ||
| // calculate required size for workspace | ||
| constexper auto size = 2048llu; | ||
| workspace = sycl::buffer<float, 1>{sycl::range<1>{size}}; | ||
| } | ||
| // use buffer | ||
| q.submit([&] (sycl::handler& cgh) { | ||
| auto acc_in = input.get_access<access::mode::read>(cgh); | ||
| auto acc_out = input.get_access<access::mode::write>(cgh); | ||
| auto acc_workspace = input.get_access<access::mode::read_write>(cgh); | ||
| cgh.single_task<name>([=]() { | ||
| // uses accessors | ||
| }); | ||
| }); | ||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.