SlideShare une entreprise Scribd logo
1  sur  17
Concurrency & Parallelism in UE4
Tips for programming with many CPU cores
Gerke Max Preussner
max.preussner@epicgames.com
Synchronization Primitives
Atomics
Locking
Signaling
Waiting
FPlatformAtomics
• InterlockedAdd
• InterlockedCompareExchange (-Pointer)
• InterlockedDecrement (-Increment)
• InterlockedExchange (-Pointer)
64- and 128-bit overloads on supported platforms
Synchronization Primitives
Atomics
Locking
Signaling
Waiting
// Example
class FThreadSafeCounter
{
public:
int32 Add( int32 Amount )
{
return FPlatformAtomics::InterlockedAdd(&Counter, Amount);
}
private:
volatile int32 Counter;
};
Synchronization Primitives
Atomics
Locking
Signaling
Waiting
Critical Sections
• FCriticalSection implements synchronization object
• FScopeLock for scope level locking using a critical section
• Fast if the lock is not activated
Spin Locks
• FSpinLock can be locked and unlocked
• Sleeps or spins in a loop until unlocked
• Default sleep time is 0.1 seconds
Synchronization Primitives
Atomics
Locking
Signaling
Waiting
Semaphores
• Like mutex with signaling mechanism
• Only implemented for Windows and hardly used
• API will probably change
• Use FEvent instead
Synchronization Primitives
Atomics
Locking
Signaling
Waiting
FEvent
• Blocks a thread until triggered or timed out
• Frequently used to wake up worker threads
FScopedEvent
• Wraps an FEvent that blocks on scope exit
// Example for scoped events
{
FScopedEvent Event;
DoWorkOnAnotherThread(Event.Get());
// stalls here until other thread triggers Event
}
High Level Constructs
Containers
Helpers
General Thread-safety
• Most containers (TArray, TMap, etc.) are not thread-safe
• Use synchronization primitives in your own code where needed
TLockFreePointerList
• Lock free, stack based and ABA resistant
• Used by Task Graph system
TQueue
• Uses a linked list under the hood
• Lock and contention free for SPSC
• Lock free for MPSC
TDisruptor (currently not part of UE4)
• Lock free MPMC queue using a ring buffer
High Level Constructs
Containers
Helpers
FThreadSafeCounter
FThreadSingleton
• Singleton that creates an instance per thread
FMemStack
• Fast, temporary per-thread memory allocation
TLockFreeClassAllocator, TLockFreeFixedSizeAllocator
• Another fast allocator for instances of T
FThreadIdleStats
• Measures how often a thread is idle
Parallelization
Threads
Task Graph
Processes
Messaging
FRunnable
• Platform agnostic interface
• Implement Init(), Run(), Stop() and Exit() in your sub-class
• Launch with FRunnableThread::Create()
• FSingleThreadRunnable when multi-threading is disabled
FQueuedThreadPool
• Carried over from UE3 and still works the same way
• Global general purpose thread pool in GThreadPool
• Not lock free
Parallelization
Threads
Task Graph
Processes
Messaging
Game Thread
• All game code, Blueprints and UI
• UObjects are not thread-safe!
Render Thread
• Proxy objects for Materials, Primitives, etc.
Stats Thread
• Engine performance counters
Parallelization
Threads
Task Graph
Processes
Messaging
Task Based Multi-Threading
• Small units of work are pushed to available worker threads
• Tasks can have dependencies to each other
• Task Graph will figure out order of execution
Used by an increasing number of systems
• Animation evaluation
• Message dispatch and serialization in Messaging system
• Object reachability analysis in garbage collector
• Render commands in Rendering sub-system
• Various tasks in Physics sub-system
• Defer execution to a particular thread
Parallelization
Threads
Task Graph
Processes
Messaging
FPlatformProcess
• CreateProc() executes an external program
• LaunchURL() launches the default program for a URL
• IsProcRunning() checks whether a process is still running
• Plus many other utilities for process management
FMonitoredProcess
• Convenience class for launching and monitoring processes
• Event delegates for cancellation, completion and output
Parallelization
Threads
Task Graph
Processes
Messaging
Unreal Message Bus (UMB)
• Zero configuration intra- and inter-process communication
• Request-Reply and Publish-Subscribe patterns supported
• Messages are simple UStructs
Transport Plug-ins
• Seamlessly connect processes across machines
• Only implemented for UDP right now (prototype)
Upcoming Features
Critical sections & events
• Better debugging and profiling support
Task Graph
• Improvements and optimizations
UObjects
• Thread-safe construction and destruction
Parallel Rendering
• Implemented inside renderers, not on RHI API level
Messaging
• UDP v2 (“Hammer”), BLOB attachments, more robust, breakpoints
• Named Pipes and other transport plug-ins
More lock-free containers
Questions?
Documentation, Tutorials and Help at:
• AnswerHub:
• Engine Documentation:
• Official Forums:
• Community Wiki:
• YouTube Videos:
• Community IRC:
Unreal Engine 4 Roadmap
• lmgtfy.com/?q=Unreal+engine+Trello+
http://answers.unrealengine.com
http://docs.unrealengine.com
http://forums.unrealengine.com
http://wiki.unrealengine.com
http://www.youtube.com/user/UnrealDevelopmentKit
#unrealengine on FreeNode

