SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
scheduler.cpp
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 
5 #include "smaug/utility/debug_stream.h"
6 #include "smaug/utility/thread_pool.h"
7 #include "smaug/core/tensor.h"
8 #include "smaug/core/types.pb.h"
9 #include "smaug/core/scheduler.h"
10 
11 namespace smaug {
12 
14  std::cout << "======================================================\n";
15  std::cout << " Tiling operators of the network...\n";
16  std::cout << "======================================================\n";
17  for (auto nameOp : network->getOperators()) {
18  Operator* op = nameOp.second;
19  dout(0) << "Tiling " << op->getName() << " ("
20  << OpType_Name(op->getOpType()) << ").\n";
21  op->tile();
22  }
23 
24  // We have finished loading the model and building the network, as well as
25  // the tiling of all the operators. Now we can stop fast forwarding.
27 
28  fastForwardMode = false;
29 
30  // The fast-forwarding mode uses simpler CPUs, which will be switched to
31  // OoO CPUs after it's done. Therefore, the initialization of the thread
32  // pool must be after the fast-forwarding, otherwise the CPU IDs will be
33  // incorrect.
34  if (threadPool)
36 
37  std::cout << "======================================================\n";
38  std::cout << " Scheduling operators of the network...\n";
39  std::cout << "======================================================\n";
40  // Initialize number of pending inputs for every operator and put Data
41  // operators into the ready queue.
42  for (auto nameOp : network->getOperators()) {
43  Operator* op = nameOp.second;
44  Vertex vertex = op->getVertex();
45  int numPendingInputs = boost::in_degree(vertex, network->getGraph());
46  op->setNumPendingInputs(numPendingInputs);
47  if (numPendingInputs == 0)
48  readyQueue.push_back(op);
49  }
50  Tensor* output;
51  {
52  auto stats =
53  gem5::ScopedStats(stats::kNetworkStart, stats::kNetworkEnd);
54  output = scheduleReady();
55  }
56  return output;
57 }
58 
60  Tensor* output;
61  for (auto op : readyQueue) {
62  dout(0) << "Scheduling " << op->getName() << " ("
63  << OpType_Name(op->getOpType()) << ").\n";
64  maybeRunOperator(op);
65  updateChildren(op);
66  output = op->getOutput(0);
67  dout(2) << *output << "\n";
68  }
69  return output;
70 }
71 
73  if (!op->isDead()) {
74  op->run();
75  } else {
76  for (auto output : op->getOutputs())
77  output->setDead();
78  }
79 }
80 
82  const Graph& graph = network->getGraph();
83  Vertex vertex = op->getVertex();
84  out_edge_iter outEdgeIt, outEdgeEnd;
85  for (boost::tie(outEdgeIt, outEdgeEnd) = out_edges(vertex, graph);
86  outEdgeIt != outEdgeEnd;
87  ++outEdgeIt) {
88  Vertex childVertex = target(*outEdgeIt, graph);
89  Operator* child = get(boost::vertex_op, graph, childVertex);
90  if (child->getNumPendingInputs() > 0) {
91  child->decrNumPendingInputs();
92  if (child->getNumPendingInputs() == 0)
93  readyQueue.push_back(child);
94  }
95  }
96 }
97 
98 } // namespace smaug
smaug::Tensor
Tensor represents a single multi-dimensional array of data.
Definition: tensor.h:344
smaug::gem5::ScopedStats
A RAII helper class which dumps and/or resets gem5 stats at construction and destruction.
Definition: utils.h:118
smaug::threadPool
ThreadPool * threadPool
The user-space thread pool used by SMAUG to run multithreaded tasks.
Definition: globals.cpp:7
smaug::dout
const DebugStream & dout(int debugLevel)
Returns a DebugStream instance for the given debug level.
Definition: debug_stream.cpp:16
smaug::Scheduler::maybeRunOperator
void maybeRunOperator(Operator *op)
If none of the inputs to the current Operator are dead, then this will run the Operator; otherwise,...
Definition: scheduler.cpp:72
smaug::Scheduler::updateChildren
void updateChildren(Operator *op)
After an Operator is run, this updates the number of pending inputs on all its children.
Definition: scheduler.cpp:81
smaug::Scheduler::runNetwork
Tensor * runNetwork()
Runs the Network to completion.
Definition: scheduler.cpp:13
smaug::Scheduler::scheduleReady
Tensor * scheduleReady()
Runs the operators in the ready queue.
Definition: scheduler.cpp:59
smaug::fastForwardMode
bool fastForwardMode
True if we are simulating in fast-forward mode.
Definition: globals.cpp:5
smaug::ThreadPool::initThreadPool
void initThreadPool()
Initialize the thread pool.
Definition: thread_pool.cpp:66
smaug::Operator::setNumPendingInputs
void setNumPendingInputs(int num)
Set the number of input tensors that this operator is waiting on.
Definition: operator.h:101
smaug::Operator::run
virtual void run()=0
Executes the Operator.
smaug::Operator
Operator is the base class for all graph operators supported by SMAUG.
Definition: operator.h:28
smaug
The smaug namespace is the parent namespace of all C++ code in SMAUG.
Definition: backend.cpp:38
smaug::gem5::switchCpu
void switchCpu()
Switches to the next CPU type.
Definition: utils.cpp:44
smaug::Scheduler::readyQueue
std::list< Operator * > readyQueue
The queue of all Operators ready to be executed.
Definition: scheduler.h:46
smaug::Operator::isDead
virtual bool isDead()
Returns true if the Operator is dead.
Definition: operator.cpp:5