2d range trees?

Post your questions, suggestions and experiences regarding to Image manipulation, 3d modeling and level editing for the Irrlicht engine here.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: 2d range trees?

Post by REDDemon »

if I remember correctly:
5us =
thought microsecond was
µs
^^

Yes you have to check every item, but cost is still linear because every sector have fixed maximum number of elements ^^ (that's like cheating, it is, but may works too).

(wikipedia): modern CPUs can execute hundreds of instructions in the time taken to fetch a single cache line from main memory. Various techniques have been employed to keep the CPU busy during this time.
(one way to keep it busy is doing 2D square distance test, very fast to compute and even faster if you know right assembly instructions.)

In a quadtree you can have to fectch a single line for every node you iterate over. Grid already have all children cached, if they are not enough checkin all of them (2D square distance is very fast to compute) should be still faster. And as additional case, if a Sector Completely Fit into your range, then all its entities needs no check. So for biggest ranges values, most of checks can be simply skipped. (since when range increase linearly the circle area increase quadratic, only sectors on borders need a full check, and perimeter increase linearly with range so the cost is still linear, constant access for 1 element, linear for multiple elements. Iterating over N is linear cost, iterating over 100 is constant cost. so NxN quadratic, but Nx100 is linear, and in average it will be Nx(number lesser-lesser than 100) )

If grid does not work for you even after the finest tuning of cell size, then there's nothing faster of 2DRangeTree, so or improve your RangeTree Implementation (not very hard to do, unless you already have the "optimiziest" code), or go parallel, or invent a new algorithm and sell it to game industries :)

Edit: another important point too.

2D range tree is limited to Square areas, with grid you can have automatically Circle area. Searching for units in a circulare radius in RangeTree will have the same cost of that check for the Grid, but with all the additional overhead of the LogN algorithm+ cache misses.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: 2d range trees?

Post by hendu »

I haven't yet started on this - no time so far. I hope to get it started around christmas.
2D range tree is limited to Square areas, with grid you can have automatically Circle area. Searching for units in a circulare radius in RangeTree will have the same cost of that check for the Grid, but with all the additional overhead of the LogN algorithm+ cache misses.
Yes, it's not for every search case.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: 2d range trees?

Post by CuteAlien »

I'm no help with range trees, but it reminds me a little on some optimization I spend on a lot of time once when working on some shooter-bots. I tried grid's, I tried quad-trees, I tried octrees... and all turned out to be too slow in the end. The real solution was then rather trivial to implement - bot path-planing was no longer forced to calculate within a frame, so the search was simply allowed to take a longer time and was distributed over several frames. I'm sure you considered that already, but just mentioning it in case you try to avoid doing that unnecessarily (as I did back then...).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: 2d range trees?

Post by hendu »

Thanks, but for this case the search has to be fast. It's not known what to search for until it's needed.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: 2d range trees?

Post by hendu »

I have a prototype running that does searches in 18 us.

It's not yet optimized at all, so I'm confident I can get it to below the originally mentioned 5 microseconds per search.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: 2d range trees?

Post by serengeor »

what algos are you using? :)
Working on game: Marrbles (Currently stopped).
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: 2d range trees?

Post by hendu »

Prototype finished (ie, all temp solutions were replaced with proper ones). No optimization has yet been done, but the numbers already look very nice:
Using 10 (10^1) points, creation took 108398 us (108 ms)
and 1M searches took 1018790 us. (1.02 us/search)
Using 100 (10^2) points, creation took 137879 us (137 ms)
and 1M searches took 1634631 us. (1.63 us/search)
Using 1000 (10^3) points, creation took 146530 us (146 ms)
and 1M searches took 1616153 us. (1.62 us/search)
Using 10000 (10^4) points, creation took 223948 us (223 ms)
and 1M searches took 1637679 us. (1.64 us/search)
Using 100000 (10^5) points, creation took 279480 us (279 ms)
and 1M searches took 1881746 us. (1.88 us/search)
Using 1000000 (10^6) points, creation took 795360 us (795 ms)
and 1M searches took 2540381 us. (2.54 us/search)
Using 10000000 (10^7) points, creation took 4889185 us (4889 ms)
and 1M searches took 3794573 us. (3.79 us/search)


Progression:
Creation 1.27x, search 1.60x
Creation 1.06x, search 0.99x
Creation 1.53x, search 1.01x
Creation 1.25x, search 1.15x
Creation 2.85x, search 1.35x
Creation 6.15x, search 1.49x
Since my target was 5us per search using one million points, I'm already at half that :D

Next is optimization from different angles: memory, and cache (this is completely memory-bound, so cache = speed for this). Once I get it to a nice shape, with docs and a website design, I'll put it up on sf.net. Currently it's just the lib, a test suite, and some benchmark programs.
what algos are you using?
This is a bog-standard 2d range tree, the O(log^2) model. I haven't yet implemented fractional cascading, which would make searches be O(log).

If you mean how it works, well it's a binary tree, and its creation uses the merge sort algorithm in places. Wikipedia has a page on range trees ;)
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: 2d range trees?

Post by hendu »

Now up at http://librangetree.sourceforge.net

Here are the optimized numbers. Reviews welcome.
Using 10 (10^1) points, creation took 26 us (0 ms)
and 1M searches took 49985 us. (0.05 us/search)
Using 100 (10^2) points, creation took 49 us (0 ms)
and 1M searches took 73406 us. (0.07 us/search)
Using 1000 (10^3) points, creation took 535 us (0 ms)
and 1M searches took 118294 us. (0.12 us/search)
Using 10000 (10^4) points, creation took 6222 us (6 ms)
and 1M searches took 279652 us. (0.28 us/search)
Using 100000 (10^5) points, creation took 72399 us (72 ms)
and 1M searches took 867422 us. (0.87 us/search)
Using 1000000 (10^6) points, creation took 689253 us (689 ms)
and 1M searches took 1923755 us. (1.92 us/search)
Using 10000000 (10^7) points, creation took 4857303 us (4857 ms)
and 1M searches took 3013128 us. (3.01 us/search)
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: 2d range trees?

Post by hendu »

After a few optimizations it's almost twice as fast as 1.0.

Now it's also finally integrated where I needed it - it's 1k - 100k faster than the previous hack solution.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: 2d range trees?

Post by serengeor »

Nice 8)
I might take a look when I'll need something like this :)
Working on game: Marrbles (Currently stopped).
Post Reply