Debian Codegraph @

packages/kokkos/core/unit_test/sycl/TestSYCL_InterOp_Streams.cpp

raw
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 <TestSYCL_Category.hpp>
18 #include <Test_InterOp_Streams.hpp>
19
20 namespace Test {
21 // Test Interoperability with SYCL Streams
22 TEST(sycl, raw_sycl_queues) {
23 // Make sure all queues use the same context
24 Kokkos::initialize();
25 Kokkos::SYCL default_space;
26 sycl::context default_context = default_space.sycl_queue().get_context();
27
28 sycl::queue queue(default_context, sycl::default_selector_v,
29 sycl::property::queue::in_order());
30 int* p = sycl::malloc_device<int>(100, queue);
31 using MemorySpace = typename TEST_EXECSPACE::memory_space;
32
33 {
34 TEST_EXECSPACE space0(queue);
35 Kokkos::View<int*, TEST_EXECSPACE> v(p, 100);
36 Kokkos::deep_copy(space0, v, 5);
37 int sum = 0;
38
39 Kokkos::parallel_for("Test::sycl::raw_sycl_queue::Range",
40 Kokkos::RangePolicy<TEST_EXECSPACE>(space0, 0, 100),
41 FunctorRange<MemorySpace>(v));
42 Kokkos::parallel_reduce("Test::sycl::raw_sycl_queue::RangeReduce",
43 Kokkos::RangePolicy<TEST_EXECSPACE>(space0, 0, 100),
44 FunctorRangeReduce<MemorySpace>(v), sum);
45 space0.fence();
46 ASSERT_EQ(6 * 100, sum);
47
48 Kokkos::parallel_for("Test::sycl::raw_sycl_queue::MDRange",
49 Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>>(
50 space0, {0, 0}, {10, 10}),
51 FunctorMDRange<MemorySpace>(v));
52 space0.fence();
53 Kokkos::parallel_reduce(
54 "Test::sycl::raw_sycl_queue::MDRangeReduce",
55 Kokkos::MDRangePolicy<TEST_EXECSPACE, Kokkos::Rank<2>>(space0, {0, 0},
56 {10, 10}),
57 FunctorMDRangeReduce<MemorySpace>(v), sum);
58 space0.fence();
59 ASSERT_EQ(7 * 100, sum);
60
61 Kokkos::parallel_for("Test::sycl::raw_sycl_queue::Team",
62 Kokkos::TeamPolicy<TEST_EXECSPACE>(space0, 10, 10),
63 FunctorTeam<MemorySpace, TEST_EXECSPACE>(v));
64 space0.fence();
65 Kokkos::parallel_reduce("Test::sycl::raw_sycl_queue::Team",
66 Kokkos::TeamPolicy<TEST_EXECSPACE>(space0, 10, 10),
67 FunctorTeamReduce<MemorySpace, TEST_EXECSPACE>(v),
68 sum);
69 space0.fence();
70 ASSERT_EQ(8 * 100, sum);
71 }
72 Kokkos::finalize();
73
74 // Try to use the queue after Kokkos' copy got out-of-scope.
75 // This kernel corresponds to "offset_streams" in the HIP and CUDA tests.
76 queue.submit([&](sycl::handler& cgh) {
77 cgh.parallel_for(sycl::range<1>(100), [=](int idx) { p[idx] += idx; });
78 });
79 queue.wait_and_throw();
80
81 int h_p[100];
82 queue.memcpy(h_p, p, sizeof(int) * 100);
83 queue.wait_and_throw();
84 int64_t sum = 0;
85 int64_t sum_expect = 0;
86 for (int i = 0; i < 100; i++) {
87 sum += h_p[i];
88 sum_expect += 8 + i;
89 }
90
91 ASSERT_EQ(sum, sum_expect);
92 }
93 } // namespace Test