1 /******************************************************************************
2  * Copyright 1998-2019 Lawrence Livermore National Security, LLC and other
3  * HYPRE Project Developers. See the top-level COPYRIGHT file for details.
4  *
5  * SPDX-License-Identifier: (Apache-2.0 OR MIT)
6  ******************************************************************************/
7 
8 #include "_hypre_utilities.h"
9 
10 #if defined(HYPRE_USING_ROCTX)
11 #include "hip/hip_runtime_api.h"
12 #include "roctx.h"
13 #endif
14 
15 #if defined(HYPRE_USING_NVTX)
16 
17 #include <string>
18 #include <algorithm>
19 #include <vector>
20 #include "nvToolsExt.h"
21 #include "nvToolsExtCudaRt.h"
22 
23 /* 16 named colors by HTML 4.01. Repalce white with Orange */
24 typedef enum
25 {
26    /* White, */
27    Orange,
28    Silver,
29    Gray,
30    Black,
31    Red,
32    Maroon,
33    Yellow,
34    Olive,
35    Lime,
36    Green,
37    Aqua,
38    Teal,
39    Blue,
40    Navy,
41    Fuchsia,
42    Purple
43 } color_names;
44 
45 static const uint32_t colors[] =
46 {
47    /* 0xFFFFFF, */
48    0xFFA500,
49    0xC0C0C0,
50    0x808080,
51    0x000000,
52    0xFF0000,
53    0x800000,
54    0xFFFF00,
55    0x808000,
56    0x00FF00,
57    0x008000,
58    0x00FFFF,
59    0x008080,
60    0x0000FF,
61    0x000080,
62    0xFF00FF,
63    0x800080
64 };
65 
66 static const HYPRE_Int hypre_nvtx_num_colors = sizeof(colors) / sizeof(uint32_t);
67 static std::vector<std::string> hypre_nvtx_range_names;
68 
69 #endif // defined(HYPRE_USING_NVTX)
70 
hypre_GpuProfilingPushRangeColor(const char * name,HYPRE_Int color_id)71 void hypre_GpuProfilingPushRangeColor(const char *name, HYPRE_Int color_id)
72 {
73 #ifdef HYPRE_USING_NVTX
74    color_id = color_id % hypre_nvtx_num_colors;
75    nvtxEventAttributes_t eventAttrib = {0};
76    eventAttrib.version = NVTX_VERSION;
77    eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
78    eventAttrib.colorType = NVTX_COLOR_ARGB;
79    eventAttrib.color = colors[color_id];
80    eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII;
81    eventAttrib.message.ascii = name;
82    nvtxRangePushEx(&eventAttrib);
83 #endif
84 
85 #ifdef HYPRE_USING_ROCTX
86    roctxRangePush(name);
87 #endif
88 }
89 
hypre_GpuProfilingPushRange(const char * name)90 void hypre_GpuProfilingPushRange(const char *name)
91 {
92 #ifdef HYPRE_USING_NVTX
93    std::vector<std::string>::iterator p = std::find(hypre_nvtx_range_names.begin(),
94                                                     hypre_nvtx_range_names.end(),
95                                                     name);
96 
97    if (p == hypre_nvtx_range_names.end())
98    {
99       hypre_nvtx_range_names.push_back(name);
100       p = hypre_nvtx_range_names.end() - 1;
101    }
102 
103    HYPRE_Int color = p - hypre_nvtx_range_names.begin();
104 
105    hypre_GpuProfilingPushRangeColor(name, color);
106 #endif
107 
108 #ifdef HYPRE_USING_ROCTX
109    roctxRangePush(name);
110 #endif
111 }
112 
hypre_GpuProfilingPopRange()113 void hypre_GpuProfilingPopRange()
114 {
115 #ifdef HYPRE_USING_NVTX
116    hypre_GpuProfilingPushRangeColor("StreamSync0", Red);
117    cudaStreamSynchronize(0);
118    nvtxRangePop();
119    nvtxRangePop();
120 #endif
121 
122 #ifdef HYPRE_USING_ROCTX
123    roctxRangePush("StreamSync0");
124    hipStreamSynchronize(0);
125    roctxRangePop();
126    roctxRangePop();
127 #endif
128 }
129