9.2
general documentation
cs_profiling.h
Go to the documentation of this file.
1#pragma once
2
3/*============================================================================
4 * Building with profiling annotations.
5 *============================================================================*/
6
7/*
8 This file is part of code_saturne, a general-purpose CFD tool.
9
10 Copyright (C) 1998-2026 EDF S.A.
11
12 This program is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free Software
14 Foundation; either version 2 of the License, or (at your option) any later
15 version.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
24 Street, Fifth Floor, Boston, MA 02110-1301, USA.
25*/
26
27/*----------------------------------------------------------------------------*/
28
29// Numeric values for different profiling libraries.
30
32#define CS_PROFILING_NONE 0
33
35#define CS_PROFILING_NVTX 1
36
38#define CS_PROFILING_ROCTX 2
39
40// No profiling library is used by default.
41#ifndef CS_PROFILING
42#define CS_PROFILING CS_PROFILING_NONE
43#endif
44
45// Make sure NVTX profiling is used in host code only, not CUDA.
46#if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) \
47 || defined(__SYCL_DEVICE_ONLY__)
48#undef CS_PROFILING
49#define CS_PROFILING CS_PROFILING_NONE
50#endif
51
52#define CS_COMBINE_DETAIL(x, y) x##y
53#define CS_COMBINE(x, y) CS_COMBINE_DETAIL(x, y)
54
55#define CS_STRINGIFY_DETAIL(x) #x
56#define CS_STRINGIFY(x) CS_STRINGIFY_DETAIL(x)
57
58#if CS_PROFILING == CS_PROFILING_NONE
59
60/*----------------------------------------------------------------------------
61 * Default: no profiling library
62 *----------------------------------------------------------------------------*/
63
64// Start profiling, when activated y code.
65#define CS_PROFILE_START()
66
67// Stop profiling, when activated y code.
68#define CS_PROFILE_STOP()
69
71#define CS_PROFILE_FUNC_RANGE()
72
74#define CS_PROFILE_MARK_LINE()
75
76/*----------------------------------------------------------------------------
77 * Profiling with NVIDIA NVTX3
78 *----------------------------------------------------------------------------*/
79
80#elif CS_PROFILING == CS_PROFILING_NVTX
81
82#include <cuda_profiler_api.h>
83
84#include <nvtx3/nvtx3.hpp>
85// #include <nvToolsExt.h>
86
87// Start profiling, when activated by code.
88#define CS_PROFILE_START() {cudaProfilerStart();}
89
90// Stop profiling, when activated by code.
91#define CS_PROFILE_STOP() {cudaProfilerStop();}
92
94#define CS_PROFILE_FUNC_RANGE() NVTX3_FUNC_RANGE()
95
97#define CS_PROFILE_MARK_LINE() nvtx3::mark(__FILE__ ":" CS_STRINGIFY(__LINE__))
98
99/*----------------------------------------------------------------------------
100 * Profiling with AMD ROCTX
101 *----------------------------------------------------------------------------*/
102
103#elif CS_PROFILING == CS_PROFILING_ROCTX
104
105#include <rocprofiler-sdk-roctx/roctx.h>
106
107/* Helper class for scoped range
108 *------------------------------*/
109
110class cs_profiling_scoped_range {
111public:
112 // Constructor
113 cs_profiling_scoped_range(const char *descr)
114 { roctxRangePushA(descr); }
115
116 // Destructor
117 ~cs_profiling_scoped_range()
118 { roctxRangePop(); }
119};
120
121// Start profiling, when activated by code.
122
123#define CS_PROFILE_START() {roctxProfilerResume();}
124
125// Stop profiling, when activated by code.
126#define CS_PROFILE_STOP() {roctxProfilerPause();}
127
129#define CS_PROFILE_FUNC_RANGE() \
130 cs_profiling_scoped_range _cs_psr = cs_profiling_scoped_range(__func__);
131
133#define CS_PROFILE_MARK_LINE() roctxMarkA(__FILE__ ":" CS_STRINGIFY(__LINE__))
134
135/*----------------------------------------------------------------------------*/
136
137#endif
138
139/*----------------------------------------------------------------------------*/