Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash in clustered fork-join algorithm #2219

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion HopsanCore/include/ComponentSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace hopsan {
bool initialize(const double startT, const double stopT);
void simulate(const double stopT);
bool startRealtimeSimulation(double realTimeFactor=1);
virtual void simulateMultiThreaded(const double startT, const double stopT, const size_t nDesiredThreads = 0, const bool noChanges=false, ParallelAlgorithmT algorithm=OfflineSchedulingAlgorithm);
virtual void simulateMultiThreaded(const double startT, const double stopT, const size_t nDesiredThreads = 0, const bool noChanges=false, ParallelAlgorithmT algorithm=APrioriScheduling);
void finalize();

bool simulateAndMeasureTime(const size_t nSteps);
Expand Down
10 changes: 5 additions & 5 deletions HopsanCore/include/CoreUtilities/SimulationHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@

namespace hopsan {

enum ParallelAlgorithmT {OfflineSchedulingAlgorithm,
enum ParallelAlgorithmT {APrioriScheduling,
TaskPoolAlgorithm,
TaskStealingAlgorithm,
ParallelForAlgorithm,
GroupedParallelForAlgorithm};
ForkJoinAlgorithm,
ClusteredForkJoinAlgorithm};

// Forward declaration
class ComponentSystem;
Expand All @@ -59,8 +59,8 @@ class HOPSANCORE_DLLAPI SimulationHandler
bool initializeSystem(const double startT, const double stopT, ComponentSystem* pSystem);
bool initializeSystem(const double startT, const double stopT, std::vector<ComponentSystem*> &rSystemVector);

bool simulateSystem(const double startT, const double stopT, const int nDesiredThreads, ComponentSystem* pSystem, bool noChanges=false, ParallelAlgorithmT algorithm=OfflineSchedulingAlgorithm);
bool simulateSystem(const double startT, const double stopT, const int nDesiredThreads, std::vector<ComponentSystem*> &rSystemVector, bool noChanges=false, ParallelAlgorithmT algorithm=OfflineSchedulingAlgorithm);
bool simulateSystem(const double startT, const double stopT, const int nDesiredThreads, ComponentSystem* pSystem, bool noChanges=false, ParallelAlgorithmT algorithm=APrioriScheduling);
bool simulateSystem(const double startT, const double stopT, const int nDesiredThreads, std::vector<ComponentSystem*> &rSystemVector, bool noChanges=false, ParallelAlgorithmT algorithm=APrioriScheduling);

bool startRealtimeSimulation(ComponentSystem *pSystem, double realtimeFactor=1);
void stopRealtimeSimulation(ComponentSystem *pSystem);
Expand Down
26 changes: 13 additions & 13 deletions HopsanCore/src/ComponentSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2470,9 +2470,9 @@ void ComponentSystem::simulateMultiThreaded(const double startT, const double st
size_t nSteps = calcNumSimSteps(startT, stopT);

//Execute simulation
if(algorithm == OfflineSchedulingAlgorithm)
if(algorithm == APrioriScheduling)
{
addInfoMessage("Using offline scheduling algorithm with "+threadStr+" threads.");
addInfoMessage("Using a priori scheduling algorithm with "+threadStr+" threads.");

mpMultiThreadPrivates->mvTimePtrs.push_back(&mTime);
BarrierLock *pBarrierLock_S = new BarrierLock(nThreads); //Create synchronization barriers
Expand Down Expand Up @@ -2611,7 +2611,7 @@ void ComponentSystem::simulateMultiThreaded(const double startT, const double st
}
else if(algorithm == TaskStealingAlgorithm)
{
addInfoMessage("Using task stealing algorithm with "+threadStr+" threads.");
addInfoMessage("Using task-stealing algorithm with "+threadStr+" threads.");

mpMultiThreadPrivates->mvTimePtrs.push_back(&mTime);
BarrierLock *pBarrierLock_S = new BarrierLock(nThreads); //Create synchronization barriers
Expand Down Expand Up @@ -2684,9 +2684,9 @@ void ComponentSystem::simulateMultiThreaded(const double startT, const double st
delete(pVectorsC);
delete(pVectorsQ);
}
else if(algorithm == ParallelForAlgorithm)
else if(algorithm == ForkJoinAlgorithm)
{
addInfoMessage("Using parallel for-loop algorithm 1 with unlimited number of threads.");
addInfoMessage("Using fork-join algorithm with unlimited number of threads.");

// Round to nearest, we may not get exactly the stop time that we want
size_t numSimulationSteps = calcNumSimSteps(mTime, stopT); //Here mTime is the last time step since it is not updated yet
Expand Down Expand Up @@ -2743,9 +2743,9 @@ void ComponentSystem::simulateMultiThreaded(const double startT, const double st
logTimeAndNodes(mTotalTakenSimulationSteps);
}
}
else if(algorithm == GroupedParallelForAlgorithm)
else if(algorithm == ClusteredForkJoinAlgorithm)
{
addInfoMessage("Using grouped parallel for loop algorithm with unlimited number of threads.");
addInfoMessage("Using clustered fork-join algorithm with unlimited number of threads.");

// Round to nearest, we may not get exactly the stop time that we want
size_t numSimulationSteps = calcNumSimSteps(mTime, stopT); //Here mTime is the last time step since it is not updated yet
Expand All @@ -2770,28 +2770,28 @@ void ComponentSystem::simulateMultiThreaded(const double startT, const double st
}

//C components
tt = new std::thread[mComponentCptrs.size()];
for (size_t c=0; c < mComponentCptrs.size(); ++c)
tt = new std::thread[mpMultiThreadPrivates->mSplitCVector.size()];
for (size_t c=0; c < mpMultiThreadPrivates->mSplitCVector.size(); ++c)
{
tt[c] = std::thread(simOneStep,
&mpMultiThreadPrivates->mSplitCVector[c],
mTime);
}
for(size_t c=0; c<mComponentCptrs.size(); ++c)
for(size_t c=0; c<mpMultiThreadPrivates->mSplitCVector.size(); ++c)
{
tt[c].join();
}
delete[] tt;

//Q components
tt = new std::thread[mComponentQptrs.size()];
for (size_t q=0; q < mComponentQptrs.size(); ++q)
tt = new std::thread[mpMultiThreadPrivates->mSplitQVector.size()];
for (size_t q=0; q < mpMultiThreadPrivates->mSplitQVector.size(); ++q)
{
tt[q] = std::thread(simOneStep,
&mpMultiThreadPrivates->mSplitQVector[q],
mTime);
}
for(size_t q=0; q<mComponentQptrs.size(); ++q)
for(size_t q=0; q<mpMultiThreadPrivates->mSplitQVector.size(); ++q)
{
tt[q].join();
}
Expand Down
9 changes: 6 additions & 3 deletions HopsanGUI/HcomHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4179,18 +4179,21 @@ void HcomHandler::executeGetCommand(const QString cmd)

switch (getConfigPtr()->getParallelAlgorithm())
{
case hopsan::OfflineSchedulingAlgorithm :
output.append("pre-simulation scheduling");
case hopsan::APrioriScheduling :
output.append("a priori scheduling");
break;
case hopsan::TaskPoolAlgorithm :
output.append("task pool scheduling");
break;
case hopsan::TaskStealingAlgorithm :
output.append("task-stealing");
break;
case hopsan::ParallelForAlgorithm :
case hopsan::ForkJoinAlgorithm :
output.append("fork-join scheduling");
break;
case hopsan::ClusteredForkJoinAlgorithm :
output.append("clustered fork-join scheduling");
break;
default :
output.append("unknown ("+QString::number(getConfigPtr()->getParallelAlgorithm())+")");
break;
Expand Down
Loading