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 // hipStream_t
75 template <>
76 inline std::string ToString(hipStream_t v) {
77  std::ostringstream ss;
78  if (v == NULL) {
79  ss << "stream:<null>";
80  } else {
81  ss << *v;
82  }
83 
84  return ss.str();
85 };
86 
87 // hipMemcpyKind specialization
88 template <>
89 inline std::string ToString(hipMemcpyKind v) {
90  switch (v) {
91  CASE_STR(hipMemcpyHostToHost);
92  CASE_STR(hipMemcpyHostToDevice);
93  CASE_STR(hipMemcpyDeviceToHost);
94  CASE_STR(hipMemcpyDeviceToDevice);
95  CASE_STR(hipMemcpyDefault);
96  default:
97  return ToHexString(v);
98  };
99 };
100 
101 
102 template <>
103 inline std::string ToString(hipError_t v) {
104  return ihipErrorString(v);
105 };
106 
107 
108 // Catch empty arguments case
109 inline std::string ToString() { return (""); }
110 
111 
112 //---
113 // C++11 variadic template - peels off first argument, converts to string, and calls itself again to
114 // peel the next arg. Strings are automatically separated by comma+space.
115 template <typename T, typename... Args>
116 inline std::string ToString(T first, Args... args) {
117  return ToString(first) + ", " + ToString(args...);
118 }
119 
120 #endif
Definition: hip_hcc_internal.h:732
Definition: hip_hcc_internal.h:556