Use std::thread in compiler on all platforms.

PiperOrigin-RevId: 651005412
Change-Id: I8dfb142dc2d87ab0af79583f2df168c105b50138
This commit is contained in:
Yuval Tassa
2024-07-10 07:09:51 -07:00
committed by Copybara-Service
parent 7e97caaafd
commit b483ce608e
2 changed files with 5 additions and 40 deletions
+1 -2
View File
@@ -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
+4 -38
View File
@@ -24,6 +24,7 @@
#include <map>
#include <string>
#include <string_view>
#include <thread>
#include <vector>
#include <mujoco/mjdata.h>
@@ -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 <thread>
using std::thread;
int getnumproc(void) {
return thread::hardware_concurrency();
}
#else
#include <pthread.h>
#include <sys/sysinfo.h>
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<nthread; i++) {
th[i] = thread(LRfunc, arg+i);
th[i] = std::thread(LRfunc, arg+i);
}
// wait for threads to finish
@@ -1622,20 +1602,6 @@ void mjCModel::LengthRange(mjModel* m, mjData* data) {
th[i].join();
}
// use pthread
#else
// launch threads
pthread_t th[16];
for (int i=0; i<nthread; i++) {
pthread_create(th+i, NULL, LRfunc, arg+i);
}
// wait for threads to finish
for (int i=0; i<nthread; i++) {
pthread_join(th[i], NULL);
}
#endif
// free mjData allocated here
for (int i=1; i<nthread; i++) {
mj_deleteData(pdata[i]);