• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Mike.Haddow

Atomically Update Vector3D In Win32

7 posts in this topic

Hi. This is my first post, but I've been following the forums for some time. I'm currently making the transition from XNA to C++ (various libs). I have successfully ported several projects, but my reason for making the change was for the performance benefits. I've been reading a number of posts/tutorials/presentations on data oriented design (notably Mike Acton's posts). This lead to a small performance boost in a single threaded environment, but now I want to multi-thread my task scheduler. I was hoping to make use of a lock free approach (similar to the gateway approach outlined here: [url="http://macton.smugmug.com/gallery/8611752_9SU2a/1/568079120_gzhk8#!i=568079120&k=gzhk8"]http://macton.smugmug.com/gallery/8611752_9SU2a/1/568079120_gzhk8#!i=568079120&k=gzhk8[/url]) but I'm struggling.

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?
0

Share this post


Link to post
Share on other sites
I'm don't know of any way to update 3 32-bit values atomically. You could however store a pointer to the Vector3D, and then change the pointer atomically.
0

Share this post


Link to post
Share on other sites
Thanks Evil Steve. I'd thought of that before, but my original approach still had a race condition (I was using one array of data and 2 arrays of pointers - one of active but read only data and the other write only but mutable). But I may be able to overcome it using tagged pointers. I'll try it out and post some code shortly.
0

Share this post


Link to post
Share on other sites
[quote]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?[/quote]

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. [code]Vector3D old_position[];
Vector3D new_position[];
// similar for other values, such as velocity perhaps
[/code]To 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.

[quote]Likewise I have a collision resolution task.[/quote]

Same solution as above, use old_position.
0

Share this post


Link to post
Share on other sites
Thanks Antheus.

[quote name='Antheus' timestamp='1329142383' post='4912604']
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.
[/quote]

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.
0

Share this post


Link to post
Share on other sites
And forget about that article for the time of being. It's about tweaking the weight of the color of oil gauge on a F1 car. You don't even have a car yet.

[quote](from profiling I know that the physics systems are the bottleneck[/quote]

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.
0

Share this post


Link to post
Share on other sites
Interesting. I had assumed that the threading overhead would be low enough that splitting the update integration and the collision systems to different cores would make sense. I'm still interested in learning about lock-free and data orientated approaches though (so that I know what to look for if/when I get an F1 car). Are there any open source projects that you know of demonstrating these ideas?
0

Share this post


Link to post
Share on other sites
[quote name='Mike.Haddow' timestamp='1329149456' post='4912638']
Interesting. I had assumed that the threading overhead would be low enough[/quote]
Passing data between threads is incredibly expensive, so work done separately must be orders of magnitude bigger to absorb it.

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.

[quote]that splitting the update integration and the collision systems to different cores would make sense[/quote]
Yes, but one core does updates, the other does collision. Both operate on their local copy of entire state they need.

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.

[quote]Are there any open source projects that you know of demonstrating these ideas?[/quote]

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".
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0