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

156 lines
4.4 KiB
C++

//===- Logging.h - General logging class ------------------------*- 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
/// Provides ompTest-tailored logging, with log-levels and formatting/coloring.
///
//===----------------------------------------------------------------------===//
#ifndef OPENMP_TOOLS_OMPTEST_INCLUDE_LOGGING_H
#define OPENMP_TOOLS_OMPTEST_INCLUDE_LOGGING_H
#include "OmptAssertEvent.h"
#include <iostream>
#include <map>
#include <mutex>
#include <set>
#include <sstream>
#include <string>
namespace omptest {
namespace logging {
enum class Level : uint32_t {
// Levels (Note: DEBUG may already be reserved)
Diagnostic = 10,
Info = 20,
Warning = 30,
Error = 40,
Critical = 50,
// Types used for formatting options
Default,
ExpectedEvent,
ObservedEvent,
OffendingEvent,
// Suppress all prints
Silent = 0xFFFFFFFF
};
enum class FormatOption : uint32_t {
// General options
// Note: Bold is actually "BRIGHT" -- But it will be perceived as 'bold' font
// It is implicitly switching colors to the 'Light' variant
// Thus, it has -NO EFFECT- when already using a Light* color
None = 0,
Bold = 1,
Dim = 2,
Underlined = 4,
Blink = 5,
Inverted = 7,
Hidden = 8,
// Foreground colors
ColorDefault = 39,
ColorBlack = 30,
ColorRed = 31,
ColorGreen = 32,
ColorYellow = 33,
ColorBlue = 34,
ColorMagenta = 35,
ColorCyan = 36,
ColorLightGray = 37,
ColorDarkGray = 90,
ColorLightRed = 91,
ColorLightGreen = 92,
ColorLightYellow = 93,
ColorLightBlue = 94,
ColorLightMagenta = 95,
ColorLightCyan = 96,
ColorWhite = 97,
// Background colors
ColorBackgroundDefault = 49,
ColorBackgroundBlack = 40,
ColorBackgroundRed = 41,
ColorBackgroundGreen = 42,
ColorBackgroundYellow = 43,
ColorBackgroundBlue = 44,
ColorBackgroundMagenta = 45,
ColorBackgroundCyan = 46,
ColorBackgroundLightGray = 47,
ColorBackgroundDarkGray = 100,
ColorBackgroundLightRed = 101,
ColorBackgroundLightGreen = 102,
ColorBackgroundLightYellow = 103,
ColorBackgroundLightBlue = 104,
ColorBackgroundLightMagenta = 105,
ColorBackgroundLightCyan = 106,
ColorBackgroundWhite = 107
};
/// Returns a string representation of the given logging level.
const char *to_string(Level LogLevel);
/// Returns the format options as escaped sequence, for the given logging level
std::string getFormatSequence(Level LogLevel = Level::Default);
/// Format the given message with the provided option(s) and return it.
/// Here formatting is only concerning control sequences using <Esc> character
/// which can be obtained using '\e' (on console), '\033' or '\x1B'.
std::string format(const std::string &Message, FormatOption Option);
std::string format(const std::string &Message, std::set<FormatOption> Options);
class Logger {
public:
Logger(Level LogLevel = Level::Warning, std::ostream &OutStream = std::cerr,
bool FormatOutput = true);
~Logger();
/// Log the given message to the output.
void log(const std::string &Message, Level LogLevel) const;
/// Log a single event mismatch.
void logEventMismatch(const std::string &Message,
const omptest::OmptAssertEvent &OffendingEvent,
Level LogLevel = Level::Error) const;
/// Log an event-pair mismatch.
void logEventMismatch(const std::string &Message,
const omptest::OmptAssertEvent &ExpectedEvent,
const omptest::OmptAssertEvent &ObservedEvent,
Level LogLevel = Level::Error) const;
/// Set if output is being formatted (e.g. colored).
void setFormatOutput(bool Enabled);
/// Return the current (minimum) Logging Level.
Level getLoggingLevel() const;
/// Set the (minimum) Logging Level.
void setLoggingLevel(Level LogLevel);
private:
/// The minimum logging level that is considered by the logger instance.
Level LoggingLevel;
/// The output stream used by the logger instance.
std::ostream &OutStream;
/// Determine if log messages are formatted using control sequences.
bool FormatOutput;
/// Mutex to ensure serialized logging
mutable std::mutex LogMutex;
};
} // namespace logging
} // namespace omptest
#endif