ThreadManager.AddThread callbacks are invoked twice

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
ecosky
Posts: 69
Joined: Fri Nov 04, 2011 7:13 am
Contact:

ThreadManager.AddThread callbacks are invoked twice

Post by ecosky »

I was recently starting to use the initialization callback for ThreadManager.AddThread, and was surprised to find the callback was invoked twice. It took a while to discover this was a problem for me because it never occurred to me this might happen. The side effects were subtle behavior problems elsewhere in the game due to thread-local objects being added to lists twice as often as they should have.

It might be worth considering making sure this callback is only invoked once, or alternatively provide the action an enumeration parameter to indicate which stage (task or loop) the callback is being called from. FWIW, my vote would be to simply call it once.

Code: Select all

        
        public void AddThread(Action<object> initialization, object initializationInformation)
        {
            taskManager.AddThread(initialization, initializationInformation);
            loopManager.AddThread(initialization, initializationInformation);
        }
Thanks again for all things Bepu!
ecosky
Posts: 69
Joined: Fri Nov 04, 2011 7:13 am
Contact:

Re: ThreadManager.AddThread callbacks are invoked twice

Post by ecosky »

I now see that calling ThreadManager.AddThread actually creates two threads, one for the ParallelLoopWorker and one for the ThreadTaskManager. Makes more sense now.

I have to say I'm not really keen on the extra context switches required for these to do regular work (xbox suffers a bit there and so many apis seem to create threads that aren't IMHO entirely necessary sometimes), I would prefer if the AddThread just created a single thread that knew how to do both of these tasks. Not sure how that would be done yet though, just getting into the threading side of Bepu so it's still a bit of a mystery how to best work with it. An ideal situation would allow me to have my existing threads call the Bepu work helper functions when they reach a natural stopping point where they would otherwise wait for an event, but I can see how that would complicate the task distribution logic.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: ThreadManager.AddThread callbacks are invoked twice

Post by Norbo »

I have to say I'm not really keen on the extra context switches required for these to do regular work (xbox suffers a bit there and so many apis seem to create threads that aren't IMHO entirely necessary sometimes), I would prefer if the AddThread just created a single thread that knew how to do both of these tasks.
The Xbox360 used a ThreadTaskManager instead of a SpecializedTaskManager once upon a time for that reason. Later, the loop-focused thread manager was improved and the SpecializedThreadManager started beating the ThreadTaskManager alone on the Xbox360 by a little bit in my performance tests at the time.

If desired, you can swap back to just using the ThreadTaskManager by setting the Space.ThreadManager = new ThreadTaskManager(). The performance should be pretty similar on the Xbox360.
An ideal situation would allow me to have my existing threads call the Bepu work helper functions when they reach a natural stopping point where they would otherwise wait for an event, but I can see how that would complicate the task distribution logic.
Custom managers are pretty easy to implement if desired. An example is the ThreadManagerTPL which uses the .NET task parallel library to do loops. The thread manager just needs to be capable of handling forloops and individual parallel tasks (though loops are guaranteed to never get called by BEPUphysics while regular BEPUphysics-spawned parallel tasks are in progress).

As far as performance goes, though, it will likely be a bit hard to beat the SpecializedThreadManager (especially on the PC). The loop-focused manager has extremely cheap and effective load balancing and very low overhead (which is good, because the majority of the engine is big forloops).
ecosky
Posts: 69
Joined: Fri Nov 04, 2011 7:13 am
Contact:

Re: ThreadManager.AddThread callbacks are invoked twice

Post by ecosky »

Thanks Norbo, your insights are always appreciated.
ecosky
Posts: 69
Joined: Fri Nov 04, 2011 7:13 am
Contact:

Re: ThreadManager.AddThread callbacks are invoked twice

Post by ecosky »

In case you are interested in such a minor thing, I added an overload to the Space constructor to let me avoid the overhead of creating a IThreadManager that is due to be replaced immediately after construction,

Code: Select all

		public Space()
			: this(new SpecializedThreadManager())
		{
		}

		///<summary>
 		/// Constructs a new space for things to live in.
		///</summary>
		public Space(IThreadManager threadManager)
       		{
			this.threadManager = threadManager;

Post Reply