Skip to content

Commit

Permalink
- redo changes of rev22212, but this time without circular dependency
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@22234 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Marcus Walther committed Sep 10, 2014
1 parent 5543876 commit 57bfe73
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 135 deletions.
109 changes: 99 additions & 10 deletions Compiler/BackEnd/HpcOmScheduler.mo
Expand Up @@ -1097,10 +1097,11 @@ algorithm
end match;
end convertScheduleStrucToInfo1;

protected function convertScheduleStrucToInfoLevel
protected function convertScheduleStrucToInfoLevel "author: marcusw
Convert the given task list, representing a level of the level-scheduler, to a scheduler info."
input list<HpcOmSimCode.TaskList> taskLst;
input Integer sectionsNumber;
input array<tuple<Integer,Integer,Real>> iScheduleInfo;
input array<tuple<Integer,Integer,Real>> iScheduleInfo; //maps each Task to <threadId, orderId, startCalcTime>
output array<tuple<Integer,Integer,Real>> oScheduleInfo;
algorithm
oScheduleInfo := matchcontinue(taskLst,sectionsNumber,iScheduleInfo)
Expand Down Expand Up @@ -1129,22 +1130,24 @@ protected function convertScheduleStrucToInfoLevel1
input list<HpcOmSimCode.Task> tasks;
input Integer sectionsNumber;
input Integer sectionIdx;
input array<tuple<Integer,Integer,Real>> iScheduleInfo;
input array<tuple<Integer,Integer,Real>> iScheduleInfo; //maps each Task to <threadId, orderId, startCalcTime>
output array<tuple<Integer,Integer,Real>> oScheduleInfo;
algorithm
oScheduleInfo := matchcontinue(tasks,sectionsNumber,sectionIdx,iScheduleInfo)
local
Integer numNodes;
Integer numNodes, threadIdx;
list<Integer> nodeIdc;
array<tuple<Integer,Integer,Real>> scheduleInfo;
list<tuple<Integer,Integer,Real>> tuplLst;
list<HpcOmSimCode.Task> rest;
Option<Integer> threadIdxOpt;
case({},_,_,_)
then iScheduleInfo;
case(HpcOmSimCode.CALCTASK_LEVEL(nodeIdc=nodeIdc)::rest,_,_,_)
case(HpcOmSimCode.CALCTASK_LEVEL(nodeIdc=nodeIdc,threadIdx=threadIdxOpt)::rest,_,_,_)
equation
numNodes = listLength(nodeIdc);
tuplLst = List.threadMap1(List.fill(sectionsNumber,numNodes),List.fill(sectionIdx,numNodes),Util.make3Tuple,0.0);
threadIdx = Util.getOptionOrDefault(threadIdxOpt,-1);
tuplLst = List.threadMap1(List.fill(threadIdx,numNodes),List.fill(-1,numNodes),Util.make3Tuple,0.0);
List.threadMap1_0(nodeIdc,tuplLst,Util.arrayUpdateIndexFirst,iScheduleInfo);
then convertScheduleStrucToInfoLevel1(rest,sectionsNumber,sectionIdx+1,iScheduleInfo);
end matchcontinue;
Expand Down Expand Up @@ -1680,22 +1683,25 @@ public function createFixedLevelSchedule
output HpcOmTaskGraph.TaskGraphMeta oMeta;
protected
list<list<Integer>> levelTasks;
//for each node (arrayIdx): list of threads that handle predecessor tasks. If a node has multiple
//predecessors handled by the same thread, the thread-index occurs multiple times in the list.
array<list<Integer>> adviceLists;
HpcOmSimCode.Schedule tmpSchedule;
list<HpcOmSimCode.TaskList> levelTaskLists;
algorithm
// 1. Create a task list for each thread and a advice list for each task which is empty at beginning
// 2. Iterate over all levels
// 2.1. Create an ready-list and set all values to 0
// 2.1. Create an thread-ready-list and set all values to 0
// 2.2. Iterate over all tasks of the current level
// 2.2.1. Find the thread that should calulcate the task
// 2.2.1. (1) This could be the thread with the lowest value in the ready list if no task is in the advice list
// 2.2.1. (1) This could be the thread with the lowest value in the ready list if no thread is in the advice list
// 2.2.1. (2) This could be the first thread in the advice list, if the thread has not already an execution time > (sum(exec(levelTasks)) / numThreads)
// 2.2.1. (3) Otherwise case (1)
// 2.2.2. Append the task to the thread and add the execution cost the the ready list
// 2.2.3. Add the thread to the advice-list of all successor tasks
levelTasks := HpcOmTaskGraph.getLevelNodes(iGraph);
adviceLists := arrayCreate(arrayLength(iGraph), {});
HpcOmTaskGraph.printTaskGraph(iGraph);
levelTaskLists := List.fold5(levelTasks, createFixedLevelScheduleForLevel, adviceLists, iGraph, iMeta, iNumberOfThreads, iSccSimEqMapping, {});
levelTaskLists := listReverse(levelTaskLists);
oSchedule := HpcOmSimCode.LEVELSCHEDULE(levelTaskLists);
Expand All @@ -1722,6 +1728,7 @@ protected
list<HpcOmSimCode.Task> tasksOfLevel;
array<list<Integer>> inComps;
algorithm
print("createFixedLevelScheduleForLevel: entering next level\n");
HpcOmTaskGraph.TASKGRAPHMETA(exeCosts=exeCosts,inComps=inComps) := iMeta;
levelExecCosts := HpcOmTaskGraph.getCostsForContractedNodes(iTasksOfLevel, exeCosts);
threadReadyList := arrayCreate(iNumberOfThreads, 0.0);
Expand All @@ -1731,6 +1738,7 @@ algorithm
((_,tasksOfLevel)) := Util.arrayFold2(threadTaskList, createFixedLevelScheduleForLevel0, inComps, iSccSimEqMapping, (1,{}));
taskList := HpcOmSimCode.PARALLELTASKLIST(tasksOfLevel);
oLevelTaskLists := taskList :: iLevelTaskLists;
print("createFixedLevelScheduleForLevel: leaving level\n");
end createFixedLevelScheduleForLevel;

