-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsm.cpp
319 lines (286 loc) · 7.12 KB
/
sm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include "sm.h"
#include "clhelp.h"
#include "docs.h"
#include "operations.h"
extern Docs docs;
/* Semantic model node constructors */
Node::Node(std::string label) : label(label) {};
Node::Node() : label("GenericNode") {};
Stmt::Stmt(std::string label) : Node(label) {};
Stmt::Stmt() : Node("Stmt") {};
/* Symbol Table */
SymbolTable::SymbolTable() : Node("SymbolTable") {};
ConstantTable::ConstantTable() : Node("ConstantTable") {};
Variable::Variable() : Node("Variable"), lives_in_python(false), level(PLATFORM), operand(NULL)
{
if(getenv("ZERO_COPY"))
{
use_host_ptr = true;
}
else
{
use_host_ptr = false;
}
};
Constant::Constant() : Node("Constant") {};
ScalarConstant::ScalarConstant() : Constant() {};
Array::Array() : Variable()
{
cpu_data = NULL;
// Get num devices
gpu_data = new cl_mem[MAX_DEVICES];
};
Array2D::Array2D() : Variable()
{
cpu_data = NULL;
// Get num devices
gpu_data = new cl_mem[MAX_DEVICES];
}
Array3D::Array3D() : Variable()
{
cpu_data = NULL;
// Get num devices
gpu_data = new cl_mem[MAX_DEVICES];
}
Stencil::Stencil() : Variable()
{
cpu_data = NULL; cpu_offx = NULL; cpu_offy = NULL;
gpu_data = new cl_mem[MAX_DEVICES];
gpu_offx = new cl_mem[MAX_DEVICES];
gpu_offy = new cl_mem[MAX_DEVICES];
};
SpMat::SpMat() : Variable()
{
cpu_data = NULL;
gpu_data = new cl_mem[MAX_DEVICES];
gpu_offsets = new cl_mem[MAX_DEVICES];
};
SpMat2D::SpMat2D() : Variable()
{
cpu_data = NULL;
gpu_data = new cl_mem[MAX_DEVICES];
gpu_offx = new cl_mem[MAX_DEVICES];
gpu_offy = new cl_mem[MAX_DEVICES];
};
CSRMat::CSRMat() : Variable()
{
cpu_data = NULL;
gpu_data = new cl_mem[MAX_DEVICES];
gpu_indices = new cl_mem[MAX_DEVICES];
gpu_indptr = new cl_mem[MAX_DEVICES];
};
Scalar::Scalar() : Variable()
{
cpu_data = NULL;
gpu_data = new cl_mem[MAX_DEVICES];
};
/* Hindemith AST */
SemanticModel::SemanticModel() : Node("SemanticModel") {};
FunctionDef::FunctionDef() : Node("FunctionDef") {};
Iterator::Iterator() : Node("Iterator") {};
PipeAndFilter::PipeAndFilter() : Node("PipeAndFilter") {};
Name::Name() : Node("Name") {};
void Node::parse(pugi::xml_node node)
{
for(pugi::xml_attribute attribute = node.first_attribute() ;
attribute; attribute = attribute.next_attribute())
{
properties[attribute.name()] = attribute.value();
}
}
int Node::get_property_int(std::string key)
{
return atoi(properties[key].c_str());
}
struct sm_visitor: pugi::xml_tree_walker
{
std::vector<Node*> parents;
virtual bool for_each(pugi::xml_node& node)
{
while((int)parents.size() > (int)depth())
{
parents.pop_back();
}
Node * ast_node;
/* Pick the right node */
/* Symbol table */
if(!strcmp(node.name(), "SymbolTable"))
{
ast_node = new SymbolTable();
ast_node->parse(node);
}
else if(!strcmp(node.name(), "ConstantTable"))
{
ast_node = new ConstantTable();
ast_node->parse(node);
}
else if(!strcmp(node.name(), "ScalarConstant"))
{
ast_node = new ScalarConstant();
ast_node->parse(node);
}
else if(!strcmp(node.name(), "SpMat"))
{
ast_node = new SpMat();
ast_node->parse(node);
}
else if(!strcmp(node.name(), "SpMat2D"))
{
ast_node = new SpMat2D();
ast_node->parse(node);
}
else if(!strcmp(node.name(), "Stencil"))
{
ast_node = new Stencil();
ast_node->parse(node);
}
else if(!strcmp(node.name(), "CSRMat"))
{
ast_node = new CSRMat();
ast_node->parse(node);
}
else if(!strcmp(node.name(), "Array"))
{
ast_node = new Array();
ast_node->parse(node);
}
else if(!strcmp(node.name(), "Array2D"))
{
ast_node = new Array2D();
ast_node->parse(node);
}
else if(!strcmp(node.name(), "Array3D"))
{
ast_node = new Array3D();
ast_node->parse(node);
}
else if(!strcmp(node.name(), "Scalar"))
{
ast_node = new Scalar();
ast_node->parse(node);
}
/* Hindemith AST */
else if(!strcmp(node.name(), "SemanticModel"))
{
ast_node = new SemanticModel();
ast_node->parse(node);
ast_node->label = ast_node->properties["name"];
}
else if(!strcmp(node.name(), "FunctionDef"))
{
ast_node = new FunctionDef();
ast_node->parse(node);
ast_node->label = ast_node->properties["name"];
}
else if(!strcmp(node.name(), "Iterator"))
{
ast_node = new Iterator();
ast_node->parse(node);
ast_node->label = ast_node->properties["name"];
}
else if(!strcmp(node.name(), "PipeAndFilter"))
{
ast_node = new PipeAndFilter();
ast_node->parse(node);
ast_node->label = ast_node->properties["name"];
}
else if(!strcmp(node.name(), "Filter"))
{
ast_node = new Stmt();
ast_node->parse(node);
ast_node->label = ast_node->properties["name"];
}
else if(!strcmp(node.name(), "Name"))
{
ast_node = new Name();
ast_node->parse(node);
ast_node->label = ast_node->properties["name"];
}
else
{
std::cout << "Unknown node: " << node.name() << std::endl;
exit(-1);
}
setFunctionPointer((Stmt*)ast_node);
if(depth() > 0)
{
Node * parent = parents[depth() - 1];
parent->children.push_back(ast_node);
}
if((int)parents.size() <= (int)depth())
{
parents.push_back(ast_node); // Push self
}
return true; // continue traversal
}
};
void smparse(const char * xmlstring, Node ** p)
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_buffer(xmlstring, strlen(xmlstring));
if(!result)
{
std::cout << "XML parsing failed" << std::endl;
return;
}
else
{
#ifdef VERBOSE_COMPILATION
docs.compilation_ss << "Parsing successful" << std::endl;
#endif
}
sm_visitor vst;
doc.traverse(vst);
// Return the root FunctionDef node
*p = (Node*)vst.parents[0];
}
void print_info(Node * node)
{
}
void build_symbol_table(SymbolTable * sm, std::map<std::string, Variable*> & sym_tab)
{
sym_tab.clear();
#ifdef VERBOSE_COMPILATION
docs.compilation_ss << "Building symbol table" << std::endl;
#endif
for(std::vector<Node*>::iterator it = sm->children.begin() ;
it != sm->children.end() ; it++)
{
sym_tab[(*it)->properties["name"]] = (Variable *) (*it);
}
}
void build_constant_table(ConstantTable * sm, std::map<std::string, Constant*> & cst_tab)
{
cst_tab.clear();
#ifdef VERBOSE_COMPILATION
docs.compilation_ss << "Building constant table" << std::endl;
#endif
for(std::vector<Node*>::iterator it = sm->children.begin() ;
it != sm->children.end() ; it++)
{
cst_tab[(*it)->properties["name"]] = (Constant*) (*it);
}
}
void Variable::allocate(cl_vars_t clv) {}
void allocate_memory(std::map<std::string, Variable*> & sym_tab, cl_vars_t & clv)
{
#ifdef VERBOSE_COMPILATION
docs.compilation_ss << "Allocating memory" << std::endl;
#endif
for(std::map<std::string, Variable*>::iterator it = sym_tab.begin() ; it != sym_tab.end() ; it++)
{
Variable * v = it->second;
v->allocate(clv);
}
}
void print_vars(SymbolTable * sm, const cl_vars_t clv)
{
std::cout << "Printing variable CPU data" << std::endl;
}