brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.9 KiB · c7b3774 Raw
110 lines · c
1//===-- OpenMP/OMPT/Connector.h - OpenMP Tooling lib connector -*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// Support used by OMPT implementation to establish communication between10// various OpenMP runtime libraries: host openmp library, target-independent11// runtime library, and device-dependent runtime libraries.12//13//===----------------------------------------------------------------------===//14 15#ifndef OMPTARGET_OPENMP_OMPT_CONNECTOR_H16#define OMPTARGET_OPENMP_OMPT_CONNECTOR_H17 18#ifdef OMPT_SUPPORT19 20#include "llvm/Support/DynamicLibrary.h"21 22#include <memory>23#include <string>24 25#include "omp-tools.h"26#include "omptarget.h"27 28#include "Shared/Debug.h"29 30#pragma push_macro("DEBUG_PREFIX")31#undef DEBUG_PREFIX32#define DEBUG_PREFIX "OMPT"33 34/// Type for the function to be invoked for connecting two libraries.35typedef void (*OmptConnectRtnTy)(ompt_start_tool_result_t *result);36 37/// Establish connection between openmp runtime libraries38///39/// This class is used to communicate between an OMPT implementation in40/// libomptarget and libomp. It is also used to communicate between an41/// OMPT implementation in a device-specific plugin and42/// libomptarget. The decision whether OMPT is enabled or not needs to43/// be made when the library is loaded before any functions in the44/// library are invoked. For that reason, an instance of this class is45/// intended to be defined in the constructor for libomptarget or a46/// plugin so that the decision about whether OMPT is supposed to be47/// enabled is known before any interface function in the library is48/// invoked.49class OmptLibraryConnectorTy {50public:51  /// Use \p LibName as the prefix of the global function used for connecting52  /// two libraries, the source indicated by \p LibName and the destination53  /// being the one that creates this object.54  OmptLibraryConnectorTy(const char *Ident) {55    LibIdent.append(Ident);56    IsInitialized = false;57  }58  OmptLibraryConnectorTy() = delete;59  /// Use \p OmptResult init to connect the two libraries denoted by this60  /// object. The init function of \p OmptResult will be used during connection61  /// and the fini function of \p OmptResult will be used during teardown.62  void connect(ompt_start_tool_result_t *OmptResult) {63    initialize();64    if (!LibConnHandle)65      return;66    // Call the function provided by the source library for connect67    LibConnHandle(OmptResult);68  }69 70private:71  void initialize() {72    if (IsInitialized)73      return;74 75    std::string ErrMsg;76    std::string LibName = LibIdent;77    LibName += ".so";78 79    DP("OMPT: Trying to load library %s\n", LibName.c_str());80    auto DynLibHandle = std::make_unique<llvm::sys::DynamicLibrary>(81        llvm::sys::DynamicLibrary::getPermanentLibrary(LibName.c_str(),82                                                       &ErrMsg));83    if (!DynLibHandle->isValid()) {84      // The upper layer will bail out if the handle is null.85      LibConnHandle = nullptr;86    } else {87      auto LibConnRtn = "ompt_" + LibIdent + "_connect";88      DP("OMPT: Trying to get address of connection routine %s\n",89         LibConnRtn.c_str());90      LibConnHandle = reinterpret_cast<OmptConnectRtnTy>(91          DynLibHandle->getAddressOfSymbol(LibConnRtn.c_str()));92    }93    DP("OMPT: Library connection handle = %p\n", LibConnHandle);94    IsInitialized = true;95  }96 97  /// Ensure initialization occurs only once98  bool IsInitialized;99  /// Handle of connect routine provided by source library100  OmptConnectRtnTy LibConnHandle;101  /// Name of connect routine provided by source library102  std::string LibIdent;103};104 105#endif // OMPT_SUPPORT106 107#pragma pop_macro("DEBUG_PREFIX")108 109#endif // OMPTARGET_OPENMP_OMPT_CONNECTOR_H110