protected function createFixedLevelScheduleForLevel0
Expand Down Expand Up @@ -1768,10 +1776,16 @@ protected
Integer threadIdx;
Real threadReadyTime, exeCost;
algorithm
print("\tcreateFixedLevelScheduleForTask: handling task: " +& intString(iTaskIdx) +& "\n");
adviceElem := arrayGet(iAdviceList, iTaskIdx);
print("\t\tAdvice-list: " +& stringDelimitList(List.map(adviceElem, intString), ",") +& "\n");
adviceElem := flattenAdviceList(adviceElem, arrayLength(iThreadReadyList));
print("\t\tAdvice-list-flattened: " +& stringDelimitList(List.map(adviceElem, intString), ",") +& "\n");
threadIdx := getBestFittingThread(adviceElem, iLevelExecCosts, iThreadReadyList);
print("\t\tBest-thread: " +& intString(threadIdx) +& "\n");
threadTasks := arrayGet(iThreadTasks, threadIdx);
successorList := arrayGet(iGraph, threadIdx);
successorList := arrayGet(iGraph, iTaskIdx);
print("\t\tSuccessors: " +& stringDelimitList(List.map(successorList, intString), ",") +& "\n");
//update the advice list
_ := List.fold1(successorList, createFixedLevelScheduleForTask0, threadIdx, iAdviceList);
threadReadyTime := arrayGet(iThreadReadyList, threadIdx);
Expand All @@ -1785,6 +1799,8 @@ algorithm
end createFixedLevelScheduleForTask;

protected function createFixedLevelScheduleForTask0
"author: marcusw
Update the given advice list, by adding the iThreadAdvice to the successor-task-entry."
input Integer iSuccessor;
input Integer iThreadAdvice;
input array<list<Integer>> iAdviceList;
Expand All @@ -1797,8 +1813,81 @@ algorithm
oAdviceList := arrayUpdate(iAdviceList, iSuccessor, adviceElem);
end createFixedLevelScheduleForTask0;

protected function getBestFittingThread
protected function flattenAdviceList
"author: marcusw
Flatten the given advice list and order the entries regarding their occurency count.
For example: {2,3,1,1,2,2} -> {2,1,3}"
input list<Integer> iAdviceList;
input Integer iNumOfThreads;
output list<Integer> oAdviceList;
protected
array<Integer> counterArray;
list<tuple<Integer,Integer>> tupleList;
algorithm
counterArray := arrayCreate(iNumOfThreads,0);
counterArray := List.fold(iAdviceList, flattenAdviceListElem, counterArray);
tupleList := arrayToTupleListZeroRemoved(counterArray, 1, {});
oAdviceList := List.map(List.sort(tupleList, intTpl22Gt),Util.tuple21);
end flattenAdviceList;

