Skip to content

Commit f0ae727

Browse files
added nvtx support
1 parent 08b4992 commit f0ae727

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

CMakeLists.txt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,33 @@ add_library(RNG STATIC ${RNG_Source})
355355

356356

357357
# Create Utils library
358-
file(GLOB Utils_Source Simulator/Utils/*.cpp Simulator/Utils/*.h)
358+
file(GLOB Utils_Source Simulator/Utils/*.cpp Simulator/Utils/*.h)
359359
list(REMOVE_ITEM Utils_Source "${CMAKE_CURRENT_SOURCE_DIR}/Simulator/Utils/Factory.cpp")
360-
add_library(Utils ${Utils_Source})
360+
361+
if(CMAKE_BUILD_TYPE STREQUAL "Profiling")
362+
if(ENABLE_CUDA)
363+
# Find NVTX Library
364+
find_library(NVTX_LIBRARY nvToolsExt)
365+
if(NVTX_LIBRARY)
366+
message(STATUS "Found NVTX: ${NVTX_LIBRARY} included in Profiling")
367+
add_compile_definitions(ENABLE_NVTX)
368+
else()
369+
message(STATUS "NVTX library not found! Not included in Profiling.")
370+
list(REMOVE_ITEM Utils_Source "${CMAKE_CURRENT_SOURCE_DIR}/Simulator/Utils/NvtxHelper.cpp")
371+
endif()
372+
endif()
373+
374+
else()
375+
list(REMOVE_ITEM Utils_Source "${CMAKE_CURRENT_SOURCE_DIR}/Simulator/Utils/NvtxHelper.cpp")
376+
endif()
377+
378+
# Always create the Utils library (even if NVTX and CUDA are missing)
379+
add_library(Utils ${Utils_Source})
380+
381+
# Only link NVTX if it was found
382+
if(NVTX_LIBRARY)
383+
target_link_libraries(Utils PRIVATE ${NVTX_LIBRARY})
384+
endif()
361385

362386

363387
# Used to locate and run other CMakeLists.txt files from Third Party resources for further compilation of the project.

Simulator/Core/GPUModel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "AllVertices.h"
1414
#include "Connections.h"
1515
#include "Global.h"
16+
#include "NvtxHelper.h"
1617

1718
#ifdef PERFORMANCE_METRICS
1819
float g_time;
@@ -186,9 +187,11 @@ void GPUModel::calcSummationPoint()
186187
int blocksPerGrid
187188
= (Simulator::getInstance().getTotalVertices() + threadsPerBlock - 1) / threadsPerBlock;
188189

190+
nvtxPushColor("calcSummation", GREEN);
189191
calcSummationPointDevice<<<blocksPerGrid, threadsPerBlock>>>(
190192
Simulator::getInstance().getTotalVertices(), allVerticesDevice_, synapseIndexMapDevice_,
191193
allEdgesDevice_);
194+
nvtxPop();
192195
}
193196

194197
/// Update the connection of all the Neurons and Synapses of the simulation.

Simulator/Utils/NvtxHelper.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "NvtxHelper.h"
2+
#include <cuda_runtime.h>
3+
#include <nvToolsExt.h>
4+
5+
void nvtxPushColor(const std::string &name, uint32_t color)
6+
{
7+
nvtxEventAttributes_t eventAttrib = {};
8+
eventAttrib.version = NVTX_VERSION;
9+
eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
10+
eventAttrib.colorType = NVTX_COLOR_ARGB;
11+
eventAttrib.color = color;
12+
eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII;
13+
eventAttrib.message.ascii = name.c_str();
14+
15+
nvtxRangePushEx(&eventAttrib);
16+
}
17+
18+
void nvtxPop()
19+
{
20+
nvtxRangePop();
21+
}

Simulator/Utils/NvtxHelper.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef NVTX_HELPER_H
2+
#define NVTX_HELPER_H
3+
4+
#include <cstdint>
5+
#include <string>
6+
7+
// Define NVTX colors (ARGB format)
8+
#define RED 0xFFFF0000 // Red
9+
#define GREEN 0xFF00FF00 // Green
10+
#define BLUE 0xFF0000FF // Blue
11+
#define YELLOW 0xFFFFFF00 // Yellow
12+
#define ORANGE 0xFFFFA500 // Orange
13+
#define PURPLE 0xFF800080 // Purple
14+
15+
#ifdef ENABLE_NVTX
16+
17+
// Function to push an NVTX range with a given name and color
18+
void nvtxPushColor(const std::string &name, uint32_t color);
19+
20+
// Function to pop the most recent NVTX range
21+
void nvtxPop();
22+
23+
#else
24+
inline void nvtxPushColor(const std::string &, uint32_t)
25+
{
26+
}
27+
inline void nvtxPop()
28+
{
29+
}
30+
31+
#endif // ENABLE_NVTX
32+
33+
34+
#endif // NVTX_HELPER_H

0 commit comments

Comments
 (0)