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 new HSA type.
36 
37 
38 // Handy macro to convert an enumeration to a stringified version of same:
39 #define CASE_STR(x) case x: return #x;
40 
41 
42 // Building block functions:
43 template <typename T>
44 inline std::string ToHexString(T v)
45 {
46  std::ostringstream ss;
47  ss << "0x" << std::hex << v;
48  return ss.str();
49 };
50 
51 
52 //---
53 // Template overloads for ToString to handle specific types
54 
55 // This is the default which works for most types:
56 template <typename T>
57 inline std::string ToString(T v)
58 {
59  std::ostringstream ss;
60  ss << v;
61  return ss.str();
62 };
63 
64 
65 // hipEvent_t specialization. TODO - maybe add an event ID for debug?
66 template <>
67 inline std::string ToString(hipEvent_t v)
68 {
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 {
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 {
91  switch(v) {
92  CASE_STR(hipMemcpyHostToHost);
93  CASE_STR(hipMemcpyHostToDevice);
94  CASE_STR(hipMemcpyDeviceToHost);
95  CASE_STR(hipMemcpyDeviceToDevice);
96  CASE_STR(hipMemcpyDefault);
97  default : return ToHexString(v);
98  };
99 };
100 
101 
102 template <>
103 inline std::string ToString(hipError_t v)
104 {
105  return ihipErrorString(v);
106 };
107 
108 
109 // Catch empty arguments case
110 inline std::string ToString()
111 {
112  return ("");
113 }
114 
115 
116 //---
117 // C++11 variadic template - peels off first argument, converts to string, and calls itself again to peel the next arg.
118 // Strings are automatically separated by comma+space.
119 template <typename T, typename... Args>
120 inline std::string ToString(T first, Args... args)
121 {
122  return ToString(first) + ", " + ToString(args...) ;
123 }
124 
125 #endif
Host-to-Device Copy.
Definition: hip_runtime_api.h:209
Device-to-Host Copy.
Definition: hip_runtime_api.h:210
hipError_t
Definition: hip_runtime_api.h:154
hipMemcpyKind
Definition: hip_runtime_api.h:207
Device-to-Device Copy.
Definition: hip_runtime_api.h:211
Runtime will automatically determine copy-kind based on virtual addresses.
Definition: hip_runtime_api.h:212
Definition: hip_hcc_internal.h:603
Definition: hip_hcc_internal.h:491
Host-to-Host Copy.
Definition: hip_runtime_api.h:208