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

142 lines
4.4 KiB
C++

#include "OmptAssertEvent.h"
#include "OmptAsserter.h"
#include "OmptTester.h"
#include <omp-tools.h>
#include "gtest/gtest.h"
using OS = omptest::ObserveState;
using OAE = omptest::OmptAssertEvent;
TEST(CompareOperatorTests, ThreadBeginIdentity) {
auto TBInitial =
OAE::ThreadBegin("dflt", "", OS::Always, ompt_thread_initial);
auto TBWorker = OAE::ThreadBegin("dflt", "", OS::Always, ompt_thread_worker);
auto TBOther = OAE::ThreadBegin("dflt", "", OS::Always, ompt_thread_other);
auto TBUnknown =
OAE::ThreadBegin("dflt", "", OS::Always, ompt_thread_unknown);
ASSERT_EQ(TBInitial, TBInitial);
ASSERT_EQ(TBWorker, TBWorker);
ASSERT_EQ(TBOther, TBOther);
ASSERT_EQ(TBUnknown, TBUnknown);
}
TEST(CompareOperatorTests, ThreadEndIdentity) {
auto TE = OAE::ThreadEnd("dflt", "", OS::Always);
ASSERT_EQ(TE, TE);
}
TEST(CompareOperatorTests, ParallelBeginIdentity) {
auto PBNumT = OAE::ParallelBegin("thrdenable", "", OS::Always, 3);
ASSERT_EQ(PBNumT, PBNumT);
}
TEST(CompareOperatorTests, ParallelEndIdentity) {
auto PEDflt = OAE::ParallelEnd("dflt", "", OS::Always);
// TODO: Add cases with parallel data set, task data set, flags
ASSERT_EQ(PEDflt, PEDflt);
}
TEST(CompareOperatorTests, WorkIdentity) {
auto WDLoopBgn =
OAE::Work("loopbgn", "", OS::Always, ompt_work_loop, ompt_scope_begin);
auto WDLoopEnd =
OAE::Work("loopend", "", OS::Always, ompt_work_loop, ompt_scope_end);
ASSERT_EQ(WDLoopBgn, WDLoopBgn);
ASSERT_EQ(WDLoopEnd, WDLoopEnd);
auto WDSectionsBgn = OAE::Work("sectionsbgn", "", OS::Always,
ompt_work_sections, ompt_scope_begin);
auto WDSectionsEnd = OAE::Work("sectionsend", "", OS::Always,
ompt_work_sections, ompt_scope_end);
// TODO: singleexecutor, single_other, workshare, distribute, taskloop, scope,
// loop_static, loop_dynamic, loop_guided, loop_other
ASSERT_EQ(WDSectionsBgn, WDSectionsBgn);
ASSERT_EQ(WDSectionsEnd, WDSectionsEnd);
}
TEST(CompareOperatorTests, DispatchIdentity) {
auto DIDflt = OAE::Dispatch("dflt", "", OS::Always);
ASSERT_EQ(DIDflt, DIDflt);
}
TEST(CompareOperatorTests, TaskCreateIdentity) {
auto TCDflt = OAE::TaskCreate("dflt", "", OS::Always);
ASSERT_EQ(TCDflt, TCDflt);
}
TEST(CompareOperatorTests, TaskScheduleIdentity) {
auto TS = OAE::TaskSchedule("dflt", "", OS::Always);
ASSERT_EQ(TS, TS);
}
TEST(CompareOperatorTests, ImplicitTaskIdentity) {
auto ITDfltBgn =
OAE::ImplicitTask("dfltbgn", "", OS::Always, ompt_scope_begin);
auto ITDfltEnd = OAE::ImplicitTask("dfltend", "", OS::Always, ompt_scope_end);
ASSERT_EQ(ITDfltBgn, ITDfltBgn);
ASSERT_EQ(ITDfltEnd, ITDfltEnd);
}
TEST(CompareOperatorTests, SyncRegionIdentity) {
auto SRDfltBgn =
OAE::SyncRegion("srdfltbgn", "", OS::Always,
ompt_sync_region_barrier_explicit, ompt_scope_begin);
auto SRDfltEnd =
OAE::SyncRegion("srdfltend", "", OS::Always,
ompt_sync_region_barrier_explicit, ompt_scope_end);
ASSERT_EQ(SRDfltBgn, SRDfltBgn);
ASSERT_EQ(SRDfltEnd, SRDfltEnd);
}
TEST(CompareOperatorTests, TargetIdentity) {
auto TargetDfltBgn =
OAE::Target("dfltbgn", "", OS::Always, ompt_target, ompt_scope_begin);
auto TargetDfltEnd =
OAE::Target("dfltend", "", OS::Always, ompt_target, ompt_scope_end);
ASSERT_EQ(TargetDfltBgn, TargetDfltBgn);
ASSERT_EQ(TargetDfltEnd, TargetDfltEnd);
auto TargetDevBgn = OAE::Target("tgtdevbgn", "", OS::Always, ompt_target,
ompt_scope_begin, 1);
auto TargetDevEnd =
OAE::Target("tgtdevend", "", OS::Always, ompt_target, ompt_scope_end, 1);
ASSERT_EQ(TargetDevBgn, TargetDevBgn);
ASSERT_EQ(TargetDevEnd, TargetDevEnd);
}
TEST(CompareOperatorTests, BufferRecordIdentity) {
// Default, no time limit or anything
auto BRDflt =
OAE::BufferRecord("dflt", "", OS::Always, ompt_callback_target_submit);
// Minimum time set, no max time
auto BRMinSet = OAE::BufferRecord("minset", "", OS::Always,
ompt_callback_target_submit, 10);
// Minimum time and maximum time set
auto BRMinMaxSet = OAE::BufferRecord("minmaxset", "", OS::Always,
ompt_callback_target_submit, {10, 100});
ASSERT_EQ(BRDflt, BRDflt);
ASSERT_EQ(BRMinSet, BRMinSet);
ASSERT_EQ(BRMinMaxSet, BRMinMaxSet);
}
// Add main definition
OMPTEST_TESTSUITE_MAIN()