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