Repository navigation
Expand file tree
/
Copy pathtesttCache.cpp
More file actions
156 lines (129 loc) · 4.1 KB
/
Copy pathtesttCache.cpp
File metadata and controls
156 lines (129 loc) · 4.1 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
#include <iostream>
#include <fstream>
#include <vector>
#include <math.h>
#include <sstream>
#include <chrono>
#include <iomanip>
#include "tCache.h"
using namespace std;
using namespace std::chrono;
tCache<kunsat_ips_key,double> cache(200);
double time_in_get_Kunsat = 0;
double time_in_cache = 0;
/* Original function for which compute cache is needed */
double get_Kunsat(double Th, double Psi, double nVG,double alphaVG, double Ksat, int &hit)
{
double mpow, ahn, ahn_1, c1, c2;
double Kunsat;
auto start = high_resolution_clock::now();
mpow = 1.0-1.0/nVG;
ahn = pow((alphaVG*Psi),nVG);
ahn_1 = pow((alphaVG*Psi),(nVG-1.0));
c1 = pow((1.0+ahn),(-mpow));
c2 = pow((1.0+ahn),(0.5*mpow));
Kunsat = Ksat*(1.0-ahn_1*c1)*(1.0-ahn_1*c1)/c2;
auto stop = high_resolution_clock::now();
duration<double, std::milli> ms_double = stop - start;
//cout<< "Time in get_Kunsat: "<<ms_double.count()<<endl;
time_in_get_Kunsat += ms_double.count();
return Kunsat;
}
/* Cached function implementation */
double cached_getKunsat(double Th, double Psi, double nVG,double alphaVG, double Ksat, int &hit)
{
double Kunsat = 0;
auto start = high_resolution_clock::now();
kunsat_ips_key lookupkey(Th,Psi,nVG,alphaVG,Ksat);
if(cache.exists(lookupkey)){
Kunsat = cache.get(lookupkey);
if(Kunsat == 0)
cout << "Cached 0" <<endl;
hit = 1;
return Kunsat;
}
Kunsat = get_Kunsat(Th,Psi,nVG,alphaVG,Ksat,hit);
cache.put(lookupkey,Kunsat);
//cout << "Caching " <<Kunsat<<endl;
hit = 0;
auto stop = high_resolution_clock::now();
duration<double, std::milli> ms_double = stop - start;
//cout<< "Time in cache: "<<ms_double.count()<<endl;
time_in_cache += ms_double.count();
return Kunsat;
}
/* Pocess each line in the function trace */
int process_call_trace_line(std::string line, bool cacheEnabled, double &outkunsat)
{
double Th, Psi, nVG,alphaVG,Ksat;
double Kunsat = 0;
int hit = 0;
// parse line here & populate above
vector<string> tokens;
stringstream ss(line);
while (ss.good()) {
string token;
getline(ss, token, ',');
tokens.push_back(token);
}
Th = stod(tokens[0]);
Psi = stod(tokens[1]);
nVG = stod(tokens[2]);
alphaVG = stod(tokens[3]);
Ksat = stod(tokens[4]);
//cout << Th << "," << Psi << ", " << nVG << "," <<alphaVG << "," <<Ksat <<",";
if(cacheEnabled){
outkunsat = cached_getKunsat(Th,Psi,nVG,alphaVG,Ksat,hit);
}
else{
outkunsat = get_Kunsat(Th,Psi,nVG,alphaVG,Ksat,hit);
}
return hit;
}
int main(int argc, char* argv[])
{
int res = 0;
double hit=0, miss=0;
int num_lines = 0;
bool cache_enabled = 0;
double outkunsat = 0;
// handle different command line arguements
// nothing specified - run with cache disabled
// 0 as cmd line arg - cache disabled
// 1 as cmd line arg - cache enabled
if (argc == 1)
std:cout<<"cache not specified - running without cache"<<endl;
if (argc == 2) {
std::cout<<"cache enabled: "<< argv[1] <<endl;
cache_enabled = true;
}
std::string line;
std::ifstream infile("onehrfullprecisiontrace.txt");
//std::ifstream infile("calltrace.txt");
std::string outQualifier = cache_enabled?"cached":"nocache";
std::string outFileName = std::string("calloutput").append(outQualifier);
std::ofstream outputFile;
outputFile.open(outFileName,std::ios::binary);
for (; std::getline(infile,line) && num_lines < 100000; ++num_lines)
{
//cout << line <<endl;
res = process_call_trace_line(line, cache_enabled, outkunsat);
if(res == 0){
miss++;
}
else{
hit++;
}
if (outputFile.is_open())
outputFile<<setprecision(14)<< outkunsat<<endl;
}
outputFile.close();
std::cout<<endl<<"----------------------"<<endl;
std::cout<<"Total calls: "<<num_lines<<endl;
std::cout<<"Time spent in get_Kunsat: "<<time_in_get_Kunsat<<endl;;
std::cout<<"Time spent in cache: "<<time_in_cache<<endl;
std::cout<<"Total time: "<<time_in_cache+time_in_get_Kunsat<<endl;
std::cout<<"hit ratio: "<< hit/(num_lines)<<endl;
std::cout<<"miss ratio: "<<miss/(num_lines) <<endl;
std::cout<<endl<<"----------------------"<<endl;
}