Skip to content

Commit 2321b3a

Browse files
[SYCL][Bindless] Add interop memory mapping to USM. (#14701)
This patch introduces `map_external_linear_memory` to enable mapping interop memory to linear USM, returning a `void *`. The PI function `piextMemMapExternalLinearMemory` has been added to enable this in the PI layer. A Vulkan test case has been added to test this functionality. --------- Co-authored-by: Duncan Brawley <duncan.brawley@codeplay.com>
1 parent f7b4a88 commit 2321b3a

File tree

8 files changed

+476
-18
lines changed

8 files changed

+476
-18
lines changed

sycl/cmake/modules/FetchUnifiedRuntime.cmake

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ if(SYCL_PI_UR_USE_FETCH_CONTENT)
115115
CACHE PATH "Path to external '${name}' adapter source dir" FORCE)
116116
endfunction()
117117

118-
set(UNIFIED_RUNTIME_REPO "https://github.com/oneapi-src/unified-runtime.git")
119-
# commit 3e762e00bcf13d158fb58e8e8c2eabcfc8934b4e
120-
# Merge: c805a71a a2a053de
118+
set(UNIFIED_RUNTIME_REPO "https://github.com/oneapi-src/unified-runtime.git")
119+
# commit c5d2175b5823d5b74de1e7e0d6081ab6d885bc34
120+
# Merge: 99489ad4 c86beb60
121121
# Author: Omar Ahmed <omar.ahmed@codeplay.com>
122-
# Date: Wed Jul 31 12:26:34 2024 +0100
123-
# Merge pull request #1884 from callumfare/callum/fix_printtrace
124-
# Enable PrintTrace when SYCL UR tracing is enabled
125-
set(UNIFIED_RUNTIME_TAG 3e762e00bcf13d158fb58e8e8c2eabcfc8934b4e)
122+
# Date: Wed Jul 31 14:52:26 2024 +0100
123+
# Merge pull request #1882 from przemektmalon/przemek/interop-map-memory
124+
# [Bindless][Exp] Add interop memory mapping to USM.
125+
set(UNIFIED_RUNTIME_TAG c5d2175b5823d5b74de1e7e0d6081ab6d885bc34)
126126

127127
set(UMF_BUILD_EXAMPLES OFF CACHE INTERNAL "EXAMPLES")
128128
# Due to the use of dependentloadflag and no installer for UMF and hwloc we need

