| 1 |
//@HEADER |
| 2 |
// ************************************************************************ |
| 3 |
// |
| 4 |
// Kokkos v. 4.0 |
| 5 |
// Copyright (2022) National Technology & Engineering |
| 6 |
// Solutions of Sandia, LLC (NTESS). |
| 7 |
// |
| 8 |
// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 |
// the U.S. Government retains certain rights in this software. |
| 10 |
// |
| 11 |
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
// See https://kokkos.org/LICENSE for license information. |
| 13 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 |
// |
| 15 |
//@HEADER |
| 16 |
|
| 17 |
#include <Kokkos_Core.hpp> |
| 18 |
#include <TestSYCL_Category.hpp> |
| 19 |
|
| 20 |
#include <array> |
| 21 |
|
| 22 |
namespace Test { |
| 23 |
|
| 24 |
// Test whether allocations survive Kokkos initialize/finalize if done via Raw |
| 25 |
// SYCL. |
| 26 |
TEST(sycl, raw_sycl_interop) { |
| 27 |
// Make sure all queues use the same context |
| 28 |
Kokkos::initialize(); |
| 29 |
Kokkos::SYCL default_space; |
| 30 |
sycl::context default_context = default_space.sycl_queue().get_context(); |
| 31 |
|
| 32 |
sycl::queue queue(default_context, sycl::default_selector_v, |
| 33 |
sycl::property::queue::in_order()); |
| 34 |
constexpr int n = 100; |
| 35 |
int* p = sycl::malloc_device<int>(n, queue); |
| 36 |
{ |
| 37 |
TEST_EXECSPACE space(queue); |
| 38 |
Kokkos::View<int*, Kokkos::MemoryTraits<Kokkos::Unmanaged>> v(p, n); |
| 39 |
Kokkos::deep_copy(space, v, 5); |
| 40 |
} |
| 41 |
Kokkos::finalize(); |
| 42 |
|
| 43 |
queue.submit([&](sycl::handler& cgh) { |
| 44 |
cgh.parallel_for(sycl::range<1>(n), [=](int idx) { p[idx] += idx; }); |
| 45 |
}); |
| 46 |
queue.wait_and_throw(); |
| 47 |
|
| 48 |
std::array<int, n> h_p; |
| 49 |
queue.memcpy(h_p.data(), p, sizeof(int) * n); |
| 50 |
queue.wait_and_throw(); |
| 51 |
sycl::free(p, queue); |
| 52 |
|
| 53 |
int64_t sum = 0; |
| 54 |
int64_t sum_expect = 0; |
| 55 |
for (int i = 0; i < n; i++) { |
| 56 |
sum += h_p[i]; |
| 57 |
sum_expect += 5 + i; |
| 58 |
} |
| 59 |
|
| 60 |
ASSERT_EQ(sum, sum_expect); |
| 61 |
} |
| 62 |
} // namespace Test |