protected function flattenAdviceListElem "author: marcusw
Increment the value in the counter array of the given thread (iAdviceElem)."
input Integer iAdviceElem;
input array<Integer> iCounterArray;
output array<Integer> oCounterArray;
protected
Integer counter;
algorithm
counter := arrayGet(iCounterArray, iAdviceElem);
counter := counter + 1;
oCounterArray := arrayUpdate(iCounterArray, iAdviceElem, counter);
end flattenAdviceListElem;

protected function arrayToTupleListZeroRemoved "author: marcusw
Convert a integer array, to list of tuples <arrayIndex, value> if the value is != 0.
For example: [1,4,2] -> {<1,1>,<2,4>,<3,2>}"
input array<Integer> iArray;
input Integer iCurrentIdx;
input list<tuple<Integer,Integer>> iTupleList;
output list<tuple<Integer,Integer>> oTupleList;
protected
list<tuple<Integer,Integer>> tmpTupleList;
Integer currentValue;
algorithm
oTupleList := matchcontinue(iArray, iCurrentIdx, iTupleList)
case(_,_,_)
equation
true = intLe(iCurrentIdx, arrayLength(iArray));
currentValue = arrayGet(iArray, iCurrentIdx);
true = intNe(currentValue, 0);
tmpTupleList = (iCurrentIdx, currentValue)::iTupleList;
tmpTupleList = arrayToTupleListZeroRemoved(iArray, iCurrentIdx+1, tmpTupleList);
then tmpTupleList;
case(_,_,_)
equation
true = intLe(iCurrentIdx, arrayLength(iArray));
tmpTupleList = arrayToTupleListZeroRemoved(iArray, iCurrentIdx+1, iTupleList);
then tmpTupleList;
else then iTupleList;
end matchcontinue;
end arrayToTupleListZeroRemoved;

protected function intTpl22Gt
input tuple<Integer,Integer> iTpl1;
input tuple<Integer,Integer> iTpl2;
output Boolean oRes;
protected
Integer val1, val2;
algorithm
(_,val1) := iTpl1;
(_,val2) := iTpl2;
oRes := intGt(val1, val2);
end intTpl22Gt;

protected function getBestFittingThread
"author: marcusw
Get the optimal thread for the task, regarding the given advice list."
input list<Integer> iAdviceList; //advice list of the task - the list is traversed from front to back, until a suitable thread is found
input Real iLevelExecCosts; //sum of all execosts
input array<Real> iThreadReadyList;
output Integer oThreadIdx;
Expand Down
1 change: 1 addition & 0 deletions Compiler/BackEnd/HpcOmSimCode.mo
Expand Up @@ -32,6 +32,7 @@
encapsulated package HpcOmSimCode

public import HashTableCrILst;
//public import SimCode;

public uniontype MemoryMap //stores information to organize the memory for the parallel code in an efficient way
record MEMORYMAP_ARRAY
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/HpcOmSimCodeMain.mo
Expand Up @@ -161,7 +161,7 @@ algorithm

Boolean taskGraphMetaValid, numFixed;
String taskGraphMetaMessage, criticalPathInfo;
array<tuple<Integer,Integer,Real>> schedulerInfo;
array<tuple<Integer,Integer,Real>> schedulerInfo; //maps each Task to <threadId, orderId, startCalcTime>
HpcOmSimCode.Schedule schedule;
array<tuple<Integer,Integer,Integer>> eqCompMapping, varCompMapping;
Real graphCosts;
Expand Down Expand Up @@ -331,7 +331,7 @@ algorithm
SimCodeUtil.execStat("hpcom create schedule");

