SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
utils.cpp
1 #include <cassert>
2 
3 #include "smaug/core/datatypes.h"
5 #include "smaug/utility/utils.h"
6 
7 namespace smaug {
8 
9 void* malloc_aligned(size_t size, bool zeroOut) {
10  void* ptr = NULL;
11  int err = posix_memalign(
12  (void**)&ptr, CACHELINE_SIZE, next_multiple(size, CACHELINE_SIZE));
13  assert(err == 0 && "Failed to allocate memory!");
14  if (zeroOut)
15  memset(ptr, 0, next_multiple(size, CACHELINE_SIZE));
16  return ptr;
17 }
18 
19 std::string dataLayoutToStr(DataLayout layout) {
20  switch (layout) {
21  case DataLayout::NCHW:
22  return "NCHW";
23  case DataLayout::NHWC:
24  return "NHWC";
25  case DataLayout::NC:
26  return "NC";
27  case DataLayout::X:
28  return "X";
29  default:
30  assert(false && "Unknown data layout!");
31  return "";
32  }
33 }
34 
35 int calc_padding(int value, unsigned alignment) {
36  if (alignment == 0 || value % alignment == 0)
37  return 0;
38  return (alignment - (value % alignment));
39 }
40 
41 namespace gem5 {
42 
43 #ifndef TRACE_MODE
44 void switchCpu() {
46  m5_switch_cpu();
47 }
48 
49 void dumpStats(const char* msg, int period) {
51  m5_dump_stats(0, period, msg);
52 }
53 
54 void dumpResetStats(const char* msg, int period) {
56  m5_dump_reset_stats(0, period, msg);
57 }
58 
59 void quiesce() {
60  if (runningInSimulation) {
61  m5_quiesce();
62  }
63 }
64 
65 void wakeCpu(int id) {
66  if (runningInSimulation) {
67  m5_wake_cpu(id);
68  }
69 }
70 
71 int getCpuId() { return runningInSimulation ? m5_get_cpuid() : 0; }
72 #else
73 void switchCpu() {}
74 
75 void dumpStats(const char* msg, int period) {}
76 
77 void dumpResetStats(const char* msg, int period) {}
78 
79 void quiesce() {}
80 
81 void wakeCpu(int id) {}
82 
83 int getCpuId() { return 0; }
84 #endif
85 
86 ScopedStats::ScopedStats(const char* _startLabel,
87  const char* _endLabel,
88  bool _resetStats)
89  : startLabel(_startLabel), endLabel(_endLabel),
90  resetStats(_resetStats) {
91  if (resetStats)
92  dumpResetStats(startLabel, 0);
93  else
94  dumpStats(startLabel, 0);
95 }
96 
97 ScopedStats::~ScopedStats() {
98  if (resetStats)
99  dumpResetStats(endLabel, 0);
100  else
101  dumpStats(endLabel, 0);
102 }
103 
104 } // namespace gem5
105 
106 } // namespace smaug
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::gem5::wakeCpu
void wakeCpu(int id)
Wakes up a quiesced CPU.
Definition: utils.cpp:65
smaug::runningInSimulation
bool runningInSimulation
This is true if the user chooses to run the network in gem5 simulation.
Definition: globals.cpp:4
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
common.h
Utilities for writing and invoking Aladdin kernels from Operators.
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
next_multiple
size_t next_multiple(size_t request, size_t align)
Returns the smallest multiple of align that is >= request.
Definition: common.cpp:36
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