-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedLoop.cpp
More file actions
293 lines (227 loc) · 7.76 KB
/
Copy pathNestedLoop.cpp
File metadata and controls
293 lines (227 loc) · 7.76 KB
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
/*******************************************************************************
* Copyright (c) 2016, 2018 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include "NestedLoop.hpp"
//===========================================
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <string.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
using vec = vector<string>;
using matrix = vector<vec>;
//=============================================
//store the matrix in a vector
matrix readCSV(string filename)
{
char separator = ',';
matrix result;
string row, item;
ifstream in(filename);
while (getline(in, row))
{
vec R;
stringstream ss(row);
while (getline(ss, item, separator)) R.push_back(item);
result.push_back(R);
}
in.close();
return result;
}
//=================================================
void printMatrix(const matrix& M)
{
for (vec row : M)
{
for (string s : row) cout << setw(12) << left << s << " ";
cout << '\n';
}
}
//======================================================================
MatMult::MatMult(OMR::JitBuilder::TypeDictionary *types)
: OMR::JitBuilder::MethodBuilder(types)
{
DefineLine(LINETOSTR(__LINE__));
DefineFile(__FILE__);
DefineName("matmult");
pString = types->PointerTo(types->toIlType<char *>());
pStr = types->toIlType<char *>();
DefineParameter("A", pString);
DefineParameter("B", pString);
DefineParameter("R", pString);
DefineParameter("N", Int32);
DefineParameter("res", Int32);
DefineReturnType(Int32);
}
//==================Load and store the strings=====================================================
void
MatMult::Store2DString(OMR::JitBuilder::IlBuilder *bldr,
OMR::JitBuilder::IlValue *base,
OMR::JitBuilder::IlValue *first,
OMR::JitBuilder::IlValue *second,
OMR::JitBuilder::IlValue *N,
OMR::JitBuilder::IlValue *value)
{
bldr->StoreAt(
bldr-> IndexAt(pString,
base,
first),
value);
}
OMR::JitBuilder::IlValue *
MatMult::Load2DString(OMR::JitBuilder::IlBuilder *bldr,
OMR::JitBuilder::IlValue *base,
OMR::JitBuilder::IlValue *first,
OMR::JitBuilder::IlValue *second,
OMR::JitBuilder::IlValue *N)
{
return
bldr->LoadAt(pString,
bldr-> IndexAt(pString,
base,
first));
}
//=================================================BUILDIL for String=============================================
bool
MatMult::buildIL()
{
OMR::JitBuilder::IlValue *A_val, *B_val;
//store no_res only if a value is not match for both arrays
OMR::JitBuilder::IlValue *nores_ik = ConstString("Not Found");
OMR::JitBuilder::IlValue *A = Load("A");
OMR::JitBuilder::IlValue *B = Load("B");
OMR::JitBuilder::IlValue *R = Load("R");
OMR::JitBuilder::IlValue *N = Load("N");
DefineLocal("result", Int32);
Store("result", ConstInt32(0));
OMR::JitBuilder::IlBuilder *aLoop=NULL;
ForLoopUp("a", &aLoop,
ConstInt32(1),
Load("N"),
ConstInt32(1));
OMR::JitBuilder::IlBuilder *bLoop=NULL;
aLoop->ForLoopUp("b", &bLoop,
aLoop-> ConstInt32(1),
aLoop-> Load("N"),
aLoop-> ConstInt32(1));
OMR::JitBuilder::IlBuilder *res=NULL, *nores=NULL;
bLoop-> IfThenElse(&res, &nores,
bLoop-> EqualTo(
bLoop-> LoadAt(pStr,
bLoop-> IndexAt(pStr,
A,
bLoop-> Load("a"))),
bLoop-> LoadAt(pStr,
bLoop-> IndexAt(pStr,
B,
bLoop-> Load("b")))
));
//If value matched
res-> StoreAt(
res-> IndexAt(pString,
R,
res-> Load("a")),
res-> LoadAt(pString,
res-> IndexAt(pString,
A,
res-> Load("a")))
);
res-> Store("result",Add(
Load("result"),
ConstInt32(1)
));
//If value not matched
nores-> StoreAt(
nores-> IndexAt(pString,
R,
nores-> Load("a")),
nores_ik
);
Return(
Load("result"));
return true;
}
//======================================================================================================
//print the array
void
printStringMatrix(const char **S, int32_t M){
for(int32_t i = 1; i < M; i++){
cout<<S[i]<<endl;
}
cout<<"\n";
}
void
MatMult::run(int n)
{
//read matrices before initialising
printf("Step 0: read matrices before initialising\n");
//read csv into temp1 and temp2
matrix temp1 = readCSV("data1_10000.csv");
matrix temp2 = readCSV("data2_10000.csv");
//================================================================================
//define the 1-D array size
const int32_t M_final=temp1.size();
//================================================================================
printf("Step 1: initialize JIT\n");
bool initialized = initializeJit();
if (!initialized)
{
fprintf(stderr, "FAIL: could not initialize JIT\n");
exit(-1);
}
printf("Step 2: define matrices\n");
const char* E_M[M_final];
const char* E_M1[M_final];
const char* R[M_final];
for(int32_t i = 1; i < M_final; i++){
E_M[i] = temp1[i][3].c_str();
E_M1[i] = temp2[i][1].c_str();
}
printf("Step 3: define type dictionaries\n");
OMR::JitBuilder::TypeDictionary types;
printf("Step 4: compile MatMult method builder\n");
MatMult method(&types);
void *entry=0;
int32_t rc = compileMethodBuilder(&method, &entry);
if (rc != 0)
{
fprintf(stderr,"FAIL: compilation error %d\n", rc);
exit(-2);
}
printf("Step 5: invoke MatMult compiled code\n");
MatMultFunctionType *test = (MatMultFunctionType *)entry;
int32_t res = 0;
int32_t Count = test(E_M, E_M1, R, M_final, res);
cout<<"print res: "<<Count<<endl;
printStringMatrix(R, M_final);
printf ("Step 6: shutdown JIT\n");
shutdownJit();
printf("PASS\n");
}