Contenu connexe

Tendances

East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...Gerke Max Preussner
 
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for CodersEast Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for CodersGerke Max Preussner
 
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)Gerke Max Preussner
 
GDCE 2015: Blueprint Components to C++
GDCE 2015: Blueprint Components to C++GDCE 2015: Blueprint Components to C++
GDCE 2015: Blueprint Components to C++Gerke Max Preussner
 
West Coast DevCon 2014: Build Automation - Epic’s Build Tools & Infrastructure
West Coast DevCon 2014: Build Automation - Epic’s Build Tools & InfrastructureWest Coast DevCon 2014: Build Automation - Epic’s Build Tools & Infrastructure
West Coast DevCon 2014: Build Automation - Epic’s Build Tools & InfrastructureGerke Max Preussner
 
Course 102: Lecture 1: Course Overview
Course 102: Lecture 1: Course Overview Course 102: Lecture 1: Course Overview
Course 102: Lecture 1: Course Overview Ahmed El-Arabawy
 
Python intro and competitive programming
Python intro and competitive programmingPython intro and competitive programming
Python intro and competitive programmingSuraj Shah
 
.NET MeetUp Prague 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund
.NET MeetUp Prague 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund.NET MeetUp Prague 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund
.NET MeetUp Prague 2017 - Challenges of Managing CoreFX repo -- Karel ZikmundKarel Zikmund
 
.NET Fringe 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund
.NET Fringe 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund.NET Fringe 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund
.NET Fringe 2017 - Challenges of Managing CoreFX repo -- Karel ZikmundKarel Zikmund
 
.NET MeetUp Prague 2017 - .NET Standard -- Karel Zikmund
.NET MeetUp Prague 2017 - .NET Standard -- Karel Zikmund.NET MeetUp Prague 2017 - .NET Standard -- Karel Zikmund
.NET MeetUp Prague 2017 - .NET Standard -- Karel ZikmundKarel Zikmund
 
Embedded Systems: Lecture 1: Course Overview
Embedded Systems: Lecture 1: Course OverviewEmbedded Systems: Lecture 1: Course Overview
Embedded Systems: Lecture 1: Course OverviewAhmed El-Arabawy
 
Embedded Systems: Lecture 4: Selecting the Proper RTOS
Embedded Systems: Lecture 4: Selecting the Proper RTOSEmbedded Systems: Lecture 4: Selecting the Proper RTOS
Embedded Systems: Lecture 4: Selecting the Proper RTOSAhmed El-Arabawy
 
DevOps: What is This Puppet You Speak Of?
DevOps: What is This Puppet You Speak Of?DevOps: What is This Puppet You Speak Of?
DevOps: What is This Puppet You Speak Of?Rob Reynolds
 
Course 101: Lecture 5: Linux & GNU
Course 101: Lecture 5: Linux & GNU Course 101: Lecture 5: Linux & GNU
Course 101: Lecture 5: Linux & GNU Ahmed El-Arabawy
 
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)Ahmed El-Arabawy
 

Tendances (20)

East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
 
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for CodersEast Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
East Coast DevCon 2014: Programming in UE4 - A Quick Orientation for Coders
 
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
FMX 2017: Extending Unreal Engine 4 with Plug-ins (Master Class)
 
