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

Load CH preparations in parallel #2455

Merged
merged 3 commits into from
Nov 9, 2021
Merged

Load CH preparations in parallel #2455

merged 3 commits into from
Nov 9, 2021

Conversation

easbar
Copy link
Member

@easbar easbar commented Nov 2, 2021

Fixes #2454

@easbar easbar added this to the 5.0 milestone Nov 2, 2021
Comment on lines +734 to +741
executorService.shutdown();
try {
for (int i = 0; i < callables.size(); i++)
completionService.take().get();
} catch (Exception e) {
executorService.shutdownNow();
throw new RuntimeException(e);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
executorService.shutdown();
try {
for (int i = 0; i < callables.size(); i++)
completionService.take().get();
} catch (Exception e) {
executorService.shutdownNow();
throw new RuntimeException(e);
}
try {
for (int i = 0; i < callables.size(); i++)
completionService.take().get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
} finally {
executorService.shutdown();
}

Copy link
Member Author

@easbar easbar Nov 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to simply keep it as it was before. There is a long, possibly related discussion about this here: #980. But sure maybe this can be improved.

@@ -723,6 +727,20 @@ public static long calcMillisWithTurnMillis(Weighting weighting, EdgeIteratorSta
}
}

public static void runConcurrently(List<Callable<String>> callables, int threads) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static void runConcurrently(List<Callable<String>> callables, int threads) {
public static void runConcurrently(List<Callable<String>> callables, int threads, String threadPrefix) {

Would be nice if we could remove the Thread renaming from the Callables and let the ThreadFactory of the Executor handle this.

Copy link
Member Author

@easbar easbar Nov 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maybe, no idea tbh. There is also this comment in CHPreparationHandler:

// toString is not taken into account so we need to cheat, see http://stackoverflow.com/q/6113746/194609 for other options

Comment on lines -137 to -148
}, name);
}

threadPool.shutdown();

try {
for (int i = 0; i < preparations.size(); i++) {
completionService.take().get();
}
} catch (Exception e) {
threadPool.shutdownNow();
throw new RuntimeException(e);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this code to a helper method in GHUtility.

return c.chConfig.getName();
})
.collect(Collectors.toList());
int numThreads = Math.max(1, Math.min(4, callables.size()));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to only use one thread here. The prepare.ch.threads parameter is not available here, so I hard-coded the default to 4 (max). Should be ok, because the memory usage won't be increased because of this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If memory consumption doesn't scale with the threadcount here, why don't we use all available cores?

int numThreads = Math.max(1, Math.min(Runtime.availableProcessors(), callables.size()));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this would also be possible, I guess. I was just trying to be conservative. If we did that we could also just use .parallelStream().

Copy link
Contributor

@otbutz otbutz Nov 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using parallelStream() has some sideeffects as it's ForkJoinPool is shared by the whole JVM. Probably not that bad for standalone GraphHopper but a pitfall for people which integrate it as a library.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, good to know. 👍

@easbar
Copy link
Member Author

easbar commented Nov 8, 2021

@karussell any comments here?

@karussell
Copy link
Member

LGTM - thanks!

@easbar
Copy link
Member Author

easbar commented Nov 9, 2021

I'll merge this so we can move forward. @otbutz if you still think something should be improved please don't hesitate to open a separate PR.

@easbar easbar merged commit 91afeb7 into master Nov 9, 2021
@easbar easbar deleted the parallel_ch_load branch November 9, 2021 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Load CH preparations in parallel
3 participants