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

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