GDCE 2015: Blueprint Components to C++
GDCE 2015: Blueprint Components to C++GDCE 2015: Blueprint Components to C++
GDCE 2015: Blueprint Components to C++
 
West Coast DevCon 2014: Build Automation - Epic’s Build Tools & Infrastructure
West Coast DevCon 2014: Build Automation - Epic’s Build Tools & InfrastructureWest Coast DevCon 2014: Build Automation - Epic’s Build Tools & Infrastructure
West Coast DevCon 2014: Build Automation - Epic’s Build Tools & Infrastructure
 
Course 102: Lecture 1: Course Overview
Course 102: Lecture 1: Course Overview Course 102: Lecture 1: Course Overview
Course 102: Lecture 1: Course Overview
 
Java and the JVM
Java and the JVMJava and the JVM
Java and the JVM
 
Python intro and competitive programming
Python intro and competitive programmingPython intro and competitive programming
Python intro and competitive programming
 
CPAN Curation
CPAN CurationCPAN Curation
CPAN Curation
 
.NET MeetUp Prague 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund
.NET MeetUp Prague 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund.NET MeetUp Prague 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund
.NET MeetUp Prague 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund
 
.NET Fringe 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund
.NET Fringe 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund.NET Fringe 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund
.NET Fringe 2017 - Challenges of Managing CoreFX repo -- Karel Zikmund
 
.NET MeetUp Prague 2017 - .NET Standard -- Karel Zikmund
.NET MeetUp Prague 2017 - .NET Standard -- Karel Zikmund.NET MeetUp Prague 2017 - .NET Standard -- Karel Zikmund
.NET MeetUp Prague 2017 - .NET Standard -- Karel Zikmund
 
Embedded Systems: Lecture 1: Course Overview
Embedded Systems: Lecture 1: Course OverviewEmbedded Systems: Lecture 1: Course Overview
Embedded Systems: Lecture 1: Course Overview
 
C 102 lec_29_what_s_next
C 102 lec_29_what_s_nextC 102 lec_29_what_s_next
C 102 lec_29_what_s_next
 
Embedded Systems: Lecture 4: Selecting the Proper RTOS
Embedded Systems: Lecture 4: Selecting the Proper RTOSEmbedded Systems: Lecture 4: Selecting the Proper RTOS
Embedded Systems: Lecture 4: Selecting the Proper RTOS
 
XNA L01–Introduction
XNA L01–IntroductionXNA L01–Introduction
XNA L01–Introduction
 
DevOps: What is This Puppet You Speak Of?
DevOps: What is This Puppet You Speak Of?DevOps: What is This Puppet You Speak Of?
DevOps: What is This Puppet You Speak Of?
 
Course 101: Lecture 5: Linux & GNU
Course 101: Lecture 5: Linux & GNU Course 101: Lecture 5: Linux & GNU
Course 101: Lecture 5: Linux & GNU
 
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
Embedded Systems: Lecture 10: Introduction to Git & GitHub (Part 1)
 
Killer Robots 101 with Gobot
Killer Robots 101 with GobotKiller Robots 101 with Gobot
Killer Robots 101 with Gobot
 

Similaire à West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programming with many CPU cores

.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/MultitaskingSasha Kravchuk
 
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017OpenEBS
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreadingssusere538f7
 
Multithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfMultithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfHarika Pudugosula
 
Threading Successes 03 Gamebryo
Threading Successes 03   GamebryoThreading Successes 03   Gamebryo
Threading Successes 03 Gamebryoguest40fc7cd
 
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...CanSecWest
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsPeter Tröger
 
Systems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesSystems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesHelpWithAssignment.com
 
MULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxMULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxSaiDhanushM
 
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...Alexandre Moneger
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfKrystian Zybała
 
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1Building the Internet of Things with Thingsquare and Contiki - day 2 part 1
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1Adam Dunkels
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreadingTuan Chau
 

Similaire à West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programming with many CPU cores (20)

.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 
Operating System lab
Operating System labOperating System lab
Operating System lab
 
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
 
Multithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfMultithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdf
 
Threading Successes 03 Gamebryo
Threading Successes 03   GamebryoThreading Successes 03   Gamebryo
Threading Successes 03 Gamebryo
 
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
CSW2017Richard Johnson_harnessing intel processor trace on windows for vulner...
 
Os lectures
Os lecturesOs lectures
Os lectures
 
