Skip to content

Commit

Permalink
[OpenMP][OMPT] Add 'Initialized' flag
Browse files Browse the repository at this point in the history
We observed some overhead and unnecessary debug output.
This can be alleviated by (re-)introduction of a boolean that indicates, if the
OMPT initialization has been performed.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D155186
  • Loading branch information
mhalk committed Jul 21, 2023
1 parent 209694d commit d82eace
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
3 changes: 3 additions & 0 deletions openmp/libomptarget/include/OmptCallback.h
Expand Up @@ -81,6 +81,9 @@ void finalizeLibrary(ompt_data_t *tool_data);
/// functions to their respective higher layer.
void connectLibrary();

/// OMPT initialization status; false if initializeLibrary has not been executed
extern bool Initialized;

} // namespace ompt
} // namespace target
} // namespace omp
Expand Down
Expand Up @@ -24,6 +24,8 @@

using namespace llvm::omp::target::ompt;

bool llvm::omp::target::ompt::Initialized = false;

ompt_get_callback_t llvm::omp::target::ompt::lookupCallbackByCode = nullptr;
ompt_function_lookup_t llvm::omp::target::ompt::lookupCallbackByName = nullptr;

Expand All @@ -43,6 +45,8 @@ int llvm::omp::target::ompt::initializeLibrary(ompt_function_lookup_t lookup,
// Store pointer of 'ompt_libomp_target_fn_lookup' for use by the plugin
lookupCallbackByName = lookup;

Initialized = true;

return 0;
}

Expand Down
Expand Up @@ -406,10 +406,11 @@ GenericDeviceTy::GenericDeviceTy(int32_t DeviceId, int32_t NumDevices,
OmptInitialized.store(false);
// Bind the callbacks to this device's member functions
#define bindOmptCallback(Name, Type, Code) \
if (ompt::lookupCallbackByCode) \
if (ompt::Initialized && ompt::lookupCallbackByCode) { \
ompt::lookupCallbackByCode((ompt_callbacks_t)(Code), \
((ompt_callback_t *)&(Name##_fn))); \
DP("OMPT: class bound %s=%p\n", #Name, ((void *)(uint64_t)Name##_fn));
DP("OMPT: class bound %s=%p\n", #Name, ((void *)(uint64_t)Name##_fn)); \
}

FOREACH_OMPT_DEVICE_EVENT(bindOmptCallback);
#undef bindOmptCallback
Expand All @@ -422,14 +423,16 @@ Error GenericDeviceTy::init(GenericPluginTy &Plugin) {
return Err;

#ifdef OMPT_SUPPORT
bool ExpectedStatus = false;
if (OmptInitialized.compare_exchange_strong(ExpectedStatus, true))
performOmptCallback(device_initialize,
/* device_num */ DeviceId,
/* type */ getComputeUnitKind().c_str(),
/* device */ reinterpret_cast<ompt_device_t *>(this),
/* lookup */ ompt::lookupCallbackByName,
/* documentation */ nullptr);
if (ompt::Initialized) {
bool ExpectedStatus = false;
if (OmptInitialized.compare_exchange_strong(ExpectedStatus, true))
performOmptCallback(device_initialize,
/* device_num */ DeviceId,
/* type */ getComputeUnitKind().c_str(),
/* device */ reinterpret_cast<ompt_device_t *>(this),
/* lookup */ ompt::lookupCallbackByName,
/* documentation */ nullptr);
}
#endif

// Read and reinitialize the envars that depend on the device initialization.
Expand Down Expand Up @@ -488,9 +491,11 @@ Error GenericDeviceTy::deinit() {
return Err;

#ifdef OMPT_SUPPORT
bool ExpectedStatus = true;
if (OmptInitialized.compare_exchange_strong(ExpectedStatus, false))
performOmptCallback(device_finalize, /* device_num */ DeviceId);
if (ompt::Initialized) {
bool ExpectedStatus = true;
if (OmptInitialized.compare_exchange_strong(ExpectedStatus, false))
performOmptCallback(device_finalize, /* device_num */ DeviceId);
}
#endif

return deinitImpl();
Expand Down Expand Up @@ -536,16 +541,19 @@ GenericDeviceTy::loadBinary(GenericPluginTy &Plugin,
return std::move(Err);

#ifdef OMPT_SUPPORT
size_t Bytes = getPtrDiff(InputTgtImage->ImageEnd, InputTgtImage->ImageStart);
performOmptCallback(device_load,
/* device_num */ DeviceId,
/* FileName */ nullptr,
/* File Offset */ 0,
/* VmaInFile */ nullptr,
/* ImgSize */ Bytes,
/* HostAddr */ InputTgtImage->ImageStart,
/* DeviceAddr */ nullptr,
/* FIXME: ModuleId */ 0);
if (ompt::Initialized) {
size_t Bytes =
getPtrDiff(InputTgtImage->ImageEnd, InputTgtImage->ImageStart);
performOmptCallback(device_load,
/* device_num */ DeviceId,
/* FileName */ nullptr,
/* File Offset */ 0,
/* VmaInFile */ nullptr,
/* ImgSize */ Bytes,
/* HostAddr */ InputTgtImage->ImageStart,
/* DeviceAddr */ nullptr,
/* FIXME: ModuleId */ 0);
}
#endif

// Return the pointer to the table of entries.
Expand Down
6 changes: 5 additions & 1 deletion openmp/libomptarget/src/OmptCallback.cpp
Expand Up @@ -394,6 +394,8 @@ class LibomptargetRtlFinalizer {
/// Object that will maintain the RTL finalizer from the plugin
LibomptargetRtlFinalizer *LibraryFinalizer = nullptr;

bool llvm::omp::target::ompt::Initialized = false;

ompt_get_callback_t llvm::omp::target::ompt::lookupCallbackByCode = nullptr;
ompt_function_lookup_t llvm::omp::target::ompt::lookupCallbackByName = nullptr;

Expand Down Expand Up @@ -421,6 +423,8 @@ int llvm::omp::target::ompt::initializeLibrary(ompt_function_lookup_t lookup,

LibraryFinalizer = new LibomptargetRtlFinalizer();

Initialized = true;

return 0;
}

Expand Down Expand Up @@ -463,7 +467,7 @@ extern "C" {
/// Used for connecting libomptarget with a plugin
void ompt_libomptarget_connect(ompt_start_tool_result_t *result) {
DP("Enter ompt_libomptarget_connect\n");
if (result && LibraryFinalizer) {
if (Initialized && result && LibraryFinalizer) {
// Cache each fini function, so that they can be invoked on exit
LibraryFinalizer->registerRtl(result->finalize);
// Invoke the provided init function with the lookup function maintained
Expand Down

0 comments on commit d82eace

Please sign in to comment.