// File: /workspace/github_cpp_makefile_star2_O1/richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::runAsyncGeneration()
void Swarm::runAsyncGeneration() {   //Raquel: added new function to run assynchronous generations
	// TODO: Implement walltime
	unsigned int nModels = options.models.size();
	if(options.verbosity >= 1) {
		cout << "Running generation " << currentGeneration << " with " << options.swarmSize << " particles..." << endl;
	}

	//unsigned int numFinishedParticles = 0;

//	vector<unsigned int, vector<bool> > runningParticles;
//vector<unsigned int, vector<bool> > finishedParticles;
//	vector<unsigned int> runningSubParticles;
//	vector<unsigned int> finishedSubParticles;

	unsigned int p, mid, sp;
	vector<unsigned int> newFinishedParticles;
	// razi: handle running particles that include subparticles, dont exceed parallel count
	//we need to track number of completed particles which is not simply equal to (nModels * number of subParticles)
	//a particle is considered completed of all its subparticles are finished. XXXX


	sp=0;
	currentsubparticleBestFitsByFit_.clear();
	currentsubswarmBestFits_.clear();
	finishedParticles_.clear();
	finishedSubParticles_.clear(); //Raquel added to solve problem of less and less result files as generations go
	unsigned int maxSubPar = fcalcsubParID(options.swarmSize, options.models.size()-1, options.models.size());
	int tries = 0;

	if(options.swarmSize<4){

		finishFit();
		outputError("Error: If running an asynchronous run please set a swarm size of at least 4, otherwise run the synchronous method.");


	}

	while ( (finishedParticles_.size() <= (unsigned) (options.swarmSize/2) || finishedParticles_.size() <= 2 ) ) { //razi: loop over particles, each particle includes nModels subPArticles

		while (runningSubParticles_.size()< options.parallelCount && sp < maxSubPar) {//razi: make sure the number of subparticles don't exceed parallel count limit
			sp++;
			p = fcalcParID(sp, nModels);
			mid = fcalcMID(sp, nModels);

			launchSubParticle(p, mid, false);
			//launchParticle(p, false);
			//cout << "RAQUEL runningSubParticles_.size() " << runningSubParticles_.size() << " options.parallelCount " << options.parallelCount << endl;

			tries = 0;
		}
		// Check for any messages from particles
		//usleep(250000);
		//cout << "RAQUEL Entering checkMasterMessages" << endl;
		newFinishedParticles = checkMasterMessages();
		if(options.verbosity>=3){

			cout << "@@@@@@@@@@@@@@ SIZE AFTER CHECKING MESSAGES " <<  currentsubswarmBestFits_.size() << " @@@@@@@@@@@@@@@@@@@@@@@" <<endl;

		}
		flightCounter_ += newFinishedParticles.size();
		//cout << "RAQUEL Done checkMasterMessages newFinishedParticles.size() " << newFinishedParticles.size() << endl;
		//cout << "Flight counter = " << flightCounter_ << endl;
		//cout << "finishedSubParticles_.size() " << finishedSubParticles_.size() << " runningSubParticles_.size() " << runningSubParticles_.size() << " maxSubPar " << maxSubPar << " sp " << sp << endl;
		//numFinishedParticles = finishedParticles_.size();
		//cout << "RAQUEL rungeneration numFinishedParticles " << numFinishedParticles << endl;
		//tries++; // Enable this fix if the program stays stuck waiting for a message from the slave, even though the master already sent the message
		if(tries > 50){

			cout << "RAQUEL " << runningSubParticles_.size() << " subparticles failed in generation " << currentGeneration << endl;
//			cout << "RAQUEL resending message NEXT_GENERATION to failed particles" << endl;
			for (auto stuckParticle = runningSubParticles_.begin(); stuckParticle != runningSubParticles_.end(); ++stuckParticle ){

				swarmComm->sendToSwarm(0, *stuckParticle, NEXT_GENERATION, false, swarmComm->univMessageSender);

			}

			tries = 0;
		}
		if(options.verbosity >= 3){
			cout << "FINISHED SUBPARTICLES: " << finishedSubParticles_.size() << endl;
			cout << "FINISHED PARTICLES: " << finishedParticles_.size() << endl;
		}

	}



	/*
	while (numFinishedParticles < options.swarmSize) { //razi: loop over particles, each particle includes nModels subPArticles

		if ((runningSubParticles_.size()*nModels+nModels )<= options.parallelCount && numLaunchedParticles < options.swarmSize) {//razi: make sure subparticles don't exceed parallel count limit
			launchParticle(p);
			numLaunchedSubParticles += 1;
			++p;
		}

		// Check for any messages from particles
		usleep(10000);
		finishedSubParticles = checkMasterMessages();
		numFinishedSubParticles += finishedSubParticles.size();
		finishedSubParticles.clear();
	}
*/

	if ( options.verbosity >=3){ cout<< "running a swarm generation finished .... Particles finished: " << finishedParticles_.size() << endl;}

	if (failedParticles_.size() > (unsigned) (options.swarmSize - 3) ) {
		finishFit();
		outputError("Error: You had too many failed runs. Check simulation output (.BNG_OUT files) or adjust walltime.");
	}
	currentGeneration += 1;



		//finishedParticles_.clear();
		finishedSubParticles_.clear(); //Raquel added to solve problem of less and less result files as generations go




}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
movq 0x15c8(%rdi), %r13
subq 0x15c0(%rdi), %r13
cmpl $0x0, 0x1614(%rdi)
je 0x906cd
movq 0xe5948(%rip), %r14     # 0x175f90
leaq 0x6eded(%rip), %rsi     # 0xff43c
movl $0x13, %edx
movq %r14, %rdi
callq 0x22ab0
movl 0x13e8(%rbx), %esi
movq %r14, %rdi
callq 0x22650
movq %rax, %r14
leaq 0x6eddc(%rip), %rsi     # 0xff450
movl $0x6, %edx
movq %rax, %rdi
callq 0x22ab0
movl 0x1620(%rbx), %esi
movq %r14, %rdi
callq 0x231e0
movq %rax, %r14
leaq 0x6edbe(%rip), %rsi     # 0xff457
movl $0xd, %edx
movq %rax, %rdi
callq 0x22ab0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r14, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
leaq 0x1b70(%rbx), %rdi
callq 0xb02de
leaq 0x1c30(%rbx), %rdi
callq 0xb02de
leaq 0x18c0(%rbx), %rdi
callq 0xb030c
leaq 0x1890(%rbx), %r14
movq %r14, %rdi
callq 0xb030c
movl 0x1620(%rbx), %edi
movq 0x15c8(%rbx), %rdx
subq 0x15c0(%rbx), %rdx
shrq $0x3, %rdx
leal -0x1(%rdx), %esi
callq 0xbf2a3
movq %r14, 0x40(%rsp)
cmpl $0x4, 0x1620(%rbx)
jl 0x90a45
movl %eax, %ebp
movq 0x18e8(%rbx), %rax
xorl %r15d, %r15d
cmpq $0x3, %rax
jb 0x90762
movl 0x1620(%rbx), %ecx
movl %ecx, %edx
shrl $0x1f, %edx
addl %ecx, %edx
sarl %edx
cmpq %rdx, %rax
jbe 0x90762
xorl %esi, %esi
xorl %r12d, %r12d
jmp 0x9099a
shrq $0x3, %r13
movq $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
movl 0x1628(%rbx), %eax
cmpq %rax, 0x1848(%rbx)
jae 0x907b8
cmpl %ebp, %r15d
jae 0x907b8
incl %r15d
movl %r15d, %edi
movl %r13d, %esi
callq 0xbf2b8
movl %eax, %r14d
movl %r15d, %edi
movl %r13d, %esi
callq 0xbf2ae
movq %rbx, %rdi
movl %r14d, %esi
movl %eax, %edx
xorl %ecx, %ecx
callq 0x8c884
jmp 0x90777
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x8df1a
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %r12
movq 0x30(%rsp), %rax
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x90805
movq (%rsp), %rsi
subq %rdi, %rsi
callq 0x229c0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x9081c
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x229c0
cmpl $0x3, 0x1614(%rbx)
jb 0x9088e
movl $0x2c, %edx
movq 0xe575f(%rip), %rdi     # 0x175f90
leaq 0x6ede4(%rip), %rsi     # 0xff61c
callq 0x22ab0
movq 0x1c58(%rbx), %rsi
movq 0xe5745(%rip), %rdi     # 0x175f90
callq 0x22650
movq %rax, %r14
movl $0x18, %edx
movq %rax, %rdi
leaq 0x6ede7(%rip), %rsi     # 0xff649
callq 0x22ab0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r14, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
subq 0x10(%rsp), %r12
shrq $0x2, %r12
addl %r12d, 0x1d84(%rbx)
cmpl $0x3, 0x1614(%rbx)
jb 0x90955
movl $0x17, %edx
movq 0xe56d9(%rip), %rdi     # 0x175f90
leaq 0x6eda4(%rip), %rsi     # 0xff662
callq 0x22ab0
movq 0x18b8(%rbx), %rsi
movq 0xe56bf(%rip), %rdi     # 0x175f90
callq 0x22650
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r14, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movl $0x14, %edx
movq 0xe5684(%rip), %rdi     # 0x175f90
leaq 0x6ed67(%rip), %rsi     # 0xff67a
callq 0x22ab0
movq 0x18e8(%rbx), %rsi
movq 0xe566a(%rip), %rdi     # 0x175f90
callq 0x22650
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r14, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movq 0x18e8(%rbx), %rax
movq 0x10(%rsp), %r12
movq %r12, 0x8(%rsp)
movq 0x18(%rsp), %rsi
movq %rsi, (%rsp)
cmpq $0x3, %rax
jb 0x90777
movl 0x1620(%rbx), %ecx
movl %ecx, %edx
shrl $0x1f, %edx
addl %ecx, %edx
sarl %edx
movq %r12, 0x8(%rsp)
movq %rsi, (%rsp)
cmpq %rdx, %rax
jbe 0x90777
movq %rsi, %r15
cmpl $0x3, 0x1614(%rbx)
jb 0x909fb
movq 0xe55e3(%rip), %rdi     # 0x175f90
leaq 0x6ecdb(%rip), %rsi     # 0xff68f
movl $0x3d, %edx
callq 0x22ab0
movq 0x18e8(%rbx), %rsi
movq 0xe55c4(%rip), %rdi     # 0x175f90
callq 0x22650
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r14, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movl 0x1620(%rbx), %eax
addl $-0x3, %eax
cmpq %rax, 0x19f8(%rbx)
ja 0x90a81
movq %r15, %r14
incl 0x13e8(%rbx)
movq 0x40(%rsp), %rdi
callq 0xb030c
testq %r12, %r12
je 0x90a33
subq %r12, %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x229c0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x86b24
leaq 0x78(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x6eb46(%rip), %rsi     # 0xff5a9
leaq 0x6ebb1(%rip), %rdx     # 0xff61b
leaq 0x68(%rsp), %rdi
callq 0x32210
leaq 0x68(%rsp), %rsi
movq %rbx, %rdi
callq 0x78b66
movq %rbx, %rdi
callq 0x86b24
leaq 0x58(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x6eaae(%rip), %rsi     # 0xff547
leaq 0x6eb08(%rip), %rdx     # 0xff5a8
leaq 0x48(%rsp), %rdi
callq 0x32210
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x78b66
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r14, %rdi
je 0x90b00
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x90b00
jmp 0x90afd
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x90af6
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x90af6
jmp 0x90af3
movq %rax, %rbx
xorl %esi, %esi
xorl %r12d, %r12d
jmp 0x90b22
movq %rax, %rbx
movq %r15, %rsi
jmp 0x90b22
jmp 0x90b16
movq %rax, %rbx
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r12
jmp 0x90b22
movq %rax, %rbx
movq (%rsp), %rsi
movq 0x8(%rsp), %r12
testq %r12, %r12
je 0x90b32
subq %r12, %rsi
movq %r12, %rdi
callq 0x229c0
movq %rbx, %rdi
callq 0x23240