From 64a9f1a912112ac14fe53bad4309ec464c6cb923 Mon Sep 17 00:00:00 2001 From: Vivek Kale <11766050+vlkale@users.noreply.github.com> Date: Mon, 1 Apr 2024 15:55:04 -0700 Subject: [PATCH] test_sampler.cpp: put in code for sampler test --- tests/sampler/test_sampler.cpp | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/tests/sampler/test_sampler.cpp b/tests/sampler/test_sampler.cpp index 8b1378917..88c84fda7 100644 --- a/tests/sampler/test_sampler.cpp +++ b/tests/sampler/test_sampler.cpp @@ -1 +1,66 @@ +#include +#include +#include +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "Kokkos_Core.hpp" + +struct Tester { + template + explicit Tester(const execution_space& space) { + //! Explicitly launch a kernel with a name, and run it 150 times with kernel + //! logger. Use a periodic sampling with skip rate 51. This should print + //! out 2 invocation, and there is a single matcher with a regular + //! expression to check this. + + for (int iter = 0; iter < 150; iter++) { + Kokkos::parallel_for("named kernel", + Kokkos::RangePolicy(space, 0, 1), + *this); + } + } + + KOKKOS_FUNCTION void operator()(const int) const {} +}; + +static const std::vector matchers{ + "(.*)KokkosP: sample 51 calling child-begin function...(.*)", + "(.*)KokkosP: sample 51 finished with child-begin function.(.*)", + "(.*)KokkosP: sample 51 calling child-end function...(.*)", + "(.*)KokkosP: sample 51 calling child-end function.(.*)", + "(.*)KokkosP: sample 102 calling child-begin function...(.*)", + "(.*)KokkosP: sample 102 finished with child-begin function.(.*)", + "(.*)KokkosP: sample 102 calling child-end function...(.*)", + "(.*)KokkosP: sample 102 calling child-end function.(.*)"}; + +/** + * @test This test checks that the tool effectively samples. + * + + */ +TEST(SamplerTest, ktoEnvVarDefault) { + //! Initialize @c Kokkos. + Kokkos::initialize(); + + //! Redirect output for later analysis. + std::cout.flush(); + std::ostringstream output; + std::streambuf* coutbuf = std::cout.rdbuf(output.rdbuf()); + + //! Run tests. @todo Replace this with Google Test. + Tester tester(Kokkos::DefaultExecutionSpace{}); + + //! Finalize @c Kokkos. + Kokkos::finalize(); + + //! Restore output buffer. + // std::cout.flush(); + std::cout.rdbuf(coutbuf); + std::cout << output.str() << std::endl; + + //! Analyze test output. + for (const auto& matcher : matchers) { + EXPECT_THAT(output.str(), ::testing::ContainsRegex(matcher)); + } // end TEST +}