fileName = ("taskGraph"+&filenamePrefix+&"ODE_schedule.graphml");
HpcOmTaskGraph.dumpAsGraphMLSccLevel(taskGraphScheduled, taskGraphDataScheduled, inBackendDAE, fileName, criticalPathInfo, HpcOmTaskGraph.convertNodeListToEdgeTuples(List.first(criticalPaths)), HpcOmTaskGraph.convertNodeListToEdgeTuples(List.first(criticalPathsWoC)), sccSimEqMapping, schedulerInfo, HpcOmTaskGraph.GRAPHDUMPOPTIONS(true,true,true,true));
HpcOmTaskGraph.dumpAsGraphMLSccLevel(taskGraphScheduled, taskGraphDataScheduled, inBackendDAE, fileName, criticalPathInfo, HpcOmTaskGraph.convertNodeListToEdgeTuples(List.first(criticalPaths)), HpcOmTaskGraph.convertNodeListToEdgeTuples(List.first(criticalPathsWoC)), sccSimEqMapping, schedulerInfo, HpcOmTaskGraph.GRAPHDUMPOPTIONS(true,false,true,true));
//HpcOmScheduler.printSchedule(schedule);

SimCodeUtil.execStat("hpcom dump schedule TaskGraph");
Expand Down
57 changes: 33 additions & 24 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -1269,28 +1269,28 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__)) then
#include <Core/ModelicaDefine.h>
#include <SimCoreFactory/Policies/FactoryConfig.h>
#include <SimController/ISimController.h>
<% if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then '#include "Core/Utils/extension/measure_time.hpp"' %>
<% if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then '#include "Core/Utils/extension/measure_time_rdtsc.hpp"' %>
#if defined(_MSC_VER) || defined(__MINGW32__)
#include <tchar.h>
int _tmain(int argc, const _TCHAR* argv[])
#else
int main(int argc, const char* argv[])
#endif
{
<% if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then 'RDTSC_MeasureTime::initialize();' %>
<% if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then 'MeasureTimeRDTSC::initialize();' %>
try
{
boost::shared_ptr<OMCFactory> _factory = boost::shared_ptr<OMCFactory>(new OMCFactory());
boost::shared_ptr<OMCFactory> _factory = boost::shared_ptr<OMCFactory>(new OMCFactory());
//SimController to start simulation
std::pair<boost::shared_ptr<ISimController>,SimSettings> simulation = _factory->createSimulation(argc,argv);
//create Modelica system
//create Modelica system
std::pair<boost::shared_ptr<IMixedSystem>,boost::shared_ptr<ISimData> > system = simulation.first->LoadSystem("OMCpp<%fileNamePrefix%><%makefileParams.dllext%>","<%lastIdentOfPath(modelInfo.name)%>");
simulation.first->Start(system.first,simulation.second,"<%lastIdentOfPath(modelInfo.name)%>");
<% if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then '//RDTSC_MeasureTime::deinitialize();' %>
return 0;
<% if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then '//MeasureTimeRDTSC::deinitialize();' %>
return 0;
}
catch(std::exception& ex)
Expand Down Expand Up @@ -1651,12 +1651,14 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
>>
%>
<%if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then
let numOfEqs = SimCodeUtil.getMaxSimEqSystemIndex(simCode)
<<
MeasureTime::data ref;
ref.max_time = 0;
ref.num_calcs = 0;
ref.sum_time = 0;
measureTimeArray = std::vector<MeasureTime::data>(<%intAdd(listLength(simCode.allEquations), listLength(simCode.initialEquations))%>,ref);
measureTimeArray = std::vector<MeasureTimeData>(<%numOfEqs%>);
measuredStartValues = MeasureTime::getZeroValues();
measuredEndValues = MeasureTime::getZeroValues();
for(int i = 0; i < <%numOfEqs%>; i++)
measureTimeArray[i] = MeasureTimeData();
>>
%>
//DAE's are not supported yet, Index reduction is enabled
Expand All @@ -1681,9 +1683,9 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
<%if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then
<<
MeasureTime::getInstance()->writeTimeToJason("<%dotPath(modelInfo.name)%>",measureTimeArray);
MeasureTime::getInstance()->writeTimeToJason("<%dotPath(modelInfo.name)%>",measureTimeArray);
>>
%>
%>
}
Expand Down Expand Up @@ -4284,7 +4286,11 @@ match modelInfo
boost::shared_ptr<ISimData> _simData;
<% if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then 'std::vector<MeasureTime::data> measureTimeArray;' %>
<% if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then
<<
std::vector<MeasureTimeData> measureTimeArray;
MeasureTimeValues *measuredStartValues, *measuredEndValues, *measuredOverheadValues;
>>%>
<%memberfuncs%>
Expand Down Expand Up @@ -7278,20 +7284,23 @@ end equation_function_call;
template measureTimeStart(String eq_number)
::=
<<
long long unsigned startMeasure_t, endMeasure_t,measure_t;
startMeasure_t = MeasureTime::getTime();
MeasureTime::getTimeValues(measuredStartValues);
for(int i = 0; i < 10; i++) {
>>
end measureTimeStart;
template measureTimeStop(String eq_number)
::=
let eqNumber = intSub(stringInt(eq_number),1)
<<
endMeasure_t = MeasureTime::getTime();
measure_t = endMeasure_t - startMeasure_t;
if(measure_t > measureTimeArray[<%eqNumber%>].max_time) measureTimeArray[<%eqNumber%>].max_time = measure_t;
measureTimeArray[<%eqNumber%>].sum_time += measure_t;
++(measureTimeArray[<%eqNumber%>].num_calcs);
}
MeasureTime::getTimeValues(measuredEndValues);
measuredEndValues->sub(measuredStartValues);
measuredEndValues->div(10);
//measuredEndValues->sub(MeasureTime::getOverhead());
//if(measure_t > measureTimeArray[<%eqNumber%>].max_time) measureTimeArray[<%eqNumber%>].max_time = measuredEndValues;
(measureTimeArray[<%eqNumber%>].sumMeasuredValues)->add(measuredEndValues);
++(measureTimeArray[<%eqNumber%>].numCalcs);
>>
end measureTimeStop;

