itrace
Instrumented Trace
itrace_numpy_defines.h
Go to the documentation of this file.
1 
11 #ifndef ITRACE_NUMPY_DEFINES_H
12 #define ITRACE_NUMPY_DEFINES_H
13 
14 #include <cassert>
15 #include <string_view>
16 
17 /* Header for all itrace numpy messages */
18 constexpr std::string_view itrace_numpy_header {"('thread','<i8'),('start','<u8'),('end','<u8'),('cycles', '<u8')"};
19 
22 template <typename T>
23 inline const char* get_numpy_element()
24 {
25  static_assert(std::is_arithmetic_v<T>, "get_numpy_element is only for arithmetic types - check numpy implementation");
26  if (std::is_floating_point_v<T>) // floating point case
27  {
28  if (sizeof(T) > 4)
29  return "<f8";
30  else
31  return "<f4";
32  }
33  else if (std::is_integral_v<T>)
34  {
35  if constexpr (std::is_unsigned_v<T>)
36  {
37  switch (sizeof(T))
38  {
39  case 1:
40  return "<u1";
41  case 2:
42  return "<u2";
43  case 4:
44  return "<u4";
45  case 8:
46  return "<u8";
47  }
48  }
49  else
50  {
51  switch (sizeof(T))
52  {
53  case 1:
54  return "<i1";
55  case 2:
56  return "<i2";
57  case 4:
58  return "<i4";
59  case 8:
60  return "<i8";
61  }
62  }
63  }
64  assert(false);
65  return "";
66 }
67 
70 template <typename T>
72 {
73  static std::string get(const std::string& name)
74  {
75  return "('" + name + "','" + get_numpy_element<T>() + "')";
76  }
77 };
78 
79 template <typename EL, int SIZE>
80 struct compute_numpy_type<std::array<EL, SIZE>>
81 {
82  static std::string get(const std::string& name)
83  {
84  const int BUFFER_SIZE = 64;
85  char buffer[BUFFER_SIZE];
86  int len = 0;
87  if constexpr (std::is_same_v<EL,char>)
88  {
89  len = snprintf(buffer, sizeof(buffer), "('%s', 'S%d')", name.c_str(), SIZE);
90  }
91  else
92  {
93  len = snprintf(buffer, sizeof(buffer), "('%s', '%s', (%d,))", name.c_str(), get_numpy_element<EL>(), SIZE);
94  }
95 
96  assert(len < BUFFER_SIZE);
97 
98  if (len < 0)
99  {
100  return {};
101  }
102 
103  return { buffer, static_cast<size_t>(len) };
104  }
105 };
106 
107 
108 #endif // ITRACE_NUMPY_DEFINES_H
const char * get_numpy_element()
Definition: itrace_numpy_defines.h:23
Definition: itrace_numpy_defines.h:71