llvm-project/openmp/tools/omptest/include/OmptTesterStandalone.h
Michael Halkenhäuser 35f01cea65
[OpenMP] Add ompTest library to OpenMP (#147381)
Description
===========
OpenMP Tooling Interface Testing Library (ompTest) ompTest is a unit
testing framework for testing OpenMP implementations. It offers a
simple-to-use framework that allows a tester to check for OMPT events in
addition to regular unit testing code, supported by linking against
GoogleTest by default. It also facilitates writing concise tests while
bridging the semantic gap between the unit under test and the OMPT-event
testing.

Background
==========
This library has been developed to provide the means of testing OMPT
implementations with reasonable effort. Especially, asynchronous or
unordered events are supported and can be verified with ease, which may
prove to be challenging with LIT-based tests. Additionally, since the
assertions are part of the code being tested, ompTest can reference all
corresponding variables during assertion.

Basic Usage
===========
OMPT event assertions are placed before the code, which shall be tested.
These assertion can either be provided as one block or interleaved with
the test code. There are two types of asserters: (1) sequenced
"order-sensitive" and (2) set "unordered" assserters. Once the test is
being run, the corresponding events are triggered by the OpenMP runtime
and can be observed. Each of these observed events notifies asserters,
which then determine if the test should pass or fail.

Example (partial, interleaved)
==============================
```c++
  int N = 100000;
  int a[N];
  int b[N];

  OMPT_ASSERT_SEQUENCE(Target, TARGET, BEGIN, 0);
  OMPT_ASSERT_SEQUENCE(TargetDataOp, ALLOC, N * sizeof(int)); // a ?
  OMPT_ASSERT_SEQUENCE(TargetDataOp, H2D, N * sizeof(int), &a);
  OMPT_ASSERT_SEQUENCE(TargetDataOp, ALLOC, N * sizeof(int)); // b ?
  OMPT_ASSERT_SEQUENCE(TargetDataOp, H2D, N * sizeof(int), &b);
  OMPT_ASSERT_SEQUENCE(TargetSubmit, 1);
  OMPT_ASSERT_SEQUENCE(TargetDataOp, D2H, N * sizeof(int), nullptr, &b);
  OMPT_ASSERT_SEQUENCE(TargetDataOp, D2H, N * sizeof(int), nullptr, &a);
  OMPT_ASSERT_SEQUENCE(TargetDataOp, DELETE);
  OMPT_ASSERT_SEQUENCE(TargetDataOp, DELETE);
  OMPT_ASSERT_SEQUENCE(Target, TARGET, END, 0);

#pragma omp target parallel for
  {
    for (int j = 0; j < N; j++)
      a[j] = b[j];
  }
```

References
==========
This work has been presented at SC'24 workshops, see:
https://ieeexplore.ieee.org/document/10820689

Current State and Future Work
=============================
ompTest's development was mostly device-centric and aimed at OMPT device
callbacks and device-side tracing. Consequentially, a substantial part
of host-related events or features may not be supported in its current
state. However, we are confident that the related functionality can be
added and ompTest provides a general foundation for future OpenMP and
especially OMPT testing. This PR will allow us to upstream the
corresponding features, like OMPT device-side tracing in the future with
significantly reduced risk of introducing regressions in the process.

Build
=====
ompTest is linked against LLVM's GoogleTest by default, but can also be
built 'standalone'. Additionally, it comes with a set of unit tests,
which in turn require GoogleTest (overriding a standalone build). The
unit tests are added to the `check-openmp` target.

Use the following parameters to perform the corresponding build: 
`LIBOMPTEST_BUILD_STANDALONE` (Default: ${OPENMP_STANDALONE_BUILD})
`LIBOMPTEST_BUILD_UNITTESTS` (Default: OFF)

---------

Co-authored-by: Jan-Patrick Lehr <JanPatrick.Lehr@amd.com>
Co-authored-by: Joachim <protze@rz.rwth-aachen.de>
2025-08-21 13:46:30 +02:00

124 lines
4.5 KiB
C++

//===- OmptTesterStandalone.h - Standalone header variant -------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file represents the 'standalone' header variant, defining the actual
/// test classes and their behavior (it does not have external dependencies).
///
//===----------------------------------------------------------------------===//
#ifndef OPENMP_TOOLS_OMPTEST_INCLUDE_OMPTTESTERSTANDALONE_H
#define OPENMP_TOOLS_OMPTEST_INCLUDE_OMPTTESTERSTANDALONE_H
#include "OmptAssertEvent.h"
#include "OmptAsserter.h"
#include "OmptTesterGlobals.h"
#include <map>
#include <vector>
// Forward declarations.
namespace omptest {
struct OmptEventAsserter;
class OmptEventReporter;
class OmptSequencedAsserter;
} // namespace omptest
struct Error {
operator bool() { return Fail; }
bool Fail;
};
/// A pretty crude test case abstraction
struct TestCase {
TestCase(const std::string &name)
: IsDisabled(name.rfind("DISABLED_", 0) == 0), Name(name) {}
TestCase(const std::string &name, const omptest::AssertState &expected)
: IsDisabled(name.rfind("DISABLED_", 0) == 0), Name(name),
ExpectedState(expected) {}
virtual ~TestCase() = default;
Error exec();
virtual void execImpl() { assert(false && "Allocating base class"); }
bool IsDisabled{false};
std::string Name;
omptest::AssertState ExpectedState{omptest::AssertState::Pass};
omptest::AssertState ResultState{omptest::AssertState::Pass};
std::unique_ptr<omptest::OmptSequencedAsserter> SequenceAsserter =
std::make_unique<omptest::OmptSequencedAsserter>();
std::unique_ptr<omptest::OmptEventAsserter> SetAsserter =
std::make_unique<omptest::OmptEventAsserter>();
std::unique_ptr<omptest::OmptEventReporter> EventReporter =
std::make_unique<omptest::OmptEventReporter>();
};
/// A pretty crude test suite abstraction
struct TestSuite {
using TestCaseVec = std::vector<std::unique_ptr<TestCase>>;
std::string Name;
TestSuite() = default;
TestSuite(const TestSuite &O) = delete;
TestSuite(TestSuite &&O);
void setup();
void teardown();
TestCaseVec::iterator begin();
TestCaseVec::iterator end();
TestCaseVec TestCases;
};
/// Static class used to register all test cases and provide them to the driver
class TestRegistrar {
public:
static TestRegistrar &get();
static std::vector<TestSuite> getTestSuites();
static void addCaseToSuite(TestCase *TC, std::string TSName);
private:
TestRegistrar() = default;
TestRegistrar(const TestRegistrar &o) = delete;
TestRegistrar operator=(const TestRegistrar &o) = delete;
// Keep tests in order 'of appearance' (top -> bottom), avoid unordered_map
static std::map<std::string, TestSuite> Tests;
};
/// Hack to register test cases
struct Registerer {
Registerer(TestCase *TC, const std::string SuiteName);
};
/// Eventually executes all test suites and cases, should contain logic to skip
/// stuff if needed
struct Runner {
Runner() : TestSuites(TestRegistrar::get().getTestSuites()) {}
int run();
void reportError(const Error &Err);
void abortOrKeepGoing();
// Print an execution summary of all testsuites and their corresponding
// testcases.
void printSummary();
std::vector<TestSuite> TestSuites;
};
/// MACROS TO DEFINE A TESTSUITE + TESTCASE (like GoogleTest does)
#define XQUOTE(str) QUOTE(str)
#define QUOTE(str) #str
#define TEST_TEMPLATE(SuiteName, CaseName, ExpectedState) \
struct SuiteName##_##CaseName : public TestCase { \
SuiteName##_##CaseName() \
: TestCase(XQUOTE(CaseName), omptest::AssertState::ExpectedState) {} \
virtual void execImpl() override; \
}; \
static Registerer R_##SuiteName##CaseName(new SuiteName##_##CaseName(), \
#SuiteName); \
void SuiteName##_##CaseName::execImpl()
#define TEST(SuiteName, CaseName) \
TEST_TEMPLATE(SuiteName, CaseName, /*ExpectedState=*/pass)
#define TEST_XFAIL(SuiteName, CaseName) \
TEST_TEMPLATE(SuiteName, CaseName, /*ExpectedState=*/fail)
#endif