SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
utils.h
1 #ifndef _UTILITY_UTILS_H_
2 #define _UTILITY_UTILS_H_
3 
4 #include <array>
5 #include <string>
6 #include <vector>
7 
8 #include "smaug/core/datatypes.h"
9 #include "gem5/m5ops.h"
10 
11 namespace smaug {
12 
13 // TODO: Allow these to take rvalue references.
14 template <typename T>
15 int product(std::vector<T> array) {
16  int prod = 1;
17  for (auto val : array)
18  prod *= val;
19  return prod;
20 }
21 
26 template <typename T>
27 std::vector<T> sum(std::vector<T> array0, std::vector<T> array1) {
28  assert(array0.size() == array1.size());
29  std::vector<T> sum(array0.size());
30  for (int i = 0; i < array0.size(); i++)
31  sum[i] = array0[i] + array1[i];
32  return sum;
33 }
34 
35 template <typename T>
36 void variadicToVector(std::vector<T>& vector, T elem) {
37  vector.push_back(elem);
38 }
39 
43 template <typename T, typename... Args>
44 void variadicToVector(std::vector<T>& vector, T e, Args... elems) {
45  vector.push_back(e);
46  variadicToVector(vector, elems...);
47 }
48 
56 template <typename T, typename... Args>
57 std::array<T, sizeof...(Args) + 1> variadicToArray(T i, Args... elems) {
58  return {{ i, elems... }};
59 }
60 
64 void* malloc_aligned(size_t size, bool zeroOut = false);
65 
69 int calc_padding(int value, unsigned alignment);
70 
72 std::string dataLayoutToStr(DataLayout layout);
73 
78 namespace gem5 {
79 
85 void switchCpu();
86 
93 void dumpStats(const char* msg, int period = 0);
94 
96 void dumpResetStats(const char* msg, int period = 0);
97 
103 void quiesce();
104 
106 void wakeCpu(int id);
107 
112 int getCpuId();
113 
118 class ScopedStats {
119  public:
120  ScopedStats(const char* _startLabel,
121  const char* _endLabel,
122  bool _resetStats = true);
123  ~ScopedStats();
124 
125  protected:
126  const char* startLabel;
127  const char* endLabel;
128  bool resetStats;
129 };
130 
131 } // namespace gem5
132 
133 namespace stats {
134 constexpr const char* kNetworkStart = "Network start";
135 constexpr const char* kNetworkEnd = "Network end";
136 constexpr const char* kReorderingStart = "Reordering start";
137 constexpr const char* kReorderingEnd = "Reordering end";
138 constexpr const char* kTensorPrepStart = "Tensor preparation start";
139 constexpr const char* kTensorPrepEnd = "Tensor preparation end";
140 constexpr const char* kTensorFinalStart = "Tensor finalization start";
141 constexpr const char* kTensorFinalEnd = "Tensor finalization end";
142 } // namespace stats
143 
144 } // namespace smaug
145 
146 #endif
smaug::gem5::ScopedStats
A RAII helper class which dumps and/or resets gem5 stats at construction and destruction.
Definition: utils.h:118
smaug::gem5::getCpuId
int getCpuId()
Returns the logical CPU number.
Definition: utils.cpp:71
smaug::malloc_aligned
void * malloc_aligned(size_t size, bool zeroOut)
Return heap-allocated cacheline-aligned memory.
Definition: utils.cpp:9
smaug::sum
std::vector< T > sum(std::vector< T > array0, std::vector< T > array1)
Returns the elementwise-sum of the two arrays, which must be of the same size.
Definition: utils.h:27
smaug::gem5::wakeCpu
void wakeCpu(int id)
Wakes up a quiesced CPU.
Definition: utils.cpp:65
smaug
The smaug namespace is the parent namespace of all C++ code in SMAUG.
Definition: backend.cpp:38
smaug::calc_padding
int calc_padding(int value, unsigned alignment)
Return the difference between value and the next multiple of alignment.
Definition: utils.cpp:35
smaug::dataLayoutToStr
std::string dataLayoutToStr(DataLayout layout)
Get the string version of DataLayout.
Definition: utils.cpp:19
smaug::gem5::quiesce
void quiesce()
Puts the CPU to sleep.
Definition: utils.cpp:59
smaug::gem5::dumpStats
void dumpStats(const char *msg, int period)
Dumps gem5 stats to the stats.txt file.
Definition: utils.cpp:49
smaug::gem5::switchCpu
void switchCpu()
Switches to the next CPU type.
Definition: utils.cpp:44
smaug::variadicToArray
std::array< T, sizeof...(Args)+1 > variadicToArray(T i, Args... elems)
Returns a std::array populated with the given elements.
Definition: utils.h:57
smaug::gem5::dumpResetStats
void dumpResetStats(const char *msg, int period)
Dumps gem5 stats to the stats.txt file and resets all stats.
Definition: utils.cpp:54