Repository navigation
Use temporary helper graph to speed up CH preparation - #2132
Conversation
| if (nodeB > nodeA) | ||
| keyBwd += 1; | ||
| return keyBwd; | ||
| } |
There was a problem hiding this comment.
Why do we need those 4 methods? The methods getOrigEdgeKeyLastBA and getOrigEdgeKeyFirstBA
and then getOrigEdgeKeyFirstAB and getOrigEdgeKeyLastAB look identical and could we just omit two of them as this isn't used elsewhere? Also instead of
if (nodeA > nodeB) keyFwd += 1;
return keyFwd;
we could write:
return nodeA > nodeB? keyFwd + 1 : keyFwd;
but probably this is done from the JIT already
There was a problem hiding this comment.
Ah, sorry. Forget this comment I just found another usage (EdgeBasedPrepareShortcut)...
There was a problem hiding this comment.
Ok I forgot it already :) (but I made use of the ternary operator as its a bit more concise). And yes we need four methods because for base edges we use the same BasePrepareEdge for both directions (AB vs BA) and for edge-based shortcuts we only store one direction but there is a difference between first/last orig edge.
| } | ||
|
|
||
| private int getEdgeWithFlags(int edge, boolean fwd, boolean bwd) { | ||
| // we use only 30 bits for the edge Id and store two access flags along with the same int |
There was a problem hiding this comment.
This is a bit low. Can we avoid this reduction to 30 bits somehow? (we could use 5 bytes and a RAMDataAccess?)
There was a problem hiding this comment.
Oh good point, I almost forgot about this. But 30bits should be enough, because this is only for base graph edges. And if they exceeded 30bits the edge-based preparation would fail anyway, because there are more than twice as many shortcuts than base edges. This is only used for edge-based. Also it would be very easy to change this limit here (use another in, or dataaccess with byte[]..)
| return prepareGraph.getLevel(node) != maxLevel; | ||
| } | ||
|
|
||
| void setOrigEdgeCount(int edgeId, int value) { |
There was a problem hiding this comment.
Thes orig edge counts are now included in the prepare graph's edges
| // node contraction, node-based | ||
| public static final String EDGE_DIFFERENCE_WEIGHT = Parameters.CH.PREPARE + "node.edge_difference_weight"; | ||
| public static final String ORIGINAL_EDGE_COUNT_WEIGHT = Parameters.CH.PREPARE + "node.original_edge_count_weight"; | ||
| public static final String CONTRACTED_NEIGHBORS_WEIGHT = Parameters.CH.PREPARE + "node.contracted_neighbors_weight"; |
There was a problem hiding this comment.
I removed this heuristic for now, might be worth to re-implement it later, but not sure really
| private int[] edges; | ||
| private int[] incEdges; | ||
| private int[] prepareEdges; |
There was a problem hiding this comment.
Here we got rid of one of the int[] arrays, i.e. 4byte*N where N=2*number of base graph edges. We no longer need to store the incEdges, because we use the edge keys directly now and the index position equals the edge key for each element.
| private int[] parents; | ||
| private int[] edgeIds; |
There was a problem hiding this comment.
No need for these two arrays as we never need to find the actual shortest path (we just need to check its existence). This saved 2*4byte*N where N=number of nodes of the graph
| prepareGraph = PrepareCHGraph.edgeBased(chGraph, chConfig.getWeighting()); | ||
| nodeContractor = new EdgeBasedNodeContractor(prepareGraph, pMap); | ||
| } else { | ||
| prepareGraph = PrepareCHGraph.nodeBased(chGraph, chConfig.getWeighting()); | ||
| nodeContractor = new NodeBasedNodeContractor(prepareGraph, pMap); |
There was a problem hiding this comment.
Here we were allocating quite a bit of memory even when re-starting the server and just loading the preparations (no new preparation). We should really make PrepareContractionHierarchies about preparing CH and not use it as a 'handle' to the preparations available on the server...
|
|
||
| /** | ||
| * Returns 1 if existingScFlags of an existing shortcut can be overwritten with a new shortcut by | ||
| * newScFlags without limiting or changing the directions of the existing shortcut. | ||
| * The method returns 2 for the same condition but only if the new shortcut has to be added | ||
| * even if weight is higher than existing shortcut weight. | ||
| * <pre> | ||
| * | newScFlags: | ||
| * existingScFlags | -> | <- | <-> | ||
| * -> | 1 | 0 | 2 | ||
| * <- | 0 | 1 | 2 | ||
| * <-> | 0 | 0 | 1 | ||
| * </pre> | ||
| * | ||
| * @return 1 if newScFlags is identical to existingScFlags for the two direction bits and 0 otherwise. | ||
| * There are two special cases when it returns 2. | ||
| */ | ||
| public static final int getScMergeStatus(int existingScFlags, int newScFlags) { | ||
| if ((existingScFlags & scDirMask) == (newScFlags & scDirMask)) | ||
| return 1; | ||
| else if ((newScFlags & scDirMask) == scDirMask) | ||
| return 2; | ||
|
|
||
| return 0; | ||
| } |
There was a problem hiding this comment.
This is no longer needed because after we wrote a shortcut to CHGraph we never change it again
| * The elements in each sub-array are divided into two parts with ranges [0,mid[ and [mid,size[. We can add elements | ||
| * to either of the two parts. | ||
| */ | ||
| class SplitArray2D<T> { |
There was a problem hiding this comment.
Any idea for a better name? I am trying to say this is an array of arrays (2D) where each of the inner arrays is split into two parts (the elements of the two parts are separated from each other). But maybe not so important as this is only a package private class used by CHPreparationGraph...
|
|
||
| private void grow(int n) { | ||
| // todo: think about grow factor: trimming and then doubling the size might be not what we want, rather | ||
| // increase by 50% or something? |
There was a problem hiding this comment.
How large are these sub-arrays on average? Could it make sense to initialize them with the average+20% then they do not need to grow that often and the size mismatch could be lower?
There was a problem hiding this comment.
Good question. In this commit: 1c2ca0f I trimmed the arrays after the prepare graph is built for the first time to save some memory at the beginning of the contraction. For maximum speed it would of course be better to initialize them with some expected size (but no idea so far how important this is). I am currently running the import with this trimming to compare to the version without the trimming.
There was a problem hiding this comment.
I did a quick check for Bayern (~6mio shortcuts) and Germany (~28mio shortcuts) and the array sizes seem to be relatively stable against changes of the map size. Before the contraction the average 'size' (not length) of the arrays (number of in+out edges) is around 4.5 (makes sense: 2.25 in/out edges per node) and average mids[n] is almost exactly half of sizes[n] (on average the number of in edges is the same as out edges -> makes sense).
The maximum size during the contraction is about 100 and the maximum mid is around 50 for both maps.
So I think its already safe to say that we can store mid/size in a single int to save some (not a lot) memory (around 900MB for planet). For the resizing strategy I think it probably does not really matter, because the arrays never get very large and maybe its even the best approach to trim them as I do currently to make sure memory is minimized.
In my opinion if we change anything here we should try linking the edge objects to each other and remove the sub-arrays entirely, but I would postpone this until we feel like its definitely needed (and we are sure it improves anything).
There was a problem hiding this comment.
to trim them as I do currently to make sure memory is minimized.
Yes, this sounds reasonable
|
This version shows even better performance for edge-based preparation, but takes even more memory: https://github.com/graphhopper/graphhopper/tree/prepare_graph_unidir |
|
This is what it looks like in our measurements for Bayern: and Germany (node-based only): Note how the import time for the custom profiles is not bigger anymore, because all the weights get pre-calculated at the beginning of the preparation. We also see a drop in the edge traversal time on the CH graph (due to better cache locality) and a related measurable reduction of the average query time (around 10%). |



This PR aims at reducing the CH preparation time by changing the contraction algorithm. The idea is pretty simple: We use a helper graph which at the beginning of the contraction is more or less a copy of the base graph. The witness searches are performed on this helper graph and the
CHGraphis only used to store the resulting shortcuts. When we contract a nodexwith neighborspandq:p-x-qwe do not insert the p-q shortcut(s) into the CHGraph directly, but instead we add it to the helper graph. When the witness searches are finished we add the x-p and x-q edges to theCHGraph(if they are shortcuts). The p-q shortcut(s) are added only later when we contract node p/q. Importantly, we can also remove all edges adjacent to the node we are contracting from the helper graph after we have performed the witness search. This is the main reason why the preparation becomes faster using this method. So far measurements show that the preparation (node- and edge-based) on this branch is about two to three times faster than master (edge-based performance seems to benefit more from the changes in this branch, also because the turn costs are cached with the helper graph).The obvious downside of this approach is that we need additional memory for the helper graph.
As the helper graph becomes smaller and smaller during the preparation (while the CH graph becomes bigger and bigger) we need extra memory mostly at the beginning of the contraction. The size of the helper graph is initially proportional to the size of the base graph. First I thought there are 'as many' shortcuts as there are base graph edges and thus the memory size of the helper graph will be about equal to the memory we need to store the shortcuts. Unfortunately this is not exactly true for two reasons. First, since we are using 'bidirectional' shortcuts for node-based CH there are less shortcuts than base graph edges. Second, to support efficient removal of helper graph edges during the contraction we need to use a less memory efficient representation of the helper graph (this is also partly because of Java's wasteful way of storing heap data). Thus this PR increases the overall memory requirement for CH preparation (node- and edge-based).I did some experiments on the Germany OSM for four (node-based) profiles (car,mtb,foot,bike) and was able to run the import with
Xmx7000m(usingXmx6500myielded an out of memory error) on master and neededXmx8500mon this branch (usingXmx8000myielded an out of memory error). The key thing to understand here is that the more profiles we prepare in parallel the more helper graph memory we need at the beginning of the import. So an obvious workaround would be using less threads. Another somewhat hacky possibility would be delaying the different preparations a bit such that the peak memory usages of the different threads do not happen at the same time.For edge-based CH we need a lot more memory than for node-based CH because there are more shortcuts, the shortcuts are all uni-directional and each shortcut needs more memory. Therefore the helper graph does not increase the overall memory usage (the helper graph memory is less than the memory we need to reserve for the shortcuts anyway). Actually I was able to speed up the preparation for edge-based even more when using a less memory efficient helper graph than the one I am using now.Besides the speed up of the preparation using the approach of this PR has the following advantages:
The shortcuts are automatically sorted by the CH levels of their base nodes. This way we might be able to get rid of the
linkA/Bpointers for shortcuts (reduce the memory we need to store shortcuts) and the the shortcut storage is more cache-friendly. In fact the CH queries are faster on this branch.After the shortcuts are written to the CHGraph they will not change again. So in principle it would be possible to start flushing some of the shortcut storage to disk already during the preparation (to reduce RAM usage).
Since we pre-calculate the base-edge weights and store them in the helper graph the preparation time is especially faster for 'expensive' weightings. I.e. if we use a custom weighting that does some costly edge-weight computations they are now done only once while the weightings get evaluated many many times during the witness searches on master. Obviously there would be other ways to 'cache' these weights, but with this PR this is already done and I checked that simply caching the weights does not nearly yield the speed up we see here.
Related #1565