C++ multithreading point pairs

   495   4   0
User Avatar
Member
9 posts
Joined: June 2018
Offline
Hi!

I'm implementing a physics solver where I want to loop over pairs of points for collision solving, is this possible to do multithreaded?

Using this I can loop over single points multithreaded:
// GA_RWHandleV3D p_handle( gdp->findAttribute(GA_ATTRIB_POINT, "P"));
// GA_SplittableRange range = gdp->getPointRange();

// in the THREADED_METHOD function
for (GA_PageIterator pit = range.beginPages(info); !pit.atEnd(); ++pit) {
                 
	GA_Offset start, end;
	for (GA_Iterator it(pit.begin()); it.blockAdvance(start,end);) {
                     
		for (GA_Offset i = start; i < end; ++i) {
                        UT_Vector3 p = p_handle.get(0);
			// do work for each point
                        p_handle.set(0, p);
		}
	}
}

The pairs I want to loop over are stored in a vector<pair<int,int>> where none of the points is listed twice.

I need to fetch multiple point attributes from each point. For example I need to get the v@P, v@v v@pscale of both points then update then solve and update the new v@P and v@v attribute.
Edited by heinzelnisse - March 13, 2024 10:32:16
User Avatar
Staff
467 posts
Joined: Aug. 2019
Offline
You should be able to use GAparallelForEachPageand GAforEachPageBlock.
Edited by johnmather - March 13, 2024 13:32:04
User Avatar
Member
9 posts
Joined: June 2018
Offline
johnmather
You should be able to use GAparallelForEachPageand GAforEachPageBlock.
Thanks! I'm quite new to c++ in houdini, can you give me an example on how you would implement it in code?
User Avatar
Staff
467 posts
Joined: Aug. 2019
Offline
Sure. You can find some examples here: https://www.sidefx.com/docs/hdk/_h_d_k__g_a__using.html [www.sidefx.com]

There is also this comment in GA_PageIterator.h above GAparallelForEachPage:
/// Invokes the body approximately once per worker thread with
/// a shared atomic int to provide load-balanced iteration over the
/// pages in the range.
/// shouldthread allows you to easily disable threading, for example if
/// you know some cases are not threadsafe or you have a larger grain
/// size.
/// Body Signature: void body(GA_PageIterator page_iterator)
/// Usage:
/// GAparallelForEachPage(range, shouldthread, [&](GA_PageIterator pit)
/// {
///     /* Thread local data structures / scratchpads */
///     UT_IntArray scratchpad;
///     GA_ROPageHandleF pagehandle;  /* page handles must be per thread */
///     GAforEachPageBlock(pit, [&](GA_Offset start, GA_Offset end)
///     {
///         pagehandle.setPage(start);
///         for (GA_Offset off = start; off < end; off++)
///         { /* Process off, using scratchpad */ }
///     });
/// });
///
/// NOTE: Using [&] capture on GAparallelForEachPage will share the outer
/// scope's stack variables between threads - you must treat them as const
/// (and in particular, re-build pagehandles inside this loop.  For non
/// trivial loops explicit binding may be safer.
User Avatar
Member
9 posts
Joined: June 2018
Offline
Thanks! I'll check it out
  • Quick Links