Atomically Update Vector3D In Win32
#1 Members - Reputation: 100
Posted 13 February 2012 - 06:04 AM
Specifically, I am currently working on my physics system. I have one task that updates forces (simple vec3 PODs) by applying drag based on the current velocity. Likewise I have a collision resolution task. How can I atomically update the vector3D's on an x86? I was considering storing a queue in the gateway of requested changes, but this still wouldn't avoid one thread reading a vector while the gateway was midway through applying an update. Is the gateway approach unsuitable for non-PS3 games?
#2 Moderators - Reputation: 1918
Posted 13 February 2012 - 06:21 AM
Steve Macpherson
Senior programmer, Firebrand Games
#3 Members - Reputation: 100
Posted 13 February 2012 - 07:06 AM
#4 Members - Reputation: 2369
Posted 13 February 2012 - 08:13 AM
Specifically, I am currently working on my physics system. I have one task that updates forces (simple vec3 PODs) by applying drag based on the current velocity. Likewise I have a collision resolution task. How can I atomically update the vector3D's on an x86? I was considering storing a queue in the gateway of requested changes, but this still wouldn't avoid one thread reading a vector while the gateway was midway through applying an update. Is the gateway approach unsuitable for non-PS3 games?
Don't update a vector3D atomically.
Atomicity, especially on multi-core depends on cache lines. The problem you should be worried about is false sharing. When two cores work on values sharing the same line, each write causes a cache sync, stalling both of them.
For physics, you have two arrays.
Vector3D old_position[]; Vector3D new_position[]; // similar for other values, such as velocity perhapsTo apply drag, read from old_position (old_velocity), compute new values, write them to new_position (at same index).
Hand each thread its own chunk that is a multiple of cache line. Exact values differ, but for OpenMP I've usually found that for large number of elements a chunk size of 64k or thereabout works best.
Gotchas: Vector3D must be struct and they must continuously allocated, hence array.
Above solution is optimal for multi-core update.
If using more complex integration, such as RK4, have more buffers, one for each partial update.
Likewise I have a collision resolution task.
Same solution as above, use old_position.
#5 Members - Reputation: 100
Posted 13 February 2012 - 09:07 AM
Atomicity, especially on multi-core depends on cache lines. The problem you should be worried about is false sharing. When two cores work on values sharing the same line, each write causes a cache sync, stalling both of them.
I see! That also may explain why my experiments haven't shown anything close to the performance increase I was hoping for (from profiling I know that the physics systems are the bottleneck). I only recently started reading about processor architectures and caching issues, so I'm still pretty new at this. I'll work what you've said into my current experiment and post some code later.
#6 Members - Reputation: 2369
Posted 13 February 2012 - 09:36 AM
(from profiling I know that the physics systems are the bottleneck
If they are, threading won't solve it.
Physics needs to run at fixed step on lowest supported hardware. Anything above cannot do more or different work, or it will impact the result. Imagine FPS collision tests depending on how fast of a CPU someone has.
And even if threading is applicable, it's mostly about batching and granularity. A single frame might have a few dozen tasks at most.
In many cases overhead is a killer. There are only 2/4 cores, offering at most that much more power. But going from a local call (1 cycle) to a queue/dispatch (12 cycles under GCD, can be thousands of cycles) means a lot of overhead. It's very easy for multi-threaded solution to peg all cores, but do less work than a single threaded version would.
#7 Members - Reputation: 100
Posted 13 February 2012 - 10:10 AM
#8 Members - Reputation: 2369
Posted 13 February 2012 - 10:42 AM
Passing data between threads is incredibly expensive, so work done separately must be orders of magnitude bigger to absorb it.Interesting. I had assumed that the threading overhead would be low enough
A single vector3D can often be updated in <1 cycle (amortized cost). By passing it between threads, the cost of update becomes irrelevant. Same is true for SIMD. Manipulating a single value requires so much overhead that it runs slower. SIMD only offers improvement if there is enough data, otherwise overhead dominates.
All designs today strive towards batching.
Yes, but one core does updates, the other does collision. Both operate on their local copy of entire state they need.that splitting the update integration and the collision systems to different cores would make sense
But they must still be guaranteed to never do more work than slowest supported machine can handle. Amount of useful work must also exceed the cost of making a copy. Value kept in register is free, writing it to memory takes several cycles in best case. For simple multiplication or addition of vectors, performance will be memory bound. Goal then becomes how to perform as much work as possible while the values are still in registers.
For example, running memcpy or memset across multiple threads will likely run no faster, at least on most machines it will not scale up to 4 cores since memory bus can't keep up. CPUs continue to trend to <1 cycle per instruction, so goal of fast code is keeping stuff in registers for as long as possible.
Are there any open source projects that you know of demonstrating these ideas?
All these optimizations are incredibly specific. Whatever source there would be, it would be optimized around author's specific problem and likely weeks of tuning with final data. There's little to no reuse at source level.
As mentioned, a single operation can add overhead of order of magnitude, negating any improvements.
General guideline remains: Computers are only good at running unconditional for loop over an array. Everything, from CPU, memory, network, disk is optimized for this case. All optimizations over past 10-20 years that went into hardware are trying to minimize the impact of all code that works differently.
As an example, imagine that web browser had to access every char of a web page by making a web request (get /home/0, get /home/1, get /home/2, ...) instead of asking once and receiving a stream. Yet OO centric design does just that.
A pragmatic approach - use existing physics libraries. They've solved all these problems already, at least you'll see how they do it and can later improve. There's simply too much knowledge in such libraries these days to simply start from scratch for sake of "learning".






