-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
295 lines (263 loc) · 7.66 KB
/
Copy pathconfig.cpp
File metadata and controls
295 lines (263 loc) · 7.66 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
294
295
#include "config.hpp"
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <fstream>
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <stdexcept>
#include "parser.hpp"
using namespace std;
string cacheSizeError = "Error: Usage is: L*CacheSize([<chip_idx>,] <size> <KB|MB|GB>)";
string blockSizeError = "Error: Usage is: L*BlockSize(<size> <B|KB>)";
string cacheAccessSpeedError = "Error: Usage is: cacheAccessSpeed([<chip_idx>,] <size> <us|ns>)";
ulong getCacheSize(string size)
{
vector<string> cacheSizeStr = parseSize(size);
ulong cacheSize = strtoul(cacheSizeStr[0].c_str(), NULL, 0) << 10;
switch(convertSize(cacheSizeStr[1]))
{
case GB:
cacheSize <<= 10;
case MB:
cacheSize <<= 10;
case KB:
break;
default:
throw invalid_argument(cacheSizeError);
}
return cacheSize;
}
ulong getBlockSize(string size)
{
vector<string> blockSizeStr = parseSize(size);
ulong blockSize = strtoul(blockSizeStr[0].c_str(), NULL, 0);
if(blockSizeStr[1] == "KB")
blockSize <<= 10;
else if(blockSizeStr[1] != "B")
throw invalid_argument(blockSizeError);
return blockSize;
}
template<class T>
void checkArray(T array, size_t size)
{
for(size_t i = 0; i < size; i++)
{
if(array[i] == 0)
throw invalid_argument("Detected missing or zero initalized argument");
}
}
template<class T>
string printArray(T array, size_t size)
{
stringstream stream;
for(size_t i = 0; i < size; i++)
{
stream << i << ": " << array[i] << ", ";
}
return stream.str();
}
ulong parseSpeed(vector<string> arguments, string command)
{
if(arguments.size() != 1)
throw invalid_argument("Error: Usage is: " + command + "(<size> <us|ns>)");
vector<string> speedStr = parseSize(arguments[0]);
ulong speed = strtoul(speedStr[0].c_str(), NULL, 0);
if(speedStr[1] == "us")
speed *= 1000;
else if(speedStr[1] != "ns")
throw invalid_argument("Error: Usage is: " + command + "(<size> <us|ns>)");
return speed;
}
ulong getCacheAccessSpeed(string size)
{
vector<string> cacheAccessSpeedStr = parseSize(size);
ulong cacheAccessSpeed = strtoul(cacheAccessSpeedStr[0].c_str(), NULL, 0);
if(cacheAccessSpeedStr[1] == "us")
cacheAccessSpeed *= 1000;
else if(cacheAccessSpeedStr[1] != "ns")
throw invalid_argument(cacheAccessSpeedError);
return cacheAccessSpeed;
}
bool Config::isInit(string command)
{
vector<string> commands {"L1CacheSize", "L1BlockSize", "L2CacheSize", "L2BlockSize", "L3CacheSize", "L3BlockSize", "blockSize", "cacheLineSize", "cacheSize", "cacheAccessSpeed", "replacementSpeed", "broadcastSpeed", "memoryAccessSpeed"};
for(size_t i = 0; i < commands.size(); i++)
{
if(command == commands[i])
return true;
}
return false;
}
void Config::initialize(int argc,char *argv[], bool skipPreamble)
{
string configFilePath = "config.txt";
//assume this is the file config
if (argc == 2)
configFilePath = argv[1];
ifstream configFile(configFilePath);
if (!configFile.is_open())
throw invalid_argument("Error: Configuration file could not be opened.");
string command = getUntilDelim(configFile, '(');
vector<string> arguments;
if (!skipPreamble)
{
string argument = getUntilDelim(configFile, ')');
arguments = parseSize(argument);
if(command != "memorySize")
throw invalid_argument("Error: First command must be \"memorySize()\"");
if(arguments.size() != 2 || (arguments[1] != "MB" && arguments[1] != "GB" && arguments[1] != "KB"))
throw invalid_argument("Error: Usage is \"memorySize(<size> <KB|MB|GB>)\"");
memSize = strtoul(arguments[0].c_str(), NULL, 0) << 10;
if(arguments[1] == "MB")
memSize <<= 10;
else if(arguments[1] == "GB")
memSize <<= 20;
command = getUntilDelim(configFile, '(');
if(command != "numOfChips" && command != "numOfCores")
throw invalid_argument("Error: Second command must be numOfChips() or numOfCores()");
if(command == "numOfChips")
{
argument = getUntilDelim(configFile, ')');
numChips = strtoul(argument.c_str(), NULL, 0);
command = getUntilDelim(configFile, '(');
}
else
{
numChips = 1;
}
numCores = new ulong[numChips];
cacheSizes = new ulong[numChips];
cacheAccessSpeeds = new ulong[numChips];
for(ulong i = 0; i < numChips; i++)
{
if(command != "numOfCores")
throw invalid_argument("Error: Expected numOfCores()");
arguments = splitUntilDelim(configFile, ',', ')');
if(arguments.size() == 1)
{
//all chips have same core #
for(ulong j = 0; j < numChips; j++)
{
numCores[j] = strtoul(arguments[0].c_str(), NULL, 0);
}
i = numChips; // end loop
}
else if(arguments.size() == 2)
{
ulong core = strtoul(arguments[0].c_str(), NULL, 0);
numCores[core] = strtoul(arguments[1].c_str(), NULL, 0);
}
else
{
throw invalid_argument("Error: Usage is: numOfCores([<chip_idx>,] <size>)");
}
command = getUntilDelim(configFile, '(');
}
}
while(isInit(command))
{
//cout << command << endl;
arguments = splitUntilDelim(configFile, ',', ')');
if(command == "cacheLineSize")
{
vector<string> cacheLineSizeStr = parseSize(arguments[0]);
cacheLineSize = strtoul(cacheLineSizeStr[0].c_str(), NULL, 0);
if(cacheLineSizeStr[1] == "KB")
cacheLineSize <<= 10;
else if(cacheLineSizeStr[1] != "B")
throw invalid_argument("Error: Usage is: cacheLineSize(<size> <B|KB>)");
}
else if(command == "cacheSize")
{
if(arguments.size() == 1)
{
if(numChips == 1)
{
//this is single L2 size
cacheSizes[0] = getCacheSize(arguments[0]);
}
else
{
//this is L3 size
l3CacheSize = getCacheSize(arguments[0]);
}
}
else if(arguments.size() == 2)
{
ulong cacheSize = getCacheSize(arguments[1]);
ulong chip = strtoul(arguments[0].c_str(), NULL, 0);
cacheSizes[chip] = cacheSize;
}
else
{
throw invalid_argument(cacheSizeError);
}
}
else if(command == "cacheAccessSpeed")
{
if(arguments.size() == 1)
{
if(numChips == 1)
{
//this is single L2 speed
cacheAccessSpeeds[0] = getCacheAccessSpeed(arguments[0]);
}
else
{
//this is L3 speed
l3CacheAccessSpeed = getCacheAccessSpeed(arguments[0]);
}
}
else if(arguments.size() == 2)
{
ulong cacheAccessSpeed = getCacheAccessSpeed(arguments[1]);
ulong chip = strtoul(arguments[0].c_str(), NULL, 0);
cacheAccessSpeeds[chip] = cacheAccessSpeed;
}
else
{
throw invalid_argument(cacheAccessSpeedError);
}
}
else if(command == "replacementSpeed")
{
replacementSpeed = parseSpeed(arguments, command);
}
else if(command == "broadcastSpeed")
{
broadcastSpeed = parseSpeed(arguments, command);
}
else if(command == "memoryAccessSpeed")
{
memoryAccessSpeed = parseSpeed(arguments, command);
}
else if(command == "L1CacheSize")
L1CacheSize = getCacheSize(arguments[0]);
else if(command == "L1BlockSize")
L1BlockSize = getBlockSize(arguments[0]);
else if(command == "L2CacheSize")
L2CacheSize = getCacheSize(arguments[0]);
else if(command == "L2BlockSize")
L2BlockSize = getBlockSize(arguments[0]);
else if(command == "L3CacheSize")
L3CacheSize = getCacheSize(arguments[0]);
else if(command == "L3BlockSize")
L3BlockSize = getBlockSize(arguments[0]);
else if(command == "blockSize")
blockSize = getBlockSize(arguments[0]);
command = getUntilDelim(configFile, '(');
}
if (!skipPreamble)
{
//TODO: check to make sure inits are non-zero
if(replacementSpeed == 0 || broadcastSpeed == 0 || memoryAccessSpeed == 0)
throw invalid_argument("Error: Detected missing or zero initialized argument");
checkArray(numCores, numChips);
checkArray(cacheSizes, numChips);
checkArray(cacheAccessSpeeds, numChips);
}
}