determinism with mt bullet?

Post Reply
nickak2003
Posts: 14
Joined: Thu Mar 04, 2010 10:35 pm

determinism with mt bullet?

Post by nickak2003 » Tue Feb 25, 2020 10:54 pm

Hello, I am trying to run bullet and use MT, is there a way to do it deterministically? Right now I have the following stuff:

Code: Select all

	setTaskScheduler(SchedulerType::DEFAULT);

Code: Select all

	btDefaultCollisionConstructionInfo cci;
	cci.m_defaultMaxPersistentManifoldPoolSize = 80000;
	cci.m_defaultMaxCollisionAlgorithmPoolSize = 80000;

	mCollisionConfiguration = new btDefaultCollisionConfiguration(cci);

	mDispatcher = new btCollisionDispatcherMt(mCollisionConfiguration, 40);

	mBroadphase =  new btDbvtBroadphase(); 
	
	auto createSolver = [&]() -> btConstraintSolverPoolMt* {

		SolverType tmpSolverType = mSolverType;
		if (tmpSolverType == SolverType::SOLVER_TYPE_SEQUENTIAL_IMPULSE_MT) {
			// pool solvers shouldn't be parallel solvers, we don't allow that kind of
			// nested parallelism because of performance issues
			//tmpSolverType = SolverType::SOLVER_TYPE_SEQUENTIAL_IMPULSE;
		}
		const int threadCount = 16;//BT_MAX_THREAD_COUNT
		btConstraintSolver* solvers[threadCount];
		for (int i = 0; i < threadCount ; ++i) {

			solvers[i] = createSolverByType(tmpSolverType);
			
		}
		return new btConstraintSolverPoolMt(solvers, threadCount);
	};

	mSolver = createSolver();
	
	if (mSolverType == SolverType::SOLVER_TYPE_SEQUENTIAL_IMPULSE_MT) {
		mImpulseSolverMt = new btSequentialImpulseConstraintSolverMt();
		mImpulseSolverMt->setRandSeed(seed);
		mImpulseSolverMt->reset();
	}

	mDynamicsWorld = new btDiscreteDynamicsWorldMt(mDispatcher, mBroadphase, mSolver, mImpulseSolverMt, mCollisionConfiguration);

	mDynamicsWorld->getSolverInfo().m_solverMode = SOLVER_SIMD | SOLVER_USE_WARMSTARTING ;

	mDynamicsWorld->applyGravity();
	
	mGhostPairCallback = new btGhostPairCallback();
	
	mDynamicsWorld->getBroadphase()->getOverlappingPairCache()->setInternalGhostPairCallback(mGhostPairCallback);
	mDynamicsWorld->getBroadphase()->resetPool(mDispatcher);
I am fairly new to bullet, any help is appreciated.

nickak2003
Posts: 14
Joined: Thu Mar 04, 2010 10:35 pm

Re: determinism with mt bullet?

Post by nickak2003 » Thu Feb 27, 2020 6:50 pm

Is deterministic MT not supported in bullet 2?

nickak2003
Posts: 14
Joined: Thu Mar 04, 2010 10:35 pm

Re: determinism with mt bullet?

Post by nickak2003 » Thu Feb 27, 2020 9:50 pm

So i tried handling the collisions in a specific order, assuming they can come in mixed-up, but that does not seem to help. To generate the order, i always attach a unique ID to userIndex when an object is created.

Code: Select all

obj->setUserIndex(uniqueID++)
here's my collision routine.

Code: Select all

	
	static std::vector< btPersistentManifold* > manifolds;
	manifolds.resize(numManifolds);

	for (int i = 0; i < numManifolds; ++i) {
		manifolds[i] = mDispatcher->getManifoldByIndexInternal(i);
	}

	std::sort(manifolds.begin(), manifolds.end(), [&](auto& a, auto& b)->bool {
		return a->getBody0()->getUserIndex() < b->getBody0()->getUserIndex();
		});
	//we now have objects sorted by user index body0
	//now we need to sort by body1
	auto begin = manifolds.begin();
	while (begin != manifolds.end()) {
		int currentIndex = (*begin)->getBody0()->getUserIndex();
		auto part = std::partition_point(begin, manifolds.end(), [&](auto& m) {
			return m->getBody0()->getUserIndex() == currentIndex;
			});

		std::sort(begin, part, [&](auto& a, auto& b)->bool {
			return a->getBody1()->getUserIndex() < b->getBody1()->getUserIndex();
			});
		begin = part;
	}
	
	
	for (int i = 0; i < numManifolds; ++i) {
	
	btPersistentManifold* contactManifold = manifolds[i];
		
	int numContacts = contactManifold->getNumContacts();
	
	if (numContacts > 0) {

		++contacts;
			
		const btCollisionObject* objA = contactManifold->getBody0();
		const btCollisionObject* objB = contactManifold->getBody1();

		mCollisionCallback->onCollision(objA, objB);
	}
}
Although I can guarantee a dispatch order for my collision routine, the actual collision pairs passed in are not consistent and deterministic. For instance, ill have numManifold=x for a while and then a divergence at frame n. Is there somewhere in bullet that i can inherit and sort there, or what needs to be done?

Post Reply