My PC has 6 cores and 12 threads.
A C++ node that does its work in a plain for loop uses exactly one of them, and the other eleven sit there watching.
Spreading the work out is the most satisfying speed-up in the HDK, and also the easiest place to hurt yourself.
The loop
Say every point needs some expensive math.
Here is the serial version and the parallel one, side by side.
// Make every point first, then fill them in.
const GA_Offset first = gdp->appendPointBlock(n);
GA_RWHandleV3 pos(gdp->getP());
// Serial: one thread does everything.
for (exint i = 0; i < n; ++i)
pos.set(first + i, expensiveThing(i));
// Parallel: Houdini hands chunks of points to all the threads.
GA_SplittableRange range(gdp->getPointRange());
UTparallelFor(range, [&](const GA_SplittableRange &r) {
GA_Offset s, e;
for (GA_Iterator it(r); it.blockAdvance(s, e);)
for (GA_Offset o = s; o < e; ++o)
pos.set(o, expensiveThing(o - first));
});It looks like more code, and it is, but the loop body is the same one line.
The extra lines are just Houdini slicing the points into chunks for you.
How fast
I tried it on a million points with some busy math in each, so the result is just my PC, not a promise.
- On one thread the whole thing took about 3.8 seconds.
- On all 12 threads it took about 0.5 to 0.7 seconds. That is five to seven times faster, and the answer was identical to the serial one.
I also limited Houdini to fewer threads to watch it scale.
With 2 threads it was about twice as fast, with 4 it was about four times, and then it flattened out a little: 6 threads gave about 4.7 times and 12 gave 7.3.
That makes sense, because the last six threads are hyper-threads sharing cores, not extra cores.
Before the speed, though, there is a rule.
The one rule: never change the geometry's shape inside the loop
I tried the obvious thing first: make each point inside the loop.
UTparallelFor(UT_BlockedRange<exint>(0, n), [&](const UT_BlockedRange<exint> &r) {
for (exint i = r.begin(); i != r.end(); ++i) {
GA_Offset p = gdp->appendPointOffset(); // <- adding points from many threads
gdp->setPos3(p, expensiveThing(i));
}
});Houdini died.
Not an error message, not a red node: a segmentation fault, and the whole session was gone.
Adding points, deleting them or adding attributes changes the geometry's structure, and that is not safe to do from several threads at once.
So the pattern is always the same: make everything first, then fill it in parallel. That is the first line of the code above, appendPointBlock.
My repo keeps a note about this.
Its advice fits in three lines:
- Allocate all the points before the loop.
- Write into them through a handle, at offsets you already own.
- If something odd happens, turn the loop back into a plain
forfirst. If the serial version is fine and the parallel one is not, it is a thread-safety problem, and you have your answer.
Sometimes the answer is "do not"
Parallel is not always the win.
In the vegetation scatter code, I once had a parallel loop that stamped asset names onto points.
It was a string attribute, and writing string attributes from several threads is not safe, so I made that one step serial again and left a comment to explain why.
The numeric part, which has no such problem, stays parallel.
That is the whole skill, really: find the part of the cook that is safe to spread out, spread it, and leave the rest alone.
Next
Next: the naming rules for handing what you build in Houdini over to Unreal Engine.
Comments