/
HpcOmScheduler.mo
6817 lines (6464 loc) · 313 KB
/
HpcOmScheduler.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-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, 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 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, 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 HpcOmScheduler
" file: HpcOmScheduler.mo
package: HpcOmScheduler
description: HpcOmScheduler contains the logic to create a schedule for a taskgraph.
"
public import BackendDAE;
public import HpcOmTaskGraph;
public import HpcOmSimCode;
public import SimCode;
public import SimCodeVar;
protected
import AdjacencyMatrix;
import Array;
import BackendDAEUtil;
import BackendVarTransform;
import ComponentReference;
import DAE;
import Error;
import Expression;
import Flags;
import HpcOmSchedulerExt;
import HpcOmSimCodeMain;
import List;
import SimCodeFunctionUtil;
import SimCodeUtil;
import System;
import Util;
public type TaskAssignment = array<Integer>; //the information which node <idx> is assigned to which processor <value>
//--------------
// No Scheduling
//--------------
public function createEmptySchedule "author: marcusw
Create a empty-schedule to produce serial code. The produces task list represents the computation order of the serial code."
input HpcOmTaskGraph.TaskGraph iTaskGraph;
input HpcOmTaskGraph.TaskGraphMeta iTaskGraphMeta;
input array<list<Integer>> iSccSimEqMapping; //Maps each scc to a list of simEqs
output HpcOmSimCode.Schedule oSchedule;
protected
list<HpcOmSimCode.Task> sortedTasks;
HpcOmTaskGraph.TaskGraph taskGraphT;
list<HpcOmSimCode.Task> allTasks = {};
array<tuple<HpcOmSimCode.Task,Integer>> allCalcTasks; //tasks with ref counter
Integer taskIdx;
Integer weighting, index, threadIdx;
Real calcTime, timeFinished;
list<Integer> eqIdc;
algorithm
taskGraphT := AdjacencyMatrix.transposeAdjacencyMatrix(iTaskGraph,arrayLength(iTaskGraph));
allCalcTasks := convertTaskGraphToTasks(taskGraphT,iTaskGraphMeta,convertNodeToTask);
for taskIdx in listReverse(List.intRange(arrayLength(allCalcTasks))) loop
((HpcOmSimCode.CALCTASK(weighting, index, calcTime, timeFinished, threadIdx, eqIdc),_)) := arrayGet(allCalcTasks, taskIdx);
eqIdc := List.map(List.map1(eqIdc,getSimEqSysIdxForComp,iSccSimEqMapping), List.last);
allTasks := HpcOmSimCode.CALCTASK(weighting, index, calcTime, timeFinished, threadIdx, eqIdc)::allTasks;
end for;
allTasks := List.sort(allTasks, compareTasksByEqIdc);
oSchedule := HpcOmSimCode.EMPTYSCHEDULE(HpcOmSimCode.SERIALTASKLIST(allTasks, true));
end createEmptySchedule;
//----------------
// List Scheduling
//----------------
public function createListSchedule "author: marcusw
Create a list-schedule out of the given informations."
input HpcOmTaskGraph.TaskGraph iTaskGraph;
input HpcOmTaskGraph.TaskGraphMeta iTaskGraphMeta;
input Integer iNumberOfThreads;
input array<list<Integer>> iSccSimEqMapping; //Maps each scc to a list of simEqs
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
output HpcOmSimCode.Schedule oSchedule;
protected
HpcOmTaskGraph.TaskGraph taskGraphT;
array<list<Integer>> inComps;
list<tuple<HpcOmSimCode.Task,Integer>> nodeList_refCount; //list of nodes which are ready to schedule
list<HpcOmSimCode.Task> nodeList;
list<Integer> rootNodes;
array<Real> threadReadyTimes;
array<tuple<HpcOmSimCode.Task,Integer>> allCalcTasks;
array<list<HpcOmSimCode.Task>> threadTasks;
array<HpcOmTaskGraph.Communications> commCosts;
HpcOmSimCode.Schedule tmpSchedule;
algorithm
HpcOmTaskGraph.TASKGRAPHMETA(commCosts=commCosts,inComps=inComps) := iTaskGraphMeta;
taskGraphT := AdjacencyMatrix.transposeAdjacencyMatrix(iTaskGraph,arrayLength(iTaskGraph));
rootNodes := HpcOmTaskGraph.getRootNodes(iTaskGraph);
allCalcTasks := convertTaskGraphToTasks(taskGraphT,iTaskGraphMeta,convertNodeToTask);
nodeList_refCount := List.map1(rootNodes, getTaskByIndex, allCalcTasks);
nodeList := List.map(nodeList_refCount, Util.tuple21);
nodeList := List.sort(nodeList, compareTasksByWeighting); //MF level
threadReadyTimes := arrayCreate(iNumberOfThreads,0.0);
threadTasks := arrayCreate(iNumberOfThreads,{});
tmpSchedule := HpcOmSimCode.THREADSCHEDULE(threadTasks,{},{},allCalcTasks);
(tmpSchedule,_) := createListSchedule1(nodeList,threadReadyTimes, iTaskGraph, taskGraphT, commCosts, inComps, iSccSimEqMapping, iSimVarMapping, getLocksByPredecessorList, tmpSchedule);
tmpSchedule := addSuccessorLocksToSchedule(iTaskGraph,addReleaseLocksToSchedule,commCosts,inComps,iSimVarMapping,tmpSchedule);
//printSchedule(tmpSchedule);
oSchedule := setScheduleLockIds(tmpSchedule);
end createListSchedule;
protected function createListSchedule1 "author: marcusw
Create a list schedule, starting with the given nodeList and ready times. This method will add calcTasks and
assignLockTasks, but no releaseLockTasks!"
input list<HpcOmSimCode.Task> iNodeList; //the sorted nodes -> this method will pick the first task
input array<Real> iThreadReadyTimes; //the time until the thread is ready to handle a new task
input HpcOmTaskGraph.TaskGraph iTaskGraph;
input HpcOmTaskGraph.TaskGraph iTaskGraphT;
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<Integer>> iSccSimEqMapping; //Maps each scc to a list of simEqs
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
input FuncType iLockWithPredecessorHandler; //Function which handles locks to all predecessors
input HpcOmSimCode.Schedule iSchedule;
output HpcOmSimCode.Schedule oSchedule;
output array<Real> oThreadReadyTimes;
partial function FuncType
input HpcOmSimCode.Task iTask;
input list<tuple<HpcOmSimCode.Task,Integer>> iPredecessors;
input Integer iThreadIdx;
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
output list<HpcOmSimCode.Task> oTasks; //lock tasks
output list<HpcOmSimCode.Task> oOutgoingDepTasks;
end FuncType;
protected
HpcOmSimCode.Task head, newTask;
Integer newTaskRefCount;
list<HpcOmSimCode.Task> rest;
Real lastChildFinishTime; //The time when the last child has finished calculation
HpcOmSimCode.Task lastChild;
list<tuple<HpcOmSimCode.Task, Integer>> predecessors, successors;
list<Integer> successorIdc;
list<HpcOmSimCode.Task> outgoingDepTasks, newOutgoingDepTasks;
array<Real> threadFinishTimes;
Integer firstEq;
array<list<HpcOmSimCode.Task>> allThreadTasks;
list<HpcOmSimCode.Task> threadTasks, lockTasks;
Integer threadId;
Real threadFinishTime;
array<Real> tmpThreadReadyTimes;
list<HpcOmSimCode.Task> tmpNodeList;
Integer weighting;
Integer index;
Real calcTime;
list<Integer> eqIdc, simEqIdc;
HpcOmSimCode.Schedule tmpSchedule;
array<tuple<HpcOmSimCode.Task,Integer>> allCalcTasks;
algorithm
(oSchedule,oThreadReadyTimes) := match(iNodeList,iThreadReadyTimes, iTaskGraph, iTaskGraphT, iCommCosts, iCompTaskMapping,
iSccSimEqMapping, iSimVarMapping, iLockWithPredecessorHandler, iSchedule)
case((head as HpcOmSimCode.CALCTASK(weighting=weighting,index=index,calcTime=calcTime,eqIdc=(eqIdc as firstEq::_)))
::rest,_,_,_,_,_,_,_,_,HpcOmSimCode.THREADSCHEDULE(threadTasks=allThreadTasks, outgoingDepTasks=outgoingDepTasks,allCalcTasks=allCalcTasks))
equation
//get all predecessors (childs)
(predecessors, _) = getSuccessorsByTask(head, iTaskGraphT, allCalcTasks);
(successors, successorIdc) = getSuccessorsByTask(head, iTaskGraph, allCalcTasks);
if(boolNot(listEmpty(predecessors))) then //in this case the node has predecessors
lastChild = getTaskWithHighestFinishTime(predecessors, NONE());
HpcOmSimCode.CALCTASK(timeFinished=lastChildFinishTime) = lastChild;
else
lastChildFinishTime = 0.0;
end if;
threadFinishTimes = calculateFinishTimes(lastChildFinishTime, head, predecessors, iCommCosts, iThreadReadyTimes);
((threadId, threadFinishTime)) = getThreadFinishTimesMin(1,threadFinishTimes,-1,0.0);
tmpThreadReadyTimes = arrayUpdate(iThreadReadyTimes, threadId, threadFinishTime);
threadTasks = arrayGet(allThreadTasks,threadId);
//print("Scheduling task " + intString(index) + " to thread " + intString(threadId) + "\n");
if(boolNot(listEmpty(predecessors))) then //in this case the node has predecessors
//find all predecessors which are scheduled to another thread and thus require a lock
(lockTasks,newOutgoingDepTasks) = iLockWithPredecessorHandler(head,predecessors,threadId,iCommCosts,iCompTaskMapping,iSimVarMapping);
outgoingDepTasks = listAppend(outgoingDepTasks,newOutgoingDepTasks);
//threadTasks = listAppend(List.map(newLockIdc,convertLockIdToAssignTask), threadTasks);
threadTasks = listAppend(lockTasks, threadTasks);
//print("Eq idc: " + stringDelimitList(List.map(eqIdc, intString), ",") + "\n");
simEqIdc = List.map(List.map1(eqIdc,getSimEqSysIdxForComp,iSccSimEqMapping), List.last);
//simEqIdc = List.sort(simEqIdc,intGt);
else
simEqIdc = List.flatten(List.map1(eqIdc,getSimEqSysIdxForComp,iSccSimEqMapping));
end if;
newTask = HpcOmSimCode.CALCTASK(weighting,index,calcTime,threadFinishTime,threadId,simEqIdc);
threadTasks = newTask::threadTasks;
allThreadTasks = arrayUpdate(allThreadTasks,threadId,threadTasks);
//add all successors with refcounter = 1
(allCalcTasks,tmpNodeList) = updateRefCounterBySuccessorIdc(allCalcTasks,successorIdc,{});
tmpNodeList = listAppend(tmpNodeList, rest);
tmpNodeList = List.sort(tmpNodeList, compareTasksByWeighting);
((_,newTaskRefCount)) = arrayGet(allCalcTasks,index);
arrayUpdate(allCalcTasks,index,(newTask,newTaskRefCount));
(tmpSchedule,tmpThreadReadyTimes) = createListSchedule1(tmpNodeList,tmpThreadReadyTimes,iTaskGraph, iTaskGraphT, iCommCosts, iCompTaskMapping, iSccSimEqMapping, iSimVarMapping, iLockWithPredecessorHandler, HpcOmSimCode.THREADSCHEDULE(allThreadTasks,outgoingDepTasks,{},allCalcTasks));
then (tmpSchedule,tmpThreadReadyTimes);
case({},_,_,_,_,_,_,_,_,_) then (iSchedule,iThreadReadyTimes);
else
equation
print("HpcOmScheduler.createListSchedule1 failed\n");
then (iSchedule,iThreadReadyTimes);
end match;
end createListSchedule1;
//----------------
// Random Scheduling
//----------------
public function createRandomSchedule "author: mflehmig
Create a schedule out of the given informations by randomly chose a thread for each task.
This implementation is very close to list scheduling algorithm but we do not need to calculate a 'best schedule'."
input HpcOmTaskGraph.TaskGraph iTaskGraph;
input HpcOmTaskGraph.TaskGraphMeta iTaskGraphMeta;
input Integer iNumberOfThreads;
input array<list<Integer>> iSccSimEqMapping; //Maps each scc to a list of simEqs
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
output HpcOmSimCode.Schedule oSchedule;
protected
HpcOmTaskGraph.TaskGraph taskGraphT;
array<list<Integer>> inComps;
list<tuple<HpcOmSimCode.Task, Integer>> nodeList_refCount; //List of nodes which are ready to schedule
list<HpcOmSimCode.Task> nodeList;
list<Integer> rootNodes;
array<Real> threadReadyTimes;
array<tuple<HpcOmSimCode.Task, Integer>> allCalcTasks;
array<list<HpcOmSimCode.Task>> threadTasks;
array<HpcOmTaskGraph.Communications> commCosts;
HpcOmSimCode.Schedule tmpSchedule;
algorithm
HpcOmTaskGraph.TASKGRAPHMETA(commCosts=commCosts, inComps=inComps) := iTaskGraphMeta;
taskGraphT := AdjacencyMatrix.transposeAdjacencyMatrix(iTaskGraph, arrayLength(iTaskGraph));
rootNodes := HpcOmTaskGraph.getRootNodes(iTaskGraph);
allCalcTasks := convertTaskGraphToTasks(taskGraphT, iTaskGraphMeta, convertNodeToTask);
nodeList_refCount := List.map1(rootNodes, getTaskByIndex, allCalcTasks);
nodeList := List.map(nodeList_refCount, Util.tuple21);
nodeList := List.sort(nodeList, compareTasksByWeighting); //MF level
threadReadyTimes := arrayCreate(iNumberOfThreads ,0.0);
threadTasks := arrayCreate(iNumberOfThreads, {});
tmpSchedule := HpcOmSimCode.THREADSCHEDULE(threadTasks, {}, {}, allCalcTasks);
(tmpSchedule,_) := createRandomSchedule1(nodeList, threadReadyTimes, iTaskGraph, taskGraphT, commCosts, inComps,
iSccSimEqMapping, iSimVarMapping, getLocksByPredecessorList, iNumberOfThreads,
tmpSchedule);
tmpSchedule := addSuccessorLocksToSchedule(iTaskGraph, addReleaseLocksToSchedule, commCosts, inComps, iSimVarMapping,
tmpSchedule);
//printSchedule(tmpSchedule);
oSchedule := setScheduleLockIds(tmpSchedule);
end createRandomSchedule;
protected function createRandomSchedule1 "author: mflehmig
Create a random schedule starting with the given nodeList. This method will add calcTasks and assignLockTasks,
but no releaseLockTasks!"
input list<HpcOmSimCode.Task> iNodeList; //The sorted nodes -> this method will pick the first task
input array<Real> iThreadReadyTimes; //The time until the thread is ready to handle a new task
input HpcOmTaskGraph.TaskGraph iTaskGraph;
input HpcOmTaskGraph.TaskGraph iTaskGraphT;
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //All StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<Integer>> iSccSimEqMapping; //Maps each scc to a list of simEqs
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
input FuncType iLockWithPredecessorHandler; //Function which handles locks to all predecessors
input Integer iNumberOfThreads;
input HpcOmSimCode.Schedule iSchedule;
output HpcOmSimCode.Schedule oSchedule;
output array<Real> oThreadReadyTimes;
partial function FuncType
input HpcOmSimCode.Task iTask;
input list<tuple<HpcOmSimCode.Task,Integer>> iPredecessors;
input Integer iThreadIdx;
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //All StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
output list<HpcOmSimCode.Task> oTasks; //Lock tasks
output list<HpcOmSimCode.Task> oOutgoingDepTasks;
end FuncType;
protected
HpcOmSimCode.Task head, newTask;
Integer newTaskRefCount;
list<HpcOmSimCode.Task> rest;
Real lastChildFinishTime; //The time when the last child has finished calculation
HpcOmSimCode.Task lastChild;
list<tuple<HpcOmSimCode.Task, Integer>> predecessors, successors;
list<Integer> successorIdc;
list<HpcOmSimCode.Task> outgoingDepTasks, newOutgoingDepTasks;
array<Real> threadFinishTimes;
Integer firstEq;
array<list<HpcOmSimCode.Task>> allThreadTasks; //All tasks of all threads, i.e., allThreadTasks[i] = {all tasks of thread i}
list<HpcOmSimCode.Task> threadTasks; //All tasks of a particular thread (used as temp. var.), i.e., threadTasks = allThreadTasks[threadId]
list<HpcOmSimCode.Task> lockTasks;
Integer threadId;
Real threadFinishTime;
array<Real> tmpThreadReadyTimes;
list<HpcOmSimCode.Task> tmpNodeList;
Integer weighting;
Integer index;
Real calcTime;
list<Integer> eqIdc, simEqIdc;
HpcOmSimCode.Schedule tmpSchedule;
array<tuple<HpcOmSimCode.Task,Integer>> allCalcTasks;
algorithm
(oSchedule,oThreadReadyTimes) := matchcontinue(iNodeList,iThreadReadyTimes, iTaskGraph, iTaskGraphT, iCommCosts, iCompTaskMapping,
iSccSimEqMapping, iSimVarMapping, iLockWithPredecessorHandler, iNumberOfThreads, iSchedule)
case((head as HpcOmSimCode.CALCTASK(weighting=weighting,index=index,calcTime=calcTime,eqIdc=(eqIdc as firstEq::_)))::rest
,_,_,_,_,_,_,_,_,_,HpcOmSimCode.THREADSCHEDULE(threadTasks=allThreadTasks, outgoingDepTasks=outgoingDepTasks, allCalcTasks=allCalcTasks))
equation
//Get all predecessors (childs)
(predecessors, _) = getSuccessorsByTask(head, iTaskGraphT, allCalcTasks);
(successors, successorIdc) = getSuccessorsByTask(head, iTaskGraph, allCalcTasks);
false = listEmpty(predecessors); //in this case the node has predecessors
//!print("Handle1 task " + intString(index) + "\n");// + " with " + intString(listLength(predecessors)) + " child nodes and "
//+ intString(listLength(successorIdc)) + " parent nodes.\n");
//!print("\tZeile 367\t" + stringDelimitList(List.map(arrayList(iThreadReadyTimes),realString), "\t\t") + "\n");
//! Randomly chose thread for scheduling.
threadId = System.intRandom(iNumberOfThreads) + 1;
threadFinishTimes = calculateFinishTimes(0.0, head, {}, iCommCosts, iThreadReadyTimes);
threadFinishTime = arrayGet(threadFinishTimes, threadId);
tmpThreadReadyTimes = arrayUpdate(iThreadReadyTimes, threadId, threadFinishTime);
threadTasks = arrayGet(allThreadTasks,threadId);
//Find all predecessors which are scheduled to another thread and thus require a lock
(lockTasks,newOutgoingDepTasks) = iLockWithPredecessorHandler(head, predecessors, threadId, iCommCosts, iCompTaskMapping,
iSimVarMapping);
outgoingDepTasks = listAppend(outgoingDepTasks, newOutgoingDepTasks);
threadTasks = listAppend(lockTasks, threadTasks);
simEqIdc = List.map(List.map1(eqIdc, getSimEqSysIdxForComp, iSccSimEqMapping), List.last);
//simEqIdc = List.sort(simEqIdc,intGt);
//! Add task to thread
newTask = HpcOmSimCode.CALCTASK(weighting, index, calcTime, threadFinishTime, threadId, simEqIdc);
threadTasks = newTask::threadTasks;
allThreadTasks = arrayUpdate(allThreadTasks, threadId, threadTasks);
//add all successors with refcounter = 1
(allCalcTasks, tmpNodeList) = updateRefCounterBySuccessorIdc(allCalcTasks, successorIdc, {});
tmpNodeList = listAppend(tmpNodeList, rest);
tmpNodeList = List.sort(tmpNodeList, compareTasksByWeighting);
(_, newTaskRefCount) = arrayGet(allCalcTasks, index);
_ = arrayUpdate(allCalcTasks, index, (newTask, newTaskRefCount));
(tmpSchedule, tmpThreadReadyTimes) = createRandomSchedule1(tmpNodeList,tmpThreadReadyTimes, iTaskGraph, iTaskGraphT,
iCommCosts, iCompTaskMapping, iSccSimEqMapping, iSimVarMapping,
iLockWithPredecessorHandler, iNumberOfThreads,
HpcOmSimCode.THREADSCHEDULE(allThreadTasks, outgoingDepTasks,
{}, allCalcTasks));
then (tmpSchedule,tmpThreadReadyTimes);
case((head as HpcOmSimCode.CALCTASK(weighting=weighting,index=index,calcTime=calcTime,eqIdc=(eqIdc as firstEq::_)))::rest
,_,_,_,_,_,_,_,_,_,
HpcOmSimCode.THREADSCHEDULE(threadTasks=allThreadTasks,outgoingDepTasks=outgoingDepTasks, allCalcTasks=allCalcTasks))
equation
(successors, successorIdc) = getSuccessorsByTask(head, iTaskGraph, allCalcTasks);
//Randomly chose thread for scheduling
threadId = System.intRandom(iNumberOfThreads)+1;
//print("ThreadId= " + intString(threadId) + "\n");
threadFinishTimes = calculateFinishTimes(0.0, head, {}, iCommCosts, iThreadReadyTimes);
threadFinishTime = arrayGet(threadFinishTimes, threadId);
// Update array containg thread finish times.
tmpThreadReadyTimes = arrayUpdate(iThreadReadyTimes, threadId, threadFinishTime);
threadTasks = arrayGet(allThreadTasks, threadId);
simEqIdc = List.flatten(List.map1(eqIdc, getSimEqSysIdxForComp, iSccSimEqMapping));
//simEqIdc = List.sort(simEqIdc,intGt);
newTask = HpcOmSimCode.CALCTASK(weighting, index, calcTime, threadFinishTime, threadId, simEqIdc);
allThreadTasks = arrayUpdate(allThreadTasks, threadId, newTask::threadTasks);
//Add all successors with refcounter = 1
(allCalcTasks, tmpNodeList) = updateRefCounterBySuccessorIdc(allCalcTasks, successorIdc, {});
tmpNodeList = listAppend(tmpNodeList, rest);
tmpNodeList = List.sort(tmpNodeList, compareTasksByWeighting);
(_, newTaskRefCount) = arrayGet(allCalcTasks, index);
_ = arrayUpdate(allCalcTasks, index, (newTask, newTaskRefCount));
(tmpSchedule,tmpThreadReadyTimes) = createRandomSchedule1(tmpNodeList, tmpThreadReadyTimes, iTaskGraph, iTaskGraphT,
iCommCosts, iCompTaskMapping, iSccSimEqMapping, iSimVarMapping,
iLockWithPredecessorHandler, iNumberOfThreads,
HpcOmSimCode.THREADSCHEDULE(allThreadTasks, outgoingDepTasks,
{}, allCalcTasks));
then (tmpSchedule,tmpThreadReadyTimes);
case({},_,_,_,_,_,_,_,_,_,_) then (iSchedule, iThreadReadyTimes);
else
equation
print("HpcOmScheduler.createRandomSchedule1 failed\n");
then fail();
end matchcontinue;
end createRandomSchedule1;
//------------------------
// List Scheduling reverse
//------------------------
public function createListScheduleReverse "author: marcusw
Create a list-schedule out of the given information, starting with all leaves."
input HpcOmTaskGraph.TaskGraph iTaskGraph;
input HpcOmTaskGraph.TaskGraphMeta iTaskGraphMeta;
input Integer iNumberOfThreads;
input array<list<Integer>> iSccSimEqMapping; //Maps each scc to a list of simEqs
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
output HpcOmSimCode.Schedule oSchedule;
protected
HpcOmTaskGraph.TaskGraph taskGraphT;
list<tuple<HpcOmSimCode.Task,Integer>> nodeList_refCount; //list of nodes which are ready to schedule
list<HpcOmSimCode.Task> nodeList;
list<Integer> leaveNodes;
array<Real> threadReadyTimes;
array<tuple<HpcOmSimCode.Task,Integer>> allCalcTasks;
array<list<HpcOmSimCode.Task>> threadTasks;
array<HpcOmTaskGraph.Communications> commCosts, commCostsT;
HpcOmSimCode.Schedule tmpSchedule;
list<HpcOmSimCode.Task> outgoingDepTasks;
array<list<Integer>> inComps;
algorithm
HpcOmTaskGraph.TASKGRAPHMETA(commCosts=commCosts,inComps=inComps) := iTaskGraphMeta;
taskGraphT := AdjacencyMatrix.transposeAdjacencyMatrix(iTaskGraph,arrayLength(iTaskGraph));
commCostsT := HpcOmTaskGraph.transposeCommCosts(commCosts);
leaveNodes := HpcOmTaskGraph.getLeafNodes(iTaskGraph);
//print("Leave nodes: " + stringDelimitList(List.map(leaveNodes,intString),", ") + "\n");
allCalcTasks := convertTaskGraphToTasks(iTaskGraph,iTaskGraphMeta,convertNodeToTaskReverse);
nodeList_refCount := List.map1(leaveNodes, getTaskByIndex, allCalcTasks);
nodeList := List.map(nodeList_refCount, Util.tuple21);
nodeList := List.sort(nodeList, compareTasksByWeighting);
threadReadyTimes := arrayCreate(iNumberOfThreads,0.0);
threadTasks := arrayCreate(iNumberOfThreads,{});
tmpSchedule := HpcOmSimCode.THREADSCHEDULE(threadTasks,{},{},allCalcTasks);
(tmpSchedule,_) := createListSchedule1(nodeList,threadReadyTimes, taskGraphT, iTaskGraph, commCostsT, inComps, iSccSimEqMapping, iSimVarMapping, getLockTasksByPredecessorListReverse, tmpSchedule);
tmpSchedule := addSuccessorLocksToSchedule(taskGraphT,addAssignLocksToSchedule,commCosts,inComps,iSimVarMapping,tmpSchedule);
HpcOmSimCode.THREADSCHEDULE(threadTasks=threadTasks,outgoingDepTasks=outgoingDepTasks) := tmpSchedule;
threadTasks := Array.map(threadTasks, listReverse);
tmpSchedule := HpcOmSimCode.THREADSCHEDULE(threadTasks,outgoingDepTasks,{},allCalcTasks);
//printSchedule(tmpSchedule);
oSchedule := setScheduleLockIds(tmpSchedule); // set unique lock ids
end createListScheduleReverse;
protected function addSuccessorLocksToSchedule
input HpcOmTaskGraph.TaskGraph iTaskGraph;
input FuncType iCreateLockFunction;
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //All StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
input HpcOmSimCode.Schedule iSchedule;
output HpcOmSimCode.Schedule oSchedule;
partial function FuncType
input tuple<HpcOmSimCode.Task,Integer> iSuccessorTask;
input HpcOmSimCode.Task iTask;
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
input list<HpcOmSimCode.Task> iReleaseTasks;
output list<HpcOmSimCode.Task> oReleaseTasks;
end FuncType;
protected
array<list<HpcOmSimCode.Task>> allThreadTasks;
HpcOmSimCode.Schedule tmpSchedule;
list<HpcOmSimCode.Task> outgoingDepTasks;
array<tuple<HpcOmSimCode.Task,Integer>> allCalcTasks;
algorithm
oSchedule := match(iTaskGraph,iCreateLockFunction,iCommCosts,iCompTaskMapping,iSimVarMapping,iSchedule)
case(_,_,_,_,_,HpcOmSimCode.THREADSCHEDULE(threadTasks=allThreadTasks,outgoingDepTasks=outgoingDepTasks,allCalcTasks=allCalcTasks))
equation
((allThreadTasks,_)) = Array.fold6(allThreadTasks, addSuccessorLocksToSchedule0, iTaskGraph, allCalcTasks, iSimVarMapping, iCommCosts, iCompTaskMapping, iCreateLockFunction, (allThreadTasks,1));
tmpSchedule = HpcOmSimCode.THREADSCHEDULE(allThreadTasks,outgoingDepTasks,{},allCalcTasks);
then tmpSchedule;
else
equation
print("HpcOmScheduler.addReleaseLocksToSchedule failed\n");
then fail();
end match;
end addSuccessorLocksToSchedule;
protected function addSuccessorLocksToSchedule0
input list<HpcOmSimCode.Task> iThreadTaskList;
input HpcOmTaskGraph.TaskGraph iTaskGraph;
input array<tuple<HpcOmSimCode.Task,Integer>> iAllCalcTasks;
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input FuncType iCreateLockFunction;
input tuple<array<list<HpcOmSimCode.Task>>, Integer> iThreadTasks; //<schedulerTasks, threadId>
output tuple<array<list<HpcOmSimCode.Task>>, Integer> oThreadTasks;
partial function FuncType
input tuple<HpcOmSimCode.Task,Integer> iSuccessorTask;
input HpcOmSimCode.Task iTask;
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
input list<HpcOmSimCode.Task> iReleaseTasks;
output list<HpcOmSimCode.Task> oReleaseTasks;
end FuncType;
protected
Integer threadId;
array<list<HpcOmSimCode.Task>> allThreadTasks;
list<HpcOmSimCode.Task> threadTasks;
algorithm
(allThreadTasks,threadId) := iThreadTasks;
threadTasks := List.fold(iThreadTaskList, function addSuccessorLocksToSchedule1(iTaskGraph=iTaskGraph, iAllCalcTasks=iAllCalcTasks, iSimVarMapping=iSimVarMapping, iCommCosts=iCommCosts, iCompTaskMapping=iCompTaskMapping, iThreadIdLockFunction=(threadId, iCreateLockFunction)), {});
allThreadTasks := arrayUpdate(allThreadTasks,threadId,threadTasks);
oThreadTasks := ((allThreadTasks,threadId+1));
end addSuccessorLocksToSchedule0;
protected function addSuccessorLocksToSchedule1
input HpcOmSimCode.Task iTask;
input HpcOmTaskGraph.TaskGraph iTaskGraph;
input array<tuple<HpcOmSimCode.Task,Integer>> iAllCalcTasks;
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input tuple<Integer,FuncType> iThreadIdLockFunction; //<threadId, createLockFunction>
input list<HpcOmSimCode.Task> iThreadTasks; //schedulerTasks
output list<HpcOmSimCode.Task> oThreadTasks;
partial function FuncType
input tuple<HpcOmSimCode.Task,Integer> iSuccessorTask;
input HpcOmSimCode.Task iTask;
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
input list<HpcOmSimCode.Task> iReleaseTasks;
output list<HpcOmSimCode.Task> oReleaseTasks;
end FuncType;
protected
Integer threadIdx,index,listIndex;
list<tuple<HpcOmSimCode.Task,Integer>> successors;
list<HpcOmSimCode.Task> tmpThreadTasks, releaseTasks;
Integer iThreadId;
FuncType iCreateLockFunction;
algorithm
oThreadTasks := match(iTask, iTaskGraph, iAllCalcTasks, iSimVarMapping, iCommCosts, iCompTaskMapping, iThreadIdLockFunction, iThreadTasks)
case(HpcOmSimCode.CALCTASK(threadIdx=threadIdx,index=index),_,_,_,_,_,(iThreadId,iCreateLockFunction),tmpThreadTasks)
equation
(successors,_) = getSuccessorsByTask(iTask, iTaskGraph, iAllCalcTasks);
successors = List.removeOnTrue(threadIdx, compareTaskWithThreadIdx, successors);
releaseTasks = List.fold4(successors, iCreateLockFunction, iTask, iCommCosts, iCompTaskMapping, iSimVarMapping, {});
tmpThreadTasks = listAppend(releaseTasks,tmpThreadTasks);
tmpThreadTasks = iTask :: tmpThreadTasks;
then tmpThreadTasks;
case(_,_,_,_,_,_,_,tmpThreadTasks)
equation
tmpThreadTasks = iTask :: tmpThreadTasks;
then tmpThreadTasks;
else
equation
print("HpcOmScheduler.addReleaseLocksToSchedule0 failed\n");
then fail();
end match;
end addSuccessorLocksToSchedule1;
protected function addReleaseLocksToSchedule "author: marcusw
Add a release lock to the schedule, releasing a lock from iTask to successor."
input tuple<HpcOmSimCode.Task,Integer> iSuccessorTask;
input HpcOmSimCode.Task iTask;
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
input list<HpcOmSimCode.Task> iReleaseTasks;
output list<HpcOmSimCode.Task> oReleaseTasks;
protected
HpcOmSimCode.Task tmpTask, successorTask;
String lockString;
Integer lockId, successorTaskId;
algorithm
(successorTask,_) := iSuccessorTask;
tmpTask := createDepTaskAndCommunicationInfo(iTask,successorTask,true,iCommCosts,iCompTaskMapping,iSimVarMapping);
oReleaseTasks := tmpTask :: iReleaseTasks;
end addReleaseLocksToSchedule;
protected function addAssignLocksToSchedule "author: marcusw
Add a assign lock to the schedule, assinging a lock from successor to iTask."
input tuple<HpcOmSimCode.Task,Integer> iSuccessorTask;
input HpcOmSimCode.Task iTask;
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
input list<HpcOmSimCode.Task> iReleaseTasks;
output list<HpcOmSimCode.Task> oReleaseTasks;
protected
HpcOmSimCode.Task tmpTask, successorTask;
algorithm
(successorTask,_) := iSuccessorTask;
tmpTask := createDepTaskAndCommunicationInfo(successorTask,iTask,false,iCommCosts,iCompTaskMapping,iSimVarMapping);
oReleaseTasks := tmpTask :: iReleaseTasks;
end addAssignLocksToSchedule;
protected function getSimEqSysIdxForComp "
Gets the simeqSys indexes for the given SCC index."
input Integer compIdx;
input array<list<Integer>> iSccSimEqMapping;
output list<Integer> simEqSysIdcs;
algorithm
simEqSysIdcs := arrayGet(iSccSimEqMapping,compIdx);
end getSimEqSysIdxForComp;
protected function getSimEqSysIdcsForCompLst "
Gets a list of simeqSys indexes for the given list of SCC indexes."
input list<Integer> compIdcs;
input array<list<Integer>> iSccSimEqMapping;
output list<Integer> simEqSysIdcs;
algorithm
//print("compIdcs: \n"+stringDelimitList(List.map(compIdcs,intString),"\n")+"\n");
simEqSysIdcs := List.flatten(List.map1(compIdcs,Array.getIndexFirst,iSccSimEqMapping));
//print("simEqSysIdcs: \n"+stringDelimitList(List.map(simEqSysIdcs,intString),"\n")+"\n");
end getSimEqSysIdcsForCompLst;
public function getSimEqSysIdcsForNodeLst "
Gets a list of simeqSys indexes for the given nodes (node = list of comps)."
input list<list<Integer>> nodeIdcs;
input array<list<Integer>> iSccSimEqMapping;
output list<list<Integer>> simEqSysIdcsLst;
algorithm
simEqSysIdcsLst := List.map1(nodeIdcs,getSimEqSysIdcsForCompLst,iSccSimEqMapping);
end getSimEqSysIdcsForNodeLst;
protected function getLocksByPredecessorList "author: marcusw
Creates incoming dependencies between the given task (iTask) and all predecessor tasks."
input HpcOmSimCode.Task iTask; //The parent task
input list<tuple<HpcOmSimCode.Task,Integer>> iPredecessorList; //All tasks with reference counter
input Integer iThreadIdx; //Thread handling task <%iTaskIdx%>
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
output list<HpcOmSimCode.Task> oLockTasks;
output list<HpcOmSimCode.Task> oOutgoingDepTasks;
protected
list<HpcOmSimCode.Task> tmpTaskList;
algorithm
oLockTasks := List.fold(iPredecessorList, function getLockTasksByPredecessorList(iTask=iTask, iThreadIdx=iThreadIdx, iCommCosts=iCommCosts, iCompTaskMapping=iCompTaskMapping, iSimVarMapping=iSimVarMapping), {});
oOutgoingDepTasks := oLockTasks;
end getLocksByPredecessorList;
protected function getLockTasksByPredecessorList "author: marcusw
Append a incoming dependency between the given iTask and the predecessor task to the output-list if they are
not handled by the same thread."
input tuple<HpcOmSimCode.Task,Integer> iPredecessorTask;
input HpcOmSimCode.Task iTask; //The parent task
input Integer iThreadIdx; //Thread handling task <%iTaskIdx%>
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
input list<HpcOmSimCode.Task> iLockTasks;
output list<HpcOmSimCode.Task> oLockTasks;
protected
Integer threadIdx, predIndex, taskIndex;
list<HpcOmSimCode.Task> tmpLockTasks;
HpcOmSimCode.Task tmpTask, predTask;
algorithm
oLockTasks := matchcontinue(iPredecessorTask,iTask,iThreadIdx,iCommCosts,iCompTaskMapping,iSimVarMapping,iLockTasks)
case((predTask as HpcOmSimCode.CALCTASK(threadIdx=threadIdx,index=predIndex),_),HpcOmSimCode.CALCTASK(index=taskIndex),_,_,_,_,tmpLockTasks)
equation
true = intNe(iThreadIdx,threadIdx);
//print("Adding a new lock for the tasks " + intString(iTaskIdx) + " " + intString(predIndex) + "\n");
tmpTask = createDepTaskAndCommunicationInfo(predTask, iTask, false, iCommCosts, iCompTaskMapping, iSimVarMapping);
//print("Because task " + intString(predIndex) + " is scheduled to " + intString(threadIdx) + "\n");
tmpLockTasks = tmpTask::tmpLockTasks;
then tmpLockTasks;
else iLockTasks;
end matchcontinue;
end getLockTasksByPredecessorList;
protected function getLockTasksByPredecessorListReverse
input HpcOmSimCode.Task iTask; //The parent task
input list<tuple<HpcOmSimCode.Task,Integer>> iPredecessorList;
input Integer iThreadIdx; //Thread handling task <%iTaskIdx%>
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
output list<HpcOmSimCode.Task> oLockTasks;
output list<HpcOmSimCode.Task> oOutgoingDepTasks;
algorithm
oLockTasks := List.fold(iPredecessorList, function getLockTasksByPredecessorListReverse0(iTask=iTask, iThreadIdx=iThreadIdx, iCommCosts=iCommCosts, iCompTaskMapping=iCompTaskMapping, iSimVarMapping=iSimVarMapping), {});
oOutgoingDepTasks := oLockTasks;
end getLockTasksByPredecessorListReverse;
protected function getLockTasksByPredecessorListReverse0
input tuple<HpcOmSimCode.Task,Integer> iPredecessorTask;
input HpcOmSimCode.Task iTask; //The parent task
input Integer iThreadIdx; //Thread handling task <%iTaskIdx%>
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
input list<HpcOmSimCode.Task> iLockTasks;
output list<HpcOmSimCode.Task> oLockTasks;
protected
Integer index,threadIdx;
HpcOmSimCode.Task predTask, tmpTask;
list<HpcOmSimCode.Task> tmpLockTasks;
algorithm
oLockTasks := matchcontinue(iPredecessorTask,iTask,iThreadIdx,iCommCosts,iCompTaskMapping,iSimVarMapping,iLockTasks)
case((predTask as HpcOmSimCode.CALCTASK(threadIdx=threadIdx,index=index),_),_,_,_,_,_,_)
equation
true = intNe(iThreadIdx,threadIdx);
//print("Adding a new lock for the tasks " + intString(iTaskIdx) + " " + intString(index) + "\n");
//print("Because task " + intString(index) + " is scheduled to " + intString(threadIdx) + "\n");
tmpTask = createDepTaskAndCommunicationInfo(iTask,predTask,true,iCommCosts,iCompTaskMapping,iSimVarMapping);
tmpLockTasks = tmpTask :: iLockTasks;
then tmpLockTasks;
else iLockTasks;
end matchcontinue;
end getLockTasksByPredecessorListReverse0;
protected function getCommunicationObjBetweenMergedTasks "author: Waurich TUD 2014-11
Gets the communicationCosts between 2 merged tasks. This is the sum of all edges between the 2 nodes."
input Integer parentNode;
input Integer node;
input array<list<Integer>> inComps;
input array<HpcOmTaskGraph.Communications> inCommCosts;
output HpcOmTaskGraph.Communication oCommunication;
protected
list<Integer> nodeTasks, parentTasks;
HpcOmTaskGraph.Communication commFold;
HpcOmTaskGraph.Communications edgesFromParents;
algorithm
nodeTasks := arrayGet(inComps,node);
parentTasks := arrayGet(inComps,parentNode);
commFold := HpcOmTaskGraph.COMMUNICATION(0,{},{},{},{},node,-1.0);
edgesFromParents := List.flatten(List.map1(parentTasks,Array.getIndexFirst,inCommCosts));
oCommunication := List.fold(edgesFromParents,function getCommunicationObjBetweenMergedTasks1(tasks=nodeTasks),commFold);
end getCommunicationObjBetweenMergedTasks;
protected function getCommunicationObjBetweenMergedTasks1 "author: Waurich TUD 2014-11
Sums up the commCosts, for the edges between parent node and the tasks."
input HpcOmTaskGraph.Communication parentCommCost;
input list<Integer> tasks;
input HpcOmTaskGraph.Communication iCommunication;
output HpcOmTaskGraph.Communication oCommunication;
algorithm
oCommunication := matchcontinue(parentCommCost,tasks,iCommunication)
local
Integer nV1,nV2,childNode; //sum of {numOfIntegers,numOfFloats,numOfBoolean, numOfStrings}
list<Integer> ints1,ints2,fl1,fl2,b1,b2,s1,s2;
Real reqT1,reqT2;
case(HpcOmTaskGraph.COMMUNICATION(nV1,ints1,fl1,b1,s1,childNode,reqT1),_,HpcOmTaskGraph.COMMUNICATION(nV2,ints2,fl2,b2,s2,_,reqT2))
equation
true = listMember(childNode,tasks);
then HpcOmTaskGraph.COMMUNICATION(nV1+nV2,listAppend(ints1,ints2),listAppend(fl1,fl2),listAppend(b1,b2),listAppend(s1,s2),childNode,reqT1+reqT2);
else iCommunication;
end matchcontinue;
end getCommunicationObjBetweenMergedTasks1;
protected function convertCommunicationToCommInfo "author: marcusw
Convert the given communication object of hpcomTaskGraph into a simcode-communicationinfo."
input HpcOmTaskGraph.Communication iCommunication;
input array<list<SimCodeVar.SimVar>> iSimVarMapping;
output HpcOmSimCode.CommunicationInfo oCommInfo;
protected
list<Integer> integerVars;
list<Integer> floatVars;
list<Integer> booleanVars;
list<SimCodeVar.SimVar> intSimVars, floatSimVars, boolSimVars;
algorithm
oCommInfo := match(iCommunication,iSimVarMapping)
case(HpcOmTaskGraph.COMMUNICATION(integerVars=integerVars,floatVars=floatVars,booleanVars=booleanVars),_)
equation
intSimVars = List.fold1(integerVars, convertVarIdxToSimVar, iSimVarMapping, {});
floatSimVars = List.fold1(floatVars, convertVarIdxToSimVar, iSimVarMapping, {});
boolSimVars = List.fold1(booleanVars, convertVarIdxToSimVar, iSimVarMapping, {});
then HpcOmSimCode.COMMUNICATION_INFO(floatSimVars,intSimVars,boolSimVars);
end match;
end convertCommunicationToCommInfo;
protected function convertVarIdxToSimVar
input Integer iVarIdx;
input array<list<SimCodeVar.SimVar>> iSimVarMapping;
input list<SimCodeVar.SimVar> iSimVar;
output list<SimCodeVar.SimVar> oSimVar;
protected
list<SimCodeVar.SimVar> tmpSimVars;
algorithm
tmpSimVars := arrayGet(iSimVarMapping, iVarIdx);
oSimVar := listAppend(iSimVar, tmpSimVars);
end convertVarIdxToSimVar;
protected function createDepTask "author: marcusw
Create a dependency task that indicates that variables of another task are required."
input HpcOmSimCode.Task iSourceTask;
input HpcOmSimCode.Task iTargetTask;
input Boolean iOutgoing; //true if lock should released, false if lock should assigned
input HpcOmSimCode.CommunicationInfo commInfo;
output HpcOmSimCode.Task oAssignTask;
algorithm
oAssignTask := HpcOmSimCode.DEPTASK(iSourceTask,iTargetTask,iOutgoing,0,commInfo);
end createDepTask;
protected function createDepTaskAndCommunicationInfo "author: marcusw
Create a dependency task that indicates that variables of another task are required.
The communication info is created out of the given communication array and the simvar-mapping."
input HpcOmSimCode.Task iSourceTask;
input HpcOmSimCode.Task iTargetTask;
input Boolean iOutgoing; //true if lock should released, false if lock should assigned
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
output HpcOmSimCode.Task oAssignTask;
protected
Integer predIndex, taskIndex;
HpcOmSimCode.Task tmpTask;
HpcOmTaskGraph.Communication commBetweenTasks;
HpcOmSimCode.CommunicationInfo commInfo;
algorithm
oAssignTask := matchcontinue(iSourceTask,iTargetTask,iOutgoing,iCommCosts,iCompTaskMapping,iSimVarMapping)
case(HpcOmSimCode.CALCTASK(index=predIndex),HpcOmSimCode.CALCTASK(index=taskIndex),_,_,_,_)
equation
//print("createDepTaskAndCommunicationInfo: Creating dependency (outgoing=" + boolString(iOutgoing) + ") between " + intString(predIndex) + " and " + intString(taskIndex) + "\n");
commBetweenTasks = getCommunicationObjBetweenMergedTasks(predIndex,taskIndex,iCompTaskMapping,iCommCosts);
commInfo = convertCommunicationToCommInfo(commBetweenTasks, iSimVarMapping);
tmpTask = createDepTask(iSourceTask, iTargetTask, iOutgoing, commInfo);
then tmpTask;
/*case(HpcOmSimCode.CALCTASK(index=predIndex),HpcOmSimCode.CALCTASK(index=taskIndex),false,_,_,_)
equation
//print("predIndex"+intString(predIndex)+"\n");
//print("taskIndex"+intString(taskIndex)+"\n");
commBetweenTasks = getCommunicationObjBetweenMergedTasks(predIndex,taskIndex,iCompTaskMapping,iCommCosts);
commInfo = convertCommunicationToCommInfo(commBetweenTasks, iSimVarMapping);
tmpTask = createDepTask(iSourceTask, iTargetTask, false, commInfo);
then tmpTask; */
else
equation
print("CreateDepTaskAndCommunicationInfo failed!\n");
then fail();
end matchcontinue;
end createDepTaskAndCommunicationInfo;
protected function createDepTaskByTaskIdc "author: marcusw
Create a dependency task that indicates that variables of another task are required or calculated. The
source and target tasks are taken from the all-tasks-array."
input Integer iSourceTaskIdx;
input Integer iTargetTaskIdx;
input array<tuple<HpcOmSimCode.Task,Integer>> iAllCalcTasks; //all tasks with ref counter
input Boolean iOutgoing; //true if lock should released, false if lock should assigned
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
output HpcOmSimCode.Task oAssignTask;
protected
HpcOmSimCode.Task sourceTask, targetTask;
algorithm
sourceTask := Util.tuple21(arrayGet(iAllCalcTasks, iSourceTaskIdx));
targetTask := Util.tuple21(arrayGet(iAllCalcTasks, iTargetTaskIdx));
oAssignTask := createDepTaskAndCommunicationInfo(sourceTask, targetTask, iOutgoing, iCommCosts, iCompTaskMapping, iSimVarMapping);
end createDepTaskByTaskIdc;
protected function createDepTaskByTaskIdcR "author: marcusw
Create a dependency task that indicates that variables of another task are required or calculated. The
source and target tasks are taken from the all-tasks-array. Additionally, this
is the revered edition of createOutgoingDummyDepTask, which means that the dependency is
leading from target to source."
input Integer iSourceTaskIdx;
input Integer iTargetTaskIdx;
input array<tuple<HpcOmSimCode.Task,Integer>> iAllCalcTasks; //all tasks with ref counter
input Boolean iOutgoing; //true if lock should released, false if lock should assigned
input array<HpcOmTaskGraph.Communications> iCommCosts;
input array<list<Integer>> iCompTaskMapping; //all StrongComponents from the BLT that belong to the Nodes [nodeId = arrayIdx]
input array<list<SimCodeVar.SimVar>> iSimVarMapping; //Maps each backend var to a list of simVars
output HpcOmSimCode.Task oAssignTask;
algorithm
oAssignTask := createDepTaskByTaskIdc(iTargetTaskIdx,iSourceTaskIdx,iAllCalcTasks,iOutgoing,iCommCosts,iCompTaskMapping,iSimVarMapping);
end createDepTaskByTaskIdcR;
protected function updateRefCounterBySuccessorIdc "author: marcusw
Decrement the ref-counter off all tasks in the successor-list. If the new ref-counter is 0, the task
will be appended to the second return argument."
input array<tuple<HpcOmSimCode.Task,Integer>> iAllCalcTasks; //all tasks with ref-counter
input list<Integer> iSuccessorIdc;
input list<HpcOmSimCode.Task> iRefZeroTasks;
output array<tuple<HpcOmSimCode.Task,Integer>> oAllCalcTasks;
output list<HpcOmSimCode.Task> oRefZeroTasks; //Tasks with new ref-counter = 0
protected
Integer head, currentRefCount;
list<Integer> rest;
list<HpcOmSimCode.Task> tmpRefZeroTasks;
HpcOmSimCode.Task currentTask;
array<tuple<HpcOmSimCode.Task,Integer>> tmpAllCalcTasks;
algorithm
(oAllCalcTasks,oRefZeroTasks) := matchcontinue(iAllCalcTasks,iSuccessorIdc,iRefZeroTasks)
case(_,head::rest,_)
equation
((currentTask,currentRefCount)) = arrayGet(iAllCalcTasks,head);
//print("\tTask " + intString(head) + " has ref-counter of " + intString(currentRefCount) + "\n");
true = intEq(currentRefCount,1); //Task-refcounter = 0
tmpAllCalcTasks = arrayUpdate(iAllCalcTasks,head,(currentTask,0));
tmpRefZeroTasks = currentTask :: iRefZeroTasks;
(tmpAllCalcTasks,tmpRefZeroTasks) = updateRefCounterBySuccessorIdc(tmpAllCalcTasks,rest,tmpRefZeroTasks);
then (tmpAllCalcTasks,tmpRefZeroTasks);
case(_,head::rest,_)
equation
((currentTask,currentRefCount)) = arrayGet(iAllCalcTasks,head); //Task-refcounter != 0
tmpAllCalcTasks = arrayUpdate(iAllCalcTasks,head,(currentTask,currentRefCount-1));
(tmpAllCalcTasks,tmpRefZeroTasks) = updateRefCounterBySuccessorIdc(tmpAllCalcTasks,rest,iRefZeroTasks);
then (tmpAllCalcTasks,tmpRefZeroTasks);
else (iAllCalcTasks,iRefZeroTasks);
end matchcontinue;
end updateRefCounterBySuccessorIdc;
protected function getThreadFinishTimesMin
input Integer iThreadIdx;
input array<Real> iThreadFinishTimes;
input Integer iCurrentMinThreadIdx;
input Real iCurrentMinFinishTime;
output tuple<Integer,Real> minThreadTime_Idx;
protected
Real threadFinishTime;
algorithm
minThreadTime_Idx := matchcontinue(iThreadIdx,iThreadFinishTimes,iCurrentMinThreadIdx,iCurrentMinFinishTime)
case(_,_,_,_)
equation
true = intGt(iThreadIdx,arrayLength(iThreadFinishTimes));
then ((iCurrentMinThreadIdx, iCurrentMinFinishTime));
case(_,_,_,_)
equation
threadFinishTime = arrayGet(iThreadFinishTimes, iThreadIdx);
true = realLt(threadFinishTime,iCurrentMinFinishTime) or intEq(iCurrentMinThreadIdx,-1);
then getThreadFinishTimesMin(iThreadIdx+1,iThreadFinishTimes, iThreadIdx, threadFinishTime);
else getThreadFinishTimesMin(iThreadIdx+1,iThreadFinishTimes, iCurrentMinThreadIdx, iCurrentMinFinishTime);
end matchcontinue;
end getThreadFinishTimesMin;
protected function getTaskWithHighestFinishTime "author: marcusw
Pick the task with the highest finish time out of the given task list."
input list<tuple<HpcOmSimCode.Task,Integer>> iTasks; //Tasks with ref-counter
input Option<HpcOmSimCode.Task> iCurrentTask;
output HpcOmSimCode.Task oTask; //The task with the highest finish time
protected
HpcOmSimCode.Task head;
HpcOmSimCode.Task tmpTask;
list<tuple<HpcOmSimCode.Task,Integer>> tail;
Real timeFinishedHead, timeFinishedCurrent;
algorithm
oTask := matchcontinue(iTasks, iCurrentTask)
case((head,_)::tail, NONE()) then getTaskWithHighestFinishTime(tail, SOME(head));
case(((head as HpcOmSimCode.CALCTASK(timeFinished=timeFinishedHead)),_)::tail, SOME(HpcOmSimCode.CALCTASK(timeFinished=timeFinishedCurrent)))
equation
true = realGt(timeFinishedHead, timeFinishedCurrent);
then getTaskWithHighestFinishTime(tail, SOME(head));
case((head,_)::tail, SOME(_)) then getTaskWithHighestFinishTime(tail, iCurrentTask);
case({},SOME(tmpTask)) then tmpTask;
else
equation
print("HpcOmScheduler.getTaskWithHighestFinishTime failed!\n");
then fail();
end matchcontinue;
end getTaskWithHighestFinishTime;
protected function convertTaskGraphToTasks "author: marcusw
Convert all tasks of the taskGraph-Structure to HpcOmScheduler.Tasks"
input HpcOmTaskGraph.TaskGraph iTaskGraphT;
input HpcOmTaskGraph.TaskGraphMeta iTaskGraphMeta;
input FuncType iConverterFunc; //Pointer to function which converts one Task
output array<tuple<HpcOmSimCode.Task,Integer>> oTasks; //all Tasks with ref_Counter
partial function FuncType
input Integer iNodeIdx;
input HpcOmTaskGraph.TaskGraphMeta iTaskGraphMeta;
output HpcOmSimCode.Task oTask;
end FuncType;
protected
array<tuple<HpcOmSimCode.Task,Integer>> tmpTaskArray;
array<list<Integer>> inComps;
algorithm
//HpcOmTaskGraph.TASKGRAPHMETA(inComps=inComps) := iTaskGraphMeta;
tmpTaskArray := arrayCreate(arrayLength(iTaskGraphT), ((HpcOmSimCode.TASKEMPTY(),0)));