Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSEdgeControl.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
21// Stores edges and lanes, performs moving of vehicle
22/****************************************************************************/
23#include <config.h>
24
25#include <iostream>
26#include <queue>
27#include <vector>
29#include <mesosim/MELoop.h>
30#include "MSEdgeControl.h"
31#include "MSVehicleControl.h"
32#include "MSGlobals.h"
33#include "MSEdge.h"
34#include "MSLane.h"
35#include "MSVehicle.h"
36
37#define PARALLEL_PLAN_MOVE
38#define PARALLEL_EXEC_MOVE
39//#define PARALLEL_CHANGE_LANES
40//#define LOAD_BALANCING
41
42//#define PARALLEL_STOPWATCH
43
44// ===========================================================================
45// member method definitions
46// ===========================================================================
47MSEdgeControl::MSEdgeControl(const std::vector< MSEdge* >& edges)
48 : myEdges(edges),
49 myLanes(MSLane::dictSize()),
50 myWithVehicles2Integrate(MSGlobals::gNumSimThreads > 1),
51 myLastLaneChange(edges.size()),
52 myInactiveCheckCollisions(MSGlobals::gNumSimThreads > 1),
53 myMinLengthGeometryFactor(1.),
54#ifdef THREAD_POOL
55 myThreadPool(false, std::vector<int>(MSGlobals::gNumThreads, 0)),
56#endif
57 myStopWatch(3) {
58 // build the usage definitions for lanes
59 for (MSEdge* const edge : myEdges) {
60 const std::vector<MSLane*>& lanes = edge->getLanes();
61 if (!edge->hasLaneChanger()) {
62 const int pos = lanes.front()->getNumericalID();
63 myLanes[pos].lane = lanes.front();
64 myLanes[pos].amActive = false;
65 myLanes[pos].haveNeighbors = false;
66 myMinLengthGeometryFactor = MIN2(edge->getLengthGeometryFactor(), myMinLengthGeometryFactor);
67 } else {
68 for (MSLane* const l : lanes) {
69 const int pos = l->getNumericalID();
70 myLanes[pos].lane = l;
71 myLanes[pos].amActive = false;
72 myLanes[pos].haveNeighbors = true;
73 myMinLengthGeometryFactor = MIN2(l->getLengthGeometryFactor(), myMinLengthGeometryFactor);
74 }
75 myLastLaneChange[edge->getNumericalID()] = -1;
76 }
77 }
78#ifndef THREAD_POOL
79#ifdef HAVE_FOX
80 if (MSGlobals::gNumThreads > 1) {
81 while (myThreadPool.size() < MSGlobals::gNumThreads) {
82 new WorkerThread(myThreadPool);
83 }
84 }
85#endif
86#endif
87}
88
89
91#ifndef THREAD_POOL
92#ifdef HAVE_FOX
93 myThreadPool.clear();
94#endif
95#endif
96#ifdef PARALLEL_STOPWATCH
98 for (MSEdge* const edge : myEdges) {
99 for (MSLane* const l : edge->getLanes()) {
100 wPlan.add(l->getStopWatch()[0]);
101 }
102 }
103 std::cout << wPlan.getHistory().size() << " lane planmove calls, average " << wPlan.getAverage() << " ns, total " << wPlan.getTotal() / double(1e9) << " s" << std::endl;
104 std::cout << myStopWatch[0].getHistory().size() << " planmove calls, average " << myStopWatch[0].getAverage() << " ns, total " << myStopWatch[0].getTotal() / double(1e9) << " s" << std::endl;
105 std::cout << myStopWatch[1].getHistory().size() << " execmove calls, average " << myStopWatch[1].getAverage() << " ns, total " << myStopWatch[1].getTotal() / double(1e9) << " s" << std::endl;
106#endif
107}
108
109void
110MSEdgeControl::setActiveLanes(std::list<MSLane*> lanes) {
111 myActiveLanes = lanes;
112 for (MSLane* lane : lanes) {
113 myLanes[lane->getNumericalID()].amActive = true;
114 }
115}
116
117void
119 for (std::set<MSLane*, ComparatorNumericalIdLess>::iterator i = myChangedStateLanes.begin(); i != myChangedStateLanes.end(); ++i) {
120 LaneUsage& lu = myLanes[(*i)->getNumericalID()];
121 // if the lane was inactive but is now...
122 if (!lu.amActive && (*i)->getVehicleNumber() > 0) {
123 // ... add to active lanes and mark as such
124 if (lu.haveNeighbors) {
125 myActiveLanes.push_front(*i);
126 } else {
127 myActiveLanes.push_back(*i);
128 }
129 lu.amActive = true;
130 }
131 }
132 myChangedStateLanes.clear();
133}
134
135
136void
138#ifdef PARALLEL_STOPWATCH
139 myStopWatch[0].start();
140#endif
141#ifdef THREAD_POOL
142 std::vector<std::future<void>> results;
143#endif
144 for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
145 const int vehNum = (*i)->getVehicleNumber();
146 if (vehNum == 0) {
147 myLanes[(*i)->getNumericalID()].amActive = false;
148 i = myActiveLanes.erase(i);
149 } else {
150#ifdef THREAD_POOL
152 results.push_back(myThreadPool.executeAsync([i, t](int) {
153 (*i)->planMovements(t);
154 }, (*i)->getRNGIndex() % MSGlobals::gNumSimThreads));
155 ++i;
156 continue;
157 }
158#else
159#ifdef HAVE_FOX
161 myThreadPool.add((*i)->getPlanMoveTask(t), (*i)->getRNGIndex() % myThreadPool.size());
162 ++i;
163 continue;
164 }
165#endif
166#endif
167 (*i)->planMovements(t);
168 ++i;
169 }
170 }
171#ifdef THREAD_POOL
172 for (auto& r : results) {
173 r.wait();
174 }
175#else
176#ifdef HAVE_FOX
178 myThreadPool.waitAll(false);
179 }
180#endif
181#endif
182#ifdef PARALLEL_STOPWATCH
183 myStopWatch[0].stop();
184#endif
185}
186
187
188void
190 // each vehicle only touches its own entries in the (locked) link maps, so the order does not matter
191#ifdef THREAD_POOL
193 std::vector<std::future<void>> results;
194 for (MSLane* const lane : myActiveLanes) {
195 results.push_back(myThreadPool.executeAsync([lane](int) {
196 lane->setJunctionApproaches();
197 }, lane->getRNGIndex() % MSGlobals::gNumSimThreads));
198 }
199 for (auto& r : results) {
200 r.wait();
201 }
202 return;
203 }
204#else
205#ifdef HAVE_FOX
207 for (MSLane* const lane : myActiveLanes) {
208 myThreadPool.add(lane->getJunctionApproachTask(0), lane->getRNGIndex() % myThreadPool.size());
209 }
210 myThreadPool.waitAll(false);
211 return;
212 }
213#endif
214#endif
215 for (MSLane* const lane : myActiveLanes) {
216 lane->setJunctionApproaches();
217 }
218}
219
220
221void
223#ifdef PARALLEL_STOPWATCH
224 myStopWatch[1].start();
225#endif
226 std::vector<MSLane*> wasActive(myActiveLanes.begin(), myActiveLanes.end());
228#ifdef PARALLEL_EXEC_MOVE
229#ifdef THREAD_POOL
231 for (MSLane* const lane : myActiveLanes) {
232 myThreadPool.executeAsync([lane, t](int) {
233 lane->executeMovements(t);
234 }, lane->getRNGIndex() % MSGlobals::gNumSimThreads);
235 }
236 myThreadPool.waitAll();
237 }
238#else
239#ifdef HAVE_FOX
241 for (MSLane* const lane : myActiveLanes) {
242 myThreadPool.add(lane->getExecuteMoveTask(t), lane->getRNGIndex() % myThreadPool.size());
243 }
244 myThreadPool.waitAll(false);
245 }
246#endif
247#endif
248#endif
249 for (std::list<MSLane*>::iterator i = myActiveLanes.begin(); i != myActiveLanes.end();) {
250 if (
253#endif
254 (*i)->getVehicleNumber() > 0) {
255 (*i)->executeMovements(t);
256 }
257 if ((*i)->getVehicleNumber() == 0) {
258 myLanes[(*i)->getNumericalID()].amActive = false;
259 i = myActiveLanes.erase(i);
260 } else {
261 ++i;
262 }
263 }
264 for (MSLane* lane : wasActive) {
265 lane->updateLengthSum();
266 }
267 // arrived vehicles should not influence lane changing
269 std::vector<MSLane*>& toIntegrate = myWithVehicles2Integrate.getContainer();
270 std::sort(toIntegrate.begin(), toIntegrate.end(), ComparatorIdLess());
272 //This should disappear when parallelization is working. Until then it would
273 //be better to use ComparatorNumericalIdLess instead of ComparatorIdLess
275 for (MSLane* const lane : toIntegrate) {
276 const bool wasInactive = lane->getVehicleNumber() == 0;
277 lane->integrateNewVehicles();
278 if (wasInactive && lane->getVehicleNumber() > 0) {
279 LaneUsage& lu = myLanes[lane->getNumericalID()];
280 if (!lu.amActive) {
281 if (lu.haveNeighbors) {
282 myActiveLanes.push_front(lane);
283 } else {
284 myActiveLanes.push_back(lane);
285 }
286 lu.amActive = true;
287 }
288 }
289 }
290#ifdef PARALLEL_STOPWATCH
291 myStopWatch[1].stop();
292#endif
293}
294
295
296void
298 std::vector<MSLane*> toAdd;
299#ifdef PARALLEL_CHANGE_LANES
300 std::vector<const MSEdge*> recheckLaneUsage;
301#endif
303 for (const MSLane* const l : myActiveLanes) {
304 if (myLanes[l->getNumericalID()].haveNeighbors) {
305 const MSEdge& edge = l->getEdge();
306 if (myLastLaneChange[edge.getNumericalID()] != t) {
308#ifdef PARALLEL_CHANGE_LANES
310 MSLane* lane = edge.getLanes()[0];
311 myThreadPool.add(lane->getLaneChangeTask(t), lane->getRNGIndex() % myThreadPool.size());
312 recheckLaneUsage.push_back(&edge);
313 } else {
314#endif
315 edge.changeLanes(t);
316 for (MSLane* const lane : edge.getLanes()) {
317 LaneUsage& lu = myLanes[lane->getNumericalID()];
318 //if ((*i)->getID() == "disabled") {
319 // std::cout << SIMTIME << " vehicles=" << toString((*i)->getVehiclesSecure()) << "\n";
320 // (*i)->releaseVehicles();
321 //}
322 if (lane->getVehicleNumber() > 0 && !lu.amActive) {
323 toAdd.push_back(lane);
324 lu.amActive = true;
325 }
327 lane->sortManeuverReservations();
328 }
329 }
330#ifdef PARALLEL_CHANGE_LANES
331 }
332#endif
333 }
334 } else {
335 break;
336 }
337 }
338
339#ifdef PARALLEL_CHANGE_LANES
341 myThreadPool.waitAll(false);
342 for (const MSEdge* e : recheckLaneUsage) {
343 for (MSLane* const l : e->getLanes()) {
344 LaneUsage& lu = myLanes[l->getNumericalID()];
345 if (l->getVehicleNumber() > 0 && !lu.amActive) {
346 toAdd.push_back(l);
347 lu.amActive = true;
348 }
350 l->sortManeuverReservations();
351 }
352 }
353 }
354 }
355#endif
356
357 MSGlobals::gComputeLC = false;
358 for (std::vector<MSLane*>::iterator i = toAdd.begin(); i != toAdd.end(); ++i) {
359 myActiveLanes.push_front(*i);
360 }
361}
362
363
364void
365MSEdgeControl::detectCollisions(SUMOTime timestep, const std::string& stage) {
366 // Detections is made by the edge's lanes, therefore hand over.
367 for (MSLane* lane : myActiveLanes) {
368 if (lane->needsCollisionCheck()) {
369 lane->detectCollisions(timestep, stage);
370 }
371 }
374 lane->detectCollisions(timestep, stage);
375 }
378 }
379}
380
381
382void
386
387void
391
392void
394 for (MSEdge* e : myEdges) {
395 e->inferEdgeType();
396 const std::vector<MSLane*>& lanes = e->getLanes();
397 for (std::vector<MSLane*>::const_iterator j = lanes.begin(); j != lanes.end(); ++j) {
398 (*j)->initRestrictions();
399 }
400 }
401}
402
403void
406 for (MSEdge* edge : myEdges) {
407 if (!edge->getLanes().empty()) {
409 }
410 }
411}
412
413void
419
420/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define PARALLEL_EXEC_MOVE
@ SUMO_TAG_EDGECONTROL
the internal state for edge control
@ SUMO_ATTR_LANES
T MIN2(T a, T b)
Definition StdDefs.h:80
void buildSegmentsFor(const MSEdge &e, const OptionsCont &oc)
Build the segments for a given edge.
Definition MELoop.cpp:398
Container & getContainer()
Definition MFXSynchQue.h:84
void unlock()
Definition MFXSynchQue.h:99
void unlock()
Definition MFXSynchSet.h:68
size_t size() const
void insert(T what)
Definition MFXSynchSet.h:82
Container & getContainer()
Definition MFXSynchSet.h:53
void setJunctionApproaches()
Register junction approaches for all vehicles after velocities have been planned. This is a prerequis...
void patchActiveLanes()
Resets information whether a lane is active for all lanes.
std::vector< StopWatch< std::chrono::nanoseconds > > myStopWatch
~MSEdgeControl()
Destructor.
void setActiveLanes(std::list< MSLane * > lanes)
Reconstruct the current state.
void setAdditionalRestrictions()
apply additional restrictions
std::list< MSLane * > myActiveLanes
The list of active (not empty) lanes.
void detectCollisions(SUMOTime timestep, const std::string &stage)
Detect collisions.
MFXSynchQue< MSLane *, std::vector< MSLane * > > myWithVehicles2Integrate
A storage for lanes which shall be integrated because vehicles have moved onto them.
MSEdgeVector myEdges
Loaded edges.
void buildMesoSegments()
build meso segments after meso edge types are loaded
void executeMovements(SUMOTime t)
Executes planned vehicle movements with regards to right-of-way.
MFXSynchSet< MSLane *, std::set< MSLane *, ComparatorNumericalIdLess > > myInactiveCheckCollisions
Additional lanes for which collision checking must be performed.
LaneUsageVector myLanes
Information about lanes' number of vehicles and neighbors.
void gotActive(MSLane *l)
Informs the control that the given lane got active.
void checkCollisionForInactive(MSLane *l)
trigger collision checking for inactive lane
std::vector< SUMOTime > myLastLaneChange
The list of active (not empty) lanes.
std::set< MSLane *, ComparatorNumericalIdLess > myChangedStateLanes
Lanes which changed the state without informing the control.
void planMovements(SUMOTime t)
Compute safe velocities for all vehicles based on positions and speeds from the last time step....
void saveState(OutputDevice &out)
Saves the current state into the given stream.
void changeLanes(const SUMOTime t)
Moves (precomputes) critical vehicles.
MSEdgeControl(const std::vector< MSEdge * > &edges)
Constructor.
double myMinLengthGeometryFactor
A road/street connecting two junctions.
Definition MSEdge.h:78
void changeLanes(SUMOTime t) const
Performs lane changing on this edge.
Definition MSEdge.cpp:962
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition MSEdge.h:169
int getNumericalID() const
Returns the numerical id of the edge.
Definition MSEdge.h:308
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition MSGlobals.h:115
static double gLateralResolution
Definition MSGlobals.h:100
static bool gComputeLC
whether the simulationLoop is in the lane changing phase
Definition MSGlobals.h:143
static int gNumSimThreads
how many threads to use for simulation
Definition MSGlobals.h:149
static int gNumThreads
how many threads to use
Definition MSGlobals.h:152
Representation of a lane in the micro simulation.
Definition MSLane.h:84
int getRNGIndex() const
returns the associated RNG index
Definition MSLane.h:241
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:201
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition MSNet.h:403
void removePending()
Removes a vehicle after it has ended.
A storage for options typed value containers)
Definition OptionsCont.h:92
static OptionsCont & getOptions()
Retrieves the options.
Static storage of an output device and its base (abstract) implementation.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const ATTR_TYPE &attr, const T &val, const bool isNull=false, const bool escape=false)
writes a named attribute
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
long long int getTotal() const
Definition StopWatch.h:75
long long int getAverage() const
Definition StopWatch.h:71
void add(const StopWatch< TimeT, ClockT > &other)
Definition StopWatch.h:63
const std::vector< TimeT > & getHistory() const
Definition StopWatch.h:67
Definition json.hpp:5639
Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle)
Definition Named.h:29
A structure holding some basic information about a simulated lane.
bool amActive
Information whether this lane is active.
bool haveNeighbors
Information whether this lane belongs to a multi-lane edge.