]> AND Private Git Repository - blast.git/blob - Graph.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
a64d8aa7dd16a1279f670758ce95d3bf6542e7a9
[blast.git] / Graph.cpp
1 #include "Graph.h"
2 #include "GroupBlock.h"
3 #include "ReferenceBlock.h"
4 #include "FunctionalBlock.h"
5
6 Graph::Graph(bool createTopGroupIface) {
7   topGroup = new GroupBlock(NULL, createTopGroupIface);
8   topGroup->setName("top group");
9   groups.append(topGroup);
10 }
11
12 Graph::~Graph() {
13   
14   delete topGroup;
15 }
16
17 QList<AbstractInterface *> Graph::getOutsideInterfaces() {
18   return topGroup->getInterfaces();
19 }
20
21 GroupBlock* Graph::createChildGroupBlock(GroupBlock* parent, bool createGroupIface) {
22   GroupBlock* b = new GroupBlock(parent, createGroupIface);
23   groups.append(b);
24   return b;
25 }
26
27 void Graph::removeGroupBlock(GroupBlock *group) {
28   group->removeAllBlocks();
29   GroupBlock* parent = AB_TO_GRP(group->getParent());
30   parent->removeBlock(group);
31   groups.removeAll(group);
32 }
33
34 GroupBlock* Graph::getGroupBlockByName(QString name) {
35   foreach(GroupBlock* group, groups) {
36     if (group->getName() == name) return group;
37   }
38   return NULL;
39 }
40
41 FunctionalBlock* Graph::createFunctionalBlock(GroupBlock* group, ReferenceBlock* ref, bool createIfaces) {
42
43   FunctionalBlock* newBlock = new FunctionalBlock(group,ref, createIfaces);
44   group->addBlock(newBlock);
45
46   return newBlock;
47 }
48
49 FunctionalBlock* Graph::duplicateFunctionalBlock(FunctionalBlock *block) {
50
51   ReferenceBlock* ref = block->getReference();
52   GroupBlock* group = AB_TO_GRP(block->getParent());
53
54   // adding to the graph
55   FunctionalBlock* newBlock = createFunctionalBlock(group,ref, true);
56   return newBlock;
57 }
58
59
60 bool Graph::removeFunctionalBlock(FunctionalBlock* block) {
61   GroupBlock* group = AB_TO_GRP(block->getParent());
62   group->removeBlock(block);
63   return true;
64 }
65
66 FunctionalBlock* Graph::getFunctionalBlockByName(QString name, GroupBlock* parent) {
67   FunctionalBlock* block = NULL;
68   if (parent != NULL) {
69     block = AB_TO_FUN(parent->getFunctionalBlockByName(name));
70   }
71   else {
72     foreach(GroupBlock* group, groups) {
73       block = AB_TO_FUN(group->getFunctionalBlockByName(name));
74       if (block != NULL) return block;
75     }
76   }
77   return block;
78 }
79
80 FunctionalBlock* Graph::createSourceBlock(ReferenceBlock* ref, bool createIfaces) {
81
82   FunctionalBlock* newBlock = new FunctionalBlock(NULL,ref, createIfaces);
83   sources.append(newBlock);
84   return newBlock;
85 }
86
87 FunctionalBlock* Graph::duplicateSourceBlock(FunctionalBlock *block) {
88
89   ReferenceBlock* ref = block->getReference();  
90
91   // adding to the graph
92   FunctionalBlock* newBlock = createSourceBlock(ref, true);
93   return newBlock;
94 }
95
96 FunctionalBlock* Graph::getSourceBlockByName(QString name) {
97   foreach(FunctionalBlock* block, sources) {
98     if (block->getName() == name) return block;
99   }
100   return NULL;
101 }
102
103 bool Graph::removeSourceBlock(FunctionalBlock *block) {
104   sources.removeAll(block);
105   return true;
106 }
107
108 void Graph::createPatterns() throw(Exception) {
109   
110   foreach(AbstractBlock* block, sources) {
111     FunctionalBlock* funBlock = AB_TO_FUN(block);
112     try {
113       funBlock->createPatterns();
114     }
115     catch(Exception e) {
116       throw(e);      
117     }
118   }
119   
120   foreach(AbstractBlock* block, groups) {
121     GroupBlock* group = AB_TO_GRP(block);    
122     foreach(AbstractBlock* inBlock, group->getBlocks()) {
123       if (inBlock->isFunctionalBlock()) {
124         FunctionalBlock* funBlock = AB_TO_FUN(inBlock);
125         try {
126           funBlock->createPatterns();
127         }
128         catch(Exception e) {
129           throw(e);
130         }
131       }
132     }
133   }  
134 }
135
136 void Graph::resetPatternComputed() {
137   foreach(AbstractBlock* block, sources) {
138     block->setPatternComputed(false);
139     block->resetTraversalLevel();
140   }
141   foreach(AbstractBlock* block, groups) {
142     GroupBlock* group = AB_TO_GRP(block);
143     group->setPatternComputed(false);
144     block->resetTraversalLevel();
145     foreach(AbstractBlock* inBlock, group->getBlocks()) {
146       inBlock->setPatternComputed(false);
147       block->resetTraversalLevel();
148     }
149   }
150 }
151
152 void Graph::computeOutputPatterns(int nbExec) throw(Exception) {
153   
154   try {
155     createPatterns();
156   }
157   catch(Exception e) {
158     throw(e);    
159   }
160
161   resetPatternComputed();
162   // search for all block that are generators.
163   QList<FunctionalBlock*> generators;
164   generators.append(sources);
165   foreach(AbstractBlock* block, groups) {    
166     GroupBlock* group = AB_TO_GRP(block);    
167     foreach(AbstractBlock* inBlock, group->getBlocks()) {
168       FunctionalBlock* funBlock = AB_TO_FUN(inBlock);
169       if ((inBlock->isFunctionalBlock()) && (inBlock->isGeneratorBlock())) {
170         generators.append(funBlock);
171       }
172     }    
173   }
174   // search for maximum PP length
175   int maxPP = 0;
176   foreach(FunctionalBlock* block, generators) {    
177     if (block->getProductionPatternLength() > maxPP) maxPP = block->getProductionPatternLength();
178   }
179   // compute output for generators
180   int maxExecLen = maxPP*nbExec;
181   foreach(FunctionalBlock* block, generators) {    
182     int d = block->getProductionPatternLength();
183     block->computeOutputPattern((maxExecLen+d-1)/d);
184   }
185   // compute output for top group
186   try {
187     topGroup->computeOutputPattern();
188   }
189   catch(Exception e) {
190     throw(e);
191   }
192 }
193
194 void Graph::generateVHDL(const QString &path) throw(Exception) {
195   try {
196     topGroup->generateVHDL(path);
197   }
198   catch(Exception e) {
199     throw(e);
200   }
201 }
202
203 QList<QString> Graph::getExternalResources() {
204   QList<QString> list = topGroup->getExternalResources();
205   return list;
206 }