btSimulationIslandManagerMt.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
  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 btGetConstraintIslandId1(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. btAssert(id >= 0);
  158. btAssert(id < m_lookupIslandFromId.size());
  159. Island* island = m_lookupIslandFromId[id];
  160. if (island == NULL)
  161. {
  162. // search for existing island
  163. for (int i = 0; i < m_activeIslands.size(); ++i)
  164. {
  165. if (m_activeIslands[i]->id == id)
  166. {
  167. island = m_activeIslands[i];
  168. break;
  169. }
  170. }
  171. m_lookupIslandFromId[id] = island;
  172. }
  173. return island;
  174. }
  175. btSimulationIslandManagerMt::Island* btSimulationIslandManagerMt::allocateIsland(int id, int numBodies)
  176. {
  177. Island* island = NULL;
  178. int allocSize = numBodies;
  179. if (numBodies < m_batchIslandMinBodyCount)
  180. {
  181. if (m_batchIsland)
  182. {
  183. island = m_batchIsland;
  184. m_lookupIslandFromId[id] = island;
  185. // if we've made a large enough batch,
  186. if (island->bodyArray.size() + numBodies >= m_batchIslandMinBodyCount)
  187. {
  188. // next time start a new batch
  189. m_batchIsland = NULL;
  190. }
  191. return island;
  192. }
  193. else
  194. {
  195. // need to allocate a batch island
  196. allocSize = m_batchIslandMinBodyCount * 2;
  197. }
  198. }
  199. btAlignedObjectArray<Island*>& freeIslands = m_freeIslands;
  200. // search for free island
  201. if (freeIslands.size() > 0)
  202. {
  203. // try to reuse a previously allocated island
  204. int iFound = freeIslands.size();
  205. // linear search for smallest island that can hold our bodies
  206. for (int i = freeIslands.size() - 1; i >= 0; --i)
  207. {
  208. if (freeIslands[i]->bodyArray.capacity() >= allocSize)
  209. {
  210. iFound = i;
  211. island = freeIslands[i];
  212. island->id = id;
  213. break;
  214. }
  215. }
  216. // if found, shrink array while maintaining ordering
  217. if (island)
  218. {
  219. int iDest = iFound;
  220. int iSrc = iDest + 1;
  221. while (iSrc < freeIslands.size())
  222. {
  223. freeIslands[iDest++] = freeIslands[iSrc++];
  224. }
  225. freeIslands.pop_back();
  226. }
  227. }
  228. if (island == NULL)
  229. {
  230. // no free island found, allocate
  231. island = new Island(); // TODO: change this to use the pool allocator
  232. island->id = id;
  233. island->bodyArray.reserve(allocSize);
  234. m_allocatedIslands.push_back(island);
  235. }
  236. m_lookupIslandFromId[id] = island;
  237. if (numBodies < m_batchIslandMinBodyCount)
  238. {
  239. m_batchIsland = island;
  240. }
  241. m_activeIslands.push_back(island);
  242. return island;
  243. }
  244. void btSimulationIslandManagerMt::buildIslands(btDispatcher* dispatcher, btCollisionWorld* collisionWorld)
  245. {
  246. BT_PROFILE("buildIslands");
  247. btCollisionObjectArray& collisionObjects = collisionWorld->getCollisionObjectArray();
  248. //we are going to sort the unionfind array, and store the element id in the size
  249. //afterwards, we clean unionfind, to make sure no-one uses it anymore
  250. getUnionFind().sortIslands();
  251. int numElem = getUnionFind().getNumElements();
  252. int endIslandIndex = 1;
  253. int startIslandIndex;
  254. //update the sleeping state for bodies, if all are sleeping
  255. for (startIslandIndex = 0; startIslandIndex < numElem; startIslandIndex = endIslandIndex)
  256. {
  257. int islandId = getUnionFind().getElement(startIslandIndex).m_id;
  258. for (endIslandIndex = startIslandIndex + 1; (endIslandIndex < numElem) && (getUnionFind().getElement(endIslandIndex).m_id == islandId); endIslandIndex++)
  259. {
  260. }
  261. //int numSleeping = 0;
  262. bool allSleeping = true;
  263. int idx;
  264. for (idx = startIslandIndex; idx < endIslandIndex; idx++)
  265. {
  266. int i = getUnionFind().getElement(idx).m_sz;
  267. btCollisionObject* colObj0 = collisionObjects[i];
  268. if ((colObj0->getIslandTag() != islandId) && (colObj0->getIslandTag() != -1))
  269. {
  270. // printf("error in island management\n");
  271. }
  272. btAssert((colObj0->getIslandTag() == islandId) || (colObj0->getIslandTag() == -1));
  273. if (colObj0->getIslandTag() == islandId)
  274. {
  275. if (colObj0->getActivationState() == ACTIVE_TAG ||
  276. colObj0->getActivationState() == DISABLE_DEACTIVATION)
  277. {
  278. allSleeping = false;
  279. break;
  280. }
  281. }
  282. }
  283. if (allSleeping)
  284. {
  285. int idx;
  286. for (idx = startIslandIndex; idx < endIslandIndex; idx++)
  287. {
  288. int i = getUnionFind().getElement(idx).m_sz;
  289. btCollisionObject* colObj0 = collisionObjects[i];
  290. if ((colObj0->getIslandTag() != islandId) && (colObj0->getIslandTag() != -1))
  291. {
  292. // printf("error in island management\n");
  293. }
  294. btAssert((colObj0->getIslandTag() == islandId) || (colObj0->getIslandTag() == -1));
  295. if (colObj0->getIslandTag() == islandId)
  296. {
  297. colObj0->setActivationState(ISLAND_SLEEPING);
  298. }
  299. }
  300. }
  301. else
  302. {
  303. int idx;
  304. for (idx = startIslandIndex; idx < endIslandIndex; idx++)
  305. {
  306. int i = getUnionFind().getElement(idx).m_sz;
  307. btCollisionObject* colObj0 = collisionObjects[i];
  308. if ((colObj0->getIslandTag() != islandId) && (colObj0->getIslandTag() != -1))
  309. {
  310. // printf("error in island management\n");
  311. }
  312. btAssert((colObj0->getIslandTag() == islandId) || (colObj0->getIslandTag() == -1));
  313. if (colObj0->getIslandTag() == islandId)
  314. {
  315. if (colObj0->getActivationState() == ISLAND_SLEEPING)
  316. {
  317. colObj0->setActivationState(WANTS_DEACTIVATION);
  318. colObj0->setDeactivationTime(0.f);
  319. }
  320. }
  321. }
  322. }
  323. }
  324. }
  325. void btSimulationIslandManagerMt::addBodiesToIslands(btCollisionWorld* collisionWorld)
  326. {
  327. btCollisionObjectArray& collisionObjects = collisionWorld->getCollisionObjectArray();
  328. int endIslandIndex = 1;
  329. int startIslandIndex;
  330. int numElem = getUnionFind().getNumElements();
  331. // create explicit islands and add bodies to each
  332. for (startIslandIndex = 0; startIslandIndex < numElem; startIslandIndex = endIslandIndex)
  333. {
  334. int islandId = getUnionFind().getElement(startIslandIndex).m_id;
  335. // find end index
  336. for (endIslandIndex = startIslandIndex; (endIslandIndex < numElem) && (getUnionFind().getElement(endIslandIndex).m_id == islandId); endIslandIndex++)
  337. {
  338. }
  339. // check if island is sleeping
  340. bool islandSleeping = true;
  341. for (int iElem = startIslandIndex; iElem < endIslandIndex; iElem++)
  342. {
  343. int i = getUnionFind().getElement(iElem).m_sz;
  344. btCollisionObject* colObj = collisionObjects[i];
  345. if (colObj->isActive())
  346. {
  347. islandSleeping = false;
  348. }
  349. }
  350. if (!islandSleeping)
  351. {
  352. // want to count the number of bodies before allocating the island to optimize memory usage of the Island structures
  353. int numBodies = endIslandIndex - startIslandIndex;
  354. Island* island = allocateIsland(islandId, numBodies);
  355. island->isSleeping = false;
  356. // add bodies to island
  357. for (int iElem = startIslandIndex; iElem < endIslandIndex; iElem++)
  358. {
  359. int i = getUnionFind().getElement(iElem).m_sz;
  360. btCollisionObject* colObj = collisionObjects[i];
  361. island->bodyArray.push_back(colObj);
  362. }
  363. }
  364. }
  365. }
  366. void btSimulationIslandManagerMt::addManifoldsToIslands(btDispatcher* dispatcher)
  367. {
  368. // walk all the manifolds, activating bodies touched by kinematic objects, and add each manifold to its Island
  369. int maxNumManifolds = dispatcher->getNumManifolds();
  370. for (int i = 0; i < maxNumManifolds; i++)
  371. {
  372. btPersistentManifold* manifold = dispatcher->getManifoldByIndexInternal(i);
  373. const btCollisionObject* colObj0 = static_cast<const btCollisionObject*>(manifold->getBody0());
  374. const btCollisionObject* colObj1 = static_cast<const btCollisionObject*>(manifold->getBody1());
  375. ///@todo: check sleeping conditions!
  376. if (((colObj0) && colObj0->getActivationState() != ISLAND_SLEEPING) ||
  377. ((colObj1) && colObj1->getActivationState() != ISLAND_SLEEPING))
  378. {
  379. //kinematic objects don't merge islands, but wake up all connected objects
  380. if (colObj0->isKinematicObject() && colObj0->getActivationState() != ISLAND_SLEEPING)
  381. {
  382. if (colObj0->hasContactResponse())
  383. colObj1->activate();
  384. }
  385. if (colObj1->isKinematicObject() && colObj1->getActivationState() != ISLAND_SLEEPING)
  386. {
  387. if (colObj1->hasContactResponse())
  388. colObj0->activate();
  389. }
  390. //filtering for response
  391. if (dispatcher->needsResponse(colObj0, colObj1))
  392. {
  393. // scatter manifolds into various islands
  394. int islandId = getIslandId(manifold);
  395. // if island not sleeping,
  396. if (Island* island = getIsland(islandId))
  397. {
  398. island->manifoldArray.push_back(manifold);
  399. }
  400. }
  401. }
  402. }
  403. }
  404. void btSimulationIslandManagerMt::addConstraintsToIslands(btAlignedObjectArray<btTypedConstraint*>& constraints)
  405. {
  406. // walk constraints
  407. for (int i = 0; i < constraints.size(); i++)
  408. {
  409. // scatter constraints into various islands
  410. btTypedConstraint* constraint = constraints[i];
  411. if (constraint->isEnabled())
  412. {
  413. int islandId = btGetConstraintIslandId1(constraint);
  414. // if island is not sleeping,
  415. if (Island* island = getIsland(islandId))
  416. {
  417. island->constraintArray.push_back(constraint);
  418. }
  419. }
  420. }
  421. }
  422. void btSimulationIslandManagerMt::mergeIslands()
  423. {
  424. // sort islands in order of decreasing batch size
  425. m_activeIslands.quickSort(IslandBatchSizeSortPredicate());
  426. // merge small islands to satisfy minimum batch size
  427. // find first small batch island
  428. int destIslandIndex = m_activeIslands.size();
  429. for (int i = 0; i < m_activeIslands.size(); ++i)
  430. {
  431. Island* island = m_activeIslands[i];
  432. int batchSize = calcBatchCost(island);
  433. if (batchSize < m_minimumSolverBatchSize)
  434. {
  435. destIslandIndex = i;
  436. break;
  437. }
  438. }
  439. int lastIndex = m_activeIslands.size() - 1;
  440. while (destIslandIndex < lastIndex)
  441. {
  442. // merge islands from the back of the list
  443. Island* island = m_activeIslands[destIslandIndex];
  444. int numBodies = island->bodyArray.size();
  445. int numManifolds = island->manifoldArray.size();
  446. int numConstraints = island->constraintArray.size();
  447. int firstIndex = lastIndex;
  448. // figure out how many islands we want to merge and find out how many bodies, manifolds and constraints we will have
  449. while (true)
  450. {
  451. Island* src = m_activeIslands[firstIndex];
  452. numBodies += src->bodyArray.size();
  453. numManifolds += src->manifoldArray.size();
  454. numConstraints += src->constraintArray.size();
  455. int batchCost = calcBatchCost(numBodies, numManifolds, numConstraints);
  456. if (batchCost >= m_minimumSolverBatchSize)
  457. {
  458. break;
  459. }
  460. if (firstIndex - 1 == destIslandIndex)
  461. {
  462. break;
  463. }
  464. firstIndex--;
  465. }
  466. // reserve space for these pointers to minimize reallocation
  467. island->bodyArray.reserve(numBodies);
  468. island->manifoldArray.reserve(numManifolds);
  469. island->constraintArray.reserve(numConstraints);
  470. // merge islands
  471. for (int i = firstIndex; i <= lastIndex; ++i)
  472. {
  473. island->append(*m_activeIslands[i]);
  474. }
  475. // shrink array to exclude the islands that were merged from
  476. m_activeIslands.resize(firstIndex);
  477. lastIndex = firstIndex - 1;
  478. destIslandIndex++;
  479. }
  480. }
  481. void btSimulationIslandManagerMt::solveIsland(btConstraintSolver* solver, Island& island, const SolverParams& solverParams)
  482. {
  483. btPersistentManifold** manifolds = island.manifoldArray.size() ? &island.manifoldArray[0] : NULL;
  484. btTypedConstraint** constraintsPtr = island.constraintArray.size() ? &island.constraintArray[0] : NULL;
  485. solver->solveGroup(&island.bodyArray[0],
  486. island.bodyArray.size(),
  487. manifolds,
  488. island.manifoldArray.size(),
  489. constraintsPtr,
  490. island.constraintArray.size(),
  491. *solverParams.m_solverInfo,
  492. solverParams.m_debugDrawer,
  493. solverParams.m_dispatcher);
  494. }
  495. void btSimulationIslandManagerMt::serialIslandDispatch(btAlignedObjectArray<Island*>* islandsPtr, const SolverParams& solverParams)
  496. {
  497. BT_PROFILE("serialIslandDispatch");
  498. // serial dispatch
  499. btAlignedObjectArray<Island*>& islands = *islandsPtr;
  500. btConstraintSolver* solver = solverParams.m_solverMt ? solverParams.m_solverMt : solverParams.m_solverPool;
  501. for (int i = 0; i < islands.size(); ++i)
  502. {
  503. solveIsland(solver, *islands[i], solverParams);
  504. }
  505. }
  506. struct UpdateIslandDispatcher : public btIParallelForBody
  507. {
  508. btAlignedObjectArray<btSimulationIslandManagerMt::Island*>& m_islandsPtr;
  509. const btSimulationIslandManagerMt::SolverParams& m_solverParams;
  510. UpdateIslandDispatcher(btAlignedObjectArray<btSimulationIslandManagerMt::Island*>& islandsPtr, const btSimulationIslandManagerMt::SolverParams& solverParams)
  511. : m_islandsPtr(islandsPtr), m_solverParams(solverParams)
  512. {
  513. }
  514. void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
  515. {
  516. btConstraintSolver* solver = m_solverParams.m_solverPool;
  517. for (int i = iBegin; i < iEnd; ++i)
  518. {
  519. btSimulationIslandManagerMt::Island* island = m_islandsPtr[i];
  520. btSimulationIslandManagerMt::solveIsland(solver, *island, m_solverParams);
  521. }
  522. }
  523. };
  524. void btSimulationIslandManagerMt::parallelIslandDispatch(btAlignedObjectArray<Island*>* islandsPtr, const SolverParams& solverParams)
  525. {
  526. BT_PROFILE("parallelIslandDispatch");
  527. //
  528. // if there are islands with many contacts, it may be faster to submit these
  529. // large islands *serially* to a single parallel constraint solver, and then later
  530. // submit the remaining smaller islands in parallel to multiple sequential solvers.
  531. //
  532. // Some task schedulers do not deal well with nested parallelFor loops. One implementation
  533. // of OpenMP was actually slower than doing everything single-threaded. Intel TBB
  534. // on the other hand, seems to do a pretty respectable job with it.
  535. //
  536. // When solving islands in parallel, the worst case performance happens when there
  537. // is one very large island and then perhaps a smattering of very small
  538. // islands -- one worker thread takes the large island and the remaining workers
  539. // tear through the smaller islands and then sit idle waiting for the first worker
  540. // to finish. Solving islands in parallel works best when there are numerous small
  541. // islands, roughly equal in size.
  542. //
  543. // By contrast, the other approach -- the parallel constraint solver -- is only
  544. // able to deliver a worthwhile speedup when the island is large. For smaller islands,
  545. // it is difficult to extract a useful amount of parallelism -- the overhead of grouping
  546. // the constraints into batches and sending the batches to worker threads can nullify
  547. // any gains from parallelism.
  548. //
  549. UpdateIslandDispatcher dispatcher(*islandsPtr, solverParams);
  550. // We take advantage of the fact the islands are sorted in order of decreasing size
  551. int iBegin = 0;
  552. if (solverParams.m_solverMt)
  553. {
  554. while (iBegin < islandsPtr->size())
  555. {
  556. btSimulationIslandManagerMt::Island* island = (*islandsPtr)[iBegin];
  557. if (island->manifoldArray.size() < btSequentialImpulseConstraintSolverMt::s_minimumContactManifoldsForBatching)
  558. {
  559. // OK to submit the rest of the array in parallel
  560. break;
  561. }
  562. // serial dispatch to parallel solver for large islands (if any)
  563. solveIsland(solverParams.m_solverMt, *island, solverParams);
  564. ++iBegin;
  565. }
  566. }
  567. // parallel dispatch to sequential solvers for rest
  568. btParallelFor(iBegin, islandsPtr->size(), 1, dispatcher);
  569. }
  570. ///@todo: this is random access, it can be walked 'cache friendly'!
  571. void btSimulationIslandManagerMt::buildAndProcessIslands(btDispatcher* dispatcher,
  572. btCollisionWorld* collisionWorld,
  573. btAlignedObjectArray<btTypedConstraint*>& constraints,
  574. const SolverParams& solverParams)
  575. {
  576. BT_PROFILE("buildAndProcessIslands");
  577. btCollisionObjectArray& collisionObjects = collisionWorld->getCollisionObjectArray();
  578. buildIslands(dispatcher, collisionWorld);
  579. if (!getSplitIslands())
  580. {
  581. btPersistentManifold** manifolds = dispatcher->getInternalManifoldPointer();
  582. int maxNumManifolds = dispatcher->getNumManifolds();
  583. for (int i = 0; i < maxNumManifolds; i++)
  584. {
  585. btPersistentManifold* manifold = manifolds[i];
  586. const btCollisionObject* colObj0 = static_cast<const btCollisionObject*>(manifold->getBody0());
  587. const btCollisionObject* colObj1 = static_cast<const btCollisionObject*>(manifold->getBody1());
  588. ///@todo: check sleeping conditions!
  589. if (((colObj0) && colObj0->getActivationState() != ISLAND_SLEEPING) ||
  590. ((colObj1) && colObj1->getActivationState() != ISLAND_SLEEPING))
  591. {
  592. //kinematic objects don't merge islands, but wake up all connected objects
  593. if (colObj0->isKinematicObject() && colObj0->getActivationState() != ISLAND_SLEEPING)
  594. {
  595. if (colObj0->hasContactResponse())
  596. colObj1->activate();
  597. }
  598. if (colObj1->isKinematicObject() && colObj1->getActivationState() != ISLAND_SLEEPING)
  599. {
  600. if (colObj1->hasContactResponse())
  601. colObj0->activate();
  602. }
  603. }
  604. }
  605. btTypedConstraint** constraintsPtr = constraints.size() ? &constraints[0] : NULL;
  606. btConstraintSolver* solver = solverParams.m_solverMt ? solverParams.m_solverMt : solverParams.m_solverPool;
  607. solver->solveGroup(&collisionObjects[0],
  608. collisionObjects.size(),
  609. manifolds,
  610. maxNumManifolds,
  611. constraintsPtr,
  612. constraints.size(),
  613. *solverParams.m_solverInfo,
  614. solverParams.m_debugDrawer,
  615. solverParams.m_dispatcher);
  616. }
  617. else
  618. {
  619. initIslandPools();
  620. //traverse the simulation islands, and call the solver, unless all objects are sleeping/deactivated
  621. addBodiesToIslands(collisionWorld);
  622. addManifoldsToIslands(dispatcher);
  623. addConstraintsToIslands(constraints);
  624. // m_activeIslands array should now contain all non-sleeping Islands, and each Island should
  625. // have all the necessary bodies, manifolds and constraints.
  626. // if we want to merge islands with small batch counts,
  627. if (m_minimumSolverBatchSize > 1)
  628. {
  629. mergeIslands();
  630. }
  631. // dispatch islands to solver
  632. m_islandDispatch(&m_activeIslands, solverParams);
  633. }
  634. }