🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Multithreaded pathfinding

Started by
11 comments, last by wodinoneeye 11 years, 8 months ago
Hello guys... i'm now working on the pathfinding of my game, and since pathfinding can really slow down the game i had three ideas :
1) i update the game (rendering) at 60 fps ( example ), i update my physics at 60/2 and since pathfinding does not require to be updated many times a second i was gonna update it once every 60 frames
2) multi-threading
3) splitting pathfinding algorithms over multiple frames

I've read that multithreading is used often when dealing with pathfinding or similar, but i've got only some basic knowledge about multi-threading (C++)
The 1) IMHO is a good idea... but it can still slow down my program in THAT frame i update the pathfinding.
The 3) seems the best idea for me... splitting a N number of paths to find over N threads, but i have really no good idea how to implement it.

i was looking for some hints & tips from people with more experience. My game is pretty simple, but the AI is the most important part (it's for learning mainly). And for pathfinding i'm using different algorithms for different purposes ( DFS,BFS,Dijkstra,A*). i've already implemented most of them and even the DFS that "is supposed to be the faster" really slow the program down.

P.S. if you have any good article i'd be glad to read it !
Advertisement
I would spend some time on this idea:
0) Make your pathfinding code as fast as possible.

I don't know anything about how you code, but I have seen enough horror stories to suspect that there is a decent chance you are doing something needlessly slow in your code. I am thinking of something like allocating dynamic memory whenever a node gets visited, or that type of thing.

I would only pursue one of the other options after I have satisfied myself that there isn't much to be gained from improving the single-threaded pathfinding code.
Before you start to multithread, make sure your algoritms are working properly and efficiently with extensive tests.

Then reduce the times it is run, why would it be needed to run at a fixed timestep?
Only times the path changes is if the goal changes or the terrain changes.

Then you can simplify the terrain to have less nodes to search.

DFS is not "supposed to be faster", only in very special cases.

You probably want A*, specially if you have an easy way to estimate distance left to goal, dijkstra's is useful if you can't.
Ahah Alvaro hopefully i havn't done it yet.
Anyway i'm already trying to optmize that. To get all the nodes in the map i've used flood fill... I'll try to reduce the number of nodes. I've got one node every 15 pixels ( except for walls or non-walkable areas ) that roughly are 8k nodes per map. I'm realizing that maybe they are too many nodes.
The entire map is around 1920x1080. Do you think that are too many ?
That doesn't sound much to me... thats just a grid of approx 90x90 nodes.

If the map is relatively open and interconnected, an A* shouldn't have to visit much more then a few hundred in average, and that is when navigating from one end of the map to the other. If start and end is relatively close to eachother, you will just visit a handful of nodes, should be very fast.

DFS will in average visit half the nodes, even when start and end is just next to each other (if you are unlucky and start searching the wrong direction, you might have to visit _every_ node before you find the goal in your neighbor node...)

I think there might be something funky in your A*
unless you have a very high number of agents pathfinding...

Another optimization is to have different levels of pathfindning, a coarse one to find (for example) the "rooms" to visit, and then a finer to navigate through each "room".

That doesn't sound much to me... thats just a grid of approx 90x90 nodes.

If the map is relatively open and interconnected, an A* shouldn't have to visit much more then a few hundred in average, and that is when navigating from one end of the map to the other. If start and end is relatively close to eachother, you will just visit a handful of nodes, should be very fast.

DFS will in average visit half the nodes, even when start and end is just next to each other (if you are unlucky and start searching the wrong direction, you might have to visit _every_ node before you find the goal in your neighbor node...)

I think there might be something funky in your A*
unless you have a very high number of agents pathfinding...

Another optimization is to have different levels of pathfindning, a coarse one to find (for example) the "rooms" to visit, and then a finer to navigate through each "room".


