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 <string>
31 //---
32 // Helper functions to convert HIP function arguments into strings.
33 // Handles POD data types as well as enumerations (ie hipMemcpyKind).
34 // The implementation uses C++11 variadic templates and template specialization.
35 // The hipMemcpyKind example below is a good example that shows how to implement conversion for a
36 // new HSA type.
37 
38 
39 // Handy macro to convert an enumeration to a stringified version of same:
40 #define CASE_STR(x) \
41  case x: \
42  return #x;
43 
44 
45 // Building block functions:
46 template <typename T>
47 inline std::string ToHexString(T v) {
48  std::ostringstream ss;
49  ss << "0x" << std::hex << v;
50  return ss.str();
51 };
52 
53 
54 //---
55 // Template overloads for ToString to handle specific types
56 
57 // This is the default which works for most types:
58 template <typename T>
59 inline std::string ToString(T v) {
60  std::ostringstream ss;
61  ss << v;
62  return ss.str();
63 };
64 
65 
66 // hipEvent_t specialization. TODO - maybe add an event ID for debug?
67 template <>
68 inline std::string ToString(hipEvent_t v) {
69  std::ostringstream ss;
70  ss << v;
71  return ss.str();
72 };
73 // hipStream_t
74 template <>
75 inline std::string ToString(hipStream_t v) {
76  std::ostringstream ss;
77  if (v == NULL) {
78  ss << "stream:<null>";
79  } else {
80  ss << *v;
81  }
82 
83  return ss.str();
84 };
85 
86 // hipMemcpyKind specialization
87 template <>
88 inline std::string ToString(hipMemcpyKind v) {
89  switch (v) {
90  CASE_STR(hipMemcpyHostToHost);
91  CASE_STR(hipMemcpyHostToDevice);
92  CASE_STR(hipMemcpyDeviceToHost);
93  CASE_STR(hipMemcpyDeviceToDevice);
94  CASE_STR(hipMemcpyDefault);
95  default:
96  return ToHexString(v);
97  };
98 };
99 
100 
101 template <>
102 inline std::string ToString(hipError_t v) {
103  return ihipErrorString(v);
104 };
105 
106 
107 // Catch empty arguments case
108 inline std::string ToString() { return (""); }
109 
110 
111 //---
112 // C++11 variadic template - peels off first argument, converts to string, and calls itself again to
113 // peel the next arg. Strings are automatically separated by comma+space.
114 template <typename T, typename... Args>
115 inline std::string ToString(T first, Args... args) {
116  return ToString(first) + ", " + ToString(args...);
117 }
118 
119 #endif
Definition: hip_hcc_internal.h:722
Definition: hip_hcc_internal.h:544