FAUST compiler  0.9.9.6b8
loop.cpp
Go to the documentation of this file.
1 #include "loop.hh"
2 extern bool gVectorSwitch;
3 extern bool gOpenMPSwitch;
4 extern bool gOpenMPLoop;
5 
6 using namespace std;
7 
13 static void tab (int n, ostream& fout)
14 {
15  fout << '\n';
16  while (n--) fout << '\t';
17 }
18 
19 
26 static void printlines (int n, list<string>& lines, ostream& fout)
27 {
28  list<string>::iterator s;
29  for (s = lines.begin(); s != lines.end(); s++) {
30  tab(n, fout); fout << *s;
31  }
32 }
33 
34 
41 Loop::Loop(Tree recsymbol, Loop* encl, const string& size)
42  : fIsRecursive(true), fRecSymbolSet(singleton(recsymbol)), fEnclosingLoop(encl), fSize(size), fOrder(-1), fIndex(-1), fUseCount(0), fPrinted(0)
43 {}
44 
45 
51 Loop::Loop(Loop* encl, const string& size)
52  : fIsRecursive(false), fRecSymbolSet(nil), fEnclosingLoop(encl), fSize(size), fOrder(-1), fIndex(-1), fUseCount(0), fPrinted(0)
53 {}
54 
55 
64 {
65  Loop* l = this;
66  while ( l && isNil(setIntersection(l->fRecSymbolSet,S)) ) l=l->fEnclosingLoop;
67  return l != 0;
68 }
69 
75 {
76  return fPreCode.empty() && fExecCode.empty() && fPostCode.empty() && (fExtraLoops.begin()==fExtraLoops.end());
77 }
78 
82 void Loop::addPreCode (const string& str)
83 {
84  // cerr << this << "->addExecCode " << str << endl;
85  fPreCode.push_back(str);
86 }
87 
91 void Loop::addExecCode (const string& str)
92 {
93  // cerr << this << "->addExecCode " << str << endl;
94  fExecCode.push_back(str);
95 }
96 
97 
101 void Loop::addPostCode (const string& str)
102 {
103  // cerr << this << "->addPostCode " << str << endl;
104  fPostCode.push_front(str);
105 }
106 
107 
113 void Loop::absorb (Loop* l)
114 {
115  // the loops must have the same number of iterations
116  assert(fSize == l->fSize);
118 
119  // update loop dependencies by adding those from the absorbed loop
121 
122  // add the line of code of the absorbed loop
123  fPreCode.insert(fPreCode.end(), l->fPreCode.begin(), l->fPreCode.end());
124  fExecCode.insert(fExecCode.end(), l->fExecCode.begin(), l->fExecCode.end());
125  fPostCode.insert(fPostCode.begin(), l->fPostCode.begin(), l->fPostCode.end());
126 }
127 
128 
134 void Loop::println(int n, ostream& fout)
135 {
136  for (list<Loop*>::const_iterator s = fExtraLoops.begin(); s != fExtraLoops.end(); s++) {
137  (*s)->println(n, fout);
138  }
139 
140  if (fPreCode.size()+fExecCode.size()+fPostCode.size() > 0) {
141 /* if (gVectorSwitch) {
142  tab(n,fout);
143  fout << ((fIsRecursive) ? "// recursive loop" : "// vectorizable loop");
144  }*/
145 
146  tab(n,fout); fout << "// LOOP " << this ;
147  if (fPreCode.size()>0) {
148  tab(n,fout); fout << "// pre processing";
149  printlines(n, fPreCode, fout);
150  }
151 
152  tab(n,fout); fout << "// exec code";
153  tab(n,fout); fout << "for (int i=0; i<" << fSize << "; i++) {";
154  printlines(n+1, fExecCode, fout);
155  tab(n,fout); fout << "}";
156 
157  if (fPostCode.size()>0) {
158  tab(n,fout); fout << "// post processing";
159  printlines(n, fPostCode, fout);
160  }
161  tab(n,fout);
162  }
163 }
164 
165 
172 void Loop::printParLoopln(int n, ostream& fout)
173 {
174  for (list<Loop*>::const_iterator s = fExtraLoops.begin(); s != fExtraLoops.end(); s++) {
175  tab(n,fout); fout << "#pragma omp single";
176  tab(n,fout); fout << "{";
177  (*s)->println(n+1, fout);
178  tab(n,fout); fout << "}";
179  }
180 
181  if (fPreCode.size()+fExecCode.size()+fPostCode.size() > 0) {
182 
183  tab(n,fout); fout << "// LOOP " << this ;
184  if (fPreCode.size()>0) {
185  tab(n,fout); fout << "#pragma omp single";
186  tab(n,fout); fout << "{";
187  tab(n+1,fout); fout << "// pre processing";
188  printlines(n+1, fPreCode, fout);
189  tab(n,fout); fout << "}";
190  }
191 
192  tab(n,fout); fout << "// exec code";
193  tab(n,fout); fout << "#pragma omp for";
194  tab(n,fout); fout << "for (int i=0; i<" << fSize << "; i++) {";
195  printlines(n+1, fExecCode, fout);
196  tab(n,fout); fout << "}";
197 
198  if (fPostCode.size()>0) {
199  tab(n,fout); fout << "#pragma omp single";
200  tab(n,fout); fout << "{";
201  tab(n+1,fout); fout << "// post processing";
202  printlines(n+1, fPostCode, fout);
203  tab(n,fout); fout << "}";
204  }
205  tab(n,fout);
206  }
207 }
208 
214 void Loop::printoneln(int n, ostream& fout)
215 {
216  if (fPreCode.size()+fExecCode.size()+fPostCode.size() > 0) {
217 /* if (gVectorSwitch) {
218  tab(n,fout);
219  fout << ((fIsRecursive) ? "// recursive loop" : "// vectorizable loop");
220  }*/
221 
222  tab(n,fout); fout << "for (int i=0; i<" << fSize << "; i++) {";
223  if (fPreCode.size()>0) {
224  tab(n+1,fout); fout << "// pre processing";
225  printlines(n+1, fPreCode, fout);
226  }
227  printlines(n+1, fExecCode, fout);
228  if (fPostCode.size()>0) {
229  tab(n+1,fout); fout << "// post processing";
230  printlines(n+1, fPostCode, fout);
231  }
232  tab(n,fout); fout << "}";
233  }
234 }
235 
236 //-------------------------------------------------------
238 {
239  assert(l->fUseCount == 1);
240  assert(fBackwardLoopDependencies.size() == 1);
241  assert((*fBackwardLoopDependencies.begin()) == l);
242 
243  fExtraLoops.push_front(l);
245 }
void println(int n, ostream &fout)
print the loop
Definition: loop.cpp:134
void printParLoopln(int n, ostream &fout)
print the loop with a #pragma omp loop
Definition: loop.cpp:172
static void tab(int n, ostream &fout)
Print n tabs (for indentation purpose)
Definition: loop.cpp:13
Loop *const fEnclosingLoop
Loop from which this one originated.
Definition: loop.hh:56
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
list< string > fPostCode
code to execute at the end of the loop
Definition: loop.hh:63
int fUseCount
how many loops depend on this one
Definition: loop.hh:68
Tree setIntersection(Tree A, Tree B)
Definition: list.cpp:327
void addExecCode(const string &str)
add a line of C++ code
Definition: loop.cpp:91
bool isNil(Tree l)
Definition: list.hh:137
void concat(Loop *l)
Definition: loop.cpp:237
bool isEmpty()
true when the loop doesn't contain any line of code
Definition: loop.cpp:74
void addPreCode(const string &str)
add a line of C++ code pre code
Definition: loop.cpp:82
void printoneln(int n, ostream &fout)
print the loop in scalar mode
Definition: loop.cpp:214
Tree fRecSymbolSet
recursive loops define a set of recursive symbol
Definition: loop.hh:55
Loop(Tree recsymbol, Loop *encl, const string &size)
create a recursive loop
Definition: loop.cpp:41
list< string > fExecCode
code to execute in the loop
Definition: loop.hh:62
Tree setUnion(Tree A, Tree B)
Definition: list.cpp:317
list< Loop * > fExtraLoops
extra loops that where in sequences
Definition: loop.hh:69
Tree singleton(Tree e)
Definition: list.cpp:302
void absorb(Loop *l)
absorb a loop inside this one
Definition: loop.cpp:113
bool hasRecDependencyIn(Tree S)
returns true is this loop or its ancestors define a symbol in S
Definition: loop.cpp:63
void addPostCode(const string &str)
add a line of C++ post code
Definition: loop.cpp:101
bool gOpenMPSwitch
Definition: main.cpp:143
Tree nil
Definition: list.cpp:116
static void printlines(int n, list< string > &lines, ostream &fout)
Print a list of lines.
Definition: loop.cpp:26
bool gOpenMPLoop
Definition: main.cpp:144
const string fSize
number of iterations of the loop
Definition: loop.hh:57
Definition: loop.hh:52
bool gVectorSwitch
Definition: main.cpp:138
list< string > fPreCode
code to execute at the begin of the loop
Definition: loop.hh:61
set< Loop * > fBackwardLoopDependencies
Loops that must be computed before this one.
Definition: loop.hh:59