Threading? Question for Linux/Max/Pc and mobile devices

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Threading? Question for Linux/Max/Pc and mobile devices

Postby Julio Jerez » Mon Mar 26, 2012 11:54 am

Is there a moder threading library that is free and corss platform.
I am going ove teh Lunux build and I now have to write teh threading library, and I fidn tah I need to rewriet the code.

I find this to be too much work for no real gain. Threading code is a Black art, it take long time to fine tune the code and doiwn in for thre OS is not real atractive to me.

I download the Intel TBL, but after opening the project I found it to be an abomination, I do not know they were thinking, but that stuff is hurrendus.
I would not mind using pThread, but it is like they go out of ther way to make it work bad on windows. teh last version was realse in 2006 and leave a lot to be desired.
http://sourceware.org/pthreads-win32/


so my quetsion is what thread library people use for cross platorm project, that is also open source an it is uptodate?

looking in the net I found this list,
http://en.wikipedia.org/wiki/List_of_C%2B%2B_multi-threading_libraries
has anyone use any of thoses?

tiny thread looks very interesting.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby Bird » Mon Mar 26, 2012 2:00 pm

I use POCO. It's very well done, lightweight, easy to use, robust and still being actively developed . It has Boost license
I've used ZThreads in the past too and it's well done but hasn't been updated since 2005 It has MIT license

-Bird
Bird
 
Posts: 636
Joined: Tue Nov 22, 2011 1:27 am

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby Bird » Sun Apr 01, 2012 7:53 pm

tiny thread looks very interesting.

Wow, that is tiny. :)

Embree has a nice clean lightweight threading api and it looks like it would be easy to extract from the rest of the library. http://software.intel.com/en-us/articles/embree-photo-realistic-ray-tracing-kernels/

-Bird
Bird
 
Posts: 636
Joined: Tue Nov 22, 2011 1:27 am

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby pHySiQuE » Tue Apr 03, 2012 12:45 pm

I attached my thread and mutex classes, which run on Windows, Mac, iOS, and Android. It's very simple and small.
Attachments
Archive.zip
(2.33 KiB) Downloaded 155 times
pHySiQuE
 
Posts: 608
Joined: Fri Sep 02, 2011 9:54 pm

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby Julio Jerez » Wed Apr 04, 2012 7:24 am

that is very small too, but niether of teh thread libaries I had seen impement codne for threa suspention.

the impelmnet Joint, an dfold but tshi si base of termination teh thread, threas supention is verye different, in windopw this is don by teh funtion call WaitOnSingleObjects or WaitOnMultipleObjects
plus a semphore.

the funtion wathon mutiple objtct, in wdoes take thread out of run circle, and is as if teh thread does no really exist, almos the equewvalenet of termination thread.
then when a semphore is signal it take teh thread out o fteh waitlist and add it to teh roudn robing thread update list.

tshi si a perfect wait for implementiong the job libaries tha take almos zeor overhead form teh system.

in Linux thare is no equvalenet of WaitOnSingleObjects and the same funtinality has to be impemnethe with mutexes and yield, but the yield funtion has a high overhead is you want to have 32 or 62 microthread thread running all the time.
so I do not knwo how to make an efficent thread pool for linux using POXI thread, but that si as goog as can be implmented on windows.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby thedmd » Fri Jun 08, 2012 6:07 pm

Hi Julio,

Just for an update. There is a new release of pthreads-win32 2.9.0 (2012-05-25). I think this may be what you're looking for.
thedmd
 

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby Julio Jerez » Fri Jun 08, 2012 6:59 pm

Wow, I just download it and open the dsw project. It compiled, this is the frist time I am able to compile pthread with visual studio.
this is in fact very important because if it works, I can unifiy the threads code for all platform.
I will start right away, because I am sick and tire of writing special code for different OS
thank you, for the info.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby Julio Jerez » Sat Jun 09, 2012 4:26 am

Ok I think I converted the dgThread class to only use pthread now.
It seems to work fine, I believe that now the engine soudl be mutithreaded in all platfroms supporting POXIT thrreads
there is still the thread emulation in case it fail in some OS but for waht I can see it sopule work everywhere.
thanks fo rthe infor about the new version, I was wating for a long time.

I only build VS 2008, tonorrow I will try to update the VS 2010 projects.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby thedmd » Sat Jun 09, 2012 9:57 am

Did you handled lack of WaitForMultipleObjects on Linux somehow?

EDIT:
I do really hope will use statically linked pthreads, so Newton.dll will still be the only one really required to run physics under Windows.
thedmd
 

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby Julio Jerez » Sat Jun 09, 2012 11:19 am

The closest to wait for Single object in Pthreas and Posit threads is sem_wait, there is not waitForMultipleobject or equivalent.

however since I refactored the dgThread class for core 300, I am not longer using WaittForMutipleObjects. basiially I was wating for an Event and a Semaphopre, but the event sfuff was not really essencial.
since it was only used for waiting when the thread terminate. Instead now I use pthread_join or (CloseHnadle in window) and wait fo rteh termination with and atomic that is set by teh thread callback.
that solved the issue with wih of waitForMutipleobjects.

for the secund question, yes the libray is statically linked, so you only need one dll. However this confuses VS2008.
Pthread in window doe snot add the code to the projects, instead it include them as header files in one c file.
This causes the depenedency checker in visual 2008 to malfution and when the target or the configuration is changed It does not clean the Pthread output and it had toi be done explicitly.
I asume this is not a big deal but is does bothers me. I spend some time trying to see if it can be fix it but I do see how.

The last thing is that Pthread is not fully cross platform. In windows they use a bunch of stuff that are only wondows thing. for example they use macros that resolve to windows Atomics that do not have equivalent for other OS.
But it is not a big deal either, I only use two atomic function in the engine. these two
Code: Select all
inline dgInt32 dgAtomicExchangeAndAdd (dgInt32* const addend, dgInt32 amount)
{
   // it is a pity that pthread does not supports cross platform atomics, it would be nice if it did
//   return PTW32_INTERLOCKED_EXCHANGE_ADD_LONG ((long*) addend, long (amount));

   #if (defined (_WIN_32_VER) || defined (_WIN_64_VER) || defined (_MINGW_32_VER) || defined (_MINGW_64_VER))
      return _InterlockedExchangeAdd((long*) addend, long (amount));
   #endif

   #if (defined (_LINUX_VER))
      return __sync_fetch_and_add ((int32_t*)addend, amount );
   #endif

   #if (defined (_MAC_VER))
      dgInt32 count = OSAtomicAdd32Barrier (amount, (int32_t*)addend);
      return count - *addend;
   #endif
}

inline dgInt32 dgInterlockedExchange(dgInt32* const ptr, dgInt32 value)
{
                // it is a pity that pthread does not supports cross platform atomics, it would be nice if it did
   //   return PTW32_INTERLOCKED_EXCHANGE_LONG ((long*) ptr, value);

   #if (defined (_WIN_32_VER) || defined (_WIN_64_VER) || defined (_MINGW_32_VER) || defined (_MINGW_64_VER))
      return _InterlockedExchange((long*) ptr, value);
   #endif

   #if (defined (_LINUX_VER))
      //return __sync_fetch_and_add ((int32_t*)ptr, value );
      __sync_synchronize();
      return __sync_lock_test_and_set((int32_t*)ptr, value);
   #endif

   #if (defined (_MAC_VER))
      // bool OSAtomicCompareAndSwapLongBarrier(long oldValue, long newValue,   volatile long *theValue);
      // compare oldValue to *theValue, and set *theValue to newValue if the comparison is equal.  The comparison
      //   and assignment occur as one atomic operation.

      // somehow this does no work in macs, I need to test it.
      //int32_t oldVal = *ptr;
      //OSAtomicCompareAndSwapLongBarrier(oldVal, value, (int32_t*)ptr);
      //return oldVal;
   #endif
}


what this mean is that these functions needs to be implemented and test it on each target patform.
I added the Window and Linux version but I am yet to test them.
I am asumimg that teh Linux are compatible with any mobile platform, and the Mac OSX are the more confusing of all.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby Julio Jerez » Sat Jun 09, 2012 12:31 pm

Ok I found the bug in the project file tah was making VS to confuse teh dependencis. no it all work fine.
I also adde the VS 2010 projects.
In windows land everything should be normal now.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby thedmd » Sun Jun 10, 2012 7:41 am

pthreads-win32 comes with semaphore.h, which, I believe, was what had been looking for. This is some kind of POSIX extension to pthreads, but I do not know the details.

I did API wrapping so many times, so I finally accepted this as a way to go while dealing with platform differences. Atomics are not an exception. That would be nice to have some unified way to handle them. Unfortunately I do not know any better yet.

Your Linux related code should work on Android, iOS and Mac OS X without modification. As far as I know one feature usually missing is pthread_cancel().

As in case of most standards, pthreads-win32 is an implementaion of one. People responsible for this one if best are loosly related to those responsible for other platform implementations. All they could do is to try to not by PITA for end-users. IMHO there is really no one to blame for Win32 specific extensions.
thedmd
 

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby Julio Jerez » Sun Jun 10, 2012 6:43 pm

Hurray the Linux build is now working.
The biggest bug was that some of the Tool make files still had the sse4.1 options
This made the code crash on the start and kgdb is not capable of debugging code if it crashes before main.

anyway the code now works in linux, and multithreading just work as well, the cool thing is that in my old Intel 2.4 hg Pentium, which is a hyper threaded by that I have turned off at the bios level,
if I set the treading code to 16 thread most demo run twice almost as fast.
This is treading on a single core, therefore I expect that in real dual and quad core the code should yield linear or better than linear performance gain by increasing the number of cores.
It seems that four thread per core is the optimal ratio at least in this machine, however adding more thread does not make it worse.
It seems that threading in Linux is better than in windows 7, or at least it is no worse.

In the demos, mouse picking and the keyboard does not work yet, next time I will fix those, but for now all demo run and it confirmed the thread seem to offer performance game linear with the thread count. 1
6 thread in my single core yield 34 fps in the terrain demos, while a single core is only 21.
This is better than I was expecting for an older machine.

fio rteh Mac teh problem taht I have now is that teh GUI does not have a posrt fo OSX, bascially teh run teh Linux version with and X11 server, I have to leran hwo to do that so that I can run etst teh mutthreadin code.
however they are only tow function that nee to be check or impelmnetd, this two

Code: Select all
inline dgInt32 dgAtomicExchangeAndAdd (dgInt32* const addend, dgInt32 amount)
{
   // it is a pity that pthread does not supports cross platform atomics, it would be nice if it did
//   return PTW32_INTERLOCKED_EXCHANGE_ADD_LONG ((long*) addend, long (amount));

   #if (defined (_WIN_32_VER) || defined (_WIN_64_VER) || defined (_MINGW_32_VER) || defined (_MINGW_64_VER))
      return _InterlockedExchangeAdd((long*) addend, long (amount));
   #endif

   #if (defined (_LINUX_VER))
      return __sync_fetch_and_add ((int32_t*)addend, amount );
   #endif

   #if (defined (_MAC_VER))
      dgInt32 count = OSAtomicAdd32Barrier (amount, (int32_t*)addend);
      return count - *addend;
   #endif
}

inline dgInt32 dgInterlockedExchange(dgInt32* const ptr, dgInt32 value)
{
   // it is a pity that pthread does not supports cross platform atomics, it would be nice if it did
   //   return PTW32_INTERLOCKED_EXCHANGE_LONG ((long*) ptr, value);

   #if (defined (_WIN_32_VER) || defined (_WIN_64_VER) || defined (_MINGW_32_VER) || defined (_MINGW_64_VER))
      return _InterlockedExchange((long*) ptr, value);
   #endif

   #if (defined (_LINUX_VER))
      //return __sync_fetch_and_add ((int32_t*)ptr, value );
      __sync_synchronize();
      return __sync_lock_test_and_set((int32_t*)ptr, value);
   #endif

   #if (defined (_MAC_VER))
      // bool OSAtomicCompareAndSwapLongBarrier(long oldValue, long newValue,   volatile long *theValue);
      // compare oldValue to *theValue, and set *theValue to newValue if the comparison is equal.  The comparison
      //   and assignment occur as one atomic operation.

      // somehow this does no work in macs, I need to test it.
      //int32_t oldVal = *ptr;
      //OSAtomicCompareAndSwapLongBarrier(oldVal, value, (int32_t*)ptr);
      //return oldVal;
   #endif
}


I beleive the first one I thnk it is right, it is the secund that I have doubts
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby Julio Jerez » Mon Jun 11, 2012 11:44 am

I now fixed the inputs, now the Linux and the windows should work identical.

the parallel solver is crashing in linux, I do not know why, however next week I will make a modification so the parallel solver is lock free like it was is core 200.
The parallel solver is very good, because it yield the same solution as the simple thread island solver, however is does that at the expense of lot of thread blocks.
it is better to add few iterations to make up for the lower convergence rate and have the benefit of a full paraller solver for very large scale scenes.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Threading? Question for Linux/Max/Pc and mobile devices

Postby thedmd » Thu Jun 14, 2012 3:28 pm

I wish I could give you a hint but I have none. All ways to implement atomics on Mac are emulation or kernel calls. Simplest way is to use OSAtomics, but your statement is they do not work.
I wonder, maybe this is a compiler fault somehow. clang was made to be compatible with gcc, so maybe will __sync_lock_test_and_set() just work?
thedmd
 

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests