HIP: Heterogenous-computing Interface for Portability
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
trace_helper.h
1 /*
2 Copyright (c) 2015-2016 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 //---
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 new HSA type.
37 
38 
39 // Handy macro to convert an enumeration to a stringified version of same:
40 #define CASE_STR(x) case x: return #x;
41 
42 
43 // Building block functions:
44 template <typename T>
45 inline std::string ToHexString(T v)
46 {
47  std::ostringstream ss;
48  ss << "0x" << std::hex << v;
49  return ss.str();
50 };
51 
52 
53 //---
54 // Template overloads for ToString to handle specific types
55 
56 // This is the default which works for most types:
57 template <typename T>
58 inline std::string ToString(T v)
59 {
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 {
70  std::ostringstream ss;
71  ss << v;
72  return ss.str();
73 };
74 
75 
76 
77 // hipStream_t
78 template <>
79 inline std::string ToString(hipStream_t v)
80 {
81  std::ostringstream ss;
82  if (v == NULL) {
83  ss << "stream:<null>";
84  } else {
85  ss << *v;
86  }
87 
88  return ss.str();
89 };
90 
91 // hipMemcpyKind specialization
92 template <>
93 inline std::string ToString(hipMemcpyKind v)
94 {
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 : return ToHexString(v);
102  };
103 };
104 
105 
106 template <>
107 inline std::string ToString(hipError_t v)
108 {
109  return ihipErrorString(v);
110 };
111 
112 
113 // Catch empty arguments case
114 inline std::string ToString()
115 {
116  return ("");
117 }
118 
119 
120 //---
121 // C++11 variadic template - peels off first argument, converts to string, and calls itself again to peel the next arg.
122 // Strings are automatically separated by comma+space.
123 template <typename T, typename... Args>
124 inline std::string ToString(T first, Args... args)
125 {
126  return ToString(first) + ", " + ToString(args...) ;
127 }
128 
129 #endif
Host-to-Device Copy.
Definition: hip_runtime_api.h:167
Device-to-Host Copy.
Definition: hip_runtime_api.h:168
hipError_t
Definition: hip_runtime_api.h:152
hipMemcpyKind
Definition: hip_runtime_api.h:165
Device-to-Device Copy.
Definition: hip_runtime_api.h:169
Definition: hip_hcc.h:558
Runtime will automatically determine copy-kind based on virtual addresses.
Definition: hip_runtime_api.h:170
Definition: hip_hcc.h:463
Host-to-Host Copy.
Definition: hip_runtime_api.h:166