Skip to content

Commit

Permalink
graph: allow access to native graph object
Browse files Browse the repository at this point in the history
The native graph objects should be accessible for advanced users.
  • Loading branch information
romintomasetti committed Mar 29, 2024
1 parent a53d30a commit 7820e8b
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core/src/Cuda/Kokkos_Cuda_Graph_Impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ struct GraphImpl<Kokkos::Cuda> {
m_execution_space, _graph_node_kernel_ctor_tag{},
aggregate_kernel_impl_t{});
}

cudaGraph_t& get_cuda_graph() { return m_graph; }
cudaGraphExec_t& get_cuda_graph_exec() { return m_graph_exec; }
};

} // end namespace Impl
Expand Down
3 changes: 3 additions & 0 deletions core/src/HIP/Kokkos_HIP_Graph_Impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class GraphImpl<Kokkos::HIP> {
template <class... PredecessorRefs>
auto create_aggregate_ptr(PredecessorRefs&&...);

hipGraph_t& get_hip_graph() { return m_graph; }
hipGraphExec_t& get_hip_graph_exec() { return m_graph_exec; }

private:
void instantiate_graph() {
constexpr size_t error_log_size = 256;
Expand Down
2 changes: 1 addition & 1 deletion core/unit_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ if(Kokkos_ENABLE_HIP)
hip/TestHIP_InterOp_Streams.cpp
)
KOKKOS_ADD_EXECUTABLE_AND_TEST(
UnitTest_HIPGraph
CoreUnitTest_HIPGraph
SOURCES
UnitTestMainInit.cpp
hip/TestHIP_Graph.cpp
Expand Down
64 changes: 64 additions & 0 deletions core/unit_test/cuda/TestCuda_Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,67 @@

#include <TestCuda_Category.hpp>
#include <TestGraph.hpp>

namespace Test {

/**
* @test This test ensures that the underlying @c CUDA objects can be retrieved
* from a @c Kokkos::Graph.
*
* It also ensures that these objects can be modified "externally", thereby
* testing "interoperability".
*/
TEST(TEST_CATEGORY, cuda_graph_instantiate_and_debug_dot_print) {
//! Build a rank-1 zero-initialized view.
using view_t = Kokkos::View<int, TEST_EXECSPACE>;
using view_h_t = Kokkos::View<int, Kokkos::HostSpace>;
view_t data("witness");

//! Build a graph, whose sole node will apply @ref AddOne on @ref data.
auto graph =
Kokkos::Experimental::create_graph<TEST_EXECSPACE>([&](auto root) {
root.then_parallel_for(
1, SetViewToValueFunctor<TEST_EXECSPACE, int>{data, 1});
});

//! Retrieve the @c Kokkos::Impl::Graph.
auto root_node_ref = Kokkos::Impl::GraphAccess::create_root_ref(graph);
auto graph_ptr_impl =
Kokkos::Impl::GraphAccess::get_graph_weak_ptr(root_node_ref).lock();

/// At this stage, the @c Cuda "executable" graph is null
/// because it has not been instantiated.
ASSERT_EQ(graph_ptr_impl->get_cuda_graph_exec(), nullptr);

//! Instantiate the graph manually.
constexpr size_t error_log_size = 256;
cudaGraphNode_t error_node = nullptr;
char error_log[error_log_size];
cudaGraphInstantiate(&graph_ptr_impl->get_cuda_graph_exec(),
graph_ptr_impl->get_cuda_graph(), &error_node, error_log,
error_log_size);
ASSERT_EQ(error_node, nullptr) << error_log;

/// At this stage, the @c Cuda "executable" graph should not be null,
/// because it has been instantiated.
ASSERT_NE(graph_ptr_impl->get_cuda_graph_exec(), nullptr);
const cudaGraphExec_t manual = graph_ptr_impl->get_cuda_graph_exec();

/// Submit the graph through @c Kokkos::Graph API.
/// Note that @c Kokkos::Graph::submit should not modify the
/// @c cudaGraphExec_t object because we instantiated the graph manually.
graph.submit();

ASSERT_EQ(graph_ptr_impl->get_cuda_graph_exec(), manual);

//! The view has been modified and contains a one.
view_h_t data_h("witness on host");
Kokkos::deep_copy(graph.get_execution_space(), data_h, data);
ASSERT_EQ(data_h(), 1);

//! Export the graph structure as a DOT graph.
cudaGraphDebugDotPrint(graph_ptr_impl->get_cuda_graph(), "cuda_graph.dot",
cudaGraphDebugDotFlagsVerbose);
}

} // namespace Test
66 changes: 66 additions & 0 deletions core/unit_test/hip/TestHIP_Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,69 @@

#include <TestHIP_Category.hpp>
#include <TestGraph.hpp>

namespace Test {

/**
* @test This test ensures that the underlying @c HIP objects can be retrieved
* from a @c Kokkos::Graph.
*
* It also ensures that these objects can be modified "externally", thereby
* testing "interoperability".
*/
TEST(TEST_CATEGORY, hip_graph_instantiate_and_debug_dot_print) {
//! Build a rank-1 zero-initialized view.
using view_t = Kokkos::View<int, TEST_EXECSPACE>;
using view_h_t = Kokkos::View<int, Kokkos::HostSpace>;
view_t data("witness");

//! Build a graph, whose sole node will apply @ref AddOne on @ref data.
auto graph =
Kokkos::Experimental::create_graph<TEST_EXECSPACE>([&](auto root) {
root.then_parallel_for(
1, SetViewToValueFunctor<TEST_EXECSPACE, int>{data, 1});
});

//! Retrieve the @c Kokkos::Impl::Graph.
auto root_node_ref = Kokkos::Impl::GraphAccess::create_root_ref(graph);
auto graph_ptr_impl =
Kokkos::Impl::GraphAccess::get_graph_weak_ptr(root_node_ref).lock();

/// At this stage, the @c HIP "executable" graph is null
/// because it has not been instantiated.
ASSERT_EQ(graph_ptr_impl->get_hip_graph_exec(), nullptr);

//! Instantiate the graph manually.
constexpr size_t error_log_size = 256;
hipGraphNode_t error_node = nullptr;
char error_log[error_log_size];
KOKKOS_IMPL_HIP_SAFE_CALL(hipGraphInstantiate(
&graph_ptr_impl->get_hip_graph_exec(),
graph_ptr_impl->get_hip_graph(), &error_node, error_log,
error_log_size));
ASSERT_EQ(error_node, nullptr) << error_log;

/// At this stage, the @c HIP "executable" graph should not be null,
/// because it has been instantiated.
ASSERT_NE(graph_ptr_impl->get_hip_graph_exec(), nullptr);
const hipGraphExec_t manual = graph_ptr_impl->get_hip_graph_exec();

/// Submit the graph through @c Kokkos::Graph API.
/// Note that @c Kokkos::Graph::submit should not modify the @c hipGraphExec_t
/// object because we instantiated the graph manually.
graph.submit();

ASSERT_EQ(graph_ptr_impl->get_hip_graph_exec(), manual);

//! The view has been modified and contains a one.
view_h_t data_h("witness on host");
Kokkos::deep_copy(graph.get_execution_space(), data_h, data);
ASSERT_EQ(data_h(), 1);

//! Export the graph structure as a DOT graph.
KOKKOS_IMPL_HIP_SAFE_CALL(
hipGraphDebugDotPrint(graph_ptr_impl->get_hip_graph(), "hip_graph.dot",
hipGraphDebugDotFlagsVerbose));
}

} // namespace Test

0 comments on commit 7820e8b

Please sign in to comment.