Skip to content
Draft
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
47 changes: 47 additions & 0 deletions sycl/include/sycl/usm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,53 @@ __SYCL_EXPORT void release_from_device_copy(const void *Ptr,
__SYCL_EXPORT void release_from_device_copy(const void *Ptr,
const queue &Queue);

__SYCL_EXPORT void *malloc_device(size_t numBytes, const device &syclDevice,
const property_list &propList = {});

template <typename T>
__SYCL_EXPORT T *malloc_device(size_t count, const device &syclDevice,
const property_list &propList = {});

__SYCL_EXPORT void *aligned_alloc_device(size_t alignment, size_t numBytes,
const device &syclDevice,
const property_list &propList = {});

template <typename T>
__SYCL_EXPORT T *aligned_alloc_device(size_t alignment, size_t count,
const device &syclDevice,
const property_list &propList = {});

__SYCL_EXPORT void *malloc_shared(size_t numBytes, const device &syclDevice,
const property_list &propList = {});

template <typename T>
__SYCL_EXPORT T *malloc_shared(size_t count, const device &syclDevice,
const property_list &propList = {});

__SYCL_EXPORT void *aligned_alloc_shared(size_t alignment, size_t numBytes,
const device &syclDevice,
const property_list &propList = {});

template <typename T>
__SYCL_EXPORT T *aligned_alloc_shared(size_t alignment, size_t count,
const device &syclDevice,
const property_list &propList = {});

__SYCL_EXPORT void *malloc(size_t numBytes, const device &syclDevice,
usm::alloc kind, const property_list &propList = {});

template <typename T>
__SYCL_EXPORT T *malloc(size_t count, const device &syclDevice, usm::alloc kind,
const property_list &propList = {});

__SYCL_EXPORT void *aligned_alloc(size_t alignment, size_t numBytes,
const device &syclDevice, usm::alloc kind,
const property_list &propList = {});

template <typename T>
__SYCL_EXPORT T *aligned_alloc(size_t alignment, size_t count,
const device &syclDevice, usm::alloc kind,
const property_list &propList = {});
} // namespace ext::oneapi::experimental

} // namespace _V1
Expand Down
88 changes: 88 additions & 0 deletions sycl/source/detail/usm/usm_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,94 @@ void release_from_device_copy(const void *Ptr, const context &Ctxt) {
void release_from_device_copy(const void *Ptr, const queue &Queue) {
release_from_usm_device_copy(Ptr, Queue.get_context());
}

void *malloc_device(size_t numBytes, const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::malloc_device(numBytes, syclDevice, ctxt, propList);
}

template <typename T>
T *malloc_device(size_t count, const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::malloc_device<T>(count, syclDevice, ctxt, propList);
}

void *aligned_alloc_device(size_t alignment, size_t numBytes,
const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::aligned_alloc_device(alignment, numBytes, syclDevice, ctxt,
propList);
}

template <typename T>
T *aligned_alloc_device(size_t alignment, size_t count,
const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::aligned_alloc_device<T>(alignment, count, syclDevice, ctxt,
propList);
}

void *malloc_shared(size_t numBytes, const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::malloc_shared(numBytes, syclDevice, ctxt, propList);
}

template <typename T>
T *malloc_shared(size_t count, const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::malloc_shared<T>(count, syclDevice, ctxt, propList);
}

void *aligned_alloc_shared(size_t alignment, size_t numBytes,
const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::aligned_alloc_shared(alignment, numBytes, syclDevice, ctxt,
propList);
}

template <typename T>
T *aligned_alloc_shared(size_t alignment, size_t count,
const device &syclDevice,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::aligned_alloc_shared<T>(alignment, count, syclDevice, ctxt,
propList);
}

void *malloc(size_t numBytes, const device &syclDevice, usm::alloc kind,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::malloc(numBytes, syclDevice, ctxt, kind, propList);
}

template <typename T>
T *malloc(size_t count, const device &syclDevice, usm::alloc kind,
const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::malloc_shared<T>(count, syclDevice, ctxt, kind, propList);
}

void *aligned_alloc(size_t alignment, size_t numBytes, const device &syclDevice,
usm::alloc kind, const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::aligned_alloc(alignment, numBytes, syclDevice, ctxt, kind,
propList);
}

template <typename T>
T *aligned_alloc(size_t alignment, size_t count, const device &syclDevice,
usm::alloc kind, const property_list &propList) {
sycl::context ctxt = syclDevice.get_platform().khr_get_default_context();
return sycl::aligned_alloc<T>(alignment, count, syclDevice, ctxt, kind,
propList);
}
} // namespace ext::oneapi::experimental

