HIP: Heterogenous-computing Interface for Portability
trace_helper.h
1 /*
2 Copyright (c) 2015-2017 Advanced Micro Devices, Inc. All rights reserved.
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 //#pragma once
24 
25 #ifndef TRACE_HELPER_H
26 #define TRACE_HELPER_H
27 
28 #include <iostream>
29 #include <iomanip>
30 #include <sstream>
31 #include <string>
32 //---
33 // Helper functions to convert HIP function arguments into strings.
34 // Handles POD data types as well as enumerations (ie hipMemcpyKind).
35 // The implementation uses C++11 variadic templates and template specialization.
36 // The hipMemcpyKind example below is a good example that shows how to implement conversion for a
37 // new HSA type.
38 
39 
40 // Handy macro to convert an enumeration to a stringified version of same:
41 #define CASE_STR(x) \
42  case x: \
43  return #x;
44 
45 
46 // Building block functions:
47 template <typename T>
48 inline std::string ToHexString(T v) {
49  std::ostringstream ss;
50  ss << "0x" << std::hex << v;
51  return ss.str();
52 };
53 
54 
55 //---
56 // Template overloads for ToString to handle specific types
57 
58 // This is the default which works for most types:
59 template <typename T>
60 inline std::string ToString(T v) {
61  std::ostringstream ss;
62  ss << v;
63  return ss.str();
64 };
65 
66 
67 // hipEvent_t specialization. TODO - maybe add an event ID for debug?
68 template <>
69 inline std::string ToString(hipEvent_t v) {
70  std::ostringstream ss;
71  ss << v;
72  return ss.str();
73 };
74 // hipIpcEventHandle_t specialization. TODO
75 template <>
76 inline std::string ToString(hipIpcEventHandle_t v) {
77  return std::string{};
78 };
79 // hipStream_t
80 template <>
81 inline std::string ToString(hipStream_t v) {
82  std::ostringstream ss;
83  if (v == NULL) {
84  ss << "stream:<null>";
85  } else {
86  ss << *v;
87  }
88 
89  return ss.str();
90 };
91 
92 // hipMemcpyKind specialization
93 template <>
94 inline std::string ToString(hipMemcpyKind v) {
95  switch (v) {
96  CASE_STR(hipMemcpyHostToHost);
97  CASE_STR(hipMemcpyHostToDevice);
98  CASE_STR(hipMemcpyDeviceToHost);
99  CASE_STR(hipMemcpyDeviceToDevice);
100  CASE_STR(hipMemcpyDefault);
101  default:
102  return ToHexString(v);
103  };
104 };
105 
106 
107 template <>
108 inline std::string ToString(hipError_t v) {
109  return ihipErrorString(v);
110 };
111 
112 
113 // Catch empty arguments case
114 inline std::string ToString() { return (""); }
115 
116 
117 //---
118 // C++11 variadic template - peels off first argument, converts to string, and calls itself again to
119 // peel the next arg. Strings are automatically separated by comma+space.
120 template <typename T, typename... Args>
121 inline std::string ToString(T first, Args... args) {
122  return ToString(first) + ", " + ToString(args...);
123 }
124 
125 #endif
hipIpcEventHandle_st
Definition: hip_runtime_api.h:120
ihipEvent_t
Definition: hip_hcc_internal.h:759
ihipStream_t
Definition: hip_hcc_internal.h:580