sycl/doc/extensions/experimental/sycl_ext_oneapi_bindless_images.asciidoc

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,19 +1712,35 @@ image_mem_handle map_external_image_memory(
17121712
external_mem externalMemHandle,
17131713
const image_descriptor &imageDescriptor,
17141714
const sycl::queue &syclQueue);
1715+
1716+
void *map_external_linear_memory(
1717+
external_mem externalMemHandle,
1718+
uint64_t size, uint64_t offset,
1719+
const sycl::device &syclDevice,
1720+
const sycl::context &syclContext);
1721+
void *map_external_linear_memory(
1722+
external_mem externalMemHandle,
1723+
uint64_t size, uint64_t offset,
1724+
const sycl::queue &syclQueue);
17151725
}
17161726
```
17171727

17181728
The resulting `external_mem` can then be mapped, where the resulting type
1719-
is an `image_mem_handle`. This can be used to construct images in the same way
1720-
as memory allocated through `alloc_image_mem`. The `ext_oneapi_copy` operations
1721-
also work with imported memory mapped to `image_mem_handle` types.
1722-
1723-
When calling `create_image` with an `image_mem_handle` mapped from an external
1724-
memory object, the user must ensure that the image descriptor they pass to
1725-
`create_image` has members that match or map to those of the external API.
1726-
A mismatch between any of the `width`, `height`, `depth`, `image_channel_type`,
1727-
or `num_channels` members will result in undefined behavior.
1729+
is an `image_mem_handle` or a `void *`. This can be used to construct images in
1730+
the same way as memory allocated through `alloc_image_mem`,
1731+
`pitched_alloc_device`, or another USM allocation method. The `ext_oneapi_copy`
1732+
operations also work with imported memory mapped to `image_mem_handle` and
1733+
`void *` types.
1734+
1735+
When calling `create_image` with an `image_mem_handle` or `void *` mapped from
1736+
an external memory object, the user must ensure that the image descriptor they
1737+
pass to `create_image` has members that match or map to those of the external
1738+
API. A mismatch between any of the `width`, `height`, `depth`,
1739+
`image_channel_type`, or `num_channels` members will result in undefined
1740+
behavior. Likewise, if the image is mapped to a linear USM (`void *`) region,
1741+
the pitch value passed to `create_image` needs to match the pitch of the image
1742+
as defined by the external API. Note that when external memory is mapped to a
1743+
linear USM region, this is device-side USM, and not accessible on the host.
17281744

17291745
Additionally, the `image_type` describing the image must match to the image of
17301746
the external API. The current supported importable image types are `standard`
@@ -2866,4 +2882,6 @@ These features still need to be handled:
28662882
- Removed `handle` keyword from `interop_xxx_handle` to
28672883
clear up possible confusion between 3rd party interop
28682884
handles and the imported `interop_xxx_handle`.
2885+
|5.17|2024-07-30| - Add support for mapping external memory to linear USM using
2886+
`map_external_linear_memory`.
28692887
|======================

sycl/include/sycl/ext/oneapi/bindless_images.hpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ image_mem_handle map_external_image_memory(external_mem extMem,
171171
const sycl::context &syclContext);
172172

173173
/**
174-
* @brief Maps an external memory handle to an image memory handle (which may
174+
* @brief Maps an external memory object to an image memory handle (which may
175175
* have a device optimized memory layout)
176176
*
177177
* @param extMem External memory object
@@ -184,6 +184,36 @@ image_mem_handle map_external_image_memory(external_mem extMem,
184184
const image_descriptor &desc,
185185
const sycl::queue &syclQueue);
186186

187+
/**
188+
* @brief Maps an external memory object to a memory region described by the
189+
* returned void *
190+
*
191+
* @param extMem External memory object
192+
* @param offset Offset of memory region to map
193+
* @param size Size of memory region to map
194+
* @param syclDevice The device in which we create our image memory handle
195+
* @param syclContext The context in which we create our image memory handle
196+
* @return Memory handle to externally allocated memory on the device
197+
*/
198+
__SYCL_EXPORT
199+
void *map_external_linear_memory(external_mem extMem, uint64_t offset,
200+
uint64_t size, const sycl::device &syclDevice,
201+
const sycl::context &syclContext);
202+
203+
/**
204+
* @brief Maps an external memory object to a memory region described by the
205+
* returned void *
206+
*
207+
* @param extMem External memory object
208+
* @param offset Offset of memory region to map
209+
* @param size Size of memory region to map
210+
* @param syclQueue The queue in which we create our image memory handle
211+
* @return Memory handle to externally allocated memory on the device
212+
*/
213+
__SYCL_EXPORT
214+
void *map_external_linear_memory(external_mem extMem, uint64_t offset,
215+
uint64_t size, const sycl::queue &syclQueue);
216+
187217
/**
188218
* @brief Import external semaphore taking an external semaphore descriptor
189219
* (the type of which is dependent on the OS & external API)

sycl/source/detail/bindless_images.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,35 @@ image_mem_handle map_external_image_memory(external_mem extMem,
556556
syclQueue.get_context());
557557
}
558558

559+
__SYCL_EXPORT
560+
void *map_external_linear_memory(external_mem extMem, uint64_t offset,
561+
uint64_t size, const sycl::device &syclDevice,
562+
const sycl::context &syclContext) {
563+
std::shared_ptr<sycl::detail::context_impl> CtxImpl =
564+
sycl::detail::getSyclObjImpl(syclContext);
565+
ur_context_handle_t C = CtxImpl->getHandleRef();
566+
std::shared_ptr<sycl::detail::device_impl> DevImpl =
567+
sycl::detail::getSyclObjImpl(syclDevice);
568+
ur_device_handle_t Device = DevImpl->getHandleRef();
569+
const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
570+
571+
ur_exp_external_mem_handle_t urExternalMem{extMem.raw_handle};
572+
573+
void *retMemory;
574+
Plugin->call<sycl::errc::invalid>(urBindlessImagesMapExternalLinearMemoryExp,
575+
C, Device, offset, size, urExternalMem,
576+
&retMemory);
577+
578+
return retMemory;
579+
}
580+
581+
__SYCL_EXPORT
582+
void *map_external_linear_memory(external_mem extMem, uint64_t offset,
583+
uint64_t size, const sycl::queue &syclQueue) {
584+
return map_external_linear_memory(
585+
extMem, offset, size, syclQueue.get_device(), syclQueue.get_context());
586+
}
587+
559588
__SYCL_EXPORT void release_external_memory(external_mem extMem,
560589
const sycl::device &syclDevice,
561590
const sycl::context &syclContext) {

0 commit comments

Comments
 (0)