Threads
ThreadsThreads
Threads
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - Threads
 
Systems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesSystems Programming Assignment Help - Processes
Systems Programming Assignment Help - Processes
 
MULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxMULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptx
 
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
 
Chap7 slides
Chap7 slidesChap7 slides
Chap7 slides
 
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1Building the Internet of Things with Thingsquare and Contiki - day 2 part 1
Building the Internet of Things with Thingsquare and Contiki - day 2 part 1
 
Java On CRaC
Java On CRaCJava On CRaC
Java On CRaC
 
Introduction to multicore .ppt
Introduction to multicore .pptIntroduction to multicore .ppt
Introduction to multicore .ppt
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreading
 

Dernier

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 

Dernier (20)

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 

West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programming with many CPU cores

  • 1. Concurrency & Parallelism in UE4 Tips for programming with many CPU cores Gerke Max Preussner max.preussner@epicgames.com
  • 2. Synchronization Primitives Atomics Locking Signaling Waiting FPlatformAtomics • InterlockedAdd • InterlockedCompareExchange (-Pointer) • InterlockedDecrement (-Increment) • InterlockedExchange (-Pointer) 64- and 128-bit overloads on supported platforms
  • 3. Synchronization Primitives Atomics Locking Signaling Waiting // Example class FThreadSafeCounter { public: int32 Add( int32 Amount ) { return FPlatformAtomics::InterlockedAdd(&Counter, Amount); } private: volatile int32 Counter; };
  • 4. Synchronization Primitives Atomics Locking Signaling Waiting Critical Sections • FCriticalSection implements synchronization object • FScopeLock for scope level locking using a critical section • Fast if the lock is not activated Spin Locks • FSpinLock can be locked and unlocked • Sleeps or spins in a loop until unlocked • Default sleep time is 0.1 seconds
  • 5. Synchronization Primitives Atomics Locking Signaling Waiting Semaphores • Like mutex with signaling mechanism • Only implemented for Windows and hardly used • API will probably change • Use FEvent instead
  • 6. Synchronization Primitives Atomics Locking Signaling Waiting FEvent • Blocks a thread until triggered or timed out • Frequently used to wake up worker threads FScopedEvent • Wraps an FEvent that blocks on scope exit // Example for scoped events { FScopedEvent Event; DoWorkOnAnotherThread(Event.Get()); // stalls here until other thread triggers Event }
  • 7. High Level Constructs Containers Helpers General Thread-safety • Most containers (TArray, TMap, etc.) are not thread-safe • Use synchronization primitives in your own code where needed TLockFreePointerList • Lock free, stack based and ABA resistant • Used by Task Graph system TQueue • Uses a linked list under the hood • Lock and contention free for SPSC • Lock free for MPSC TDisruptor (currently not part of UE4) • Lock free MPMC queue using a ring buffer
  • 8. High Level Constructs Containers Helpers FThreadSafeCounter FThreadSingleton • Singleton that creates an instance per thread FMemStack • Fast, temporary per-thread memory allocation TLockFreeClassAllocator, TLockFreeFixedSizeAllocator • Another fast allocator for instances of T FThreadIdleStats • Measures how often a thread is idle
  • 9. Parallelization Threads Task Graph Processes Messaging FRunnable • Platform agnostic interface • Implement Init(), Run(), Stop() and Exit() in your sub-class • Launch with FRunnableThread::Create() • FSingleThreadRunnable when multi-threading is disabled FQueuedThreadPool • Carried over from UE3 and still works the same way • Global general purpose thread pool in GThreadPool • Not lock free
  • 10. Parallelization Threads Task Graph Processes Messaging Game Thread • All game code, Blueprints and UI • UObjects are not thread-safe! Render Thread • Proxy objects for Materials, Primitives, etc. Stats Thread • Engine performance counters
  • 11. Parallelization Threads Task Graph Processes Messaging Task Based Multi-Threading • Small units of work are pushed to available worker threads • Tasks can have dependencies to each other • Task Graph will figure out order of execution Used by an increasing number of systems • Animation evaluation • Message dispatch and serialization in Messaging system • Object reachability analysis in garbage collector • Render commands in Rendering sub-system • Various tasks in Physics sub-system • Defer execution to a particular thread
  • 12.
  • 13. Parallelization Threads Task Graph Processes Messaging FPlatformProcess • CreateProc() executes an external program • LaunchURL() launches the default program for a URL • IsProcRunning() checks whether a process is still running • Plus many other utilities for process management FMonitoredProcess • Convenience class for launching and monitoring processes • Event delegates for cancellation, completion and output
  • 14. Parallelization Threads Task Graph Processes Messaging Unreal Message Bus (UMB) • Zero configuration intra- and inter-process communication • Request-Reply and Publish-Subscribe patterns supported • Messages are simple UStructs Transport Plug-ins • Seamlessly connect processes across machines • Only implemented for UDP right now (prototype)
  • 15.
  • 16. Upcoming Features Critical sections & events • Better debugging and profiling support Task Graph • Improvements and optimizations UObjects • Thread-safe construction and destruction Parallel Rendering • Implemented inside renderers, not on RHI API level Messaging • UDP v2 (“Hammer”), BLOB attachments, more robust, breakpoints • Named Pipes and other transport plug-ins More lock-free containers
  • 17. Questions? Documentation, Tutorials and Help at: • AnswerHub: • Engine Documentation: • Official Forums: • Community Wiki: • YouTube Videos: • Community IRC: Unreal Engine 4 Roadmap • lmgtfy.com/?q=Unreal+engine+Trello+ http://answers.unrealengine.com http://docs.unrealengine.com http://forums.unrealengine.com http://wiki.unrealengine.com http://www.youtube.com/user/UnrealDevelopmentKit #unrealengine on FreeNode

