-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
510 lines (428 loc) · 14.7 KB
/
main.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* Given a Temperature Trend, outputs the MTTF due to Thermal Cycle
*
* The MTTF due to thermal Cycle is caclulated in Three steps [1]:
*
* 1) Calculation of the thermal Cycles from a thermal profile using Downing simple rainbow counting algorithm[2]
* 2) Calculation, for each thermal cycle, the number of cycles to failure using Coffin-Mansion's rule:
* Ntc[i] = Atc(dT[i] - Tth)^-b * e^(Eatc/(KTmax[i]))
* 3) Calculate MTTF using Miner's rule:
* MTTF = (1/m)Ntc*sum,i=1 to m (ti)
*
*
* The Algorithm Works also in a Dynamic Case, calculating the R as stated in paper [4], formula (5)
* R = e ^ -(sum j=1 to i (Tj/ aj(T)))
*
*
* [1] Reinforcement Learning-Based Inter- and Intra-Application Thermal Optimization for Lifetime Improvement of Multicore Systems
* [2] S. Downing and D. Socie. Simple Rainflow Counting Algorithms. International Journal of Fatigue, 1982.
* [3] Steady-State Dynamic Temperature Analysis and Reliability Optimization for Embedded Multiprocessor Systems Ukhov I. et al.
* [4] A Lightweight and Open-source Framework for the Lifetime Estimation of Multicore Systems, Bolchini C., et Al.
* Lorenzo Di Tucci <personal AT lorenzoditucci.com>
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <vector>
#include <sstream>
#include <thread>
#include "rainflow.h"
using namespace std;
typedef struct TempTimes{
vector<float> temperatures;
vector<float> times;
}TempTimes;
void calculate_reliability(list<Cycles> *cycles);
streampos check_data_from_file(vector<float> *temperatures, vector<float> *times, streampos position, string filename, bool *firstRead);
void check_input_routine(vector<float> *e,vector<float> *t, string *filename);
void printNumbers(TempTimes *tempTimesp);
int run_dynamic(string filename, vector<float> temperature, vector<float> times);
int run_static(string filename, vector<float> temperature, vector<float> times);
int load_data_from_file(string filename, vector<float> *temperature, vector<float> *times);
float coffin_manson(Cycles cycle);
float miner_rule(list<float> Ntci, list<Cycles> cycles);
void show_usage(string exename);
int main(int argc, char *argv[]){
cout << "start" << endl;
string filename;
char version = 'N';
//int N = 8;
//int temperatures[10] = {30,25,8,50,3,5,28,45,1,12};
//int temperatures[19] = {8,6,3,2,5,10,7,5,6,7,8,10,8,4,2,3,6,0,-10};
// int temperatures[8] = {10,2,12,4,12,2,4,-10};
//int temperatures[29] = { 0, 5, 6, 5, 3, 2, 6, 8, 7, 6, 8, 9, 7, 5, 8, 9,1, 8, 4, 3, 4, 5, 6, 7, 5, 4, 6, 2, 0};
//int temperatures[30] = {10,8,24,54,76,99,76,44,52,63,23,56,21,78,43,21,4,5,6,2,1,8,6,5,4,23,72,54,76,100};
if (argc < 4) {
show_usage(argv[0]);
return 1;
}
std::string arg;
for(int i = 1; i < argc; i = i + 2){
arg = argv[i];
//cout << "arg " << arg << endl;
if(arg == "-h" || arg == "--help"){
show_usage(argv[0]);
return 1;
}
else if(arg == "-f" || arg == "--file"){
filename = argv[i+1];
}else if(arg == "-d" || arg == "--dynamic" || arg == "-s" || arg == "--static"){
if(arg[1] == '-'){
version = arg[2];
}else{
version = arg[1];
}
}else{
show_usage(argv[0]);
return 1;
}
}
cout << "filename " << filename << " version "<< version << endl;
vector<float> temperature;
vector<float> times;
if(version == 's'){
//static version
if(run_static(filename, temperature, times) != 0)
{
cerr << "ERROR: execution failed for previous error!" << endl;
return 1;
}
return 0;
}else if(version == 'd'){
if(run_dynamic(filename, temperature, times) != 0){
cerr << "ERROR: execution failed due to previous errors!" << endl;
return 1;
}
return 0;
}else{//not necessary
cerr << "ERROR: no version specified" << endl;
show_usage(argv[0]);
return 1;
}
}
int run_dynamic(string filename, vector<float> temperature, vector<float> times){
TempTimes tempTimes = TempTimes();
vector<float> *e = new vector<float>;
vector<float> *t = new vector<float>;
list<Cycles> *cycles = new list<Cycles>;
cout << "launching thread for checking input + peak&valleys...";
thread thread_pv(check_input_routine, e, t, &filename);
cout << "DONE" << endl;
cout << "launching thread for calculate the cycles......";
thread thread_cycles(rainflow_algorithm_dynamic, e, t, cycles);
cout << "DONE" << endl;
cout << "launching thread for calculate the reliability using a weibull distribution ... ";
thread thread_weibull(calculate_reliability, cycles);
cout << "DONE" << endl;
//cout << "first is executing..." << endl;
thread_pv.join();
thread_cycles.join();
cout << "we have " << (*cycles).size() - 1 << " cycles!" <<endl;
thread_weibull.join();
//cout << "main function " << tempTimes.temperatures.at(1) << endl;
cout << "program is terminated" << endl;
cout << "results..." <<endl;
for (std::list<Cycles>::iterator it=(*cycles).begin(); it != (*cycles).end(); it++){
if((*it).getRange() != -99999){ //should check also the others..
cout << "range " << (*it).getRange();
cout << " time " << (*it).getTime() << endl;
}
}
return 0;
}
void check_input_routine(vector<float> *e,vector<float> *t, string *filename){
//read value
//check if new value, for each new value...
vector<float> temperatures;
vector<float> times;
short trend = 0;
bool first = true;
bool firstRead = true;
bool end = false;
int index = 0;
int indexE = 0; //index for e...
float tempVal = 0.0;
int oldSize = 0;
streampos position;
int temperatureSize = 0;
bool check = false;
while(end != true){
position = check_data_from_file(&temperatures,×, position, filename[0], &firstRead);
//cout << "temperatures size is " <<temperatures.size() << endl;
if(temperatures.size() < 2){
//cout << "size less then 2 .." << endl;
//continue;
}else if(first) {
oldSize = 2;
check = true;
//cout << "first is true " << endl;
first = false;
if(temperatures.at(0) > temperatures.at(1)){
trend = 2; //then we give this to the algorithm...
}else if(temperatures.at(0) < temperatures.at(1)){
trend = 1;
}else{
trend = 0;
//continue;
}
}
for(int i = oldSize -1 ; i < temperatures.size() && temperatures.size() >=2 && oldSize >=2 && check; i++){
//cout << "inside for, i is " << i << "passing these vlues to function " << temperatures.at(i-1) << " - " << temperatures.at(i) << " trend " << trend << endl;
//e[indexE] = read_next_peak_valley_bw(temperatures.at(index-1), temperatures.at(index), &trend);
tempVal = read_next_peak_valley_bw(temperatures.at(i-1), temperatures.at(i), &trend);
//cout << "tempval is " << tempVal << endl;
//cout << " values e " << tempVal << " times " << times.at(i-1) << endl;
if(temperatures.at(i) == 999){ //fake stop signal
if(temperatures.at(i-1) != (*e).at((*e).size()-1)){ //so that i do not loose the last one
(*e).push_back(temperatures.at(i-1));
(*t).push_back(times.at(i-1));
(*e).push_back(-1);
//(*t).push_back(-1);
}
cout << " STOP signal received... terminating..." <<endl;
end = true;
continue;
}
//update time at index...
if(tempVal != -2){
(*e).push_back(tempVal);
(*t).push_back(times.at(i-1));
//cout << "pushing values e " << tempVal << " times " << times.at(i-1) << endl;
//t[indexE] = times.at(index);
indexE++;
}
oldSize++;
index++;
}
cout << "e: ";
for(int i = 0; i< (*e).size(); i++){
cout <<" " << (*e).at(i);
}
cout << endl;
//index++;
temperatureSize = temperatures.empty() ? 0 : temperatures.size();
if(temperatureSize == oldSize - 1){
check = false;
}else{
check = true;
//oldSize = temperatureSize;
}
cout << "old size now is " << oldSize << endl;
cout << "sleep for 5 seconds.... " << endl;
this_thread::sleep_for (std::chrono::seconds(5));
}
}
/*
new function for reading the file, gives back the position that will be used to start over next time(arabo)
*/
streampos check_data_from_file(vector<float> *temperatures, vector<float> *times, streampos position, string filename, bool *first){
int counter = 1; //1st temp, 2nd time
vector<float> temp;
ifstream inputFile (filename, ios::in);
//streampos startPos = inputFile.tellg();
if((*first) == true){
position = inputFile.tellg(); //position is the beginning of the file
(*first) = false;
}
if(inputFile.is_open()){
inputFile.seekg(position);
string tempString;
while( getline(inputFile, tempString)){
//cout << "string " << tempString << endl;
istringstream ss(tempString );
while (ss)
{
string s;
if (!getline( ss, s, ',' )) break;
if(counter % 2 ==0){
times[0].push_back(atof(s.c_str()));
}else{
temperatures[0].push_back(atof(s.c_str()));
}
counter++;
}
position = inputFile.tellg();
}
inputFile.close();
}else{
cerr << "Error while Opening " << filename << endl;
return -1;
}
/*
for(int i=0; i<temperature.size(); i++){
cout << " temp && time " << endl;
for(int j = 0; j < temperature.at(i).size(); j++)
cout << " " << temperature.at(i).at(j);
cout << endl;
}
*/
counter --;
if(counter % 2 != 0){
cerr << "mismatch in temperature/time, the number is not even " << endl;
return -1;
}
return position;
}
//used to debug, to remove...
void printNumbers(TempTimes *tempTimes){
//TempTimes *tempTimes = static_cast<TempTimes*>(tempTimesP);
for(int i = 0; i< 10; i++){
cout << "thread" << " print " << i << endl ;
}
(*tempTimes).temperatures.push_back(2.0);
cout << "pusho dentro funzione thread..." << endl;
}
int run_static(string filename, vector<float> temperature, vector<float> times){
if(load_data_from_file(filename, &temperature, ×) == -1){
cerr << "error while loading the data! " << endl;
return -1;
}
for(int i=0; i<temperature.size(); i++){
cout << "temperature "<<temperature.at(i) << "time "<<times.at(i) << endl;
}
cout << endl;
list<Cycles> cycles = rainflow_algorithm(temperature,times, temperature.size());
list<float> Ntci;
if(cycles.back().getTemp1() == -1 && cycles.back().getTemp2() == -1 && cycles.back().getRange() == -1){
printf("error in cycle calculation!");
return -1;
}
cout << cycles.size() << "cycles" << endl;
for (std::list<Cycles>::iterator it=cycles.begin(); it != cycles.end(); it++){
cout << "range " << (*it).getRange();
cout << " time " << (*it).getTime() << endl;
//fake time
//(*it).setTime(3.57);
Ntci.push_back(coffin_manson(*it));
//cout.precision(20);
cout << "Ntci "<<Ntci.back()<<endl;
}
float MTTF = miner_rule(Ntci, cycles);
cout << "MTTF " << MTTF << endl;
cout << "years : " << MTTF/(24 * 365) << endl;
return 0;
}
int load_data_from_file(string filename, vector<float> *temperature, vector<float> *times){
int counter = 1; //1st temp, 2nd time
vector<float> temp;
ifstream inputFile (filename, ios::in);
if(inputFile.is_open()){
string tempString;
while(getline(inputFile, tempString)){
//cout << "string " << tempString << endl;
istringstream ss(tempString );
while (ss)
{
string s;
if (!getline( ss, s, ',' )) break;
if(counter % 2 ==0){
times[0].push_back(atof(s.c_str()));
}else{
temperature[0].push_back(atof(s.c_str()));
}
counter++;
}
}
inputFile.close();
}else{
cerr << "Error while Opening " << filename << endl;
return -1;
}
/*
for(int i=0; i<temperature.size(); i++){
cout << " temp && time " << endl;
for(int j = 0; j < temperature.at(i).size(); j++)
cout << " " << temperature.at(i).at(j);
cout << endl;
}
*/
counter --;
if(counter % 2 != 0){
cerr << "mismatch in temperature/time, the number is not even " << endl;
return -1;
}
return 0;
}
float miner_rule(list<float> Ntci, list<Cycles> cycles){
int m = cycles.size();
float Ntc = 0.0f;
for (std::list<float>::iterator it=Ntci.begin(); it != Ntci.end(); it++){
Ntc += (1.0/(*it));
}
//cout << " Ntc " << Ntc << endl;
Ntc = m/Ntc;
cout << "Ntc is "<<Ntc <<endl;
float MTTF = 0.0;
for (std::list<Cycles>::iterator it=cycles.begin(); it != cycles.end(); ++it){
MTTF += (*it).getTime();
}
MTTF = Ntc * MTTF / m;
return MTTF;
}
void calculate_reliability(list<Cycles> *cycles){
int i = 0;
float Ntc = 0.0;
float sum = 0.0;
float Beta = 1;
float R = 0.0;
bool flag = false;
//while((*cycles).size() == 0 && (*cycles).at(i).getRange() != -99999 && (*cycles).at(i).getTemp1() != -99999 && (*cycles).at(i).getTemp2() != -99999){
while(flag == false){
if((*cycles).size() == i ){
//sleep
cout << "RELIABILITY: nothing new to calculate..zz" << endl;
this_thread::sleep_for (std::chrono::seconds(5));
}else{
for(list<Cycles>::iterator it=std::next((*cycles).begin(), i); it != (*cycles).end() && flag == false; it++){
if((*it).getRange() == -99999 && (*it).getTemp1() == -99999 && (*it).getTemp2() == -99999){
cout << "RELIABILITY: termination cycle found... terminating..." << endl;
flag = true;
}
//update the value of the R...
Ntc = coffin_manson(*it);
sum += ((*it).getTime() / Ntc);
cout << "NTC is " << Ntc << " the sum now is..." << sum << endl;
i++;
}
//give an intermediate result of the R...
R = pow(sum, Beta);
R = exp(-R);
cout << "UPDATED R : "<< R << endl;
}
}
cout << " FINAL VALUE R = " << R << endl;
}
float coffin_manson(Cycles cycle){
//Ntc[i] = Atc(dT[i] - Tth)^-b * e^(Eatc/(KTmax[i]))
//the variable have been defined following section 9.2 of the paper [3]
//variable definition
int Atc = 11; //***empirically determinedconstant
float dTi = abs(cycle.getTemp1() - cycle.getTemp2()); //amplitude of the ith thermal cycle
int Tth = 0; //**temperature at which elastic deformation begins
//int b = 6; //**Coffin-Manson exponent constant
int b = 9; //JEP122F
float Eatc = 0.5; //**activation energy
float Tmaxi = max(cycle.getTemp1(), cycle.getTemp2()); //maximum temperature in the ith thermal cycle.
float K = 8.6173324 * pow(10, -5); //Boltzman eV/°K
float Ntci = 0.0;
Ntci += Atc * (dTi - Tth);
//cout << Ntci << " t maxi " << Tmaxi;
Ntci = pow(Ntci, -b) * exp(Eatc/(K*Tmaxi));
//cout << " " <<Ntci<<endl;
return Ntci;
}
void show_usage(string exename){
//show how to use the tool
cout << "Model for Evaluating MTTF due to Temperature Cycle " << endl;
cout << "Course Dependable Systems @ Politecnico di Milano, 2015 "<< endl;
cout << "Lorenzo Di Tucci <personal AT lorenzoditucci.com >" << endl;
cout << "Usage : " << endl;
cout << exename << endl;
cout << "-h | --help show this help" <<endl;
cout << "-f | --file specify input file: <temperature1, time1,........,temperatureN, timeN> " <<endl;
cout << "TO terminate the dynamic algorithm just add 999,something as final value in the file! " <<endl;
cout << "-d | --dynamic | -s | --static choose the version of the algorithm" << endl;
return;
}