From b483ce608ee53e1a4b26f934dbcf72116a040426 Mon Sep 17 00:00:00 2001 From: Yuval Tassa Date: Wed, 10 Jul 2024 07:09:51 -0700 Subject: [PATCH] Use std::thread in compiler on all platforms. PiperOrigin-RevId: 651005412 Change-Id: I8dfb142dc2d87ab0af79583f2df168c105b50138 --- doc/modeling.rst | 3 +-- src/user/user_model.cc | 42 ++++-------------------------------------- 2 files changed, 5 insertions(+), 40 deletions(-) diff --git a/doc/modeling.rst b/doc/modeling.rst index f480d1fd..ed12659e 100644 --- a/doc/modeling.rst +++ b/doc/modeling.rst @@ -737,8 +737,7 @@ Automatic computation of actuator length ranges is done at compile time, and the mjModel.actuator_lengthrange of the compiled model. If the model is then saved (either as XML or MJB), the computation does not need to be repeated at the next load. This is important because the computation can slow down the model compiler with large musculo-skeletal models. Indeed we have made the compiler multi-threaded just to speed up this -operation (different actuators are processed in parallel in different threads). Incidentally, this is why the flag -'-pthread' is now needed when linking user code against the MuJoCo library on Linux and macOS. +operation (different actuators are processed in parallel in different threads). Automatic computation relies on modified physics simulation. For each actuator we apply force (negative when computing the minimum, positive when computing the maximum) through the actuator's transmission, advance the simulation in a diff --git a/src/user/user_model.cc b/src/user/user_model.cc index 7ad003ed..f8aa46bc 100644 --- a/src/user/user_model.cc +++ b/src/user/user_model.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -52,25 +53,6 @@ using std::string; using std::vector; } // namespace -// pthread on Linux, std::thread on Mac and Windows -#if defined(__APPLE__) || defined(_WIN32) - #include - using std::thread; - - int getnumproc(void) { - return thread::hardware_concurrency(); - } -#else - #include - #include - - int getnumproc(void) { - return get_nprocs(); - } -#endif - - - //---------------------------------- CONSTRUCTOR AND DESTRUCTOR ------------------------------------ // constructor @@ -1550,7 +1532,7 @@ void mjCModel::LengthRange(mjModel* m, mjData* data) { } // number of threads available, max 16 - const int nthread = mjMIN(16, getnumproc()/2); + const int nthread = mjMIN(16, std::thread::hardware_concurrency()/2); // count actuators that need computation int cnt = 0; @@ -1609,12 +1591,10 @@ void mjCModel::LengthRange(mjModel* m, mjData* data) { err[i][0] = 0; } - // use std::thread -#if defined(_WIN32) || defined(__APPLE__) // launch threads - thread th[16]; + std::thread th[16]; for (int i=0; i