/
HpcOmSimCode.mo
2004 lines (1848 loc) · 89.7 KB
/
HpcOmSimCode.mo
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package HpcOmSimCode
" file: HpcOmSimCode.mo
package: HpcOmSimCode
description: HpcOmSimCode contains the logic to create a parallelized simcode.
RCS: $Id: HpcOmSimCode.mo 15486 2013-05-24 11:12:35Z marcusw $
"
// public imports
public import Absyn;
public import BackendDAE;
public import DAE;
public import HashTableExpToIndex;
public import HpcOmScheduler;
public import SimCode;
// protected imports
protected import BackendEquation;
protected import BaseHashTable;
protected import BackendDAEUtil;
protected import ComponentReference;
protected import Debug;
protected import Error;
protected import Expression;
protected import Flags;
protected import GlobalScript;
protected import GraphML;
protected import HashTableCrILst;
protected import HpcOmTaskGraph;
protected import List;
protected import SimCodeUtil;
protected import System;
protected import Util;
public function createSimCode "function createSimCode
entry point to create SimCode from BackendDAE."
input BackendDAE.BackendDAE inBackendDAE;
input Absyn.Path inClassName;
input String filenamePrefix;
input String inString11;
input list<SimCode.Function> functions;
input list<String> externalFunctionIncludes;
input list<String> includeDirs;
input list<String> libs;
input Option<SimCode.SimulationSettings> simSettingsOpt;
input list<SimCode.RecordDeclaration> recordDecls;
input tuple<Integer, HashTableExpToIndex.HashTable, list<DAE.Exp>> literals;
input Absyn.FunctionArgs args;
output SimCode.SimCode simCode;
algorithm
simCode := matchcontinue (inBackendDAE, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, simSettingsOpt, recordDecls, literals, args)
local
String fileDir, cname;
Integer lastEqMappingIdx, maxDelayedExpIndex, uniqueEqIndex, numberofEqns, numberOfInitialEquations, numberOfInitialAlgorithms, numStateSets;
Integer numberofLinearSys, numberofNonLinearSys, numberofMixedSys;
BackendDAE.BackendDAE dlow, dlow2;
Option<BackendDAE.BackendDAE> initDAE;
BackendDAE.IncidenceMatrix incidenceMatrix;
DAE.FunctionTree functionTree;
BackendDAE.SymbolicJacobians symJacs;
Absyn.Path class_;
// new variables
SimCode.ModelInfo modelInfo;
list<SimCode.SimEqSystem> allEquations;
list<list<SimCode.SimEqSystem>> odeEquations; // --> functionODE
list<list<SimCode.SimEqSystem>> algebraicEquations; // --> functionAlgebraics
list<SimCode.SimEqSystem> residuals; // --> initial_residual
Boolean useSymbolicInitialization; // true if a system to solve the initial problem symbolically is generated, otherwise false
Boolean useHomotopy; // true if homotopy(...) is used during initialization
list<SimCode.SimEqSystem> initialEquations; // --> initial_equations
list<SimCode.SimEqSystem> startValueEquations; // --> updateBoundStartValues
list<SimCode.SimEqSystem> nominalValueEquations; // --> updateBoundNominalValues
list<SimCode.SimEqSystem> minValueEquations; // --> updateBoundMinValues
list<SimCode.SimEqSystem> maxValueEquations; // --> updateBoundMaxValues
list<SimCode.SimEqSystem> parameterEquations; // --> updateBoundParameters
list<SimCode.SimEqSystem> inlineEquations; // --> inline solver
list<SimCode.SimEqSystem> removedEquations;
list<SimCode.SimEqSystem> algorithmAndEquationAsserts;
list<SimCode.SimEqSystem> jacobianEquations;
list<SimCode.SimEqSystem> zeroCrossingsEquations;
//list<DAE.Statement> algorithmAndEquationAsserts;
list<DAE.Constraint> constraints;
list<DAE.ClassAttributes> classAttributes;
list<BackendDAE.ZeroCrossing> zeroCrossings, sampleZC, relations;
list<SimCode.SimWhenClause> whenClauses;
list<DAE.ComponentRef> discreteModelVars;
SimCode.ExtObjInfo extObjInfo;
SimCode.MakefileParams makefileParams;
SimCode.DelayedExpression delayedExps;
BackendDAE.Variables knownVars;
list<BackendDAE.Var> varlst;
list<SimCode.JacobianMatrix> LinearMatrices, SymbolicJacs, SymbolicJacsTemp, SymbolicJacsStateSelect;
SimCode.HashTableCrefToSimVar crefToSimVarHT;
Boolean ifcpp;
BackendDAE.EqSystems eqs;
BackendDAE.Shared shared;
BackendDAE.EquationArray removedEqs;
list<DAE.Constraint> constrsarr;
list<DAE.ClassAttributes> clsattrsarra;
list<DAE.Exp> lits;
list<SimCode.SimVar> tempvars;
SimCode.JacobianMatrix jacG;
Integer highestSccIdx, compCountPlusDummy;
Option<BackendDAE.BackendDAE> inlineDAE;
list<SimCode.StateSet> stateSets;
array<Integer> systemIndexMap;
list<tuple<Integer,Integer>> equationSccMapping, equationSccMapping1; //Maps each simEq to the scc
array<list<Integer>> sccSimEqMapping; //Maps each scc to a list of simEqs
array<Integer> simeqNodeMapping; //Maps each simEq to the scc
list<BackendDAE.TimeEvent> timeEvents;
BackendDAE.StrongComponents allComps;
HpcOmTaskGraph.TaskGraph taskGraph;
HpcOmTaskGraph.TaskGraph taskGraphOde;
HpcOmTaskGraph.TaskGraph taskGraph1;
HpcOmTaskGraph.TaskGraphMeta taskGraphData;
HpcOmTaskGraph.TaskGraphMeta taskGraphDataOde;
String fileName, fileNamePrefix;
Integer numProc;
HpcOmTaskGraph.TaskGraphMeta taskGraphData1;
list<list<Integer>> parallelSets;
list<list<Integer>> criticalPaths, criticalPathsWoC;
Real cpCosts, cpCostsWoC, serTime, parTime, speedUp, speedUpMax;
//Additional informations to append SimCode
list<DAE.Exp> simCodeLiterals;
list<SimCode.JacobianMatrix> jacobianMatrixes;
list<SimCode.SimEqSystem> residualEquations;
Option<SimCode.SimulationSettings> simulationSettingsOpt;
list<SimCode.RecordDeclaration> simCodeRecordDecls;
list<String> simCodeExternalFunctionIncludes;
Boolean taskGraphMetaValid, numFixed;
String taskGraphMetaMessage, criticalPathInfo;
array<tuple<Integer,Integer,Real>> schedulerInfo;
HpcOmScheduler.Schedule schedule;
HpcOmScheduler.ScheduleSimCode taskScheduleSimCode;
array<tuple<Integer,Integer,Integer>> eqNodeMapping, varNodeMapping;
Real graphCosts;
Integer graphOps;
case (BackendDAE.DAE(eqs=eqs), _, _, _, _, _, _, _, _, _, _, _) equation
//Setup
//-----
System.realtimeTick(GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
(simCode,(lastEqMappingIdx,equationSccMapping)) = SimCodeUtil.createSimCode(inBackendDAE, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, simSettingsOpt, recordDecls, literals, args);
(allComps,_) = HpcOmTaskGraph.getSystemComponents(inBackendDAE);
highestSccIdx = findHighestSccIdxInMapping(equationSccMapping,-1);
compCountPlusDummy = listLength(allComps)+1;
equationSccMapping1 = removeDummyStateFromMapping(equationSccMapping);
//the mapping can contain a dummy state as first scc
equationSccMapping = Util.if_(intEq(highestSccIdx, compCountPlusDummy), equationSccMapping1, equationSccMapping);
sccSimEqMapping = convertToSccSimEqMapping(equationSccMapping, listLength(allComps));
simeqNodeMapping = convertToSimeqNodeMapping(equationSccMapping, lastEqMappingIdx);
Debug.execStat("hpcom setup", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
//dumpSccSimEqMapping(sccSimEqMapping);
//Create TaskGraph
//----------------
(taskGraph,taskGraphData) = HpcOmTaskGraph.createTaskGraph(inBackendDAE,filenamePrefix);
Debug.execStat("hpcom createTaskGraph", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
fileName = ("taskGraph"+&filenamePrefix+&".graphml");
schedulerInfo = arrayCreate(arrayLength(taskGraph), (-1,-1,-1.0));
HpcOmTaskGraph.dumpAsGraphMLSccLevel(taskGraph, taskGraphData, fileName, "", {}, {}, sccSimEqMapping ,schedulerInfo);
Debug.execStat("hpcom dump TaskGraph", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
HpcOmTaskGraph.TASKGRAPHMETA(varNodeMapping=varNodeMapping,eqNodeMapping=eqNodeMapping) = taskGraphData;
//HpcOmTaskGraph.printvarNodeMapping(varNodeMapping,1);
//HpcOmTaskGraph.printeqNodeMapping(eqNodeMapping,1);
//Create Costs
//------------
taskGraphData = HpcOmTaskGraph.createCosts(inBackendDAE, filenamePrefix +& "_eqs_prof.xml" , simeqNodeMapping, taskGraphData);
Debug.execStat("hpcom create costs", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
//Get ODE System
//--------------
taskGraphOde = arrayCopy(taskGraph);
taskGraphDataOde = HpcOmTaskGraph.copyTaskGraphMeta(taskGraphData);
(taskGraphOde,taskGraphDataOde) = HpcOmTaskGraph.getOdeSystem(taskGraphOde,taskGraphDataOde,inBackendDAE,filenamePrefix);
Debug.execStat("hpcom get ODE system", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
taskGraphMetaValid = HpcOmTaskGraph.validateTaskGraphMeta(taskGraphDataOde, inBackendDAE);
taskGraphMetaMessage = Util.if_(taskGraphMetaValid, "TaskgraphMeta valid\n", "TaskgraphMeta invalid\n");
print(taskGraphMetaMessage);
//HpcOmTaskGraph.printTaskGraph(taskGraphOde);
//HpcOmTaskGraph.printTaskGraphMeta(taskGraphDataOde);
//Assign levels and get critcal path
//----------------------------------
((criticalPaths,cpCosts),(criticalPathsWoC,cpCostsWoC),parallelSets) = HpcOmTaskGraph.longestPathMethod(taskGraphOde,taskGraphDataOde);
cpCosts = HpcOmTaskGraph.roundReal(cpCosts,2);
cpCostsWoC = HpcOmTaskGraph.roundReal(cpCostsWoC,2);
criticalPathInfo = HpcOmTaskGraph.dumpCriticalPathInfo((criticalPaths,cpCosts),(criticalPathsWoC,cpCostsWoC));
((graphOps,graphCosts)) = HpcOmTaskGraph.sumUpExecCosts(taskGraphDataOde);
graphCosts = HpcOmTaskGraph.roundReal(graphCosts,2);
criticalPathInfo = criticalPathInfo +& " sum: (" +& realString(graphCosts) +& " ; " +& intString(graphOps) +& ")";
fileName = ("taskGraph"+&filenamePrefix+&"ODE.graphml");
schedulerInfo = arrayCreate(arrayLength(taskGraphOde), (-1,-1,-1.0));
Debug.execStat("hpcom assign levels / get crit. path", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
HpcOmTaskGraph.dumpAsGraphMLSccLevel(taskGraphOde, taskGraphDataOde, fileName, criticalPathInfo, HpcOmTaskGraph.convertNodeListToEdgeTuples(List.first(criticalPaths)), HpcOmTaskGraph.convertNodeListToEdgeTuples(List.first(criticalPathsWoC)), sccSimEqMapping, schedulerInfo);
Debug.execStat("hpcom dump ODE TaskGraph", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
//Apply filters
//-------------
taskGraphData1 = taskGraphDataOde;
taskGraph1 = taskGraphOde;
(taskGraph1,taskGraphData1) = applyFiltersToGraph(taskGraphOde,taskGraphDataOde,inBackendDAE,true); //TODO: Rename this to applyGRS or someting like that
Debug.execStat("hpcom GRS", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
//Debug.fcall(Flags.HPCOM_DUMP,HpcOmTaskGraph.printTaskGraph,taskGraph1);
//Debug.fcall(Flags.HPCOM_DUMP,HpcOmTaskGraph.printTaskGraphMeta,taskGraphData1);
//Create schedule
//---------------
numProc = Flags.getConfigInt(Flags.NUM_PROC);
(numProc,numFixed) = setNumProc(numProc,cpCostsWoC,taskGraphDataOde);
schedule = createSchedule(taskGraph1,taskGraphData1,sccSimEqMapping,filenamePrefix,numProc);
(schedule,numProc) = repeatScheduleWithOtherNumProc(taskGraph1,taskGraphData1,sccSimEqMapping,filenamePrefix,cpCostsWoC,schedule,numProc,numFixed);
criticalPathInfo = HpcOmScheduler.analyseScheduledTaskGraph(schedule,numProc,taskGraph1,taskGraphData1);
taskScheduleSimCode = HpcOmScheduler.convertScheduleToSimCodeSchedule(schedule);
schedulerInfo = HpcOmScheduler.convertScheduleStrucToInfo(schedule,arrayLength(taskGraph));
Debug.execStat("hpcom create schedule", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
fileName = ("taskGraph"+&filenamePrefix+&"ODE_schedule.graphml");
HpcOmTaskGraph.dumpAsGraphMLSccLevel(taskGraph1, taskGraphData1, fileName, criticalPathInfo, HpcOmTaskGraph.convertNodeListToEdgeTuples(List.first(criticalPaths)), HpcOmTaskGraph.convertNodeListToEdgeTuples(List.first(criticalPathsWoC)), sccSimEqMapping, schedulerInfo);
//HpcOmScheduler.printSchedule(schedule);
Debug.execStat("hpcom dump schedule TaskGraph", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
SimCode.SIMCODE(modelInfo, simCodeLiterals, simCodeRecordDecls, simCodeExternalFunctionIncludes, allEquations, odeEquations, algebraicEquations, residualEquations, useSymbolicInitialization, useHomotopy, initialEquations, startValueEquations, nominalValueEquations, minValueEquations, maxValueEquations,
parameterEquations, inlineEquations, removedEquations, algorithmAndEquationAsserts, zeroCrossingsEquations, jacobianEquations, stateSets, constraints, classAttributes, zeroCrossings, relations, timeEvents, whenClauses,
discreteModelVars, extObjInfo, makefileParams, delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT, _) = simCode;
checkOdeSystemSize(taskGraphOde,odeEquations);
Debug.execStat("hpcom check ODE system size", GlobalScript.RT_CLOCK_EXECSTAT_HPCOM_MODULES);
simCode = SimCode.SIMCODE(modelInfo, simCodeLiterals, simCodeRecordDecls, simCodeExternalFunctionIncludes, allEquations, odeEquations, algebraicEquations, residualEquations, useSymbolicInitialization, useHomotopy, initialEquations, startValueEquations, nominalValueEquations, minValueEquations, maxValueEquations,
parameterEquations, inlineEquations, removedEquations, algorithmAndEquationAsserts, zeroCrossingsEquations, jacobianEquations, stateSets, constraints, classAttributes, zeroCrossings, relations, timeEvents, whenClauses,
discreteModelVars, extObjInfo, makefileParams, delayedExps, jacobianMatrixes, simulationSettingsOpt, fileNamePrefix, crefToSimVarHT, SOME(taskScheduleSimCode));
analyzeCacheBehaviour(modelInfo, taskGraph1, taskGraphData1, eqs, filenamePrefix, schedulerInfo, schedule, sccSimEqMapping, criticalPaths, criticalPathsWoC, criticalPathInfo, allComps);
//evaluateCacheBehaviour(schedulerInfo,taskGraphData1,clTaskMapping, transposeCacheLineTaskMapping(clTaskMapping, arrayLength(taskGraph1)));
//printCacheMap(cacheMap);
print("HpcOm is still under construction.\n");
then simCode;
else equation
Error.addMessage(Error.INTERNAL_ERROR, {"./Compiler/BackEnd/HpcOmSimCode.mo: function createSimCode failed."});
then fail();
end matchcontinue;
end createSimCode;
protected function repeatScheduleWithOtherNumProc"checks if the scheduling with the given numProc is fine.
if n=auto, more cores are available and more speedup could be achieved repeat schedule with increased num of procs.
author:Waurich TUD 2013-011"
input HpcOmTaskGraph.TaskGraph taskGraphIn;
input HpcOmTaskGraph.TaskGraphMeta taskGraphMetaIn;
input array<list<Integer>> sccSimEqMappingIn;
input String fileNamePrefix;
input Real cpCostsWoC;
input HpcOmScheduler.Schedule scheduleIn;
input Integer numProcIn;
input Boolean numFixed;
output HpcOmScheduler.Schedule scheduleOut;
output Integer numProcOut;
protected
Integer maxNumProc, maxIter;
Real maxDiff;
algorithm
maxNumProc := System.numProcessors();
maxIter := 3;
maxDiff := 0.5;
(scheduleOut,numProcOut,_) := repeatScheduleWithOtherNumProc1(taskGraphIn,taskGraphMetaIn,sccSimEqMappingIn,fileNamePrefix,cpCostsWoC,scheduleIn,numProcIn,numFixed,maxNumProc,maxDiff,maxIter);
end repeatScheduleWithOtherNumProc;
protected function repeatScheduleWithOtherNumProc1"checks if the scheduling with the given numProc is fine.
if n=auto, more cores are available and more speedup could be achieved repeat schedule with increased num of procs.
author:Waurich TUD 2013-011"
input HpcOmTaskGraph.TaskGraph taskGraphIn;
input HpcOmTaskGraph.TaskGraphMeta taskGraphMetaIn;
input array<list<Integer>> sccSimEqMappingIn;
input String fileNamePrefix;
input Real cpCostsWoC;
input HpcOmScheduler.Schedule scheduleIn;
input Integer numProcIn;
input Boolean numFixed;
input Integer maxNumProc;
input Real maxDiff;
input Integer numIterIn;
output HpcOmScheduler.Schedule scheduleOut;
output Integer numProcOut;
output Integer numIterOut;
algorithm
(scheduleOut,numProcOut,numIterOut) := matchcontinue(taskGraphIn,taskGraphMetaIn,sccSimEqMappingIn,fileNamePrefix,cpCostsWoC,scheduleIn,numProcIn,numFixed,maxNumProc,maxDiff,numIterIn)
local
Boolean scheduleAgain;
Integer numProc, numIt;
Real serTime,parTime,speedup,speedUp,speedUpMax,diff;
HpcOmScheduler.Schedule schedule;
case(_,_,_,_,_,_,_,true,_,_,_)
equation // do not schedule again because the number of procs was given
then
(scheduleIn,numProcIn,0);
case(_,_,_,_,_,_,_,false,_,_,_)
equation
true = numIterIn == 0; // the max number of schedules with increased num of procs
then
(scheduleIn,numProcIn,0);
case(_,_,_,_,_,_,_,false,_,_,_)
equation
(serTime,parTime,speedUp,speedUpMax) = HpcOmScheduler.predictExecutionTime(scheduleIn,SOME(cpCostsWoC),numProcIn,taskGraphIn,taskGraphMetaIn);
diff = speedUpMax -. speedUp;
//print("the new speedUp with "+&intString(numProcIn)+&" processors: "+&realString(speedUp)+&"\n");
true = diff <. maxDiff;
//print("the schedule is fine\n");
then
(scheduleIn,numProcIn,numIterIn);
else
equation
numProc = numProcIn+1; // increase the number of procs
numIt = numIterIn-1; // lower the counter of scheduling runs
scheduleAgain = intLe(numProc,maxNumProc);
//print("schedule again\n");
numProc = Util.if_(scheduleAgain,numProc,numProcIn);
numIt = Util.if_(scheduleAgain,numIt,0);
schedule= Debug.bcallret5(scheduleAgain,createSchedule,taskGraphIn,taskGraphMetaIn,sccSimEqMappingIn,fileNamePrefix,numProc,scheduleIn);
(schedule,numProc,numIt) = repeatScheduleWithOtherNumProc1(taskGraphIn,taskGraphMetaIn,sccSimEqMappingIn,fileNamePrefix,cpCostsWoC,schedule,numProc,numFixed,maxNumProc,maxDiff,numIt);
then
(schedule,numProc,numIt);
end matchcontinue;
end repeatScheduleWithOtherNumProc1;
protected function setNumProc "sets the number of processors. its upper limit is the number of processsors provided by the system.
if no n-flag is set, a ideal number is suggested but the simulation fails.
author: Waurich TUD 2013-11"
input Integer numProcFlag;
input Real cpCosts;
input HpcOmTaskGraph.TaskGraphMeta taskGraphMetaIn;
output Integer numProcOut;
output Boolean numFixed;
algorithm
(numProcOut,numFixed) := match(numProcFlag,cpCosts,taskGraphMetaIn)
local
Boolean isFixed;
Integer numProcSys, numProc, numProcSched;
Real serCosts, maxSpeedUp;
String string1, string2;
case(0,_,_)
equation
serCosts = HpcOmScheduler.getSerialExecutionTime(taskGraphMetaIn);
maxSpeedUp = realDiv(serCosts,cpCosts);
numProcSched = realInt(realAdd(maxSpeedUp,1.0));
numProcSys = System.numProcessors();
numProc = intMin(numProcSched,numProcSys);
string1 = "Your system provides only "+&intString(numProcSys)+&" processors!\n";
string2 = intString(numProcSched)+&" processors might be a reasonable number of processors.\n";
string1 = Util.if_(intGt(numProcSched,numProcSys),string1,string2);
isFixed = Util.if_(intGt(numProcSched,numProcSys),true,false);
print("Please set the number of processors you want to use!\n");
print(string1);
then
fail();
else
equation
numProcSys = System.numProcessors();
numProc = Util.if_(intGt(numProcFlag,numProcSys),numProcSys,numProcFlag); // the system does not provide so many cores
Debug.bcall(intGt(numProcFlag,numProcSys),print,"Warning: Your system provides only "+&intString(numProcSys)+&" processors!\n");
then
(numProc,true);
end match;
end setNumProc;
protected function applyFiltersToGraph
input HpcOmTaskGraph.TaskGraph iTaskGraph;
input HpcOmTaskGraph.TaskGraphMeta iTaskGraphMeta;
input BackendDAE.BackendDAE inBackendDAE;
input Boolean iApplyFilters;
output HpcOmTaskGraph.TaskGraph oTaskGraph;
output HpcOmTaskGraph.TaskGraphMeta oTaskGraphMeta;
protected
String flagValue;
Boolean changed1,changed2;
HpcOmTaskGraph.TaskGraph taskGraph1;
HpcOmTaskGraph.TaskGraphMeta taskGraphMeta1;
array<list<Integer>> sccSimEqMapping;
BackendDAE.StrongComponents allComps;
array<tuple<Integer,Integer>> schedulerInfo;
algorithm
(oTaskGraph,oTaskGraphMeta) := matchcontinue(iTaskGraph,iTaskGraphMeta,inBackendDAE,iApplyFilters)
case(_,_,_,_)
equation
flagValue = Flags.getConfigString(Flags.HPCOM_SCHEDULER);
true = stringEq(flagValue, "level");
then (iTaskGraph, iTaskGraphMeta);
case(_,_,_,true)
equation
//print("Start merging\n");
//Merge simple and parent nodes
taskGraph1 = arrayCopy(iTaskGraph);
taskGraphMeta1 = HpcOmTaskGraph.copyTaskGraphMeta(iTaskGraphMeta);
(taskGraph1,taskGraphMeta1,changed1) = HpcOmTaskGraph.mergeSimpleNodes(taskGraph1,taskGraphMeta1,inBackendDAE);
//(allComps,_) = HpcOmTaskGraph.getSystemComponents(inBackendDAE);
//sccSimEqMapping = arrayCreate(listLength(allComps), {});
//schedulerInfo = arrayCreate(arrayLength(taskGraph1), (-1,-1));
//print("MergeParentNodes\n");
//HpcOmTaskGraph.dumpAsGraphMLSccLevel(taskGraph1, taskGraphMeta1, "testgraph.graphml", "", {}, {}, sccSimEqMapping, schedulerInfo);
(taskGraph1,taskGraphMeta1,changed2) = HpcOmTaskGraph.mergeParentNodes(taskGraph1, taskGraphMeta1);
(taskGraph1,taskGraphMeta1) = applyFiltersToGraph(taskGraph1,taskGraphMeta1,inBackendDAE,changed1 or changed2);
then (taskGraph1,taskGraphMeta1);
else then (iTaskGraph, iTaskGraphMeta);
end matchcontinue;
end applyFiltersToGraph;
protected function createSchedule
input HpcOmTaskGraph.TaskGraph iTaskGraph;
input HpcOmTaskGraph.TaskGraphMeta iTaskGraphMeta;
input array<list<Integer>> iSccSimEqMapping;
input String iFilenamePrefix;
input Integer numProc;
output HpcOmScheduler.Schedule oSchedule;
protected
String flagValue;
list<Integer> lst;
HpcOmTaskGraph.TaskGraph taskGraph1;
HpcOmTaskGraph.TaskGraphMeta taskGraphMeta1;
algorithm
oSchedule := matchcontinue(iTaskGraph,iTaskGraphMeta,iSccSimEqMapping,iFilenamePrefix,numProc)
case(_,_,_,_,_)
equation
true = arrayLength(iTaskGraph) == 0;
print("There is no ODE system that can be parallelized!\n");
// just did this because this works fine. TODO: make something reasonable here and do not use a scheduler.
then
HpcOmScheduler.createLevelSchedule(iTaskGraphMeta,iSccSimEqMapping);
case(_,_,_,_,_)
equation
flagValue = Flags.getConfigString(Flags.HPCOM_SCHEDULER);
true = stringEq(flagValue, "level");
print("Using level Scheduler\n");
then HpcOmScheduler.createLevelSchedule(iTaskGraphMeta,iSccSimEqMapping);
case(_,_,_,_,_)
equation
flagValue = Flags.getConfigString(Flags.HPCOM_SCHEDULER);
true = stringEq(flagValue, "ext");
print("Using external Scheduler\n");
then HpcOmScheduler.createExtSchedule(iTaskGraph, iTaskGraphMeta, numProc, iSccSimEqMapping, "taskGraph" +& iFilenamePrefix +& "_ext.graphml");
case(_,_,_,_,_)
equation
flagValue = Flags.getConfigString(Flags.HPCOM_SCHEDULER);
true = stringEq(flagValue, "metis");
print("Using METIS Scheduler\n");
then HpcOmScheduler.createExtCSchedule(iTaskGraph, iTaskGraphMeta, numProc, iSccSimEqMapping);
case(_,_,_,_,_)
equation
flagValue = Flags.getConfigString(Flags.HPCOM_SCHEDULER);
true = stringEq(flagValue, "hmet");
print("Using hMETIS Scheduler\n");
then HpcOmScheduler.createhmetSchedule(iTaskGraph, iTaskGraphMeta, numProc, iSccSimEqMapping);
case(_,_,_,_,_)
equation
flagValue = Flags.getConfigString(Flags.HPCOM_SCHEDULER);
true = stringEq(flagValue, "listr");
then HpcOmScheduler.createListScheduleReverse(iTaskGraph,iTaskGraphMeta,numProc,iSccSimEqMapping);
case(_,_,_,_,_)
equation
flagValue = Flags.getConfigString(Flags.HPCOM_SCHEDULER);
true = stringEq(flagValue, "list");
then HpcOmScheduler.createListSchedule(iTaskGraph,iTaskGraphMeta,numProc,iSccSimEqMapping);
case(_,_,_,_,_)
equation
flagValue = Flags.getConfigString(Flags.HPCOM_SCHEDULER);
true = stringEq(flagValue, "mcp");
print("Using Modified Critical Path Scheduler\n");
then HpcOmScheduler.createMCPschedule(iTaskGraph,iTaskGraphMeta,numProc,iSccSimEqMapping);
case(_,_,_,_,_)
equation
flagValue = Flags.getConfigString(Flags.HPCOM_SCHEDULER);
true = stringEq(flagValue, "taskdep");
print("Using OpenMP task dependencies\n");
then HpcOmScheduler.createTaskDepSchedule(iTaskGraph,iTaskGraphMeta,iSccSimEqMapping);
case(_,_,_,_,_)
equation
flagValue = Flags.getConfigString(Flags.HPCOM_SCHEDULER);
print("HpcOmScheduler.createSchedule warning: The scheduler '" +& flagValue +& "' is unknown. The list-scheduling algorithm is used instead.\n");
then HpcOmScheduler.createListSchedule(iTaskGraph,iTaskGraphMeta,numProc,iSccSimEqMapping);
else
equation
print("HpcOmSimCode.createSchedule failed. Maybe the n-Flag was not set correctly\n");
then fail();
end matchcontinue;
end createSchedule;
protected function convertToSccSimEqMapping "function convertToSccSimEqMapping
author: marcusw
Converts the given mapping (simEqIndex -> sccIndex) to the inverse mapping (sccIndex->simEqIndex)."
input list<tuple<Integer,Integer>> iMapping; //the mapping (simEqIndex -> sccIndex)
input Integer numOfSccs; //important for arrayCreate
output array<list<Integer>> oMapping; //the created mapping (sccIndex->simEqIndex)
protected
array<list<Integer>> tmpMapping;
algorithm
tmpMapping := arrayCreate(numOfSccs,{});
//print("convertToSccSimEqMapping with " +& intString(numOfSccs) +& " sccs.\n");
_ := List.fold(iMapping, convertToSccSimEqMapping1, tmpMapping);
oMapping := tmpMapping;
end convertToSccSimEqMapping;
protected function convertToSccSimEqMapping1 "function convertToSccSimEqMapping1
author: marcusw
Helper function for convertToSccSimEqMapping. It will update the arrayIndex of the given mapping value."
input tuple<Integer,Integer> iMapping; //<simEqIdx,sccIdx>
input array<list<Integer>> iSccMapping;
output array<list<Integer>> oSccMapping;
protected
Integer i1,i2;
List<Integer> tmpList;
algorithm
(i1,i2) := iMapping;
//print("convertToSccSimEqMapping1 accessing index " +& intString(i2) +& ".\n");
tmpList := arrayGet(iSccMapping,i2);
tmpList := i1 :: tmpList;
oSccMapping := arrayUpdate(iSccMapping,i2,tmpList);
end convertToSccSimEqMapping1;
protected function convertToSimeqNodeMapping "function convertToSimeqNodeMapping
author: marcusw
Converts the given mapping (simEqIndex -> sccIndex) bases on tuples to an array mapping."
input list<tuple<Integer,Integer>> iMapping; //<simEqIdx,sccIdx>
input Integer numOfSimEqs;
output array<Integer> oMapping; //maps each simEq to the scc
protected
array<Integer> tmpMapping;
algorithm
tmpMapping := arrayCreate(numOfSimEqs, -1);
oMapping := List.fold(iMapping, convertToSimeqNodeMapping1, tmpMapping);
end convertToSimeqNodeMapping;
protected function convertToSimeqNodeMapping1 "function convertToSimeqNodeMapping1
author: marcusw
Helper function for convertToSimeqNodeMapping. It will update the array at the given index."
input tuple<Integer,Integer> iSimEqTuple; //<simEqIdx,sccIdx>
input array<Integer> iMapping;
output array<Integer> oMapping;
protected
Integer simEqIdx,sccIdx;
algorithm
(simEqIdx,sccIdx) := iSimEqTuple;
//print("convertToSimeqNodeMapping1 " +& intString(simEqIdx) +& " .. " +& intString(sccIdx) +& " iMapping_len: " +& intString(arrayLength(iMapping)) +& "\n");
oMapping := arrayUpdate(iMapping,simEqIdx,sccIdx);
end convertToSimeqNodeMapping1;
protected function dumpSccSimEqMapping "function dumpSccSimEqMapping
author: marcusw
Prints the given mapping out to the console."
input array<list<Integer>> iSccMapping;
protected
String text;
algorithm
text := "SccToSimEqMapping";
((_,text)) := Util.arrayFold(iSccMapping, dumpSccSimEqMapping1, (1,text));
print(text +& "\n");
end dumpSccSimEqMapping;
protected function dumpSccSimEqMapping1 "function dumpSccSimEqMapping1
author: marcusw
Helper function of dumpSccSimEqMapping to print one mapping list."
input list<Integer> iMapping;
input tuple<Integer,String> iIndexText;
output tuple<Integer,String> oIndexText;
protected
Integer iIndex;
String text, iText;
algorithm
(iIndex,iText) := iIndexText;
text := List.fold(iMapping, dumpSccSimEqMapping2, " ");
text := iText +& "\nSCC " +& intString(iIndex) +& ": {" +& text +& "}";
oIndexText := (iIndex+1,text);
end dumpSccSimEqMapping1;
protected function dumpSccSimEqMapping2 "function dumpSccSimEqMapping2
author: marcusw
Helper function of dumpSccSimEqMapping1 to print one mapping element."
input Integer iIndex;
input String iText;
output String oText;
algorithm
oText := iText +& intString(iIndex) +& " ";
end dumpSccSimEqMapping2;
public function getSimCodeEqByIndex "function getSimCodeEqByIndex
author: marcusw
Returns the SimEqSystem which has the given Index. This method is called from susan."
input list<SimCode.SimEqSystem> iEqs; //All SimEqSystems
input Integer iIdx; //The index of the wanted system
output SimCode.SimEqSystem oEq;
protected
list<SimCode.SimEqSystem> rest;
SimCode.SimEqSystem head;
Integer headIdx;
algorithm
oEq := matchcontinue(iEqs,iIdx)
case(head::rest,_)
equation
headIdx = getIndexBySimCodeEq(head);
//print("getSimCodeEqByIndex listLength: " +& intString(listLength(iEqs)) +& " head idx: " +& intString(headIdx) +& "\n");
true = intEq(headIdx,iIdx);
then head;
case(head::rest,_) then getSimCodeEqByIndex(rest,iIdx);
else
equation
print("getSimCodeEqByIndex failed. Looking for Index " +& intString(iIdx) +& "\n");
then fail();
end matchcontinue;
end getSimCodeEqByIndex;
protected function getIndexBySimCodeEq "function getIndexBySimCodeEq
author: marcusw
Just a small helper function to get the index of a SimEqSystem."
input SimCode.SimEqSystem iEq;
output Integer oIdx;
protected
Integer index;
algorithm
oIdx := match(iEq)
case(SimCode.SES_RESIDUAL(index=index)) then index;
case(SimCode.SES_SIMPLE_ASSIGN(index=index)) then index;
case(SimCode.SES_ARRAY_CALL_ASSIGN(index=index)) then index;
case(SimCode.SES_IFEQUATION(index=index)) then index;
case(SimCode.SES_ALGORITHM(index=index)) then index;
case(SimCode.SES_LINEAR(index=index)) then index;
case(SimCode.SES_NONLINEAR(index=index)) then index;
case(SimCode.SES_MIXED(index=index)) then index;
case(SimCode.SES_WHEN(index=index)) then index;
else then fail();
end match;
end getIndexBySimCodeEq;
public function analyzeOdeEquations
input list<list<SimCode.SimEqSystem>> systems;
algorithm
_ := List.fold(systems, analyzeOdeEquations1, 1);
end analyzeOdeEquations;
protected function analyzeOdeEquations1
input list<SimCode.SimEqSystem> iEqs;
input Integer iLevel;
output Integer oLevel;
algorithm
print("Equation level " +& intString(iLevel) +& "\n");
_ := List.fold(iEqs,analyzeOdeEquations2,1);
oLevel := iLevel + 1;
end analyzeOdeEquations1;
protected function analyzeOdeEquations2
input SimCode.SimEqSystem iEq;
input Integer iEqIndex;
output Integer oEqIndex;
protected
Integer simEqIdx;
algorithm
simEqIdx := getIndexBySimCodeEq(iEq);
print("Equation " +& intString(simEqIdx) +& "\n");
oEqIndex := iEqIndex + 1;
end analyzeOdeEquations2;
protected function findHighestSccIdxInMapping "function findHighestSccIdxInMapping
author: marcusw
Find the highest scc-index in the mapping list."
input list<tuple<Integer,Integer>> iEquationSccMapping; //<simEqIdx,sccIdx>
input Integer iHighestIndex;
output Integer oIndex;
protected
Integer eqIdx, sccIdx;
list<tuple<Integer,Integer>> rest;
algorithm
oIndex := matchcontinue(iEquationSccMapping,iHighestIndex)
case((eqIdx,sccIdx)::rest,_)
equation
true = intGt(sccIdx,iHighestIndex);
then findHighestSccIdxInMapping(rest,sccIdx);
case((eqIdx,sccIdx)::rest,_)
then findHighestSccIdxInMapping(rest,iHighestIndex);
else
then iHighestIndex;
end matchcontinue;
end findHighestSccIdxInMapping;
protected function removeDummyStateFromMapping "function removeDummyStateFromMapping
author: marcusw
Removes all mappings with sccIdx=1 from the list and decrements all other scc-indices by 1."
input list<tuple<Integer,Integer>> iEquationSccMapping;
output list<tuple<Integer,Integer>> oEquationSccMapping;
algorithm
oEquationSccMapping := List.fold(iEquationSccMapping, removeDummyStateFromMapping1, {});
end removeDummyStateFromMapping;
protected function removeDummyStateFromMapping1 "function removeDummyStateFromMapping1
author: marcusw
Helper function of removeDummyStateFromMapping. Handles one list-element."
input tuple<Integer,Integer> iTuple; //<eqIdx,sccIdx>
input list<tuple<Integer,Integer>> iNewList;
output list<tuple<Integer,Integer>> oNewList;
protected
Integer eqIdx,sccIdx;
tuple<Integer,Integer> newElem;
algorithm
oNewList := matchcontinue(iTuple,iNewList)
case((eqIdx,sccIdx),_)
equation
true = intEq(sccIdx,1);
then iNewList;
case((eqIdx,sccIdx),_)
equation
newElem = (eqIdx,sccIdx-1);
then newElem::iNewList;
else
equation
print("removeDummyStateFromMapping1 failed\n");
then iNewList;
end matchcontinue;
end removeDummyStateFromMapping1;
// ----------------------------------
// Section to analyze cache behaviour
// ----------------------------------
//Available Mappings:
// - varName -> SimVarIdx (fillSimVarHashTable)
// - nodeIdx -> list<SimVarIdx> (getNodeSimCodeVarMapping)
// - simEqIdx -> list<SimVarIdx> (getSimEqVarMapping)
// - cacheLineIdx -> list<TaskIdx> (getCacheLineTaskMapping)
// - taskIdx -> list<CacheLineIdx> (transposeCacheLineTaskMapping)
// - scVarIdx -> ClIdx (getSCVarCacheLineMapping)
// - eqIdx -> list<SCVarIdx> (getEqSCVarMapping)
protected uniontype CacheMap
//CacheMap that stores variables of same type in the same array (different arrays for bool, float and int vars)
record CACHEMAP
Integer cacheLineSize; //cache line size in bytes
list<SimCode.SimVar> cacheVariables; //all variables that are stored in the cache
list<CacheLineMap> cacheLinesFloat;
//list<CacheLineMap> cacheLinesBool;
//list<CacheLineMap> cacheLinesInt;
end CACHEMAP;
//CacheMap that stores variables of different types in the same array
record UNIFORM_CACHEMAP
Integer cacheLineSize; //cache line size in bytes
list<SimCode.SimVar> cacheVariables; //all variables that are stored in the cache
list<CacheLineMap> cacheLines;
end UNIFORM_CACHEMAP;
end CacheMap;
protected uniontype CacheLineMap
record CACHELINEMAP
Integer idx;
list<CacheLineEntry> entries;
end CACHELINEMAP;
end CacheLineMap;
protected uniontype CacheLineEntry
record CACHELINEENTRY
Integer start; //starting with 0
Integer dataType; //1 = float, 2 = int
Integer size;
Integer scVarIdx; //see CacheMap.cacheVariables
end CACHELINEENTRY;
end CacheLineEntry;
protected uniontype MemoryMap
record MEMORYMAP_ARRAY
array<Integer> positionMapping; //map each simCodeVar to a memory (array) position
Integer floatArraySize;
end MEMORYMAP_ARRAY;
end MemoryMap;
protected function analyzeCacheBehaviour
input SimCode.ModelInfo iModelInfo;
input HpcOmTaskGraph.TaskGraph iTaskGraph;
input HpcOmTaskGraph.TaskGraphMeta iTaskGraphMeta;
input BackendDAE.EqSystems iEqSystems;
input String iFileNamePrefix;
input array<tuple<Integer,Integer,Real>> iSchedulerInfo;
input HpcOmScheduler.Schedule iSchedule;
input array<list<Integer>> iSccSimEqMapping; //Maps each scc to a list of simEqs
input list<list<Integer>> iCriticalPaths;
input list<list<Integer>> iCriticalPathsWoC;
input String iCriticalPathInfo;
input BackendDAE.StrongComponents iAllComponents;
protected
SimCode.SimVars simCodeVars;
list<SimCode.SimVar> stateVars, derivativeVars, algVars, paramVars;
HashTableCrILst.HashTable hashTable;
Integer numScVars, numCL, threadAttIdx;
array<list<Integer>> clTaskMapping;
array<Integer> scVarCLMapping, scVarTaskMapping, sccNodeMapping;
CacheMap cacheMap;
Integer graphIdx;
GraphML.GraphInfo graphInfo;
String fileName;
array<array<list<Integer>>> eqSimCodeVarMapping; //eqSystem -> eqIdx -> varIdx
array<tuple<Integer,Integer,Integer>> eqNodeMapping, varNodeMapping;
BackendDAE.IncidenceMatrix incidenceMatrix;
algorithm
_ := matchcontinue(iModelInfo, iTaskGraph, iTaskGraphMeta, iEqSystems, iFileNamePrefix, iSchedulerInfo, iSchedule, iSccSimEqMapping, iCriticalPaths, iCriticalPathsWoC, iCriticalPathInfo, iAllComponents)
case(_,_,_,_,_,_,_,_,_,_,_,_)
equation
true = Flags.isSet(Flags.HPCOM_ANALYZATION_MODE);
//Create var hash table
SimCode.MODELINFO(vars=simCodeVars) = iModelInfo;
SimCode.SIMVARS(stateVars=stateVars, derivativeVars=derivativeVars, algVars=algVars, paramVars=paramVars) = simCodeVars;
hashTable = HashTableCrILst.emptyHashTableSized(BaseHashTable.biggerBucketSize);
hashTable = fillSimVarHashTable(stateVars,0,0,hashTable);
//hashTable = fillSimVarHashTable(derivativeVars,listLength(stateVars),0,hashTable);
hashTable = fillSimVarHashTable(algVars,listLength(stateVars)*2,0,hashTable);
//hashTable = fillSimVarHashTable(paramVars,listLength(stateVars)*2 + listLength(algVars),0,hashTable);
//Create CacheMap
scVarTaskMapping = getScVarTaskMapping(iTaskGraphMeta,iEqSystems,listLength(stateVars)*2+listLength(algVars),hashTable);
(cacheMap,scVarCLMapping,numCL) = createCacheMapOptimized(stateVars,derivativeVars,algVars,paramVars,scVarTaskMapping,64,iAllComponents,iSchedule);
//Create required mappings
sccNodeMapping = HpcOmTaskGraph.getSccNodeMapping(arrayLength(iSccSimEqMapping), iTaskGraphMeta);
eqSimCodeVarMapping = getEqSCVarMapping(iEqSystems,hashTable);
(clTaskMapping,scVarTaskMapping) = getCacheLineTaskMapping(iTaskGraphMeta,iEqSystems,hashTable,numCL,scVarCLMapping);
//Append cache line nodes to graph
graphInfo = GraphML.createGraphInfo();
(graphInfo, (_,graphIdx)) = GraphML.addGraph("TasksGroupGraph", true, graphInfo);
(graphInfo, (_,_),(_,graphIdx)) = GraphML.addGroupNode("TasksGroup", graphIdx, false, "TG", graphInfo);
graphInfo = HpcOmTaskGraph.convertToGraphMLSccLevelSubgraph(iTaskGraph, iTaskGraphMeta, iCriticalPathInfo, HpcOmTaskGraph.convertNodeListToEdgeTuples(List.first(iCriticalPaths)), HpcOmTaskGraph.convertNodeListToEdgeTuples(List.first(iCriticalPathsWoC)), iSccSimEqMapping, iSchedulerInfo, graphIdx, graphInfo);
HpcOmTaskGraph.TASKGRAPHMETA(eqNodeMapping=eqNodeMapping,varNodeMapping=varNodeMapping) = iTaskGraphMeta;
SOME((_,threadAttIdx)) = GraphML.getAttributeByNameAndTarget("ThreadId", GraphML.TARGET_NODE(), graphInfo);
(_,incidenceMatrix,_) = BackendDAEUtil.getIncidenceMatrix(List.first(iEqSystems), BackendDAE.ABSOLUTE(), NONE());
graphInfo = appendCacheLinesToGraph(cacheMap, arrayLength(iTaskGraph), eqSimCodeVarMapping, iEqSystems, hashTable, eqNodeMapping, scVarTaskMapping, iSchedulerInfo, threadAttIdx, graphInfo);
fileName = ("taskGraph"+&iFileNamePrefix+&"ODE_schedule_CL.graphml");
GraphML.dumpGraph(graphInfo, fileName);
then ();
else then ();
end matchcontinue;
end analyzeCacheBehaviour;
protected function createCacheMapOptimized
input list<SimCode.SimVar> iStateVars; //float
input list<SimCode.SimVar> iDerivativeVars; //float
input list<SimCode.SimVar> iAlgVars; //float
input list<SimCode.SimVar> iParamVars; //float
input array<Integer> iScVarTaskMapping;
input Integer iCacheLineSize;
input BackendDAE.StrongComponents iAllComponents;
input HpcOmScheduler.Schedule iSchedule;
output CacheMap oCacheMap;
output array<Integer> oScVarCLMapping; //mapping for each scVar -> CLIdx
output Integer oNumCL;
protected
CacheMap cacheMap;
array<Integer> scVarCLMapping;
Integer numCL;
list<list<HpcOmScheduler.Task>> tasksOfLevels;
algorithm
(oCacheMap,oScVarCLMapping,oNumCL) := match(iStateVars,iDerivativeVars,iAlgVars,iParamVars,iScVarTaskMapping,iCacheLineSize,iAllComponents,iSchedule)
case(_,_,_,_,_,_,_,HpcOmScheduler.LEVELSCHEDULE(tasksOfLevels=tasksOfLevels))
equation
(cacheMap,scVarCLMapping,numCL) = createCacheMapLevelOptimized(iStateVars,iDerivativeVars,iAlgVars,iParamVars,iScVarTaskMapping,iCacheLineSize,iAllComponents,tasksOfLevels);
then (cacheMap,scVarCLMapping,numCL);
else
equation
print("No optimized cache map for the selected scheduler avaiable\n");
(cacheMap,scVarCLMapping,numCL) = createCacheMapDefault(iStateVars,iDerivativeVars,iAlgVars,iCacheLineSize);
then (cacheMap,scVarCLMapping,numCL);
end match;
end createCacheMapOptimized;
protected function createCacheMapLevelOptimized "author: marcusw
Create the optimized cache map for the given level-schedule."
input list<SimCode.SimVar> iStateVars; //float
input list<SimCode.SimVar> iDerivativeVars; //float
input list<SimCode.SimVar> iAlgVars; //float
input list<SimCode.SimVar> iParamVars; //float
input array<Integer> iScVarTaskMapping;
input Integer iCacheLineSize;
input BackendDAE.StrongComponents iAllComponents;
input list<list<HpcOmScheduler.Task>> iTasksOfLevels; //Schedule
output CacheMap oCacheMap;
output array<Integer> oScVarCLMapping; //mapping for each scVar -> CLIdx
output Integer oNumCL;
protected
CacheMap cacheMap;
Integer numCL;
array<Integer> scVarCLMapping;
array<list<CacheLineMap>> threadCacheLines; //cache lines of the threads (arrayIdx)
algorithm
cacheMap := CACHEMAP(iCacheLineSize,{},{});
scVarCLMapping := arrayCreate(1,-1);
numCL := 0;
//Iterate over levels
(oCacheMap,oNumCL,_) := appendParamsToCacheMap(cacheMap, numCL, iParamVars,0);
oScVarCLMapping := scVarCLMapping;
end createCacheMapLevelOptimized;
protected function createCacheMapLevelOptimized0
input list<HpcOmScheduler.Task> iLevelTasks;
input array<list<Integer>> iNodeSimCodeVarMapping;
input tuple<list<Integer>,CacheMap,Integer> iInfo; //<cacheLinesUsedByPreviousLayer,CacheMap,numCL>
output tuple<list<Integer>,CacheMap,Integer> oInfo;
protected
Integer createdCL, numCL; //number of CL created for this level
list<Integer> allCL;
list<Integer> availableCL, availableCLold, writtenCL; //all cacheLines that can be used for writing
list<Integer> cacheLinesPrevLevel; //all cache lines written in previous level
CacheMap cacheMap;
algorithm
(cacheLinesPrevLevel, cacheMap, numCL) := iInfo;
allCL := List.intRange(numCL);
//availableCLold := List.setDifferenceIntN(allCL,cacheLinesPrevLevel,numCL,intEq);
//((cacheMap,createdCL,availableCL)) := List.fold(iLevelTasks, createCacheMapLevelOptimized1, (cacheMap,0,availableCLold));
//append the used cachelines to the writtenCL-list
//writtenCL := List.setDifferenceIntN(availableCLold,availableCL,numCL,intEq);
//writtenCL := List.append(writtenCL, List.intRange2(numCL+1, numCL+createCL));
//oInfo := (writtenCL,cacheMap,cacheMap+createdCL);
oInfo := iInfo;
end createCacheMapLevelOptimized0;
protected function createCacheMapLevelOptimized1
input HpcOmScheduler.Task iTask;
input tuple<CacheMap,Integer,list<Integer>> iInfo; //<CacheMap,numNewCL, availableCL>
output tuple<CacheMap,Integer,list<Integer>> oInfo;
end createCacheMapLevelOptimized1;