• 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
sufficientreason

Immutable World State for Parallelization

6 posts in this topic

I'm designing a real-time top-down multiplayer 2D space shooter a la Subspace/Continuum (following the FPS model), and have a game state question.

I'm trying to make a scalable world state model for handling entity updates (ships, projectiles, explosions). Since the majority of the time spent in a state update will be on entity updates, I want to make them highly parallelizable for use in a C# Parallel.Foreach loop. In order to accomplish this, I've gone with an approach where each world state is completely immutable, and entities update by creating clones of themselves with changes applied, which are then placed into the next stored world state. This also makes all collisions order independent, since each entity must look at the current world state and independently decide how it should react to the collision.

[img]http://i.imgur.com/FRSdl.png[/img]

External calls to modify the world are queued as actions to perform on the next Update(). Once Update() is called, a new mutable world state is created. All queued actions are retrieved and performed on that new world state, and each entity from the old state creates an updated clone to place in the new state. The update process can generate two kinds of events. Internal events are immediately queued for the next update, while external events are stored and frozen in the world state. The new world state, with the updated entities (also added/removed entities) and any external events that were generated that timestep, gets frozen and goes out to whoever wants to visualize that state (server broadcasts to clients, clients display).

The area highlighted in orange is where I want to parallelize. Here's the update process for each entity in detail:

[img]http://i.imgur.com/3yOXN.png[/img]

Entities read the previous world state, themselves, and take any incoming events relevant to them (an explosion nearby, a change in their control state) and produce an immutable clone of themselves. Because nobody is writing any data here (and regular C# iterators are thread-safe for read access), each one of these entity updates could theoretically run on its own thread with no problem. So I want to batch their updates.

I guess my question is, does this seem reasonable? Cloning is expensive, but is it as expensive as the calculations needed for each update (collision detection, timer countdowns, control resolution, applying forces, etc.), so I think the tradeoff is worth it. Do any other games work like this? I couldn't find any FPS examples where immutable states are generated by cloning, but most of what we have to work with (Quake 3, Source SDK) didn't design servers with multicore in mind.

Any advice would be helpful. Thanks!
1

Share this post


Link to post
Share on other sites
Functional programming languages tend to encourage (or even enforce) this sort of thing. Immutable state makes sharing data between threads a lot simpler (as you are aware).

I can't really speak as to how well a C# implementation will perform - it may be that you will be hindered by the language's own bias towards mutable state.
0

Share this post


Link to post
Share on other sites
The main thing I'd be worried about is garbage collector pressure created by duplicating your state representations all the time. If possible, I'd recommend looking at double or triple buffering as a means to avoid that.
1

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1328653049' post='4910653']
The main thing I'd be worried about is garbage collector pressure created by duplicating your state representations all the time. If possible, I'd recommend looking at double or triple buffering as a means to avoid that.
[/quote]

Could you elaborate a little bit or point me to a source for double buffering for model state? Everything I turn up is for buffering draws to the screen. Do you mean buffering entity state? I think that if I hold on to previous generated immutable world states I think I'm doing that anyway.

Updating a single entity is one step. You figure out all of the values for the data stored in that entity, and then instantiate that entity with those values in the constructor (using C# readonly variables for immutability).
0

Share this post


Link to post
Share on other sites
[quote name='sufficientreason' timestamp='1328653317' post='4910658']
[quote name='ApochPiQ' timestamp='1328653049' post='4910653']
The main thing I'd be worried about is garbage collector pressure created by duplicating your state representations all the time. If possible, I'd recommend looking at double or triple buffering as a means to avoid that.
[/quote]

Could you elaborate a little bit or point me to a source for double buffering for model state? Everything I turn up is for buffering draws to the screen.[/quote]

Its pretty much analogous to screen buffering. You have two copies of whatever data structures store everything, and instead of creating a new version each update, you just change the data held in the "current" data. At the end of the update, just flip which one is current.

I've used that before on certain subsystems to prevent having to reallocate everything once per update.
1

Share this post


Link to post
Share on other sites
Oh, that's a really good way to do it, thank you! I'll start with the naive approach of constant allocation and dscarding, and try that as an optimization after some profiling if it's necessary.

Aside from that, does this approach look alright? Event queues that only take effect after Update(), and so on? I'll report back with results after I implement it.
0

Share this post


Link to post
Share on other sites
I recently did a small project with multithreading. I had a problem with race conditions, so I implemented a [url="http://en.wikipedia.org/wiki/Semaphore_(programming)"]binary semaphore [/url]to indicate if a shared resource was being used by another thread. It might be something you could find useful. Here is the general idea followed by pseudocode:

I have a static list of objects which need to be accessed by multiple threads. I also need to maintain the list of objects. Part of the list maintenance means that a thread may add or remove objects from the list. If two threads are modifying the size of the list, then a runtime error occurs. So, if a thread needs to access the list, I need to guarantee that the size of the list won't be changed by another thread. I create a static boolean variable to indicate whether or not the list is in use by another thread. When a thread wants to access the list, it checks to see if the list is unlocked before going forward.

[code]static List<gameobject> m_objects = new list<gameobject>();
static bool m_listLocked = false;

Thread 1:
while(m_listLocked);
m_listLocked = true;
foreach(item in the list)
do stuff;
m_listLocked = false;

Thread 2:
while(m_listLocked);
m_listLocked = true;
foreach(item in the list)
do stuff;
m_listLocked = false;
[/code]

I use an empty spin lock, but I could add a short sleep cycle inside of the while loop to save the CPU a bit.

Note that I don't do any thread synchronization with game frames.
1

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