Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mutex to support multithread NVTX markers #406

Merged
merged 3 commits into from
Apr 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions include/matx/core/nvtx.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#pragma once
#include<functional>
#include<map>
#include <mutex>
#include<string>
#include<utility>
#include <nvToolsExt.h>
Expand All @@ -41,7 +42,7 @@ namespace matx

/**
* @brief levels of NVTX Logging. Lower level is more selective (prints less)
*
*
*/
enum matx_nvxtLogLevels
{
Expand All @@ -68,7 +69,7 @@ static const int32_t nunNvtxColors = 10;

/**
* @brief automatic NVTX Colors
*
*
*/
static const int32_t nvtxColors[nunNvtxColors] = {
NVTX_BLACK,
Expand All @@ -83,8 +84,9 @@ static const int32_t nvtxColors[nunNvtxColors] = {
NVTX_WHITE
};

inline uint64_t curColorIdx;
inline std::map< int, nvtxRangeId_t> eventMap;
inline uint64_t curColorIdx; ///< counter for rotation of colors for sequential ranges
inline std::map< int, nvtxRangeId_t> nvtx_eventMap; ///< map of currently active NVTX ranges
inline std::mutex nvtx_memory_mtx; ///< Mutex protecting updates from map

inline matx_nvxtLogLevels globalNvtxLevel = matx_nvxtLogLevels::MATX_NVTX_LOG_API;

Expand Down Expand Up @@ -116,11 +118,10 @@ inline matx_nvxtLogLevels globalNvtxLevel = matx_nvxtLogLevels::MATX_NVTX_LOG_AP
#define MATX_NVTX_START_RANGE( message, nvtxLevel, id ) matx::NvtxEvent MATX_UNIQUE_NAME(nvtxFlag_)( __FUNCTION__, message, nvtxLevel, id );

#define MATX_NVTX_END_RANGE( id ) matx::endEvent( id );

#define MATX_NVTX_SET_LOG_LEVEL( nvtxLevel ) matx::setNVTXLogLevel( nvtxLevel );

//////////// Disable NVTX Macros /////////////////

//////////// Disable NVTX Macros /////////////////
#else

#define MATX_NVTX_1( message );
Expand All @@ -134,9 +135,9 @@ inline matx_nvxtLogLevels globalNvtxLevel = matx_nvxtLogLevels::MATX_NVTX_LOG_AP
MATX_NVTX_2(__VA_ARGS__),\
MATX_NVTX_1(__VA_ARGS__)\
)

#define MATX_NVTX_END_RANGE( id );

#define MATX_NVTX_SET_LOG_LEVEL( nvtxLevel );

#endif
Expand All @@ -146,7 +147,7 @@ inline matx_nvxtLogLevels globalNvtxLevel = matx_nvxtLogLevels::MATX_NVTX_LOG_AP

////////////////////////////////////////////////////////////////////////////////
///
///\brief Utility Function to set Global Log Level. should be called through the
///\brief Utility Function to set Global Log Level. should be called through the
/// MATX_NVTX_SET_LOG_LEVEL macro with the same parameters
///
////////////////////////////////////////////////////////////////////////////////
Expand All @@ -157,33 +158,36 @@ inline matx_nvxtLogLevels globalNvtxLevel = matx_nvxtLogLevels::MATX_NVTX_LOG_AP

////////////////////////////////////////////////////////////////////////////////
///
///\brief fucntion wrapping NVTX management for automatic creation/deletion
///\brief fucntion wrapping NVTX management for automatic creation/deletion
/// MATX_NVTX_START or MATX_NVTX_START_RANGE macro with the same parameters
///
////////////////////////////////////////////////////////////////////////////////
[[maybe_unused]] static void registerEvent( int registerId, nvtxRangeId_t eventId )
{
{
std::unique_lock lck(nvtx_memory_mtx);

std::pair< int, nvtxRangeId_t > newPair( registerId, eventId );
eventMap.insert(newPair);
nvtx_eventMap.insert(newPair);
}


////////////////////////////////////////////////////////////////////////////////
///
///\brief fucntion wrapping NVTX management for automatic creation/deletion
///\brief fucntion wrapping NVTX management for automatic creation/deletion
/// MATX_NVTX_END_RANGE macro with the same parameters
///
////////////////////////////////////////////////////////////////////////////////
static void endEvent( int id )
{
auto foundIter = eventMap.find( id );
{
std::unique_lock lck(nvtx_memory_mtx);

auto foundIter = nvtx_eventMap.find( id );

if( foundIter != eventMap.end())
if( foundIter != nvtx_eventMap.end())
{
nvtxRangeEnd(foundIter->second);
eventMap.erase( foundIter );
nvtx_eventMap.erase( foundIter );
}

}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -194,7 +198,7 @@ static void endEvent( int id )
class NvtxEvent
{
public:

////////////////////////////////////////////////////////////////////////////////
///
///\brief ctor
Expand All @@ -205,7 +209,7 @@ class NvtxEvent
/// which uses function name instead
///\param nvtxLevel level of NVTX events to use higher number reduces scope
///\param registerId customID (integer) used to reference ranges you wish to manually end

///
////////////////////////////////////////////////////////////////////////////////
NvtxEvent( std::string functionName, std::string message="", matx_nvxtLogLevels nvtxLevel = matx_nvxtLogLevels::MATX_NVTX_LOG_INTERNAL, int registerId = -1 )
Expand Down Expand Up @@ -264,7 +268,7 @@ class NvtxEvent
{
endEvent( userHandle_ );
}

nvtxRangeEnd(rangeId_);
}

Expand Down