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

Parallelize DynActivityEngine.doSimStep() #3306

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.IdCollectors;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.population.Person;
import org.matsim.contrib.dynagent.DynAgent;
Expand Down Expand Up @@ -56,13 +59,18 @@ public void doSimStep(double time) {
dynAgents.addAll(newDynAgents);
newDynAgents.clear();

// computing end times is the heaviest part here and can be parallelized
// Todo: use forkJoinPool and respect globalThreads property
Map<Id<Person>, Double> activityEndTimes = dynAgents.parallelStream()
.collect(Collectors.toMap(MobsimAgent::getId, MobsimAgent::getActivityEndTime));

Iterator<DynAgent> dynAgentIter = dynAgents.iterator();
while (dynAgentIter.hasNext()) {
DynAgent agent = dynAgentIter.next();
Preconditions.checkState(agent.getState() == State.ACTIVITY);
agent.doSimStep(time);
// ask agents about the current activity end time;
double currentEndTime = agent.getActivityEndTime();
double currentEndTime = activityEndTimes.get(agent.getId());

if (currentEndTime == Double.POSITIVE_INFINITY) { // agent says: stop simulating me
unregisterAgentAtActivityLocation(agent);
Expand Down
Loading