Notes de l'éditeur

  1. *** Prepared and presented by Gerke Max Preussner for West Coast MiniDevCon 2014, July 21-24th Hi, my name is Max. I work as a Sr. Engine Programmer at Epic Games and would like to give you a quick overview of multi-threading capabilities in Unreal Engine 4. Games and applications that perform concurrent tasks on one or multiple CPUs or CPU cores are generally hard to write, proof correct, debug and maintain. Not only are there many concepts and mechanisms you have to learn, but also a wide range of very subtle issues that are often dependent on the operating system and the processor it is running on. Unreal Engine provides a range of features, helpers and tools to develop for multi-core computers and mobile devices more easily.
  2. At the lowest level, the Engine provides a number of synchronization primitives used to synchronize memory access across different threads. Commonly used primitives are atomic operations, mechanisms for locking and signaling threads, as well as for waiting on events from other threads. Atomic operations allow the CPU to read and write a memory location in the same indivisible bus operation. Although this seems to be a rather trivial feature, it is the foundation of many higher level multi-threading mechanisms. The main advantage of atomics is that they are very quick compared to locks, and they avoid the common problems of mutual exclusion and deadlocks. Unreal Engine exposes various atomic operations, such as incrementing and compare-and-exchange in a platform agnostic API. Most atomic operations operate on 32-bit values, but we also provide APIs for 64- and 128-bit values, if the particular platform supports them.
  3. Here is an example from our code base that uses atomics to implement a thread-safe counter. A normal increment instruction for integers may involve multiple bus operations that read the current value, increment it and write it back to memory. During these multiple steps it is possible – in principle and in practice – that the operating system suspends our thread, and another thread that also wishes to increment the counter modifies our value, causing that increment to be lost when our thread resumes. The use of ‘interlocked add’ in this example ensures that the increment operation cannot be interrupted. Because the counter value can change in asynchronous ways that the compiler cannot foresee, we use the volatile keyword to prevent optimizations of code that accesses the value.
  4. The main locking mechanisms used in Unreal Engine are Critical Sections and Spin Locks. Critical Sections provide mutually exclusive access to a single resource, usually the internals of a thread-safe class. In combination with Scope Locks they ensure that a given block of code can be executed by only one thread at a time. Compared to atomic operations, they can be a lot slower when the lock is accessed by more than one thread. Spin Locks also provide exclusive access, but instead of using the mutex facilities provided by the OS they sleep in a loop until the lock is removed. The sleep time is configurable (0.1 seconds by default), and for very small durations they may actually just do a no-op spin loop under the hood.
  5. Semaphores are similar to mutual exclusion locks, but they also contain a signaling mechanism. There is currently an implementation in FPlatformProcess, but it is not quite what one would expect, only implemented for Windows and only used in Lightmass right now. A better choice for signaling between threads would be the FEvent class…
  6. … which also uses a mutex under the hood. We frequently use events in worker threads to notify them about newly added work items or that we wish to shut them down. If you just want to send a one-shot signal from another thread, you can pass an FScopedEvent into it, and your current thread will stall when the event exits its scope until it is triggered.
  7. Most container types and classes in the Engine, including TArray and TMap are not thread-safe. We usually just manually add atomics and mutual exclusion locks in our code on an as-needed basis. Of course, locks are not always desirable, so we are also starting to implement basic lock-free containers that are thread-safe. A very useful lock-free container is TQueue, which is lock- and contention-free for SPSC and lock-free for MPSC. It is used in many places that transfer data from one thread to another. (We also implemented a lock-free MPMC queue using the Disruptor pattern at some point – it is not part of UE4 right now, but if you need it, send me an email!)
  8. We also provide some high level helper objects that are built on top of the synchronization primitives, some of which are shown here. The thread-safe counter we have already seen in the code example earlier. Thread-safe singletons are special singletons that create one instance of a class per thread instead of per process. Memory stacks allow for ultra-fast per-thread memory allocations. We also have a way to measure thread idle times that is always enabled, even in Shipping builds.
  9. Traditionally, the main work horse of multi-threaded programming are operating system threads. We expose those in the form of the FRunnable base class that you can inherit from. If your thread object inherits from FSingleThreadRunnable, it can also be used when multi-threading is disabled or unavailable. The thread pool from Unreal Engine 3 is still available as well. You can create your own thread pools or just use the global one in GThreadPool.
  10. Unreal Engine 4 uses threads for a number of things, but the most important ones are GameThread, RenderThread and StatsThread. All code that interfaces with UObjects must run on the GameThread, because UObjects are not thread-safe. The RenderThread allows us to perform rendering related tasks in parallel with the game logic. We recently also added the StatsThread that collects and processes a number of perforce counters for profiling.
  11. Threads are very heavyweight. Task Based Multi-Threading allows for more fine grained, lightweight use of multiple cores. A task is a small, parallelizable unit of work that can be scheduled to available worker threads that are managed by our Task Graph system. As the name suggests, tasks can also have dependencies on each other, so that certain tasks are not executed until other tasks completed successfully. The Task Graph is used by an increasing number of systems in the Engine, and we encourage you to use it, too.
  12. The Profiling Tools in the Editor come with a handy feature for visually debugging threads. It shows the cycle counter based statistics, as well as Task Graph usage.
  13. Unreal Engine 4 also has platform agnostic APIs for creating and monitoring external processes. You can start programs, launch web URLs or open files in other applications. If you need fine control over creating and monitoring external processes, you can use the FMonitoredProcess class.
  14. Messaging is another important architectural pattern for multi-threaded and distributed applications. Unreal Message Bus implements a message passing infrastructure that can be used to send commands, events or data to other threads, processes and even computers. Our main goal was to unify all the different network communication technologies we used for our tools in UE3, and to allow users to create their own distributed applications and tools without having to deal with low level technicalities, such as network protocols and how exactly messages get from A to B. Messages are simple Unreal structures (that currently require a tiny bit of boilerplate, but we will fix that soon). They can be sent between devices, even across the internet - if desired – and all our Unreal Frontend tools heavily rely on it. Messaging is a great way to get information out of your code classes in a loosely coupled way, and future use cases may include achievement systems, event processing for AI and real-time remote editing on game consoles and mobile phones.
  15. Debugging the flow of messages tends to be quite cumbersome in code, so the Messaging system comes with a visual debugging tool as well. It is not quite finished yet, but already functional.
  16. We are continuously working on improving and extending Unreal Engine 4. That UObjects are not thread-safe is sometimes a limitation, and we are currently working on allowing their construction and destruction on non-Game threads. We are also working on parallelizing the Renderer. This will be implemented not in the RHI API, but in the renderers themselves and utilize the Task Graph. The Messaging System will become very important for the new Editor tools we have scheduled for next year, so we will make it more powerful and easier to use as well. Much of our thread-safe code is currently lock based, and we have gotten good results with it, but we are also planning to add more generic lock-free containers.
  17. With that I would like to conclude this talk. Make sure to check out our extensive materials on the internet, all of which are available for free – even if you don’t have a subscription yet. Are there any questions I can answer right now?