9 ms·
Parallelizing the Naughty Dog Engine Using Fibers [video]
- warmwaffles 10y agoI still don't quite understand what he means by a "fiber". Is this basically 160 blocks of memory allocated on the heap?
- Aeolos 10y agoFiber[1] is another term for coroutines. They are essentially an implementation of cooperative multithreading. [1] https://en.wikipedia.org/wiki/Fiber_%28computer_science%29 https://en.wikipedia.org/wiki/Fiber_%28computer_science%29
- warmwaffles 10y agoAny simple C libraries that demonstrate this? I'm rather curious.
- MarkSweep 10y agoSee libtask: https://swtch.com/libtask/ https://swtch.com/libtask/
- aidenn0 10y agoAnd mongrel2, which I hack on occasionally uses libtask to good effect for this. It's architecture is much simpler than this presentation though because mongrel2 is one thread per process, so inside the process there is only one fiber executing at any given time.
- corysama 10y agoThere's no official support for fibers in Posix. There are lots of implementations anyway, but it's hard to get completely right. Long ago, the Win32 team got tired of people rolling their own subtley incorrect/incomplete fiber implementations for Windows and added official support to put an end to that support issue https://msdn.microsoft.com/en-us/library/windows/desktop/ms682661.aspx https://msdn.microsoft.com/en-us/library/windows/desktop/ms6...
- btrask 10y agoActually, there is: https://en.wikipedia.org/wiki/Setcontext https://en.wikipedia.org/wiki/Setcontext A library I personally recommend, which supports several platforms and back-ends: http://byuu.org/library/libco/ http://byuu.org/library/libco/
- deleted 10y ago[deleted]
- sillysaurus3 10y agosetcontext isn't Posix, so corysama is the best kind of correct. But setcontext exists on most flavors of Linux, FreeBSD, and OS X, so you can basically assume it's supported.
- gpderetta 10y agosetcontext was posix until POSIX.2008.
- deleted 10y ago[deleted]
- david-given 10y agoA few years back I built an app which used coroutines extensively (<plug> greylisting SMTP proxy, now abandoned because I don't run my own mailserver any more, http://cowlark.com/spey/ http://cowlark.com/spey/ </plug>). I originally used getcontext/setcontext. Superficially it was really easy, but turned into a goddamned nightmare. The problem is that pthreads gets on really badly with user-allocated stacks; some pthreads implementations find the current thread ID by looking at a magic value on the current thread's stack, and of course with a user-allocated stack this wasn't there. But why was I using pthreads alongside coroutines, you ask? Well, I wasn't. But I was linking to a library which was referring to pthreads symbols, even though it wasn't calling them; which made the linker automatically pull in the thread-safe glibc, so malloc/free ended up wrapping in mutexes... The net effect was bizarre and infrequent crashes that only showed up on machines which weren't mine and that I didn't have access to. I spent months debugging that. Eventually I switched to simulating coroutines using pthreads with a big lock so that only one thread could run at a time. (Which actually turned out to be better, because now I could do some operations properly in parallel.) Beware of setcontext! Bewaaaaaare!
- notduncansmith 10y agoLibmill could be worth a look. https://github.com/sustrik/libmill https://github.com/sustrik/libmill
- deleted 10y ago[deleted]
- threeseed 10y agoBasically they are fine grained tasks that get executed by a pool of threads.
- corysama 10y agoA fiber is a thread in that it executes code and it has it's own stack, but it does not have premption. The OS won't thread-switch between fibers for you. You must manually switch in and out of executing it. Running in a fiber doesn't mean you've stopped using threads. A fiber runs in to context of a thread and that thread still has premption. But, inside the thread, you can call functions to switch stacks in order to manually pause and resume execution inside the fiber.
- warmwaffles 10y agoOh that actually makes a lot of sense
- 2bitencryption 10y agoI've noticed that lots of recent advances in threading are all just ways to avoid context switching of threads and instead handle those switches manually (e.g. "green threads" in Python).
- pvg 10y agoThese aren't particularly recent. Java originally had green threads; I think the term itself is from there.
- wavesplash 10y agoGreen threads were a thing long before Java.
- corysama 10y agoSimilar talk: CppCon 2014: Jeff Preshing "How Ubisoft Develops Games for Multicore - Before and After C++11" https://www.youtube.com/watch?v=X1T3IQ4N-3g https://www.youtube.com/watch?v=X1T3IQ4N-3g Ubisoft's talk spends more time getting into the weeds with atomic ops. Naughty Dog's is more of an architecture discussion. If you can only watch one, I'll recommend Naughty Dog's.
- hyperpallium 10y agoIs there a downloadable version?
- felixguendling 10y agoNot directly. I think it is part of the Playstation SDK (but I'm not a game developer). There are libraries that are based on the same concept (many execution contexts per thread): * https://github.com/RichieSams/FiberTaskingLib * https://swtch.com/libtask/ * https://github.com/halayli/lthread * https://github.com/stevedekorte/coroutine RethinkDB are using something like this: http://rethinkdb.com/blog/improving-a-large-c-project-with-coroutines/ http://rethinkdb.com/blog/improving-a-large-c-project-with-c... http://rethinkdb.com/blog/making-coroutines-fast/ http://rethinkdb.com/blog/making-coroutines-fast/
- clevernickname 10y agoCoroutine libraries are in the class of libraries that are small and easy enough to write (if you know assembly) that most people roll their own. libco (used in the bsnes/higan emulator) is a good, portable one.
- dpc_pw 10y agoFor fibers (AKA Coroutines) in Rust, see mioco https://github.com/dpc/mioco https://github.com/dpc/mioco
- rawnlq 10y agoAn older but similar approach from Doom III engine: http://fabiensanglard.net/doom3_bfg/threading.php http://fabiensanglard.net/doom3_bfg/threading.php I wonder if there is a good open-sourced C++11 project for this pattern? (a job/task queue) Also how does this pattern compare with just using future/promises with a parallel executor? https://code.facebook.com/posts/1661982097368498/futures-for-c-11-at-facebook/ https://code.facebook.com/posts/1661982097368498/futures-for... or grand central dispatch from objective C?
- lbrandy 10y agoOur (facebook's) folly library has lots of components for doing stuff using fibers[1]. That sits with folly::futures[2] and wangle[3]. That said, without proper language/compiler support, though, it's difficult to make them "safe" (for some definition). Even within fb, given these libraries, we are pretty wary of using fibers with C++ unless absolutely necessary. You gotta really need it. This is where the coroutines proposals working their way through the C++ committee can make lives better. [1] https://github.com/facebook/folly/tree/master/folly/fibers https://github.com/facebook/folly/tree/master/folly/fibers [2] https://github.com/facebook/folly/tree/master/folly/futures https://github.com/facebook/folly/tree/master/folly/futures [3] https://github.com/facebook/wangle https://github.com/facebook/wangle
- tomlu 10y agoSuper interesting, but the audio level is too quiet for my laptop even with everything set to max. Can it be downloaded from anywhere?
- wallacoloo 10y agoFWIW, youtube-dl can easily download the video. i.e. `youtube-dl http://www.gdcvault.com/play/1022186/Parallelizing-the-Naughty-Dog-Engine` http://www.gdcvault.com/play/1022186/Parallelizing-the-Naugh.... You can use it almost everywhere, despite the name.
- endergen 10y agoI had the same issue on my Mac. Ended up buying Boom 2 to boost the volume. I wouldn't have just for one talk, but it's been happening a lot lately where I find content volume to be too low.
- djhworld 10y agoNaughty Dog must be doing something completely off the charts in comparison to other developers, I challenge anyone to look at Uncharted 4 and provide to me a better looking game.
- fleetfox 10y agoIt doesn't look that great compared to fresh PC titles. Are other PS4 games looking worse?
- sangnoir 10y agoI think CD Projekt RED are definitely in the same league as Naughty Dog - The Witcher 3 looks amazing (disclaimer: I haven't seen it on the PlayStation)
- Narishma 10y agoIt has poor performance on consoles.
- k__ 10y agoCool. Me and a friend did this at university with the Ogre 3D engine and failed miserably, because I didn't know much abut C++ or thread safety. We tried the actor model, every game object became an actor and sent messages around. The actors would then be spread over the amount of CPUs and the performance should rise with every CPU. In the end we got it running on multiple cores, but the message overhead killed the performance, haha.
- wolfgke 10y ago> Me and a friend did this at university with the Ogre 3D engine and failed miserably, because I didn't know much abut C++ or thread safety. Fiber safety is much more easy to assure than thread-safety. Under Windows using fibers is very simple (just use ConvertThreadToFiber on your current thread if you have not done already and then call CreateFiber) in opposite to POSIX systems where you will have to roll up your own implementation of fibers (or use some existing library which is not part of the POSIX standard).
- gpderetta 10y agomakecontext/swapcontext was the POSIX approved way to do fibers. It was removed from the last POSIX standard because who uses coroutines or fibers today? Around the same time every other mainstream language was adding support for corutines/generators/fibers.
- wolfgke 10y agomakecontext/swapcontex doesn't have automatically growing stack (opposed to their WinAPI equivalents). Yes, you can do this, too, I know. But if you don't want to go into the gory details here you either have a alloc a "large enough" buffer for your coroutines thus probably wasting memory.
- gpderetta 10y agotechnically you only waste address space which should be plenty enough at least for a while.
- aidenn0 10y agoA note is that input latency will be 17ms longer with the pipelining versus running at 30fps. That's still almost certainly a huge win though, as the smoother rendering at the higher rate will make it perceived as more responsive, particularly since input latency doesn't go much below 100ms these days.
- w0utert 10y agoWhy? As he explains in the Q&A session after the talk, two frames of latency at 30Hz (2 x ~33ms) is still more than 3 frames latency at 60Hz (3 x ~16ms). You will always have at least 2 frames latency if you split the game logic and render stages like they did for TLoU on PS3, so in fact their PS4 engine has better framerate and latency.
- aidenn0 10y agoBefore they implemented pipelining, they had all of the stages running in under 33ms on the ps4. Yes, this is lower input latency than on the ps3, but that's beside the point.