__SYCL_EXPORT void verifyUSMAllocatorProperties(const property_list &PropList) {
Expand Down
99 changes: 99 additions & 0 deletions sycl/test-e2e/USM/usm_shortcuts_utility.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// RUN: %{build} -o %t.out
// RUN: %{run} %t.out

//==------ usm_shortcuts_utility.cpp - USM malloc and aligned_alloc test
//-------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <sycl/detail/core.hpp>

#include <sycl/ext/intel/experimental/usm_properties.hpp>
#include <sycl/usm.hpp>

#include <cassert>

using namespace sycl;
using namespace sycl::ext::oneapi::experimental;

constexpr int N = 8;

static void check_and_free(int *array, const device &dev, const context &ctxt,
usm::alloc expected_type) {
// host device treats all allocations as host allocations
assert((get_pointer_type(array, ctxt) == expected_type) &&
"Allocation pointer has unexpected type.");
assert((get_pointer_device(array, ctxt) == dev) &&
"Allocation pointer has unexpected device associated with it.");
free(array, ctxt);
}

int main() {
queue q;
auto dev = q.get_device();
auto ctxt = q.get_context();
int *array;

if (dev.get_info<sycl::info::device::usm_host_allocations>()) {
array = (int *)malloc(N * sizeof(int), dev, usm::alloc::host);
check_and_free(array, dev, ctxt, usm::alloc::host);

array =
(int *)malloc(N * sizeof(int), dev, usm::alloc::host, property_list{});
check_and_free(array, dev, ctxt, usm::alloc::host);

array = (int *)aligned_alloc(alignof(long long), N * sizeof(int), dev,
usm::alloc::host);
check_and_free(array, dev, ctxt, usm::alloc::host);

array = (int *)aligned_alloc(alignof(long long), N * sizeof(int), dev,
usm::alloc::host, property_list{});
check_and_free(array, dev, ctxt, usm::alloc::host);
}

if (dev.get_info<sycl::info::device::usm_shared_allocations>()) {
array = (int *)malloc_shared(N * sizeof(int), dev);
check_and_free(array, dev, ctxt, usm::alloc::shared);

array = (int *)malloc_shared(
N * sizeof(int), dev,
property_list{
ext::intel::experimental::property::usm::buffer_location{2}});
check_and_free(array, dev, ctxt, usm::alloc::shared);

array =
(int *)aligned_alloc_shared(alignof(long long), N * sizeof(int), dev);
check_and_free(array, dev, ctxt, usm::alloc::shared);

array = (int *)aligned_alloc_shared(
alignof(long long), N * sizeof(int), dev,
property_list{
ext::intel::experimental::property::usm::buffer_location{2}});
check_and_free(array, dev, ctxt, usm::alloc::shared);
}

if (dev.get_info<sycl::info::device::usm_device_allocations>()) {
array = (int *)malloc_device(N * sizeof(int), dev);
check_and_free(array, dev, ctxt, usm::alloc::device);

array = (int *)malloc_device(
N, dev,
property_list{
ext::intel::experimental::property::usm::buffer_location(2)});
check_and_free(array, dev, ctxt, usm::alloc::device);

array =
(int *)aligned_alloc_device(alignof(long long), N * sizeof(int), dev);
check_and_free(array, dev, ctxt, usm::alloc::device);

array = (int *)aligned_alloc_device(alignof(long long), N * sizeof(int),
dev, property_list{});
check_and_free(array, dev, ctxt, usm::alloc::device);
}

return 0;
}
Loading