mxnet
nvtx.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef MXNET_COMMON_CUDA_NVTX_H_
21 #define MXNET_COMMON_CUDA_NVTX_H_
22 
23 #if MXNET_USE_CUDA && MXNET_USE_NVTX
24 #include <cuda.h>
25 #include <cuda_runtime.h>
26 #include <nvToolsExtCuda.h>
27 #include <vector>
28 #include <string>
29 #include <cstring>
30 
31 namespace mxnet {
32 namespace common {
33 namespace cuda {
34 
35 class NVTXDuration {
36  public:
37  explicit NVTXDuration(const char* name) noexcept : range_id_(0), name_(name) {}
38 
39  inline void start() {
40  range_id_ = nvtxRangeStartA(name_);
41  }
42 
43  inline void stop() {
44  nvtxRangeEnd(range_id_);
45  }
46 
47  private:
48  nvtxRangeId_t range_id_;
49  const char* name_;
50 };
51 
52 // Utility class for NVTX
53 class nvtx {
54  public:
55  // Palette of colors (make sure to add new colors to the vector in nameToColor()).
56  static const uint32_t kRed = 0xFF0000;
57  static const uint32_t kGreen = 0x00FF00;
58  static const uint32_t kBlue = 0x0000FF;
59  static const uint32_t kYellow = 0xB58900;
60  static const uint32_t kOrange = 0xCB4B16;
61  static const uint32_t kRed1 = 0xDC322F;
62  static const uint32_t kMagenta = 0xD33682;
63  static const uint32_t kViolet = 0x6C71C4;
64  static const uint32_t kBlue1 = 0x268BD2;
65  static const uint32_t kCyan = 0x2AA198;
66  static const uint32_t kGreen1 = 0x859900;
67 
68  static void gpuRangeStart(const uint32_t rgb, const std::string& range_name) {
69  nvtxEventAttributes_t att;
70  att.version = NVTX_VERSION;
71  att.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
72  att.colorType = NVTX_COLOR_ARGB;
73  att.color = rgb | 0xff000000;
74  att.messageType = NVTX_MESSAGE_TYPE_ASCII;
75  att.message.ascii = range_name.c_str();
76  nvtxRangePushEx(&att);
77  }
78 
79  // Utility to map a range name prefix to a random color based on its hash
80  static uint32_t nameToColor(const std::string& range_name, int prefix_len) {
81  static std::vector<uint32_t> colors{
82  kRed, kGreen, kBlue, kYellow, kOrange, kRed1, kMagenta, kViolet, kBlue1, kCyan, kGreen1};
83  std::string s(range_name, 0, prefix_len);
84  std::hash<std::string> hash_fn;
85  return colors[hash_fn(s) % colors.size()];
86  }
87 
88  // Utility to map a range name to a random color based on its hash
89  static uint32_t nameToColor(const std::string& range_name) {
90  return nameToColor(range_name, range_name.size());
91  }
92 
93  static void gpuRangeStop() {
94  nvtxRangePop();
95  }
96 };
97 
98 } // namespace cuda
99 } // namespace common
100 } // namespace mxnet
101 
102 #endif // MXNET_UDE_CUDA && MXNET_USE_NVTX
103 #endif // MXNET_COMMON_CUDA_NVTX_H_
mxnet
namespace of mxnet
Definition: api_registry.h:33