A* definitively faster than DFS ( i'm finishing A* "benchmarks" ). DFS takes 1 second in the worst case
Sounds to me like that inner loop can be optimized, if you only test 8 nodes per millisecond. (assuming modern PC)
Remember, adding threads will only increase capacity by the number of cores you have.
Optimizing your inner loop could give you 100x improvement in some cases smile.png (not saying you could in this case)

What is your heuristic for A*? if its euclidian distance to goal, you could try squared distance, just as good but a lot faster.

There are also more advanced ones to handle dead ends better if your map have lots of those, you could check this out for example: http://www.ru.is/fac...jornssonH06.pdf

Though, when you've exhausted all algorithmic improvements and optimizations, at least it IS easily parallelized.
For multithreading, you should remember that you can't run more threads then cores in parellel, so you should divide n paths per thread where n = number of paths / number of cores.
some kind of thread pool that you dispatch work to, seems like a popular architecture for those things, but I must admit I don't have much experience in that.

Sounds to me like that inner loop can be optimized, if you only test 8 nodes per millisecond. (assuming modern PC)
Remember, adding threads will only increase capacity by the number of cores you have.
Optimizing your inner loop could give you 100x improvement in some cases smile.png (not saying you could in this case)

What is your heuristic for A*? if its euclidian distance to goal, you could try squared distance, just as good but a lot faster.

There are also more advanced ones to handle dead ends better if your map have lots of those, you could check this out for example: http://www.ru.is/fac...jornssonH06.pdf

Though, when you've exhausted all algorithmic improvements and optimizations, at least it IS easily parallelized, and your assumption of 1 path per thread sounds very good.


For the DFS ( that is causing me most of the trouble ) to get from node 10 to 600 ( it's doing the worst path possible ) with ~15k recursion in the inner loop and ~8400 nodes and ~32600 edges ( only 4 directions ) it takes 1 seconds. Here is my code for the DFS ( for this algorithm i'm following a book )

void GraphSearch::DFSearch(int sourceID,int targetID,std::vector<int>& path)
{
// creating containers
std::stack<const MapEdge*> stack;
std::vector<int> visitedNodes(pNodes_->size(),node::NODE_UNVISITED); // creating the container here is quite slow, but DFS still takes 1 sec
std::vector<MapEdge*> connectingNodes;
sf::Clock clock;
float timeElapsed = 0;
int deep = 0;
MapEdge dummyStart(sourceID,sourceID,10);
stack.push(&dummyStart);
while(!stack.empty())
{
timeElapsed += clock.restart().asSeconds();
deep += 1;
const MapEdge* next = stack.top();
stack.pop();
if (next->To() == targetID)
{
std::cout << "Time elapsed " << timeElapsed
<< " on : " << deep << " recursions" << std::endl;
return;
}
path[next->To()] = next->From();
visitedNodes[next->To()] = node::NODE_VISITED;
connectingNodes.clear();
FindConnectingNodes(next->To(),connectingNodes); // this might be the problem since i'm looping through 32k edges * 15k recursions
std::size_t s = connectingNodes.size();
for(int i = 0;i <s;++i)
{
if (visitedNodes[connectingNodes->To()] == node::NODE_UNVISITED)
stack.push(connectingNodes);
}
}
}


edit : yes the FindConnectingNodes is definitively a problem, i've just given a run with VerySleepy and i've found out that is the most expensive call. something like 20% of the whole program ( windows call included)
Hi Sparkon,

are you sure, that you need to do all the calculation every time?

I don't know which game you are building exactly, but there might be a way to use precalculated A* for example.

See http://www.gamedev.net/page/books/index.html/_/technical/game-programming-9/advanced-game-programming-a-gamedevnet-collection-r723

e. g. if you have many rooms and the player moves in the building as does the enemy (AI). You could calculate the routes between the room at buildtime. Than you just lookup position (enemy room) and destination (player room) and have the best way. You can even have seperate calculations for different conditions like locked doors, spawned health packs etc.

As soon as player and enemy are in the same room you can use nearly any pathfinding algorithm for the 'last mile'.

-- GWDev

Hi Sparkon,

are you sure, that you need to do all the calculation every time?

I don't know which game you are building exactly, but there might be a way to use precalculated A* for example.

See http://www.gamedev.n...collection-r723

e. g. if you have many rooms and the player moves in the building as does the enemy (AI). You could calculate the routes between the room at buildtime. Than you just lookup position (enemy room) and destination (player room) and have the best way. You can even have seperate calculations for different conditions like locked doors, spawned health packs etc.

As soon as player and enemy are in the same room you can use nearly any pathfinding algorithm for the 'last mile'.

-- GWDev


yes... i'll probably do as you suggest.

Olof Hedman i fixed the code now it's extremely faster, i'll try to improve it more before going with multi-threading :P

This topic is closed to new replies.

Advertisement