|
| 1 | +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t1.out |
| 2 | +// RUN: %RUN_ON_HOST %t1.out |
| 3 | + |
| 4 | +//==------ usm_alloc_utility.cpp - USM malloc and aligned_alloc test -------==// |
| 5 | +// |
| 6 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 7 | +// See https://llvm.org/LICENSE.txt for license information. |
| 8 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#include <CL/sycl.hpp> |
| 13 | + |
| 14 | +#include <cassert> |
| 15 | + |
| 16 | +using namespace cl::sycl; |
| 17 | + |
| 18 | +constexpr int N = 8; |
| 19 | + |
| 20 | +int main() { |
| 21 | + queue q; |
| 22 | + auto dev = q.get_device(); |
| 23 | + auto ctxt = q.get_context(); |
| 24 | + int *array; |
| 25 | + |
| 26 | + if (dev.get_info<info::device::usm_host_allocations>()) { |
| 27 | + array = (int *)malloc_host(N * sizeof(int), q); |
| 28 | + assert((get_pointer_type(array, ctxt) == usm::alloc::host) && |
| 29 | + "Allocation pointer should be host type"); |
| 30 | + assert((get_pointer_device(array, ctxt) == dev) && |
| 31 | + "Allocation pointer should be host type"); |
| 32 | + free(array, ctxt); |
| 33 | + |
| 34 | + array = |
| 35 | + (int *)aligned_alloc_host(alignof(long long), N * sizeof(int), ctxt); |
| 36 | + assert((get_pointer_type(array, ctxt) == usm::alloc::host) && |
| 37 | + "Allocation pointer should be host type"); |
| 38 | + assert((get_pointer_device(array, ctxt) == dev) && |
| 39 | + "Allocation pointer should be host type"); |
| 40 | + free(array, ctxt); |
| 41 | + } |
| 42 | + |
| 43 | + if (dev.get_info<info::device::usm_shared_allocations>()) { |
| 44 | + array = (int *)malloc_shared(N * sizeof(int), q); |
| 45 | + // host device treats all allocations as host allocations |
| 46 | + assert((get_pointer_type(array, ctxt) == usm::alloc::host) && |
| 47 | + "Allocation pointer should be host type"); |
| 48 | + assert((get_pointer_device(array, ctxt) == dev) && |
| 49 | + "Allocation pointer should be host type"); |
| 50 | + free(array, ctxt); |
| 51 | + |
| 52 | + array = (int *)aligned_alloc_shared(alignof(long long), N * sizeof(int), |
| 53 | + dev, ctxt); |
| 54 | + // host device treats all allocations as host allocations |
| 55 | + assert((get_pointer_type(array, ctxt) == usm::alloc::host) && |
| 56 | + "Allocation pointer should be host type"); |
| 57 | + assert((get_pointer_device(array, ctxt) == dev) && |
| 58 | + "Allocation pointer should be host type"); |
| 59 | + free(array, ctxt); |
| 60 | + } |
| 61 | + |
| 62 | + if (dev.get_info<info::device::usm_device_allocations>()) { |
| 63 | + array = (int *)malloc_device(N * sizeof(int), q); |
| 64 | + // host device treats all allocations as host allocations |
| 65 | + assert((get_pointer_type(array, ctxt) == usm::alloc::host) && |
| 66 | + "Allocation pointer should be host type"); |
| 67 | + assert((get_pointer_device(array, ctxt) == dev) && |
| 68 | + "Allocation pointer should be host type"); |
| 69 | + free(array, ctxt); |
| 70 | + |
| 71 | + array = (int *)aligned_alloc_device(alignof(long long), N * sizeof(int), |
| 72 | + dev, ctxt); |
| 73 | + // host device treats all allocations as host allocations |
| 74 | + assert((get_pointer_type(array, ctxt) == usm::alloc::host) && |
| 75 | + "Allocation pointer should be host type"); |
| 76 | + assert((get_pointer_device(array, ctxt) == dev) && |
| 77 | + "Allocation pointer should be host type"); |
| 78 | + free(array, ctxt); |
| 79 | + } |
| 80 | + |
| 81 | + return 0; |
| 82 | +} |
0 commit comments