// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %rdi, %rbx
movq 0x15c8(%rdi), %r14
subq 0x15c0(%rdi), %r14
cmpl $0x0, 0x1614(%rdi)
je 0x784f5
movq 0xedaf1(%rip), %rdi     # 0x165f98
leaq 0x6515e(%rip), %rsi     # 0xdd60c
callq 0x289f0
movl 0x13e8(%rbx), %esi
movq %rax, %rdi
callq 0x286e0
leaq 0x65158(%rip), %rsi     # 0xdd620
movq %rax, %rdi
callq 0x289f0
movl 0x1620(%rbx), %esi
movq %rax, %rdi
callq 0x29290
leaq 0x65142(%rip), %rsi     # 0xdd627
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
leaq 0x1b70(%rbx), %rdi
callq 0x8f982
leaq 0x1c30(%rbx), %rdi
callq 0x8f982
leaq 0x18c0(%rbx), %rdi
callq 0x8f9b0
leaq 0x1890(%rbx), %rdi
movq %rdi, 0x18(%rsp)
callq 0x8f9b0
movl 0x1620(%rbx), %edi
movq 0x15c8(%rbx), %rdx
subq 0x15c0(%rbx), %rdx
shrq $0x3, %rdx
leal -0x1(%rdx), %esi
callq 0x9ec09
movl 0x1620(%rbx), %ecx
cmpl $0x4, %ecx
jl 0x78735
movl %eax, %ebp
shrq $0x3, %r14
xorl %r12d, %r12d
leaq 0x20(%rsp), %r13
movq 0x18e8(%rbx), %rsi
movl %ecx, %eax
cltd
pushq $0x2
popq %rdi
idivl %edi
cmpq $0x3, %rsi
jb 0x78592
cmpq %rax, %rsi
ja 0x786c9
movl 0x1628(%rbx), %eax
cmpq %rax, 0x1848(%rbx)
jae 0x785d3
cmpl %ebp, %r12d
jae 0x785d3
incl %r12d
movl %r12d, %edi
movl %r14d, %esi
callq 0x9ec1e
movl %eax, %r15d
movl %r12d, %edi
movl %r14d, %esi
callq 0x9ec14
movq %rbx, %rdi
movl %r15d, %esi
movl %eax, %edx
xorl %ecx, %ecx
callq 0x76502
jmp 0x78592
movq %r13, %rdi
movq %rbx, %rsi
callq 0x770c4
movq %rsp, %rdi
movq %r13, %rsi
callq 0x8f082
movq %r13, %rdi
callq 0x5b268
cmpl $0x3, 0x1614(%rbx)
jb 0x786ab
movq 0xed993(%rip), %rdi     # 0x165f98
leaq 0x651e0(%rip), %rsi     # 0xdd7ec
callq 0x289f0
movq 0x1c58(%rbx), %rsi
movq %rax, %rdi
callq 0x286e0
movq %rax, %rdi
leaq 0x651ef(%rip), %rsi     # 0xdd819
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
movl 0x1614(%rbx), %eax
movq 0x8(%rsp), %rcx
subq (%rsp), %rcx
shrq $0x2, %rcx
addl %ecx, 0x1d84(%rbx)
cmpl $0x2, %eax
jbe 0x786be
movq 0xed93c(%rip), %rdi     # 0x165f98
leaq 0x651cf(%rip), %rsi     # 0xdd832
callq 0x289f0
movq 0x18b8(%rbx), %rsi
movq %rax, %rdi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
movq 0xed912(%rip), %rdi     # 0x165f98
leaq 0x651bd(%rip), %rsi     # 0xdd84a
callq 0x289f0
movq 0x18e8(%rbx), %rsi
movq %rax, %rdi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
jmp 0x786be
movq 0x8(%rsp), %rax
subq (%rsp), %rax
shrq $0x2, %rax
addl %eax, 0x1d84(%rbx)
movl 0x1620(%rbx), %ecx
jmp 0x78574
cmpl $0x3, 0x1614(%rbx)
jb 0x78702
movq 0xed8bf(%rip), %rdi     # 0x165f98
leaq 0x6517f(%rip), %rsi     # 0xdd85f
callq 0x289f0
movq 0x18e8(%rbx), %rsi
movq %rax, %rdi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
movl 0x1620(%rbx), %ecx
addl $-0x3, %ecx
cmpq %rcx, 0x19f8(%rbx)
ja 0x78760
incl 0x13e8(%rbx)
movq 0x18(%rsp), %rdi
callq 0x8f9b0
movq %rsp, %rdi
callq 0x5b268
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x728ee
leaq 0x65035(%rip), %rsi     # 0xdd779
leaq 0x58(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x33402
leaq 0x58(%rsp), %rsi
movq %rbx, %rdi
callq 0x6aed2
movq %rbx, %rdi
callq 0x728ee
leaq 0x64fa8(%rip), %rsi     # 0xdd717
leaq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x33402
leaq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x6aed2
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
jmp 0x7879f
jmp 0x787b0
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x295a0
jmp 0x787b3
jmp 0x787b0
jmp 0x787b0
jmp 0x787b0
jmp 0x787b0
jmp 0x787b0
movq %rax, %rbx
movq %rsp, %rdi
callq 0x5b268
movq %rbx, %rdi
callq 0x292f0
nop