The Sparta Modeling Framework
Loading...
Searching...
No Matches
DAG.hpp
Go to the documentation of this file.
1// <DAG.hpp> -*- C++ -*-
2
3
10#pragma once
11
12#include <cstdint>
13#include <string>
14#include <iostream>
15#include <sstream>
16#include <memory>
17#include <list>
18#include <map>
19#include <vector>
20#include <set>
21#include <cinttypes>
22#include <cassert>
23#include <utility>
24
29#include "sparta/kernel/Vertex.hpp"
30#include "sparta/kernel/VertexFactory.hpp"
31#include "sparta/kernel/EdgeFactory.hpp"
32
33namespace sparta
34{
35 class Unit;
36 class Scheduler;
37
38 class DAG
39 {
40
41 friend std::ostream& operator<<(std::ostream& os, const Edge &e);
42 friend std::ostream& operator<<(std::ostream& os, const Vertex &v);
43
44 friend Unit;
45
46 public:
47
48 // For backward compatibility
49 typedef Vertex GOPoint;
50 typedef std::map<std::string, Vertex*> VertexMap;
51
53 {
54 public:
55 CycleException(const typename Vertex::VertexList & cycle_set) :
57 cycle_set_(cycle_set)
58 {}
59
60 CycleException(const std::string& reason) :
61 SpartaException(reason)
62 {}
63
64 void writeCycleAsDOT (std::ostream & os) const;
65 void writeCycleAsText (std::ostream & os) const;
66
67 private:
68 //void outPutIssue_(std::ostream & os, bool dot) const;
69 typename Vertex::VertexList cycle_set_;
70 };
71
73
75
76 // DAG Nabbit
77 DAG(sparta::Scheduler * scheduler, const bool& check_cycles = false);
78
79 DAG() =delete;
80
84 early_cycle_detect_ = true;
85 }
86
91
96 uint32_t finalize();
97
99 bool isFinalized() const {
100 return finalized_;
101 }
102
110 Vertex* newFactoryVertex(const std::string& label,
111 sparta::Scheduler* const scheduler,
112 const bool isgop=false);
113
130 void link(Vertex *v, Vertex *w, const std::string & reason = "");
131
132 bool unlink(Vertex *v, Vertex *w)
133 {
134 sparta_assert(v != nullptr);
135 sparta_assert(w != nullptr);
136
137 return v->unlink(e_factory_, w);
138 }
139
140 uint32_t numGroups() {
141 return num_groups_;
142 }
143
144 bool sort();
145
151 Vertex* findGOPVertex(const std::string& label) const
152 {
153 auto loc = gops_.find(label);
154 return (loc != gops_.end()) ? loc->second : nullptr;
155 }
156
164 Vertex* newGOPVertex(const std::string& label, sparta::Scheduler* const scheduler)
165 {
166 sparta_assert(findGOPVertex(label) == nullptr);
167 Vertex* gop = this->newFactoryVertex(label, scheduler, true);
168 gops_[label] = gop;
169 return gop;
170 }
171
177 Vertex* getGOPoint(const std::string& label)
178 {
179 Vertex *gop = findGOPVertex(label);
180 if (gop == nullptr) {
181 return newGOPVertex(label, getScheduler());
182 }
183 return gop;
184 }
185
186 sparta::Scheduler * getScheduler() const{
187 return my_scheduler_;
188 }
189
191 bool detectCycle() const;
192
193 // Just print one cycle for now...
194 void printCycles(std::ostream& os) const;
195
197 void dumpToCSV(std::ostream& os_vertices, std::ostream& os_edges) const;
198
200 void print(std::ostream& os) const;
201
202 private:
203 // Just mark one cycle for now...
204 typename Vertex::VertexList getCycles_();
205
206 // Transfer Global Ordering Point GID's to associates
207 void finalizeGOPs_()
208 {
209 for (auto& i : gops_) {
210 i.second->transferGID();
211 }
212 }
213
215 VertexFactory v_factory_;
216 EdgeFactory e_factory_;
217
218 std::vector<Vertex*> alloc_vertices_;
219 uint32_t num_groups_ = 1;
220 bool early_cycle_detect_;
221 VertexMap gops_;
222 bool finalized_ = false;
223 sparta::Scheduler* my_scheduler_ = nullptr;
224 const log::MessageSource debug_logger_;
225 };//End class DAG
226
227
228 inline std::ostream& operator<<(std::ostream& os, const DAG &d)
229 {
230 d.print(os);
231 return os;
232 }
233
234 inline std::ostream& operator<<(std::ostream& os, const DAG *d)
235 {
236 os << *d;
237 return os;
238 }
239
240} // namespace sparta
241
File that defines the Scheduleable class.
File that defines the phases used in simulation.
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Exception class for all of Sparta.
Vertex * getGOPoint(const std::string &label)
Get the named GOP point; create it if not found.
Definition DAG.hpp:177
bool detectCycle() const
Look for cycles.
void link(Vertex *v, Vertex *w, const std::string &reason="")
link(): Establish a precedence relationship between two entities. This method will wrap the Schedulea...
Vertex * findGOPVertex(const std::string &label) const
Find a GOP point.
Definition DAG.hpp:151
void enableEarlyCycleDetect()
Definition DAG.hpp:83
Vertex * newFactoryVertex(const std::string &label, sparta::Scheduler *const scheduler, const bool isgop=false)
Get a new Vertex from the DAGs Vertex Factory Called in a Scheduleable after the scheduler_ has been ...
void initializeDAG_()
Initialize the DAG Creates new vertices from the VertexFactory and links them according to precedence...
void print(std::ostream &os) const
Print the DAG.
void dumpToCSV(std::ostream &os_vertices, std::ostream &os_edges) const
Dump the DAG to a CSV vertices file and an edges file.
Vertex * newGOPVertex(const std::string &label, sparta::Scheduler *const scheduler)
Create a new Vertex-GOP point.
Definition DAG.hpp:164
uint32_t finalize()
Finialize the DAG.
bool isFinalized() const
Is the DAG finalized?
Definition DAG.hpp:99
A class that lets you schedule events now and in the future.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
SpartaException()
Construct a SpartaException object with empty reason.
The is the base class for user defined blocks in simulation.
Definition Unit.hpp:38
Macros for handling exponential backoff.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo