Core300 Most Stable Update Method

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Core300 Most Stable Update Method

Postby Stucuk » Mon Dec 24, 2012 2:42 am

Whats the most stable update method? (Or the most recommended)

Code: Select all
procedure NewtonUpdate (const world : NewtonWorld; timestep : dFloat); cdecl; external NEWTON_API;
 procedure NewtonUpdateAsync (const world : NewtonWorld; timestep : dFloat); cdecl; external NEWTON_API;
 procedure NewtonWaitForUpdateToFinish (const world : NewtonWorld); cdecl; external NEWTON_API;


Im assuming that having the update done in a dedicated thread (So there is no delay from the graphics/etc) would be the best method.

P.S Whats NewtonWaitForUpdateToFinish?
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Core300 Most Stable Update Method

Postby Julio Jerez » Mon Dec 24, 2012 8:59 am

both function run the engine is a separate thread. one is a blocking thread an dteh secund non blocking
if you use NewtonUpdate the the call will wait until the simulation step return.
if you use NewtonUpdateAsync will return imidiatelly afe teh call and a tread will result the step on teh background, this is simulat to what Open GL and D3D do
like this you can have the physics and teh manin loop running cocurrentlly.
if you call another NewtonUpdateAsync before anothe one is still running the the secund will wait act as a NewtonUpdate wating fo rteh first updateAsyn To complete.

NewtonWaitForUpdateToFinish will waith until the corrent update is completed and the return back to the application, this is use for when you want destroy a world of derstroy all bodies of a world.
if you are using NewtonUpdate, then NewtonWaitForUpdateToFinish has no effect,
when using NewtonUpdateAsync then is importnt to use NewtonWaitForUpdateToFinish before destryoing a world


the best result you can get is wne using NewtonUpdateAsync if you have more than one core.
it essencially gives you the physics simulation fore free, or the main loop simulation for free depende iof which take longer to complete

using NewtonUpdateAsync with subframe interplation for you graphiscs you cn aget you can have teh maxumun performance possible, or teh order ot several hundresd of fps, soem tiem even tousands if teh copulte can handle it
while the physics runs at a fix frame rate on teh back ground

you can run a loop liek this: adapeted from http://gafferongames.com/game-physics/f ... -timestep/
Code: Select all
void GameLoop
{
    float dt = 1 / 60.0;     // 1.0 / 120.0 much better
   
    long currentTime = hires_time_in_microsecund();
    while ( !quit )
    {
         double newTime = hires_time_in_seconds();
         double frameTime = newTime - currentTime;
         currentTime = newTime;
         
         while ( frameTime > 0.0 )
         {
              NewtonUpdateAsync (NewtonWorld; dt)
             frameTime -= dt * 1000000;
         }

         render( state );
    }
}


then as along as the time step is lower than dt you simulation will run as the render frame rate.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Core300 Most Stable Update Method

Postby zak » Mon Dec 24, 2012 10:31 am

But if the NewtonUpdateAsync is used, what happens if render() is called and transform callback is used?
I mean what happens if NewtonUpdateAsync is writing on matrix in callback and render() uses those matrix?
zak
 
Posts: 87
Joined: Mon Dec 06, 2004 9:30 am

Re: Core300 Most Stable Update Method

Postby Julio Jerez » Mon Dec 24, 2012 11:33 am

there will be a race condition. but that is very eassy to solve if you look at the demos you will see that each entity have a lightweigh mutex (a newton Mutex very important!!)
somethsi like this
Code: Select all
class DemoEntity
{
   // just code

   // add a newton mutex
  int m_lock;

  // when using newton stuff from a call back, ex Get or Set Entrity Matroix from a call back
     DemoEntity::GetMatrixForRender(DemoEntityManager& world, const dQuaternion& rotation, const dVector& position)
}

void DemoEntity::GetMatrixForRender(DemoEntityManager& world, const dQuaternion& rotation, const dVector& position)
{
   // read the data in a critical section to prevent race condition from other thread 
   world.Lock(m_lock);

   m_curPosition = m_nextPosition;
   m_curRotation = m_nextRotation;

   m_nextPosition = position;
   m_nextRotation = rotation;

   dFloat angle = m_curRotation.DotProduct(m_nextRotation);
   if (angle < 0.0f) {
      m_curRotation.Scale(-1.0f);
   }

   // release the critical section
   world.Unlock(m_lock);
}



// these are the ligweght Newton Mutex implentation
Code: Select all
inline void MyScene::Lock(unsigned& atomicLock)
{
   while (NewtonAtomicSwap((int*)&atomicLock, 1)) {
      NewtonYield();
   }
}

inline void MyScene::Unlock(unsigned& atomicLock)
{
   NewtonAtomicSwap((int*)&atomicLock, 0);
}


this code run amost unterrupted, bascially teh engien update matrices as fat as in can and teh render get the as facr as it can, and very rare ther is a collision
when the is a collsion because both teh engine an dteh render try to read the same matrix the the entiry Lock fail and call NetwonYeild and, wating until the matrxi is ready
however this engine does not stops there, if there are other pending entities, it continue upating those.

That code work for but all cases, sunc, asyn, sing wtil microthread and asyn and micor threaded.
for example you can set teh engioe to run Asyn and also use 8 microthreads.
Thsi si a level of parallesm that No other library has, not even Intell, or Mocrosoft products. Bascially teh engien runs with 98 to 99% with teh treaded not blocking each other, Thread collsion are lower that 1% on typical scenes


Not only that but teh engien also exposed the paraller excution of host application, with Pre and Post listeners.

for example say you have yor AI system, and you need to update 500 NPCs, or say 10000 particles, ot many sound etc.
you can plug a Pre or Post listener. Thsi gives you and update function with user data which is executed from the Newton update and is dispached in parallel from each micro thread.
There you can do you update without worry of thread collisions, and the update happens on simulation time. You can add as many listeners as you want.
Newton 3 fully exploit multiprossesor systems.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Core300 Most Stable Update Method

Postby pHySiQuE » Wed Jan 23, 2013 5:05 am

Nice! I just found this thread because I was leaking memory running NewtonUpdate on my own background thread, but this seems to work correctly. My code is already set up to run asynchronously.
pHySiQuE
 
Posts: 608
Joined: Fri Sep 02, 2011 9:54 pm

Re: Core300 Most Stable Update Method

Postby corbingravely » Fri Apr 10, 2015 6:34 am

I hope that this works exactly as you say. I am actually making a move to go for the core300 to maintain the update methods. I hope it does not fail me. Well I can always come here for help right?
corbingravely
 
Posts: 3
Joined: Thu Dec 04, 2014 7:25 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 3 guests

cron