/
HpcOmBenchmarkExt.cpp
450 lines (411 loc) · 12 KB
/
HpcOmBenchmarkExt.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//#include <iostream>
//#include <omp.h>
//
//#ifdef WIN32
//#include <windows.h>
//#endif
//
//#define THREAD_NUM 2
//#define REPLICATIONS 10000
//#define WARMUP 10000
//
//#define PACKAGE_SIZE_BIG 128
//#define PACKAGE_SIZE_SMALL 1
//
//int itemCount = 0;
//double items[PACKAGE_SIZE_BIG];
//unsigned long long comTimes[REPLICATIONS];
//
//inline volatile long long RDTSC() {
// register long long TSC asm("eax");
// asm volatile (".byte 15, 49" : : : "eax", "edx");
// return TSC;
//}
#include "expat.h"
#include <list>
#include <string>
#include <sstream>
#include <stdio.h>
#include <fstream>
#include "cJSON.h"
struct Equation {
int id;
unsigned long calcTimeCount;
double calcTime;
Equation() :
id(-1), calcTimeCount(-1), calcTime(-1.0) {
}
};
/**
* Approximate the required time for operations (mult,add).
* result: 2-parameters (m,n) y=mx+n
*/
void* HpcOmBenchmarkExtImpl__requiredTimeForOp() {
// unsigned long long calcTimesMul[REPLICATIONS];
// unsigned long long calcTimesAdd[REPLICATIONS];
// unsigned int sumCalc = 0;
// void *res = mk_nil();
//
// //warmup
// for (int i = 0; i < REPLICATIONS; i++)
// {
// double last = 1234.123 + i;
// last = last * 3.6424;
// }
//
// //bench mul
// for (int i = 0; i < REPLICATIONS; i++)
// {
// double last = 1234.123 + i;
// unsigned long long t1 = RDTSC();
// double res = last * 3.6424;
// unsigned long long t2 = RDTSC();
// calcTimesMul[i] = (t2-t1);
// }
//
// //bench add
// for (int i = 0; i < REPLICATIONS; i++)
// {
// double last = 1234.123 + i;
// unsigned long long t1 = RDTSC();
// double res = last + 3.6424;
// unsigned long long t2 = RDTSC();
// calcTimesAdd[i] = (t2-t1);
// }
//
// for (int i = 0; i < REPLICATIONS; i++)
// {
// sumCalc += calcTimesAdd[i];
// sumCalc += calcTimesMul[i];
// }
//
// int m = 1;
// res = mk_cons(mk_icon((sumCalc/(REPLICATIONS*2))),res); //push n
// res = mk_cons(mk_icon(m),res); //push m
void *res = mk_nil();
res = mk_cons(mk_icon(24), res); //push n
res = mk_cons(mk_icon(1), res); //push m
return res;
}
//void sendMessage (int warmUp, int replications, int packageSize)
//{
// for (int i = 0; i < warmUp; i++)
// {
// for(int j=0; j < packageSize; j++)
// {
// items[j] = 672364.8897+i+j;
// }
// itemCount++;
// while(itemCount > 0);
// }
//
// for (int i = 0; i < replications; i++)
// {
// for(int j=0; j < packageSize; j++)
// {
// items[j] = 672364.8897+i+j;
// }
// itemCount++;
// unsigned long long t1 = RDTSC();
// while(itemCount > 0);
// unsigned long long t2 = RDTSC();
// comTimes[i] = (t2-t1);
// }
//}
//
//void waitForMessage(int warmUp, int replications, int packageSize)pt..str()
//{
// double last[packageSize];
//
// for (int i = 0; i < warmUp; i++)
// {
// while(itemCount == 0);
// for(int j=0; j < packageSize; j++)
// {
// last[j] = items[j];
// }
// itemCount--;
// }
//
// for (int i = 0; i < replications; i++)
// {
// while(itemCount == 0);
// for(int j=0; j < packageSize; j++)
// {
// last[j] = items[j];
// }
// itemCount--;
// }
//}
/**
* Approximate the required time to send doubles to another cpu.
* result: 2-parameters (m,n) y=mx+n
*/
void* HpcOmBenchmarkExtImpl__requiredTimeForComm() {
// void *res = mk_nil();
// unsigned int sumComSmall = 0;
// unsigned int sumComBig = 0;
//
// omp_set_num_threads(THREAD_NUM);
// omp_set_dynamic(0);
//
// //Benchmark for small package
// #pragma omp parallel for shared(items) shared(itemCount)
// for (int i=0; i < THREAD_NUM; i++)
// {
// if((i % 2) == 0)
// {
//#ifdef WIN32
// DWORD_PTR mask = (1 << omp_get_thread_num());
// SetThreadAffinityMask( GetCurrentThread(), mask );
// SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
//#endif
// sendMessage(WARMUP,REPLICATIONS, PACKAGE_SIZE_SMALL);
// }
// else
// {
//#ifdef WIN32
// DWORD_PTR mask = (1 << omp_get_thread_num());
// SetThreadAffinityMask( GetCurrentThread(), mask );
// SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
//#endif
// waitForMessage(WARMUP,REPLICATIONS, PACKAGE_SIZE_SMALL);
// }
// }
//
// for (int i = 0; i < REPLICATIONS; i++)
// sumComSmall += comTimes[i];
//
// //Benchmark for big package
// #pragma omp parallel for shared(items) shared(itemCount)
// for (int i=0; i < THREAD_NUM; i++)
// {
// if((i % 2) == 0)
// {
//#ifdef WIN32
// DWORD_PTR mask = (1 << omp_get_thread_num());
// SetThreadAffinityMask( GetCurrentThread(), mask );
// SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
//#endif
// sendMessage(WARMUP,REPLICATIONS, PACKAGE_SIZE_BIG);
// }
// else
// {
//#ifdef WIN32
// DWORD_PTR mask = (1 << omp_get_thread_num());
// SetThreadAffinityMask( GetCurrentThread(), mask );
// SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
//#endif
// waitForMessage(WARMUP,REPLICATIONS, PACKAGE_SIZE_BIG);
// }
// }
//
// for (int i = 0; i < REPLICATIONS; i++)
// sumComBig += comTimes[i];
//
// res = mk_cons(mk_icon(sumComSmall/(REPLICATIONS*2)),res); //push n
// res = mk_cons(mk_icon((sumComBig-sumComSmall)/((PACKAGE_SIZE_BIG-PACKAGE_SIZE_SMALL)*(REPLICATIONS*2))),res); //push m
void *res = mk_nil();
res = mk_cons(mk_icon(70), res); //push n
res = mk_cons(mk_icon(4), res); //push m
return res;
}
class XmlBenchReader {
private:
struct ParserUserData {
std::string *errorMsg;
int level;
Equation *currentEquation;
std::list<Equation*> *equations;
ParserUserData(std::string *_errorMsg, int _level,
Equation *_currentEquation, std::list<Equation*> *_equations) {
errorMsg = _errorMsg;
level = _level;
currentEquation = _currentEquation;
equations = _equations;
}
};
protected:
static void StartElement(void *data, const XML_Char *name,
const XML_Char **attribute) {
ParserUserData *userData = (ParserUserData*) data;
userData->level++;
if ((userData->level == 3) && (strcmp("equation", name) == 0)) {
userData->currentEquation = new Equation();
for (int i = 0; attribute[i]; i += 2) {
if (strcmp("id", attribute[i]) == 0) {
userData->currentEquation->id = strtol(attribute[i + 1] + 2,
0, 10);
}
}
}
if ((userData->level == 4) && (strcmp("calcinfo", name) == 0)
&& (userData->currentEquation != 0)) {
//find attributes
for (int i = 0; attribute[i]; i += 2) {
if (strcmp("time", attribute[i]) == 0)
userData->currentEquation->calcTime = atof(
attribute[i + 1]);
if (strcmp("count", attribute[i]) == 0)
userData->currentEquation->calcTimeCount = strtoul(
attribute[i + 1], 0, 10);
}
userData->equations->push_back(userData->currentEquation);
userData->currentEquation = 0;
}
}
static void EndElement(void *data, const XML_Char *el) {
ParserUserData *userData = (ParserUserData*) data;
userData->level--;
}
static bool deleteAll(Equation *theElement) {
delete theElement;
return true;
}
public:
XmlBenchReader(void) {
}
~XmlBenchReader(void) {
}
static std::list<std::list<double> > ReadBenchFileEquations(std::string filePath) {
FILE *xmlFile;
const int bufferSize = 10000;
char buffer[bufferSize];
XML_Parser parser;
int len; /* len is the number of bytes in the current buffer of data */
int done = 0;
std::string errMsg = "";
std::list<Equation*> eqList = std::list<Equation*>();
std::list<std::list<double> > resultList =
std::list<std::list<double> >();
ParserUserData userData = ParserUserData(&errMsg, 0, 0, &eqList);
xmlFile = fopen(filePath.c_str(), "r");
parser = XML_ParserCreate(NULL);
XML_SetUserData(parser, &userData);
XML_SetElementHandler(parser, StartElement, EndElement);
do {
//Read the graphml-file piece by piece
len = fread(buffer, sizeof(char), bufferSize, xmlFile);
if (len < bufferSize)
done = true;
if (XML_Parse(parser, buffer, len, done) == XML_STATUS_ERROR) {
//std::cout << "Error during xml-parsing" << std::endl;
break;
}
} while (!done);
XML_ParserFree(parser);
fclose(xmlFile);
//Copy equation list to result list
for (std::list<Equation*>::iterator it = eqList.begin();
it != eqList.end(); it++) {
//std::cout << "Equation " << (*it)->id << " calcTime: " << (*it)->calcTime << " calcCount: " << (*it)->calcTimeCount << std::endl;
std::list<double> tmpLst = std::list<double>();
tmpLst.push_back((*it)->id);
tmpLst.push_back((*it)->calcTime);
tmpLst.push_back((*it)->calcTimeCount);
resultList.push_back(tmpLst);
}
//Clean equation list
eqList.remove_if(deleteAll);
return resultList;
}
};
std::list<std::list<double> > ReadJsonBenchFileEquations(std::string filePath)
{
std::list<std::list<double> > resultList = std::list<std::list<double> >();
FILE *fp;
long lSize;
char *buffer;
int arraySize, i;
cJSON *root;
cJSON *profileBlocks;
fp = fopen ( filePath.c_str() , "rb" );
if( !fp ) perror(filePath.c_str()),exit(1);
fseek( fp , 0L , SEEK_END);
lSize = ftell( fp );
rewind( fp );
/* allocate memory for entire content */
buffer = (char*)calloc( 1, lSize+1 );
if( !buffer ) fclose(fp),fputs("memory alloc fails",stderr),exit(1);
/* copy the file into the buffer */
if( 1!=fread( buffer , lSize, 1 , fp) )
fclose(fp),free(buffer),fputs("entire read fails",stderr),exit(1);
/* do your work here, buffer is a string contains the whole text */
root = cJSON_Parse(buffer);
profileBlocks = cJSON_GetObjectItem(root,"profileBlocks");
arraySize = cJSON_GetArraySize(profileBlocks);
for(i = 0; i < arraySize; i++)
{
cJSON *item = cJSON_GetArrayItem(profileBlocks, i);
cJSON *idItem = cJSON_GetObjectItem(item, "id");
cJSON *ncallItem = cJSON_GetObjectItem(item, "ncall");
cJSON *timeItem = cJSON_GetObjectItem(item, "time");
std::list<double> tmpLst = std::list<double>();
tmpLst.push_back(idItem->valuedouble);
tmpLst.push_back(timeItem->valuedouble);
tmpLst.push_back(ncallItem->valuedouble);
resultList.push_back(tmpLst);
}
fclose(fp);
free(buffer);
return resultList;
}
void* HpcOmBenchmarkExtImpl__readCalcTimesFromXml(const char *filename)
{
void *res = mk_nil();
std::string errorMsg = std::string("");
std::ifstream ifile(filename);
if (!ifile)
{
errorMsg = "File '";
errorMsg += std::string(filename);
errorMsg += "' does not exist";
res = mk_cons(mk_scon(errorMsg.c_str()), mk_nil());
printf("%s\n",errorMsg.c_str());
return res;
}
std::list<std::list<double> > retLst =
XmlBenchReader::ReadBenchFileEquations(filename);
for (std::list<std::list<double> >::iterator it = retLst.begin();
it != retLst.end(); it++) {
int i = 0;
for (std::list<double>::iterator iter = (*it).begin();
iter != (*it).end(); iter++) {
if (i >= 3)
break;
res = mk_cons(mk_rcon(*iter), res);
//std::cerr << "value " << *iter << std::endl;
}
}
//std::cerr << "Blaaaa2" << std::endl;
return res;
}
void* HpcOmBenchmarkExtImpl__readCalcTimesFromJson(const char *filename)
{
void *res = mk_nil();
std::string errorMsg = std::string("");
std::ifstream ifile(filename);
if (!ifile)
{
errorMsg = "File '";
errorMsg += std::string(filename);
errorMsg += "' does not exist";
res = mk_cons(mk_scon(errorMsg.c_str()), mk_nil());
printf("%s\n",errorMsg.c_str());
return res;
}
std::list<std::list<double> > retLst = ReadJsonBenchFileEquations(filename);
for (std::list<std::list<double> >::iterator it = retLst.begin();
it != retLst.end(); it++) {
int i = 0;
for (std::list<double>::iterator iter = (*it).begin();
iter != (*it).end(); iter++) {
if (i >= 3)
break;
res = mk_cons(mk_rcon(*iter), res);
//std::cerr << "value " << *iter << std::endl;
}
}
//std::cerr << "Blaaaa2" << std::endl;
return res;
}