-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.h
420 lines (360 loc) · 10.3 KB
/
Module.h
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
#pragma once
#include"Skill.h"
#include<vector>
#include<queue>
#include<memory>
namespace Design
{
/* todo: make data transmission priority - based, for evading deadlocks.oldest context / id must go first
* for all modules i/o
* example:
* for(all awaiting outputs to send)
* pick oldest first
*/
// work flow of "build"
// prepare module-module connections (top,right,bottom,left)
// prepare module relations (cache controller - cache banks, control unit - alus, etc)
// work flow of "run"
// push to outputs
// push to inputs
// compute
// for simple processors, some instructions generate many micro operations (like 1 square root = a lot of multiplications, additions, etc)
enum DataType
{
// empty data
Null,
// data between RAM and decoder (may pass through cache)
InstructionFpu,
// dummy calc, this is only for debugging / unit-testing
InstructionAlu,
// real work for ALU
// r[i] = r[j] + r[k]
// i= parameter 1
// j= parameter 2
// k= parameter 3
InstructionAluSumRegisterRegister,
InstructionMemRead,
InstructionMemWrite,
InstructionParameter1,
InstructionParameter2,
InstructionParameter3,
InstructionParameter4,
InstructionParameter5,
InstructionJump,
InstructionSync, // this is for a new & easy multi-threading model that runs on single core
// data between anything
DataChunk,
// (command) data between decoder and other modules
MicroOpFpu,
// dummy alu operation for unit-testing
MicroOpAlu,
MicroOpMemRead,
MicroOpMemWrite,
MicroOpParameter1,
MicroOpParameter2,
MicroOpParameter3,
MicroOpParameter4,
MicroOpParameter5,
MicroOpBranchPrediction,
MicroOpSync,
MicroOpDecode,
MicroOpCacheBankReadTag,
MicroOpCacheBankWriteDataToTag,
MicroOpCacheBankInvalidateTag,
// result (which has a target module type or id to go)
Result
};
enum FpuInstructionType
{
Add,
Mul,
Sub,
Div,
Sqrt,
Exp,
Pow,
FMA
};
enum ModuleType
{
BUS,
CONTROL_UNIT,
CACHE_CONTROL,
CACHE_CONTROL_CACHE, // cache can be cached (by a closer & smaller cache)
CACHE_CONTROL_CONTROL, // context data (thread variables, register states, etc) can be cached
CACHE_CONTROL_RAM, // data can be cached
CACHE_CONTROL_DECODER, // instructions (between RAM and DECODER) can be cached
CACHE_CONTROL_ALU, // there's no harm in caching integer operations if ALU is as slow as 100 cycles per integer-division
CACHE_CONTROL_FPU, // even fp operations can be cached (i.e. 16 bit fp power/exp/log/etc --> 128kB cache required)
MEMORY_CONTROL,
ALU,
FPU,
FPU_CLOSEST,
FPU_WIDEST,
FPU_WIDTH2,
FPU_WIDTH4,
FPU_WIDTH8,
FPU_WIDTH16,
FPU_WIDTH32,
FPU_WIDTH64,
FPU_WIDTH128,
FPU_WIDTH256,
FPU_SPECIAL_FUNCTION, // any fpu that can compute square root / exponential / power / logarithm / erf / sin / cos / etc
FPU_SQRT, // any fpu that has square-root capability
FPU_DIV,
FPU_MUL,
FPU_ADD, // also works as FPU_SUB. just negate an input to do subtraction
FPU_EXP,
FPU_RND, // FPU with random number generation
CACHE_BANK,
BRANCH_PREDICTOR,
DECODER,
// for searching modules through bus connections
ANY,
ANY_MEMORY_READ_CAPABLE,
ANY_MEMORY_WRITE_CAPABLE,
ANY_COMPUTE_CAPABLE,
ANY_WITH_EMPTY_SLOT,
ANY_EMPTY,
ANY_EMPTY_ROUND_ROBIN_BUS_PATH,
ANY_EMPTY_ROUND_ROBIN_RESOURCE
};
// the data that is passing between modules
class Data
{
public:
Data(DataType dataTypePrm = DataType::Null, ModuleType targetModuleTypePrm = ModuleType::ANY,
int targetModuleIdPrm = -1, int valuePrm = -1, ModuleType sourceModuleTypePrm = ModuleType::ANY,
int sourceModuleIdValue = -1, int clockCycleId=-1)
{
_sourceModuleType = sourceModuleTypePrm;
_sourceModuleId = sourceModuleIdValue;
_targetModuleType = targetModuleTypePrm;
_targetModuleId = targetModuleIdPrm;
_dataType = dataTypePrm;
_value = valuePrm;
_context = -1;
_contextType = -1;
_localId = -1;
_clockCycleId = clockCycleId;
}
int GetLocalId()
{
return _localId;
}
void SetLocalId(int newLocalId)
{
_localId = newLocalId;
}
int GetClockCycleId()
{
return _clockCycleId;
}
DataType GetDataType()
{
return _dataType;
}
void SetTargetModuleId(int newTargetModuleId)
{
_targetModuleId = newTargetModuleId;
}
int GetValue()
{
return _value;
}
void SetDataType(DataType dType)
{
_dataType = dType;
}
ModuleType GetTargetModuleType()
{
return _targetModuleType;
}
int GetTargetModuleId()
{
return _targetModuleId;
}
int GetSourceModuleId()
{
return _sourceModuleId;
}
private:
// how many cycles passed since creation of this data
int _clockCycleId;
// the module that has sent this data
ModuleType _sourceModuleType;
int _sourceModuleId;
// type of module to take this data
// if input queue of module is full, it goes to another module
ModuleType _targetModuleType;
int _targetModuleId;
DataType _dataType;
// whose context is this: id of instruction or branch prediction or thread
int _context;
// 0: instruction, 1: branch, 2: thread
int _contextType;
// can be instruction or parameter
/*
fpu:
0=add
1=mul
2=sub
3=div
4=sqrt
5=pow
6=exp
7=log
8=mod
9=less than
10=greater than
11=equal
12=fma
*/
int _value;
// every module assigns a local id to its currently stored data objects for detection/tracking of deadlock
int _localId;
};
static int GetUniqueId()
{
static int id=0;
return id++;
}
// adjacent modules (top,bot,left,right) make connections automatically.
// output is broadcasted to 4 neighbors
// input is made of 4 outputs of neighbors
// if there is a bus connected, all connected modules to the bus are neighbors (unlimited number of modules)
class Module
{
public:
Module(int parallelism,int busynessLevelMax, int lithography,
int numTransistors, ModuleType moduleType,int thermalDissipationPower,
int frequency,float failProbability)
{
// derived classes such as ALU have to change this value accordingly in constructor
_parallelism = parallelism;
_id = GetUniqueId();
for (int j = 0; j < 4; j++)
{
_input[j].resize(_parallelism);
for (int i = 0; i < _parallelism; i++)
_input[j][i] = Data();
}
for (int j = 0; j < 4; j++)
{
_inputRegister[j].resize(_parallelism);
for (int i = 0; i < _parallelism; i++)
_inputRegister[j][i] = Data();
}
_output.resize(_parallelism);
for(int i=0;i< _parallelism;i++)
_output[i] = Data();
_directConnectedModules.resize(4);
for (int i = 0; i < 4; i++)
_directConnectedModules[i] = nullptr;
_busynessLevel = 0;
_busynessLevelMax = busynessLevelMax;
_failProbability = failProbability;
_lithography = lithography;
_type = moduleType; // just a default
_numTransistors = numTransistors;
_thermalDissipationPower = thermalDissipationPower;
_frequency = frequency; // 1 means equal frequency to outer source. 2 means 2x frequency or 2 iterations per cycle
_numCompletedOperations = 0;
_numStartedOperations = 0;
}
int GetCompletedOperationCount() { return _numCompletedOperations; }
virtual void SetBusy() { _busynessLevel++; }
virtual void SetIdle() { _busynessLevel = 0; }
// percentage level: 100 = full, 0 = idle
virtual int GetBusyness() { return 100 * (_busynessLevel / (float)_busynessLevelMax); }
int GetId() { return _id; }
virtual void ComputeOutput() {}
virtual Data GetInput(int index, int channel)
{
return _inputRegister[index][channel];
}
virtual void SetInput(Data input, int index, int channel)
{
// when data leaves a module, localId is reset
input.SetLocalId(-1);
_inputRegister[index][channel] = input;
}
virtual void ApplyInput()
{
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < _parallelism; i++)
{
// if output & input register is empty, then its ok to compute later
// because if output is clogged, there is no reason to compute something that won't go anywhere
if (GetOutput(i).GetDataType() == Design::DataType::Null && _input[j][i].GetDataType() == Design::DataType::Null)
{
_input[j][i] = _inputRegister[j][i];
_inputRegister[j][i] = Design::Data();
}
}
}
}
virtual void SendOutput() {}
virtual void SetOutput(Data data, int channel) { _output[channel] = data; }
virtual Data GetOutput(int channel) { return _output[channel]; }
virtual ModuleType GetModuleType() { return _type; }
virtual bool CheckDeadlock() { return false; }
virtual int GetParallelism() { return _parallelism; }
std::vector<Module*> GetConnectedModulesExceptThis(Module* source = nullptr)
{
std::vector<Module*> result;
for (int i = 0; i < 4; i++)
{
auto ptr = _directConnectedModules[i].get();
if (ptr)
{
if (ptr != source)
result.push_back(ptr);
}
}
return result;
}
// clock cycle id is used for giving instructions and opcodes a relative age value for priority checks
virtual void Compute(int clockCycleId){ }
template<typename T>
T* AsPtr()
{
return (T*)this;
}
void Connect(std::shared_ptr<Module> conn, int index)
{
_directConnectedModules[index]=conn;
}
protected:
int _id;
ModuleType _type;
int _busynessLevel;
int _busynessLevelMax;
float _failProbability; // all transistors have a failure probability, hence the module failure, per clock. the more transistors the more failure chance.
int _lithography;
// actual frequency is controlled from outside, this is just a multiplier (that is checked for triggering compute at signal)
int _frequency;
int _numTransistors;
int _thermalDissipationPower;
// N inputs per direction, N operations per cycle
int _parallelism;
// array index
// 0 index: top
// 1 index: right
// 2 index: bottom
// 3 index: left
// vector index: channels
std::vector<Design::Data> _input[4];
std::vector<Design::Data> _inputRegister[4];
// N internal outputs that can take any of 4 input paths
std::vector<Design::Data> _output;
std::vector<SkillRequirement> _skillRequirements;
std::vector<StatRequirement> _statRequirements;
// maximum 4 modules can be connected to a module. top, right, bottom, left
std::vector<std::shared_ptr<Module>> _directConnectedModules;
std::vector<DataType> _commandFilter; // takes only these kind of commands to work
int _numCompletedOperations;
int _numStartedOperations;
};
}