Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions source/examples/usm-system-shared.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: 2026 The Khronos Group Inc.
//
// SPDX-License-Identifier: Apache-2.0

#include <sycl/sycl.hpp>

#include <cstdlib>
#include <iostream>

int main() {
// Create a default queue to enqueue work to the default device
sycl::queue myQueue;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You need to check if the device support aspect::usm_system_allocations (https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html#api:aspect-usm-system-allocations)


// Allocate normal system memory - let the Linux kernel do the work!
// You can also use a std::vector, etc.
int *data = (int *) malloc(1024);

myQueue.parallel_for(1024, [=](sycl::id<1> idx) {
// Initialize each buffer element with its own rank number starting at 0
data[idx] = idx;
}); // End of the kernel function

// Explicitly wait for kernel execution since there is no accessor involved
myQueue.wait();

// Print result
for (int i = 0; i < 1024; i++)
std::cout << "data[" << i << "] = " << data[i] << std::endl;

// Clean up with normal system free
free(data);

return 0;
}
24 changes: 19 additions & 5 deletions source/iface/usm_basic_concept.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
..
Copyright 2024 The Khronos Group Inc.
Copyright 2026 The Khronos Group Inc.
SPDX-License-Identifier: CC-BY-4.0

.. _usm_basic_concept:
Expand Down Expand Up @@ -170,7 +170,7 @@ supports ``sycl::aspect::usm_device_allocations``.

.. rubric:: Example

See `usm-example-2`_.
See `usm-example-3`_.


Shared allocations
Expand Down Expand Up @@ -239,7 +239,7 @@ shared allocations can be queried through the aspect

.. rubric:: Example

See `usm-example-1`_.
See `usm-example-2`_.


System allocations
Expand All @@ -260,15 +260,17 @@ through ``sycl::aspect::usm_system_allocations``.
must still be allocated using their respective USM functions in order to
guarantee their behavior.

See `usm-example-1`_.

.. _usm-example-1:

=========
Example 1
=========

Example of how shared memory can be used between host and device:
Example of how system shared memory can be used between host and device:

.. literalinclude:: /examples/usm-shared.cpp
.. literalinclude:: /examples/usm-system-shared.cpp
:lines: 5-
:linenos:

Expand All @@ -278,6 +280,18 @@ Example of how shared memory can be used between host and device:
Example 2
=========

Example of how shared memory can be used between host and device:

.. literalinclude:: /examples/usm-shared.cpp
:lines: 5-
:linenos:

.. _usm-example-3:

=========
Example 3
=========

Example of using less capable device memory, which requires
an explicit copy between the device and the host:

Expand Down
Loading