@@ -8,7 +8,7 @@ cmake_minimum_required(VERSION 3.12)
88#
99#You can also pass this flag when running cmake from the command line like this:
1010#
11- #cmake..- D ENABLE_CUDA = YES
11+ #cmake - D ENABLE_CUDA= YES ..
1212#
1313#"YES" / GPU choice only available if CUDA library is installed and the GPU is CUDA capable.
1414############################################################################################
@@ -21,17 +21,24 @@ if(NOT PERFORMANCE_METRICS)
2121 set (PERFORMANCE_METRICS NO )
2222endif ()
2323
24- #CONDITIONAL FLAG to turn on the Gprof profiler( \
25- # Gprof is a performance analysis tool for Unix applications)
26- #Steps to run Gprof
27- #Step 01 : set(GPROF YES) below
28- #Step 02 : Compile and run the simulation on CPU or GPU as usual
29- #Step 03 : Run the generated gmon.out file from the build directory and save the output in an txt \
30- # file to improve readability \
31- #If using CPU - "~/Graphitti/build$ gprof cgraphitti gmon.out > analysis_test.txt"
32- #If using GPU - "~/Graphitti/build$ gprof ggraphitti gmon.out > analysis_test.txt"
33- if (NOT GPROF)
34- set (GPROF NO )
24+ ############################################################################################
25+ #CONDITIONAL FLAG to change target architecture for the GPU simulator from the default
26+ #
27+ #You can pass this flag when running cmake from the command line like this, setting TARGET_ARCH \
28+ # to your desired architecture: \
29+ #
30+ #cmake -D ENABLE_CUDA=YES -D TARGET_ARCH=70 ..
31+ #
32+ #"YES" / GPU choice only available if CUDA library is installed and the GPU is CUDA capable.
33+ #If no TARGET_ARCH is passed in then it will default to 37 which is the kepler architecture
34+ ############################################################################################
35+ if (NOT DEFINED TARGET_ARCH)
36+ set (TARGET_ARCH 37)
37+ endif ()
38+
39+ #CONDITIONAL FLAG to turn on the validation mode
40+ if (NOT VALIDATION_MODE)
41+ set (VALIDATION_MODE NO )
3542endif ()
3643
3744#Creates the Graphitti project with the correct languages, depending on if using GPU or not
@@ -45,31 +52,112 @@ if(ENABLE_CUDA)
4552 add_compile_definitions (USE_GPU)
4653#Specify the CUDA architecture / gencode that will be targeted
4754 ### Set gencode and architecture variables to the correct values for your specific NVIDIA hardware
48- set (CMAKE_CUDA_ARCHITECTURES 37)
49- set (CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} ;-gencode=arch=compute_37,code=sm_37)
55+ set (CMAKE_CUDA_ARCHITECTURES ${TARGET_ARCH} )
56+ set (CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} ;-gencode=arch=compute_${TARGET_ARCH} ,code=sm_${TARGET_ARCH} )
57+ message (STATUS "Using CUDA architecture: ${TARGET_ARCH} " )
5058
5159else ()
5260 message ("\n ----Generating Makefile for Graphitti CPU version----" )
5361 project (Graphitti LANGUAGES CXX C)
5462endif ()
5563
64+ # -----------------------------------------------------------------------------
65+ # Build Type Configuration
66+ #
67+ # CMake support for different build types controling optimization, debugging and profiling:
68+ #
69+ # - Debug : No optimizations (`-O0`), includes debug symbols (`-g`).
70+ # - Release : Optimized build (`-O3`), removes debug symbols.
71+ # - RelWithDebInfo: Optimized (`-O2`) but keeps debug symbols (`-g`) for profiling.
72+ # - Profiling : Custom build type (defined in this project) that enables:
73+ # - CPU profiling via `-pg` (GPROF)
74+ # - CUDA profiling via `-lineinfo` (for Nsight Compute)
75+ #
76+ # Selecting a Build Type:
77+ # - By default, CMake does NOT set a build type for single-config generators.
78+ # - If no build type is specified, this script defaults to "Release" for performance.
79+ # - You can explicitly set the build type when configuring CMake:
80+ #
81+ # cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug # Debug mode
82+ # cmake -S . -B build -DCMAKE_BUILD_TYPE=Release # Release mode
83+ # cmake -S . -B build -DCMAKE_BUILD_TYPE=Profiling # Profiling mode
84+ #
85+ # If you don't want to pass in the build type flag, you can edit this file and add...
86+ # set(CMAKE_BUILD_TYPE "Debug") or whichever build type you want
87+ # -----------------------------------------------------------------------------
88+ set (CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo;Profiling" CACHE STRING "Supported build types" FORCE)
89+
90+ # Ensure single-config generators use a valid default
91+ if (NOT CMAKE_BUILD_TYPE )
92+ set (CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the build type." FORCE)
93+ endif ()
94+
95+ # Set flags for all build types
96+ set (CMAKE_CXX_FLAGS_DEBUG "-g -O0" )
97+ # We should consider using the -DNDEBUG flag for release code, it disables assert() calls and is higher performance
98+ set (CMAKE_CXX_FLAGS_RELEASE "-O3" )
99+ set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g" )
100+
101+ # Define a custom build type: "Profiling"
102+ set (CMAKE_CXX_FLAGS_PROFILING "-pg -O2" )
103+ set (CMAKE_EXE_LINKER_FLAGS_PROFILING "-pg" )
104+ set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg" )
105+
106+ # Apply the correct flags based on the selected build type
107+ if (CMAKE_BUILD_TYPE STREQUAL "Debug" )
108+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_DEBUG} " )
109+ if (ENABLE_CUDA)
110+ set (CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -g -G" )
111+ endif ()
112+ elseif (CMAKE_BUILD_TYPE STREQUAL "Release" )
113+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_RELEASE} " )
114+ if (ENABLE_CUDA)
115+ set (CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -O3" )
116+ endif ()
117+ elseif (CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" )
118+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} " )
119+ elseif (CMAKE_BUILD_TYPE STREQUAL "Profiling" )
120+ message (STATUS "Profiling build enabled: Adding -pg (GPROF)" )
121+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_PROFILING} " )
122+ set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS_PROFILING} " )
123+ if (ENABLE_CUDA)
124+ set (CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -lineinfo" )
125+ # set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -lineinfo -Xptxas=-v")
126+ endif ()
127+ endif ()
128+
129+
130+ # Gprof is a performance analysis tool for Unix applications)
131+ #Steps to run Gprof
132+ #Step 01 : set build configuration to Profiling ... -DCMAKE_BUILD_TYPE=Profiling
133+ #Step 02 : Compile and run the simulation on CPU or GPU as usual
134+ #Step 03 : Run the generated gmon.out file from the build directory and save the output in an txt \
135+ # file to improve readability \
136+ #If using CPU - "~/Graphitti/build$ gprof cgraphitti gmon.out > analysis_test.txt"
137+ #If using GPU - "~/Graphitti/build$ gprof ggraphitti gmon.out > analysis_test.txt"
138+
139+
140+ # Print build type for verification
141+ message (STATUS "Build Type: ${CMAKE_BUILD_TYPE} " )
142+ message (STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS} " )
143+
144+ message (STATUS "ENABLE_CUDA: ${ENABLE_CUDA} " )
145+ if (ENABLE_CUDA)
146+ message (STATUS "CMAKE_CUDA_FLAGS: ${CMAKE_CUDA_FLAGS} " )
147+ endif ()
148+
149+
56150#Setting the base version to C++ 17
57151set (CMAKE_CXX_STANDARD 17)
58152
59- #set(DEBUG_MODE YES) for debugging, no optimization
60- #set(DEBUG_MODE NO) for production code, -O3 optimization enabled
61- set (DEBUG_MODE NO )
62-
63153if (PERFORMANCE_METRICS)
64154 message ("-- Setting PEREFORMANCE_METRICS: ON" )
65155 add_definitions (-DPERFORMANCE_METRICS)
66156endif ()
67157
68- if (GPROF)
69- message ("-- Setting GPROF: ON" )
70- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg" )
71- set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg" )
72- set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg" )
158+ if (VALIDATION_MODE)
159+ message ("-- Setting VALIDATION_MODE: ON" )
160+ add_definitions (-DVALIDATION_MODE)
73161endif ()
74162
75163#HDF5 Support, finds HDF5 package for C and C++ and links the hdf5 libraries to the executable \
@@ -116,11 +204,6 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
116204#Set extra warning flags
117205#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
118206
119- if (NOT DEBUG_MODE)
120- message ("-- Setting Optimization flag: O3" )
121- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3" )
122- endif ()
123-
124207#define TIXML_USE_STL as a preproccersser macro to use the C++ standard library with TinyXML
125208add_compile_definitions (TIXML_USE_STL)
126209message ("-- Setting Compile Definition: TIMXL_USE_STL" )
@@ -282,9 +365,33 @@ add_library(RNG STATIC ${RNG_Source})
282365
283366
284367# Create Utils library
285- file (GLOB Utils_Source Simulator/Utils/*.cpp Simulator/Utils/*.h)
368+ file (GLOB Utils_Source Simulator/Utils/*.cpp Simulator/Utils/*.h)
286369list (REMOVE_ITEM Utils_Source "${CMAKE_CURRENT_SOURCE_DIR} /Simulator/Utils/Factory.cpp" )
287- add_library (Utils ${Utils_Source} )
370+
371+ if (CMAKE_BUILD_TYPE STREQUAL "Profiling" )
372+ if (ENABLE_CUDA)
373+ # Find NVTX Library
374+ find_library (NVTX_LIBRARY nvToolsExt)
375+ if (NVTX_LIBRARY)
376+ message (STATUS "Found NVTX: ${NVTX_LIBRARY} included in Profiling" )
377+ add_compile_definitions (ENABLE_NVTX)
378+ else ()
379+ message (STATUS "NVTX library not found! Not included in Profiling." )
380+ list (REMOVE_ITEM Utils_Source "${CMAKE_CURRENT_SOURCE_DIR} /Simulator/Utils/NvtxHelper.cpp" )
381+ endif ()
382+ endif ()
383+
384+ else ()
385+ list (REMOVE_ITEM Utils_Source "${CMAKE_CURRENT_SOURCE_DIR} /Simulator/Utils/NvtxHelper.cpp" )
386+ endif ()
387+
388+ # Always create the Utils library (even if NVTX and CUDA are missing)
389+ add_library (Utils ${Utils_Source} )
390+
391+ # Only link NVTX if it was found
392+ if (NVTX_LIBRARY)
393+ target_link_libraries (Utils PRIVATE ${NVTX_LIBRARY} )
394+ endif ()
288395
289396
290397# Used to locate and run other CMakeLists.txt files from Third Party resources for further compilation of the project.
@@ -352,6 +459,15 @@ endif()
352459# ------ TESTS EXECUTABLE ------
353460# Add the file that contains main (RunTests.cpp) and all test files. GoogleTest will only recognize them if they are
354461# included in the executable.
462+ target_compile_options (gtest PRIVATE -Wno-error=maybe-uninitialized)
463+ target_compile_options (gtest_main PRIVATE -Wno-error=maybe-uninitialized)
464+
465+ if (ENABLE_CUDA)
466+ set (cuda_TestSources
467+ Testing/UnitTesting/DeviceVectorTests.cpp)
468+ set_source_files_properties (${cuda_TestSources} PROPERTIES LANGUAGE CUDA)
469+ endif ()
470+
355471add_executable (tests
356472 Testing/RunTests.cpp
357473 Testing/UnitTesting/OperationManagerTests.cpp
@@ -371,7 +487,8 @@ add_executable(tests
371487 Testing/Utils/CircularBufferTests.cpp
372488 Testing/UnitTesting/EventBufferTests.cpp
373489 Testing/UnitTesting/XmlRecorderTests.cpp
374- Testing/UnitTesting/Hdf5RecorderTests.cpp)
490+ Testing/UnitTesting/Hdf5RecorderTests.cpp
491+ Testing/UnitTesting/DeviceVectorTests.cpp)
375492
376493# Links the Googletest framework with the testing executable
377494target_link_libraries (tests gtest gtest_main)
@@ -426,3 +543,7 @@ target_link_libraries(serialSecondHalfTest combinedLib)
426543unset (ENABLE_CUDA CACHE )
427544unset (PERFORMANCE_METRICS CACHE )
428545unset (GPROF CACHE )
546+ unset (CMAKE_BUILD_TYPE CACHE )
547+ unset (NVTX_LIBRARY CACHE )
548+ unset (TARGET_ARCH CACHE )
549+ unset (VALIDATION_MODE CACHE )
0 commit comments