Expand Down Expand Up @@ -7339,10 +7348,10 @@ template equation_function_create_single_func(SimEqSystem eq, Context context, S
*/
void <%lastIdentOfPathFromSimCode(simCode)%><%classnameext%>::<%method%>_<%ix_str%>()
{
<%measureTimeStartVar%>
<%varDeclsLocal%>
<%body%>
<%measureTimeEndVar%>
<%measureTimeStartVar%>
<%body%>
<%measureTimeEndVar%>
}
>>

Expand Down
11 changes: 4 additions & 7 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -835,6 +835,10 @@ package SimCodeUtil
output Integer outVariableIndex;
end getVariableIndex;

function getMaxSimEqSystemIndex
input SimCode.SimCode simCode;
output Integer idxOut;
end getMaxSimEqSystemIndex;
end SimCodeUtil;


Expand Down Expand Up @@ -1129,9 +1133,6 @@ package DAE
record BCONST
Boolean bool;
end BCONST;
record CLKCONST
Absyn.ClockKind clk;
end CLKCONST;
record ENUM_LITERAL
Absyn.Path name;
Integer index;
Expand Down Expand Up @@ -3110,10 +3111,6 @@ package Types
input list<DAE.Exp> inDimensionLst;
output DAE.Type outType;
end liftArrayListExp;
function unliftArray
input DAE.Type inType;
output DAE.Type outType;
end unliftArray;
end Types;

package FMI
Expand Down
7 changes: 4 additions & 3 deletions SimulationRuntime/cpp/Core/Utils/extension/CMakeLists.txt
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 2.6)

project(${ExtensionUtilitiesName})

add_library(${ExtensionUtilitiesName} SHARED measure_time.cpp)
add_library(${ExtensionUtilitiesName} SHARED measure_time.cpp measure_time_rdtsc.cpp)
target_link_libraries (${ExtensionUtilitiesName} ${Boost_LIBRARIES})

if (UNIX)
Expand All @@ -11,5 +11,6 @@ endif(UNIX)

install (TARGETS ${ExtensionUtilitiesName} DESTINATION lib/omc/${LIBINSTALLEXT})

install (FILES ${CMAKE_SOURCE_DIR}/Include/Core/Utils/extension/measure_time.hpp
DESTINATION include/omc/cpp/Core/Utils/extension)
install (FILES ${CMAKE_SOURCE_DIR}/Include/Core/Utils/extension/measure_time.hpp
${CMAKE_SOURCE_DIR}/Include/Core/Utils/extension/measure_time_rdtsc.hpp
DESTINATION include/omc/cpp/Core/Utils/extension)

0 comments on commit 57bfe73

Please sign in to comment.