-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliasenv_simu.h
181 lines (148 loc) · 6.62 KB
/
aliasenv_simu.h
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
#ifndef aliasenv_SIMU_GUARD
#define aliasenv_SIMU_GUARD
#include "defines.h"
#include "writer.h"
#include "aliasenv_simu.h"
#include <map>
namespace ctb
{
/**
* for general aliasenv documentation see the aliasenv_generator.h
*
* This specific class relizes generation environment for a simu testing output. That is this class drives generation of a simu method test which somehow tests whether internal instruction generators work as expected without providing full input/output syntax of bobox. When writting this, bobox is still unavailable to the tester and thus it makes sense to have 'something' which can be compiled.
*
* Specifically we use internal aliases defined by aliasenv_generator and on top we define expansions for 'input' and 'output' aliases, which are output-environment dependent and thus are defined neither as part of instruction table nor part of the internal generator. Then we load into memory templates from file and direct their composition. During this process we use also an actual generator to generate actual 'isntruction code'.
* */
class aliasenv_simu : public aliasenv_generator
{
protected:
typedef map<string, string> aliastab_t;
static aliastab_t aliases;
static void init();
template <class G> static writer<aliasenv_simu> generate_body(int m, G& generator, string name);
public:
typedef language_cpp language;
static string get_name();
static string alias(const string& a, bool* s = NULL);
template <class G> static writer<aliasenv_simu> generate(int m, G& generator, string name, stringlist args);
} ;
map<string, string> aliasenv_simu::aliases;
#define ADD(a,b) aliases.insert(aliastab_t::value_type(a,b))
string aliasenv_simu::get_name()
{
return "simu";
}
string aliasenv_simu::alias(const string& a, bool* s)
{
auto itr = aliases.find(a);
if(itr == aliases.end())
return aliasenv_generator::alias(a, s);
if(s != NULL)
*s = true;
return itr->second;
}
void aliasenv_simu::init()
{
static bool initialized = false;
if(initialized)
return;
aliasenv_generator::init();
ADD("input", "data_in_$ioindex[pos_in_$ioindex+j+$iindex]");
ADD("inputg", "data_in_$ioindex,pos_in_$ioindex+j+$iindex");
ADD("output", "data_out_$ioindex[pos_out_$ioindex+j+$iindex]");
ADD("outputg", "data_out_$ioindex,pos_out_$ioindex+j+$iindex");
ADD("fdeclin", writer<aliasenv_generator>::from_file(string().append(exec_path).append("templates/simu_decl_in.h")));
ADD("fdeclcont", writer<aliasenv_generator>::from_file(string().append(exec_path).append("templates/simu_decl_cont.h")));
ADD("fdeclout", writer<aliasenv_generator>::from_file(string().append(exec_path).append("templates/simu_decl_out.h")));
ADD("fenvin", writer<aliasenv_generator>::from_file(string().append(exec_path).append("templates/simu_env_in.h")));
ADD("fenvout", writer<aliasenv_generator>::from_file(string().append(exec_path).append("templates/simu_env_out.h")));
ADD("fbox", writer<aliasenv_generator>::from_file(string().append(exec_path).append("templates/simu_box.h")));
ADD("fbody", writer<aliasenv_generator>::from_file(string().append(exec_path).append("templates/simu_body.h")));
initialized = true;
}
template <class G>
writer<aliasenv_simu> aliasenv_simu::generate(int granularity, G& generator, string name, stringlist args)
{
#ifdef TMPTEST
if(granularity > 16)
granularity = 16;
#endif
init();
typedef writer<aliasenv_simu> wrt;
wrt decl;
string type_string;
for( auto n : generator.graph.out)
{
n->data.op->get_type_string(1, type_string);
decl.print("$fdeclcont", n->data.get_param("ioindex"), type_string);
}
writer<aliasenv_simu> bodies;
writer<aliasenv_simu> calls;
for(int i = 1; i <= granularity; i *= 2)
{
verbose(string() + "aliasenv simu - generating body for width " + ctb::to_string(i));
bodies += generate_body(i, generator, name);
calls.print("test_body$1();", i);
}
verbose(string() + "aliasenv simu - composing output ");
writer<aliasenv_simu> box;
box.print("$fbox", decl, bodies, calls) ;
return box;
}
template <class G>
writer<aliasenv_simu> aliasenv_simu::generate_body(int granularity, G& generator, string name)
{
typedef imp_contB<writer<aliasenv_simu>> wrt;
auto opts = generator.option_struct();
wrt ilist;
wrt olist;
wrt dummy;
wrt decl;
string type_string;
for( auto n : generator.graph.in)
{
n->data.op->get_type_string(1, type_string);
decl.print("$fdeclin", n->data.get_param("ioindex"), type_string, n->data.op->out_type, n->id);
}
for(auto n : generator.graph.out)
{
n->data.op->get_type_string(1, type_string);
decl.print("$fdeclout", n->data.get_param("ioindex"), type_string, n->data.op->out_type, n->id);
}
wrt envelopes;
for(auto n : generator.graph.in)
{
n->data.op->get_type_string(1, type_string);
envelopes.print("$fenvin", n->data.get_param("ioindex"), type_string, n->data.op->out_type);
}
for(auto n : generator.graph.out)
{
n->data.op->get_type_string(1, type_string);
envelopes.print("$fenvout", n->data.get_param("ioindex"), type_string, n->data.op->out_type, granularity, n->id);
}
wrt minlist;
minlist.print("std::numeric_limits<unsigned>::max()");
for(auto n : generator.graph.in)
minlist = wrt().print("std::min($2, size_in_$1 - pos_in_$1)", n->data.get_param("ioindex"), minlist);
for(auto n : generator.graph.out)
minlist = wrt().print("std::min($2, size_out_$1 - pos_out_$1)", n->data.get_param("ioindex"), minlist);
wrt code;
generator.generate(granularity, code, opts);
verbose(string() + "aliasenv simu - generated vectorized " );
wrt code2;
if(granularity != 1)
generator.generate(1, code2, opts);
verbose(string() + "aliasenv simu - generated singular " );
wrt inc;
for(auto n : generator.graph.in)
inc.print("pos_in_$1 += batch_size;", n->data.get_param("ioindex"));
for(auto n : generator.graph.out)
inc.print("pos_out_$1 += batch_size;", n->data.get_param("ioindex"));
verbose(string() + "aliasenv simu - printing body of a single function" );
wrt box;
box.print("$fbody", name, ilist, olist,decl, envelopes, minlist, code, code2, inc, granularity);
verbose(string() + "aliasenv simu - function composed" );
return box;
}
};
#endif