Debian Codegraph @

packages/kokkos/core/unit_test/sycl/TestSYCL_InterOp_Graph.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 <filesystem>
18 #include <fstream>
19 #include <regex>
20
21 #include <TestSYCL_Category.hpp>
22 #include <Kokkos_Core.hpp>
23 #include <Kokkos_Graph.hpp>
24
25 #include <gtest/gtest.h>
26
27 namespace {
28
29 template <typename ViewType>
30 struct Increment {
31 ViewType data;
32
33 KOKKOS_FUNCTION
34 void operator()(const int) const { ++data(); }
35 };
36
37 TEST(TEST_CATEGORY, graph_get_native_return_types_are_references) {
38 using graph_t = Kokkos::Experimental::Graph<Kokkos::SYCL>;
39 static_assert(
40 std::is_reference_v<decltype(std::declval<graph_t>().native_graph())>);
41 static_assert(std::is_reference_v<
42 decltype(std::declval<graph_t>().native_graph_exec())>);
43 }
44
45 // This test checks the promises of Kokkos::Graph against its
46 // underlying SYCL native objects.
47 TEST(TEST_CATEGORY, graph_promises_on_native_objects) {
48 auto graph = Kokkos::Experimental::create_graph<Kokkos::SYCL>();
49
50 auto root = Kokkos::Impl::GraphAccess::create_root_ref(graph);
51
52 // Before instantiation, the SYCL graph is valid, but the SYCL executable
53 // graph is still null. Since the SYCL command graph is a regular object,
54 // no check is needed.
55 // However, the executable SYCL command graph is stored as an optional,
56 // so let's check it is empty for now.
57 ASSERT_FALSE(graph.native_graph_exec().has_value());
58
59 // After instantiation, both native objects are valid.
60 graph.instantiate();
61
62 ASSERT_TRUE(graph.native_graph_exec().has_value());
63 }
64
65 // Use native SYCL graph to generate a DOT representation.
66 TEST(TEST_CATEGORY, graph_instantiate_and_debug_dot_print) {
67 using view_t = Kokkos::View<int, Kokkos::SYCL>;
68
69 const Kokkos::SYCL exec{};
70
71 view_t data(Kokkos::view_alloc(exec, "witness"));
72
73 auto graph = Kokkos::Experimental::create_graph(exec);
74
75 auto root = Kokkos::Impl::GraphAccess::create_root_ref(graph);
76
77 root.then_parallel_for(1, Increment<view_t>{data});
78
79 graph.instantiate();
80
81 ASSERT_EQ(graph.native_graph().get_nodes().size(), 2u);
82
83 #if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 9
84 GTEST_SKIP()
85 << "The GNU C++ Library (libstdc++) versions less than 9.1 "
86 "require linking with `-lstdc++fs` when using std::filesystem";
87 #elif defined(_LIBCPP_VERSION) && _LIBCPP_VERSION < 110000
88 GTEST_SKIP()
89 << "The LLVM C++ Standard Library (libc++) versions less than "
90 "11 require linking with `-lc++fs` when using std::filesystem";
91 #else
92 const auto dot = std::filesystem::temp_directory_path() / "sycl_graph.dot";
93
94 graph.native_graph().print_graph(dot, true);
95
96 ASSERT_TRUE(std::filesystem::exists(dot));
97 ASSERT_GT(std::filesystem::file_size(dot), 0u);
98
99 // We could write a check against the full kernel's function signature, but
100 // it would make the test rely too much on internal implementation details.
101 // Therefore, we just look for the functor and policy. Note that the
102 // signature is mangled in the 'dot' output.
103 const std::string expected("[A-Za-z0-9_]+Increment[A-Za-z0-9_]+RangePolicy");
104
105 std::stringstream buffer;
106 buffer << std::ifstream(dot).rdbuf();
107
108 ASSERT_TRUE(std::regex_search(buffer.str(), std::regex(expected)))
109 << "Could not find expected signature regex " << std::quoted(expected)
110 << " in " << dot;
111 #endif
112 }
113
114 } // namespace