btSimulationIslandManagerMt.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "LinearMath/btScalar.h"
  14. #include "LinearMath/btThreads.h"
  15. #include "btSimulationIslandManagerMt.h"
  16. #include "BulletCollision/BroadphaseCollision/btDispatcher.h"
  17. #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h"
  18. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  19. #include "BulletCollision/CollisionDispatch/btCollisionWorld.h"
  20. #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
  21. #include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolverMt.h" // for s_minimumContactManifoldsForBatching
  22. //#include <stdio.h>
  23. #include "LinearMath/btQuickprof.h"
  24. SIMD_FORCE_INLINE int calcBatchCost(int bodies, int manifolds, int constraints)
  25. {
  26. // rough estimate of the cost of a batch, used for merging
  27. int batchCost = bodies + 8 * manifolds + 4 * constraints;
  28. return batchCost;
  29. }
  30. SIMD_FORCE_INLINE int calcBatchCost(const btSimulationIslandManagerMt::Island* island)
  31. {
  32. return calcBatchCost(island->bodyArray.size(), island->manifoldArray.size(), island->constraintArray.size());
  33. }
  34. btSimulationIslandManagerMt::btSimulationIslandManagerMt()
  35. {
  36. m_minimumSolverBatchSize = calcBatchCost(0, 128, 0);
  37. m_batchIslandMinBodyCount = 32;
  38. m_islandDispatch = parallelIslandDispatch;
  39. m_batchIsland = NULL;
  40. }
  41. btSimulationIslandManagerMt::~btSimulationIslandManagerMt()
  42. {
  43. for (int i = 0; i < m_allocatedIslands.size(); ++i)
  44. {
  45. delete m_allocatedIslands[i];
  46. }
  47. m_allocatedIslands.resize(0);
  48. m_activeIslands.resize(0);
  49. m_freeIslands.resize(0);
  50. }
  51. inline int getIslandId(const btPersistentManifold* lhs)
  52. {
  53. const btCollisionObject* rcolObj0 = static_cast<const btCollisionObject*>(lhs->getBody0());
  54. const btCollisionObject* rcolObj1 = static_cast<const btCollisionObject*>(lhs->getBody1());
  55. int islandId = rcolObj0->getIslandTag() >= 0 ? rcolObj0->getIslandTag() : rcolObj1->getIslandTag();
  56. return islandId;
  57. }
  58. SIMD_FORCE_INLINE int btGetConstraintIslandId(const btTypedConstraint* lhs)
  59. {
  60. const btCollisionObject& rcolObj0 = lhs->getRigidBodyA();
  61. const btCollisionObject& rcolObj1 = lhs->getRigidBodyB();
  62. int islandId = rcolObj0.getIslandTag() >= 0 ? rcolObj0.getIslandTag() : rcolObj1.getIslandTag();
  63. return islandId;
  64. }
  65. /// function object that routes calls to operator<
  66. class IslandBatchSizeSortPredicate
  67. {
  68. public:
  69. bool operator()(const btSimulationIslandManagerMt::Island* lhs, const btSimulationIslandManagerMt::Island* rhs) const
  70. {
  71. int lCost = calcBatchCost(lhs);
  72. int rCost = calcBatchCost(rhs);
  73. return lCost > rCost;
  74. }
  75. };
  76. class IslandBodyCapacitySortPredicate
  77. {
  78. public:
  79. bool operator()(const btSimulationIslandManagerMt::Island* lhs, const btSimulationIslandManagerMt::Island* rhs) const
  80. {
  81. return lhs->bodyArray.capacity() > rhs->bodyArray.capacity();
  82. }
  83. };
  84. void btSimulationIslandManagerMt::Island::append(const Island& other)
  85. {
  86. // append bodies
  87. for (int i = 0; i < other.bodyArray.size(); ++i)
  88. {
  89. bodyArray.push_back(other.bodyArray[i]);
  90. }
  91. // append manifolds
  92. for (int i = 0; i < other.manifoldArray.size(); ++i)
  93. {
  94. manifoldArray.push_back(other.manifoldArray[i]);
  95. }
  96. // append constraints
  97. for (int i = 0; i < other.constraintArray.size(); ++i)
  98. {
  99. constraintArray.push_back(other.constraintArray[i]);
  100. }
  101. }
  102. bool btIsBodyInIsland(const btSimulationIslandManagerMt::Island& island, const btCollisionObject* obj)
  103. {
  104. for (int i = 0; i < island.bodyArray.size(); ++i)
  105. {
  106. if (island.bodyArray[i] == obj)
  107. {
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. void btSimulationIslandManagerMt::initIslandPools()
  114. {
  115. // reset island pools
  116. int numElem = getUnionFind().getNumElements();
  117. m_lookupIslandFromId.resize(numElem);
  118. for (int i = 0; i < m_lookupIslandFromId.size(); ++i)
  119. {
  120. m_lookupIslandFromId[i] = NULL;
  121. }
  122. m_activeIslands.resize(0);
  123. m_freeIslands.resize(0);
  124. // check whether allocated islands are sorted by body capacity (largest to smallest)
  125. int lastCapacity = 0;
  126. bool isSorted = true;
  127. for (int i = 0; i < m_allocatedIslands.size(); ++i)
  128. {
  129. Island* island = m_allocatedIslands[i];
  130. int cap = island->bodyArray.capacity();
  131. if (cap > lastCapacity)
  132. {
  133. isSorted = false;
  134. break;
  135. }
  136. lastCapacity = cap;
  137. }
  138. if (!isSorted)
  139. {
  140. m_allocatedIslands.quickSort(IslandBodyCapacitySortPredicate());
  141. }
  142. m_batchIsland = NULL;
  143. // mark all islands free (but avoid deallocation)
  144. for (int i = 0; i < m_allocatedIslands.size(); ++i)
  145. {
  146. Island* island = m_allocatedIslands[i];
  147. island->bodyArray.resize(0);
  148. island->manifoldArray.resize(0);
  149. island->constraintArray.resize(0);
  150. island->id = -1;
  151. island->isSleeping = true;
  152. m_freeIslands.push_back(island);
  153. }
  154. }
  155. btSimulationIslandManagerMt::Island* btSimulationIslandManagerMt::getIsland(int id)
  156. {
  157. Island* island = m_lookupIslandFromId[id];
  158. if (island == NULL)
  159. {
  160. // search for existing island
  161. for (int i = 0; i < m_activeIslands.size(); ++i)
  162. {
  163. if (m_activeIslands[i]->id == id)
  164. {
  165. island = m_activeIslands[i];
  166. break;
  167. }
  168. }
  169. m_lookupIslandFromId[id] = island;
  170. }
  171. return island;
  172. }
  173. btSimulationIslandManagerMt::Island* btSimulationIslandManagerMt::allocateIsland(int id, int numBodies)
  174. {
  175. Island* island = NULL;
  176. int allocSize = numBodies;
  177. if (numBodies < m_batchIslandMinBodyCount)
  178. {
  179. if (m_batchIsland)
  180. {
  181. island = m_batchIsland;
  182. m_lookupIslandFromId[id] = island;
  183. // if we've made a large enough batch,
  184. if (island->bodyArray.size() + numBodies >= m_batchIslandMinBodyCount)
  185. {
  186. // next time start a new batch
  187. m_batchIsland = NULL;
  188. }
  189. return island;
  190. }
  191. else
  192. {
  193. // need to allocate a batch island
  194. allocSize = m_batchIslandMinBodyCount * 2;
  195. }
  196. }
  197. btAlignedObjectArray<Island*>& freeIslands = m_freeIslands;
  198. // search for free island
  199. if (freeIslands.size() > 0)
  200. {
  201. // try to reuse a previously allocated island
  202. int iFound = freeIslands.size();
  203. // linear search for smallest island that can hold our bodies
  204. for (int i = freeIslands.size() - 1; i >= 0; --i)
  205. {
  206. if (freeIslands[i]->bodyArray.capacity() >= allocSize)
  207. {
  208. iFound = i;
  209. island = freeIslands[i];
  210. island->id = id;
  211. break;
  212. }
  213. }
  214. // if found, shrink array while maintaining ordering
  215. if (island)
  216. {
  217. int iDest = iFound;
  218. int iSrc = iDest + 1;
  219. while (iSrc < freeIslands.size())
  220. {
  221. freeIslands[iDest++] = freeIslands[iSrc++];
  222. }
  223. freeIslands.pop_back();
  224. }
  225. }
  226. if (island == NULL)
  227. {
  228. // no free island found, allocate
  229. island = new Island(); // TODO: change this to use the pool allocator
  230. island->id = id;
  231. island->bodyArray.reserve(allocSize);
  232. m_allocatedIslands.push_back(island);
  233. }
  234. m_lookupIslandFromId[id] = island;
  235. if (numBodies < m_batchIslandMinBodyCount)
  236. {
  237. m_batchIsland = island;
  238. }
  239. m_activeIslands.push_back(island);
  240. return island;
  241. }
  242. void btSimulationIslandManagerMt::buildIslands(btDispatcher* dispatcher, btCollisionWorld* collisionWorld)
  243. {
  244. BT_PROFILE("buildIslands");
  245. btCollisionObjectArray& collisionObjects = collisionWorld->getCollisionObjectArray();
  246. //we are going to sort the unionfind array, and store the element id in the size
  247. //afterwards, we clean unionfind, to make sure no-one uses it anymore
  248. getUnionFind().sortIslands();
  249. int numElem = getUnionFind().getNumElements();
  250. int endIslandIndex = 1;
  251. int startIslandIndex;
  252. //update the sleeping state for bodies, if all are sleeping
  253. for (startIslandIndex = 0; startIslandIndex < numElem; startIslandIndex = endIslandIndex)
  254. {
  255. int islandId = getUnionFind().getElement(startIslandIndex).m_id;
  256. for (endIslandIndex = startIslandIndex + 1; (endIslandIndex < numElem) && (getUnionFind().getElement(endIslandIndex).m_id == islandId); endIslandIndex++)
  257. {
  258. }
  259. //int numSleeping = 0;
  260. bool allSleeping = true;
  261. int idx;
  262. for (idx = startIslandIndex; idx < endIslandIndex; idx++)
  263. {
  264. int i = getUnionFind().getElement(idx).m_sz;
  265. btCollisionObject* colObj0 = collisionObjects[i];
  266. if ((colObj0->getIslandTag() != islandId) && (colObj0->getIslandTag() != -1))
  267. {
  268. // printf("error in island management\n");
  269. }
  270. btAssert((colObj0->getIslandTag() == islandId) || (colObj0->getIslandTag() == -1));
  271. if (colObj0->getIslandTag() == islandId)
  272. {
  273. if (colObj0->getActivationState() == ACTIVE_TAG ||
  274. colObj0->getActivationState() == DISABLE_DEACTIVATION)
  275. {
  276. allSleeping = false;
  277. break;
  278. }
  279. }
  280. }
  281. if (allSleeping)
  282. {
  283. int idx;
  284. for (idx = startIslandIndex; idx < endIslandIndex; idx++)
  285. {
  286. int i = getUnionFind().getElement(idx).m_sz;
  287. btCollisionObject* colObj0 = collisionObjects[i];
  288. if ((colObj0->getIslandTag() != islandId) && (colObj0->getIslandTag() != -1))
  289. {
  290. // printf("error in island management\n");
  291. }
  292. btAssert((colObj0->getIslandTag() == islandId) || (colObj0->getIslandTag() == -1));
  293. if (colObj0->getIslandTag() == islandId)
  294. {
  295. colObj0->setActivationState(ISLAND_SLEEPING);
  296. }
  297. }
  298. }
  299. else
  300. {
  301. int idx;
  302. for (idx = startIslandIndex; idx < endIslandIndex; idx++)
  303. {
  304. int i = getUnionFind().getElement(idx).m_sz;
  305. btCollisionObject* colObj0 = collisionObjects[i];
  306. if ((colObj0->getIslandTag() != islandId) && (colObj0->getIslandTag() != -1))
  307. {
  308. // printf("error in island management\n");
  309. }
  310. btAssert((colObj0->getIslandTag() == islandId) || (colObj0->getIslandTag() == -1));
  311. if (colObj0->getIslandTag() == islandId)
  312. {
  313. if (colObj0->getActivationState() == ISLAND_SLEEPING)
  314. {
  315. colObj0->setActivationState(WANTS_DEACTIVATION);
  316. colObj0->setDeactivationTime(0.f);
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }
  323. void btSimulationIslandManagerMt::addBodiesToIslands(btCollisionWorld* collisionWorld)
  324. {
  325. btCollisionObjectArray& collisionObjects = collisionWorld->getCollisionObjectArray();
  326. int endIslandIndex = 1;
  327. int startIslandIndex;
  328. int numElem = getUnionFind().getNumElements();
  329. // create explicit islands and add bodies to each
  330. for (startIslandIndex = 0; startIslandIndex < numElem; startIslandIndex = endIslandIndex)
  331. {
  332. int islandId = getUnionFind().getElement(startIslandIndex).m_id;
  333. // find end index
  334. for (endIslandIndex = startIslandIndex; (endIslandIndex < numElem) && (getUnionFind().getElement(endIslandIndex).m_id == islandId); endIslandIndex++)
  335. {
  336. }
  337. // check if island is sleeping
  338. bool islandSleeping = true;
  339. for (int iElem = startIslandIndex; iElem < endIslandIndex; iElem++)
  340. {
  341. int i = getUnionFind().getElement(iElem).m_sz;
  342. btCollisionObject* colObj = collisionObjects[i];
  343. if (colObj->isActive())
  344. {
  345. islandSleeping = false;
  346. }
  347. }
  348. if (!islandSleeping)
  349. {
  350. // want to count the number of bodies before allocating the island to optimize memory usage of the Island structures
  351. int numBodies = endIslandIndex - startIslandIndex;
  352. Island* island = allocateIsland(islandId, numBodies);
  353. island->isSleeping = false;
  354. // add bodies to island
  355. for (int iElem = startIslandIndex; iElem < endIslandIndex; iElem++)
  356. {
  357. int i = getUnionFind().getElement(iElem).m_sz;
  358. btCollisionObject* colObj = collisionObjects[i];
  359. island->bodyArray.push_back(colObj);
  360. }
  361. }
  362. }
  363. }
  364. void btSimulationIslandManagerMt::addManifoldsToIslands(btDispatcher* dispatcher)
  365. {
  366. // walk all the manifolds, activating bodies touched by kinematic objects, and add each manifold to its Island
  367. int maxNumManifolds = dispatcher->getNumManifolds();
  368. for (int i = 0; i < maxNumManifolds; i++)
  369. {
  370. btPersistentManifold* manifold = dispatcher->getManifoldByIndexInternal(i);
  371. const btCollisionObject* colObj0 = static_cast<const btCollisionObject*>(manifold->getBody0());
  372. const btCollisionObject* colObj1 = static_cast<const btCollisionObject*>(manifold->getBody1());
  373. ///@todo: check sleeping conditions!
  374. if (((colObj0) && colObj0->getActivationState() != ISLAND_SLEEPING) ||
  375. ((colObj1) && colObj1->getActivationState() != ISLAND_SLEEPING))
  376. {
  377. //kinematic objects don't merge islands, but wake up all connected objects
  378. if (colObj0->isKinematicObject() && colObj0->getActivationState() != ISLAND_SLEEPING)
  379. {
  380. if (colObj0->hasContactResponse())
  381. colObj1->activate();
  382. }
  383. if (colObj1->isKinematicObject() && colObj1->getActivationState() != ISLAND_SLEEPING)
  384. {
  385. if (colObj1->hasContactResponse())
  386. colObj0->activate();
  387. }
  388. //filtering for response
  389. if (dispatcher->needsResponse(colObj0, colObj1))
  390. {
  391. // scatter manifolds into various islands
  392. int islandId = getIslandId(manifold);
  393. // if island not sleeping,
  394. if (Island* island = getIsland(islandId))
  395. {
  396. island->manifoldArray.push_back(manifold);
  397. }
  398. }
  399. }
  400. }
  401. }
  402. void btSimulationIslandManagerMt::addConstraintsToIslands(btAlignedObjectArray<btTypedConstraint*>& constraints)
  403. {
  404. // walk constraints
  405. for (int i = 0; i < constraints.size(); i++)
  406. {
  407. // scatter constraints into various islands
  408. btTypedConstraint* constraint = constraints[i];
  409. if (constraint->isEnabled())
  410. {
  411. int islandId = btGetConstraintIslandId(constraint);
  412. // if island is not sleeping,
  413. if (Island* island = getIsland(islandId))
  414. {
  415. island->constraintArray.push_back(constraint);
  416. }
  417. }
  418. }
  419. }
  420. void btSimulationIslandManagerMt::mergeIslands()
  421. {
  422. // sort islands in order of decreasing batch size
  423. m_activeIslands.quickSort(IslandBatchSizeSortPredicate());
  424. // merge small islands to satisfy minimum batch size
  425. // find first small batch island
  426. int destIslandIndex = m_activeIslands.size();
  427. for (int i = 0; i < m_activeIslands.size(); ++i)
  428. {
  429. Island* island = m_activeIslands[i];
  430. int batchSize = calcBatchCost(island);
  431. if (batchSize < m_minimumSolverBatchSize)
  432. {
  433. destIslandIndex = i;
  434. break;
  435. }
  436. }
  437. int lastIndex = m_activeIslands.size() - 1;
  438. while (destIslandIndex < lastIndex)
  439. {
  440. // merge islands from the back of the list
  441. Island* island = m_activeIslands[destIslandIndex];
  442. int numBodies = island->bodyArray.size();
  443. int numManifolds = island->manifoldArray.size();
  444. int numConstraints = island->constraintArray.size();
  445. int firstIndex = lastIndex;
  446. // figure out how many islands we want to merge and find out how many bodies, manifolds and constraints we will have
  447. while (true)
  448. {
  449. Island* src = m_activeIslands[firstIndex];
  450. numBodies += src->bodyArray.size();
  451. numManifolds += src->manifoldArray.size();
  452. numConstraints += src->constraintArray.size();
  453. int batchCost = calcBatchCost(numBodies, numManifolds, numConstraints);
  454. if (batchCost >= m_minimumSolverBatchSize)
  455. {
  456. break;
  457. }
  458. if (firstIndex - 1 == destIslandIndex)
  459. {
  460. break;
  461. }
  462. firstIndex--;
  463. }
  464. // reserve space for these pointers to minimize reallocation
  465. island->bodyArray.reserve(numBodies);
  466. island->manifoldArray.reserve(numManifolds);
  467. island->constraintArray.reserve(numConstraints);
  468. // merge islands
  469. for (int i = firstIndex; i <= lastIndex; ++i)
  470. {
  471. island->append(*m_activeIslands[i]);
  472. }
  473. // shrink array to exclude the islands that were merged from
  474. m_activeIslands.resize(firstIndex);
  475. lastIndex = firstIndex - 1;
  476. destIslandIndex++;
  477. }
  478. }
  479. void btSimulationIslandManagerMt::solveIsland(btConstraintSolver* solver, Island& island, const SolverParams& solverParams)
  480. {
  481. btPersistentManifold** manifolds = island.manifoldArray.size() ? &island.manifoldArray[0] : NULL;
  482. btTypedConstraint** constraintsPtr = island.constraintArray.size() ? &island.constraintArray[0] : NULL;
  483. solver->solveGroup(&island.bodyArray[0],
  484. island.bodyArray.size(),
  485. manifolds,
  486. island.manifoldArray.size(),
  487. constraintsPtr,
  488. island.constraintArray.size(),
  489. *solverParams.m_solverInfo,
  490. solverParams.m_debugDrawer,
  491. solverParams.m_dispatcher);
  492. }
  493. void btSimulationIslandManagerMt::serialIslandDispatch(btAlignedObjectArray<Island*>* islandsPtr, const SolverParams& solverParams)
  494. {
  495. BT_PROFILE("serialIslandDispatch");
  496. // serial dispatch
  497. btAlignedObjectArray<Island*>& islands = *islandsPtr;
  498. btConstraintSolver* solver = solverParams.m_solverMt ? solverParams.m_solverMt : solverParams.m_solverPool;
  499. for (int i = 0; i < islands.size(); ++i)
  500. {
  501. solveIsland(solver, *islands[i], solverParams);
  502. }
  503. }
  504. struct UpdateIslandDispatcher : public btIParallelForBody
  505. {
  506. btAlignedObjectArray<btSimulationIslandManagerMt::Island*>& m_islandsPtr;
  507. const btSimulationIslandManagerMt::SolverParams& m_solverParams;
  508. UpdateIslandDispatcher(btAlignedObjectArray<btSimulationIslandManagerMt::Island*>& islandsPtr, const btSimulationIslandManagerMt::SolverParams& solverParams)
  509. : m_islandsPtr(islandsPtr), m_solverParams(solverParams)
  510. {
  511. }
  512. void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
  513. {
  514. btConstraintSolver* solver = m_solverParams.m_solverPool;
  515. for (int i = iBegin; i < iEnd; ++i)
  516. {
  517. btSimulationIslandManagerMt::Island* island = m_islandsPtr[i];
  518. btSimulationIslandManagerMt::solveIsland(solver, *island, m_solverParams);
  519. }
  520. }
  521. };
  522. void btSimulationIslandManagerMt::parallelIslandDispatch(btAlignedObjectArray<Island*>* islandsPtr, const SolverParams& solverParams)
  523. {
  524. BT_PROFILE("parallelIslandDispatch");
  525. //
  526. // if there are islands with many contacts, it may be faster to submit these
  527. // large islands *serially* to a single parallel constraint solver, and then later
  528. // submit the remaining smaller islands in parallel to multiple sequential solvers.
  529. //
  530. // Some task schedulers do not deal well with nested parallelFor loops. One implementation
  531. // of OpenMP was actually slower than doing everything single-threaded. Intel TBB
  532. // on the other hand, seems to do a pretty respectable job with it.
  533. //
  534. // When solving islands in parallel, the worst case performance happens when there
  535. // is one very large island and then perhaps a smattering of very small
  536. // islands -- one worker thread takes the large island and the remaining workers
  537. // tear through the smaller islands and then sit idle waiting for the first worker
  538. // to finish. Solving islands in parallel works best when there are numerous small
  539. // islands, roughly equal in size.
  540. //
  541. // By contrast, the other approach -- the parallel constraint solver -- is only
  542. // able to deliver a worthwhile speedup when the island is large. For smaller islands,
  543. // it is difficult to extract a useful amount of parallelism -- the overhead of grouping
  544. // the constraints into batches and sending the batches to worker threads can nullify
  545. // any gains from parallelism.
  546. //
  547. UpdateIslandDispatcher dispatcher(*islandsPtr, solverParams);
  548. // We take advantage of the fact the islands are sorted in order of decreasing size
  549. int iBegin = 0;
  550. if (solverParams.m_solverMt)
  551. {
  552. while (iBegin < islandsPtr->size())
  553. {
  554. btSimulationIslandManagerMt::Island* island = (*islandsPtr)[iBegin];
  555. if (island->manifoldArray.size() < btSequentialImpulseConstraintSolverMt::s_minimumContactManifoldsForBatching)
  556. {
  557. // OK to submit the rest of the array in parallel
  558. break;
  559. }
  560. // serial dispatch to parallel solver for large islands (if any)
  561. solveIsland(solverParams.m_solverMt, *island, solverParams);
  562. ++iBegin;
  563. }
  564. }
  565. // parallel dispatch to sequential solvers for rest
  566. btParallelFor(iBegin, islandsPtr->size(), 1, dispatcher);
  567. }
  568. ///@todo: this is random access, it can be walked 'cache friendly'!
  569. void btSimulationIslandManagerMt::buildAndProcessIslands(btDispatcher* dispatcher,
  570. btCollisionWorld* collisionWorld,
  571. btAlignedObjectArray<btTypedConstraint*>& constraints,
  572. const SolverParams& solverParams)
  573. {
  574. BT_PROFILE("buildAndProcessIslands");
  575. btCollisionObjectArray& collisionObjects = collisionWorld->getCollisionObjectArray();
  576. buildIslands(dispatcher, collisionWorld);
  577. if (!getSplitIslands())
  578. {
  579. btPersistentManifold** manifolds = dispatcher->getInternalManifoldPointer();
  580. int maxNumManifolds = dispatcher->getNumManifolds();
  581. for (int i = 0; i < maxNumManifolds; i++)
  582. {
  583. btPersistentManifold* manifold = manifolds[i];
  584. const btCollisionObject* colObj0 = static_cast<const btCollisionObject*>(manifold->getBody0());
  585. const btCollisionObject* colObj1 = static_cast<const btCollisionObject*>(manifold->getBody1());
  586. ///@todo: check sleeping conditions!
  587. if (((colObj0) && colObj0->getActivationState() != ISLAND_SLEEPING) ||
  588. ((colObj1) && colObj1->getActivationState() != ISLAND_SLEEPING))
  589. {
  590. //kinematic objects don't merge islands, but wake up all connected objects
  591. if (colObj0->isKinematicObject() && colObj0->getActivationState() != ISLAND_SLEEPING)
  592. {
  593. if (colObj0->hasContactResponse())
  594. colObj1->activate();
  595. }
  596. if (colObj1->isKinematicObject() && colObj1->getActivationState() != ISLAND_SLEEPING)
  597. {
  598. if (colObj1->hasContactResponse())
  599. colObj0->activate();
  600. }
  601. }
  602. }
  603. btTypedConstraint** constraintsPtr = constraints.size() ? &constraints[0] : NULL;
  604. btConstraintSolver* solver = solverParams.m_solverMt ? solverParams.m_solverMt : solverParams.m_solverPool;
  605. solver->solveGroup(&collisionObjects[0],
  606. collisionObjects.size(),
  607. manifolds,
  608. maxNumManifolds,
  609. constraintsPtr,
  610. constraints.size(),
  611. *solverParams.m_solverInfo,
  612. solverParams.m_debugDrawer,
  613. solverParams.m_dispatcher);
  614. }
  615. else
  616. {
  617. initIslandPools();
  618. //traverse the simulation islands, and call the solver, unless all objects are sleeping/deactivated
  619. addBodiesToIslands(collisionWorld);
  620. addManifoldsToIslands(dispatcher);
  621. addConstraintsToIslands(constraints);
  622. // m_activeIslands array should now contain all non-sleeping Islands, and each Island should
  623. // have all the necessary bodies, manifolds and constraints.
  624. // if we want to merge islands with small batch counts,
  625. if (m_minimumSolverBatchSize > 1)
  626. {
  627. mergeIslands();
  628. }
  629. // dispatch islands to solver
  630. m_islandDispatch(&m_activeIslands, solverParams);
  631. }
  632. }