multithreading in visual effects
TRANSCRIPT
![Page 1: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/1.jpg)
![Page 2: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/2.jpg)
Multithreading in Visual EffectsJeff Lait
Side Effects Software Inc.
Your logo on white
centered in this space
![Page 3: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/3.jpg)
Multithreading in Houdini
Definitions
![Page 4: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/4.jpg)
Houdini
![Page 5: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/5.jpg)
What are Nodes?
![Page 6: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/6.jpg)
Mantra
![Page 7: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/7.jpg)
VEX
• Our SIMD shading language
• Superficially similar to Renderman
Shading Language or GLSL
– But is not just used for shading!
– Deformation, Simulation, Procedurals, etc..
![Page 8: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/8.jpg)
Multithreading in Houdini
Distribution
![Page 9: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/9.jpg)
Distribution• Why thread when you can run multiple
processes?
– Usual excuse for GIL in Python
• We already all distribute: Renderfarms
![Page 10: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/10.jpg)
Only One Mouse
![Page 11: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/11.jpg)
Distribution• "All programming is an exercise in
caching.”
– Terje Mathisen
• Difficulty is sharing cached structures
• CPU architectures hardware-
accelerate sharing
![Page 12: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/12.jpg)
Distribution• Distribution forces data locality
![Page 13: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/13.jpg)
Multithreading in Houdini
Challenges
![Page 14: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/14.jpg)
Venerable
• Houdini 1.0 released in 1996
– Some code survives from original PRISMS
• Core duo released in 2007
![Page 15: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/15.jpg)
Interrelated• Every part talks to
every other part
• This behavior is not
the exception!
![Page 16: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/16.jpg)
Dependencies are Computed
![Page 17: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/17.jpg)
Multithreading in Houdini
Methodology
![Page 18: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/18.jpg)
Incremental Changes
• Continuous
integration
• Bend without
breaking
![Page 19: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/19.jpg)
Statics: Not just for physics!
• Grepping for statics– nm libGEO.so | c++filt | grep -i '^[0-9a-F]* [bcdgs]‘– Need additional filters for guards, vtables, etc.
• Intel Compiler warnings– icc –ww1711 –c foo.cc– Warns about writes to statically allocated data
– Avoids false positives about const-like structures
– #define TLA_THREADSAFE_STATIC_WRITE(code) \__pragma(warning(disable:1711));CODE;__pragma(warning(default:1711))
![Page 20: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/20.jpg)
Focusing on the Stupidly Parallel
• Make it Easy
– To Write
– To Convert
– To Undo
![Page 21: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/21.jpg)
Stupidly Parallel: VEX
![Page 22: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/22.jpg)
Stupidly Parallel: Task vs
Thread• Task Based
– tbb::parallel_for
– Functor runs once per chunk
– Better load balancing
• Thread Based
– Thread-pools like UT_ThreadedAlgorithm
– Functor runs once per thread
– Less marshalling
![Page 23: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/23.jpg)
Stupidly Parallel: Thread
• For existing code, parameter list is often already minimized
– The state before the critical for loop is often not!
• Task based functors require marshalling parms and locals
• The inner loop is textually separated from the setup code
![Page 24: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/24.jpg)
Stupidly Parallel: Task
• UTparallelFor
– Mandatory grain size
– Early exit to inline version
• UTparallelForLightItems
• UTparallelForHeavyItems
• UTserialFor
![Page 25: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/25.jpg)
Multithreading in Houdini
Patterns
![Page 26: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/26.jpg)
Reentrancy
• Always be reentrant.
![Page 27: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/27.jpg)
Never Lock
• Everything you were taught about clever
synchronization is irrelevant
• You do not own the CPU
• You do not even know if there is a CPU
![Page 28: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/28.jpg)
Atomics are Slow
• Treat them as guaranteed uncached
memory
![Page 29: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/29.jpg)
Never Blindly Fork
• Always consider grain size
• Always consider single vs multithreaded
![Page 30: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/30.jpg)
Determinism• Produce same results regardless of
lock resolution.
– Without this, debugging is a nightmare!
• Produce same results regardless of
machine architecture (# threads, etc)
– Task based is useful
– We don’t do very well at this
![Page 31: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/31.jpg)
Command Line Control
• -j ncpu
• Allows multiple processes to share a
machine
• Allows you to easily debug and perf test
![Page 32: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/32.jpg)
Memory Constant in Cores
• How to multithread a random write pattern?
– Lock writes
– Find a pattern that is thread safe
– Copy the destination per thread and merge after
• What if you have a four-socket, ten-core,
machine with hyperthreading?
![Page 33: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/33.jpg)
Memory Allocation
• tbb::scalable_malloc fragments
• jemalloc
– http://www.canonware.com/jemalloc
![Page 34: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/34.jpg)
Copy on Write
• Reader/Writer Locks
• Const Correctness
• Importance of Ownership
• Sole Ownership is a Writer Lock
• How this Fails
![Page 35: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/35.jpg)
Copy on Write: Reader/Writer
• Natural needs for a piece of geometry
– Many readers
– Many writers
• Need to ensure readers are not made inconsistent by
writes
• Reader/Writer Lock is a solution
– But we do not want any locks!
![Page 36: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/36.jpg)
Shared Geometry Cache
Main
Python
Write
Read Read Read
![Page 37: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/37.jpg)
Shared Geometry Cache
Main
Python
Write
Read Read Read
Barring other synchronization primitives;
this is also a possible execution order.
![Page 38: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/38.jpg)
Shared Geometry Cache
Main
Python
Write Copy
Read Read Original Read New
This has the same effect but with
overlapping computation.
![Page 39: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/39.jpg)
Copy on Write: Const
• Const Correctness
– Does not provide speed improvements
– Does create a compiler-enforced contract on the code!
• Easy to find lazy programming
– const_cast
– mutable
– Most importantly, easier to just not make things const!
![Page 40: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/40.jpg)
Copy on Write: Ownership
• Lack of garbage collection is a feature
Network
Sphere Convert
Network
Sphere Convert
![Page 41: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/41.jpg)
Copy on Write: Ownership
• shared_ptr should be minimized!
![Page 42: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/42.jpg)
Copy on Write: Sole Ownership
• Two types of readers
– Readers from foreign threads/algorithms
– Our own readers
• Preventing external readers lets you reason about your
own readers
• Sole ownership is a Writer Lock
– If no external algorithm has a copy of your data structure, you can
reason about a lock-free approach
![Page 43: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/43.jpg)
Copy on Write: Sole Ownership
![Page 44: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/44.jpg)
Copy on Write: Failure
• Excessive sharing
![Page 45: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/45.jpg)
Copy on Write: Failure
• Pointer aliasing
![Page 46: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/46.jpg)
Multithreading in Houdini
Task Locks
![Page 47: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/47.jpg)
Task Locks• Not all threads are equal
• A cook lock block Python from reading intermediate state
• VEX tasks spawn to many threads
• VEX can then trigger cooks
• Which needs the cook lock!
• Task locks allow reentrancy to child tasks, even if they are currently on a different thread
Cook
Python
VEX1
VEX2 Cook
![Page 48: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/48.jpg)
Global Cook Lock
Main
TBB 1
TBB 2
TBB 3
Python
Cook
Cook
Cook
![Page 49: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/49.jpg)
Global Cook LockPosted Tasks
Vex1, Vex2, Vex3,
Vex4
d
Main
TBB 1
TBB 2
TBB 3
Cook
![Page 50: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/50.jpg)
Global Cook Lock
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
![Page 51: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/51.jpg)
Global Cook Lock
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
Cook
![Page 52: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/52.jpg)
Task Lock: Details
• Task Owner defines a tree of child tasks
• Thread Local stores current thread’s Task
Owner
• Tasks update their thread’s owner on start
• Lock is a condition + mutex which allows a
stack of locks provided owner is strictly a
child
![Page 53: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/53.jpg)
Vex2
Task Cook Lock
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
Cook
Cook
![Page 54: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/54.jpg)
ComposabilityPosted Tasks
Vex1, Vex2, Vex3,
Vex4
d
Main
TBB 1
TBB 2
TBB 3
Cook
![Page 55: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/55.jpg)
Composability
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
![Page 56: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/56.jpg)
Composability
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
KD
KD
KD
KD
Posted Tasks
KD1, KD2, KD3,
KD4
![Page 57: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/57.jpg)
Composability
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
KD
KD
KD
KD
Posted Tasks
KD2, KD3, KD4
KD1
![Page 58: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/58.jpg)
Composability
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
KD
KD
KD
KD
KD1 KD2 KD3 KD4
![Page 59: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/59.jpg)
How we hope it works…
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
KD
KD
KD
KD
KD1 KD2 KD3 KD4
![Page 60: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/60.jpg)
Deadlock…Posted Tasks
Vex1, Vex2, Vex3,
Vex4
d
Main
TBB 1
TBB 2
TBB 3
Cook
![Page 61: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/61.jpg)
Deadlock…
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Posted Tasks
Vex4
d
![Page 62: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/62.jpg)
Deadlock…
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Posted Tasks
Vex4, KD1, KD2,
KD3, KD4
dKD
KD
KD
![Page 63: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/63.jpg)
Deadlock…
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Posted Tasks
Vex4, KD3, KD4
d
KD
KD
KD
KD1
KD2
![Page 64: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/64.jpg)
Deadlock…
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Posted Tasks
KD4
d
KD
KD
KD
KD1
KD2
Vex4
KD3
![Page 65: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/65.jpg)
Deadlock…
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
KD
KD
KD
KD1
KD2
Vex4
KD3 KD4
KD
![Page 66: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/66.jpg)
Task Locks• Task stealing leads to deadlocks
• The stack creates an implicit lock!
![Page 67: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/67.jpg)
Multithreading in Houdini
Task Arenas
![Page 68: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/68.jpg)
Task Arena• Never call back to the TBB scheduler
if a lock is held
• If you must, wrap a Task Arena
![Page 69: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/69.jpg)
Task Arena• Moved from Preview to mainline in
TBB 4.3
• Documentation suggests for
controlling thread pools
• Solves the locking problem
![Page 70: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/70.jpg)
Task Arena• Acquire your lock
• Build just-in-time a tbb::task_arena
• Execute code (task_arena::execute())
– No external tasks will be assigned to this
thread!
• Release your lock
![Page 71: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/71.jpg)
Task ArenaPosted Tasks
Vex1, Vex2, Vex3,
Vex4
d
Main
TBB 1
TBB 2
TBB 3
Cook
![Page 72: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/72.jpg)
Task Arena
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Posted Tasks
Vex4
d
![Page 73: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/73.jpg)
Task Arena
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Posted Tasks
Vex4, KD1, KD2,
KD3, KD4
dKD
KD
KD
![Page 74: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/74.jpg)
Task Arena
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Posted Tasks
Vex4, KD3, KD4
d
KD
KD
KD
KD1
KD2
![Page 75: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/75.jpg)
Task Arena
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Posted Tasks
KD4
d
KD
KD
KD
KD1
KD2 Vex4
KD3
Vex4 CANNOT be
assigned to TBB1
until TBB1 leaves
the arena!
d
![Page 76: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/76.jpg)
KD
Task Arena
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
KD
KD
KD
KD1
KD2 Vex4
KD3 KD4
KD
![Page 77: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/77.jpg)
Task Arena• Beware implicit locks from stack
unwinding
• Do not re-use
• Catch exceptions
![Page 78: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/78.jpg)
Multithreading in Houdini
Task Exclusive
![Page 79: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/79.jpg)
Task Exclusive• Many threads want the same cached
item
• Task Lock results in single threaded
computation of the item.
• Why can’t blocked threads contribute
work?
![Page 80: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/80.jpg)
Task Exclusive• Lock Free Singleton
• Tasks compete to get right to compute
resource
• Winning task computes as normal
• Losing tasks add a dependent task
and return to scheduler
![Page 81: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/81.jpg)
Task ExclusivePosted Tasks
Vex1, Vex2, Vex3,
Vex4
d
Main
TBB 1
TBB 2
TBB 3
Cook
![Page 82: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/82.jpg)
Task Exclusive
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
![Page 83: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/83.jpg)
Task Exclusive
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
KD
Posted Tasks
rKD1, rKD2, rKD3,
rKD4
KD
KD
KD
![Page 84: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/84.jpg)
Task Exclusive
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
KD
Posted Tasks
KD
KD
KD rKD1
rKD2
rKD3
rKD4
Posted Tasks
rKD3 wins race
![Page 85: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/85.jpg)
Task Exclusive
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
KD
Posted Tasks
KD1, KD2, KD3,
KD4
Waiting on rKD3:
rKD1, rKD2, rKD4KD
KD
KD rKD1
rKD2
rKD3
rKD4
![Page 86: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/86.jpg)
Task Exclusive
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
KD
Posted Tasks
Waiting on rKD3:
rKD1, rKD2, rKD4
KD
KD
KD rKD1
rKD2
rKD3
rKD4
KD1
KD2
KD3
KD4
![Page 87: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/87.jpg)
Task Exclusive
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4
KD
KD
KD
KD rKD1
rKD2
rKD3
rKD4
KD1
KD2
KD3
KD4
rKD1
rKD2
rKD4
![Page 88: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/88.jpg)
Task Exclusive• Lock Free Singleton
– But the stack creates an implicit lock!
– Need a Task Arena to ensure stack can
empty back to the recycled task.
![Page 89: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/89.jpg)
Task Exclusive
Main
TBB 1
TBB 2
TBB 3
Cook Vex1
Vex2
Vex3
Vex4KD
KD
KD
KD rKD1
rKD2
rKD3
rKD4
KD1
KD3 KD4
![Page 90: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/90.jpg)
Stack Locks
![Page 91: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/91.jpg)
Stack Locks
![Page 92: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/92.jpg)
Stack Locks
![Page 93: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/93.jpg)
Multithreading in Houdini
Conclusion
![Page 94: Multithreading in Visual Effects](https://reader036.vdocuments.us/reader036/viewer/2022072703/62dfa744faacf020a8005a9a/html5/thumbnails/94.jpg)
Final Thoughts• Make it easy to parallelize
• Make it easy to switch to serial
• Use jemalloc
• You do not need to rewrite
Thank you