Houdini Inline C++, any way to multithread?

   Views 342   Replies 1   Subscribers 0
User Avatar
Member
93 posts
Joined: Feb. 2020
Offline
As far as I can tell its not possible to created multithreaded code with Houdini's UT_ThreadedAlgorithm using `inlinecpp` because inlinecpp only allows you define a single function, where it seems UT_ThreadedAlgorithm would require you to define at least two (the function which will be used across the threads, and a function to initialize that multi-threaded process). I can't seem to find a single example of it's use outside of a class method in the HDK docs.

The HDK docs makes it seem like multithreading with is really only something you do when writing class methods.

Is there a different approach to running multithreaded code in inline C++? Would it make sense to use an external library like taskflow [github.com] to do the threading instead of using one of houdini's built in libraries, if we want to do threading with just a single function?

Yes, I know VEX can handle the multithreading for you in the virtual machine, but I'm curious about this subject, so entertain me.

void wave(GU_Detail *gdp, float period, float phase, float amp)
{
    tf::Executor executor;
    tf::Taskflow taskflow;

    // Collect points to process
    UT_Array<GA_Offset> offsets;
    GA_Offset ptoff;
    GA_FOR_ALL_PTOFF(gdp, ptoff)
    {
        offsets.append(ptoff);
    }

    // Multi-threaded for_each using Taskflow
    taskflow.for_each(offsets.begin(), offsets.end(), [=](GA_Offset ptoff) {
        UT_Vector3 p = gdp->getPos3(ptoff);
        p.y() += SYSsin((p.x() / period + phase) * M_PI * 2) * amp;
        gdp->setPos3(ptoff, p);
    });

    executor.run(taskflow).wait();
}
[/code]
Edited by wyhinton1 - April 6, 2025 05:52:02
User Avatar
Staff
566 posts
Joined: Aug. 2019
Offline
I haven't tried it, but you might be able to use UTparallelFor instead. This allows you to define the parallel region in a lambda.

To use it,
for (int i = 0; i < n; i++)
{
    // do something with i
}

Is written as:
UT_BlockedRange range(0, n);

UTparallelFor(range, [&](const UT_BlockedRange<int> &my_range)
{
    for (int i = my_range.begin(); i != my_range.end(); ++i)
    {
        // do something with i
    }
});

Depending on your types, you may also be interested in UTparallelForLightItems or UTparallelForHeavyItems. Be sure to pay attention to the subscribe_ratio and min_grain_size variables as well.

https://www.sidefx.com/docs/hdk/_u_t___parallel_util_8h.html#a23f60e1e8a814cbb4a0838cd140e6e2c [www.sidefx.com]
  • Quick Links