// File: /workspace/github_cpp_makefile_star2_O2/richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::runGeneration()
void Swarm::runGeneration () {   //razi: modified to include subparticles
	// TODO: Implement walltime

	//Raquel added, master will handle the directory creation
	string path = options.jobOutputDir + toString(currentGeneration) + "/";

	if (!checkIfFileExists(path)) {
		runCommand("mkdir " + path);
	}


	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
	if(options.verbosity>=3){

		cout << "@@@@@@@@@@@@@@@@@ size before cleaning " << currentsubswarmBestFits_.size() << " @@@@@@@@@@@@@@@@@@@@@@@@@@@@" << endl;

	}

	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.verbosity>=3){
		cout << "Maxsubpar = "  << maxSubPar << endl;

	}

	generationTime_.reset(); //Raquel implementing walltime

	while (finishedSubParticles_.size() < maxSubPar ) { //&& runningSubParticles_.size() < maxSubPar) { //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();
		//cout << "RAQUEL Done checkMasterMessages finishedSubParticles_.size() " << finishedSubParticles_.size() << 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(options.verbosity >= 5) {
			cout << numFinishedParticles << " finished particles" << endl;

		}
		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 >= 2) {

		cout << "Total time to run generation: " << generationTime_.elapsed() << " seconds." << endl;
		cout << "Max fit time: " << options.maxFitTime << endl;
		int convertedMaxFitTime = convertTimeToSeconds(options.maxFitTime);


		cout << "Max fit time converted: " << convertedMaxFitTime << endl;
	}
	finishedParticles_.clear();
	finishedSubParticles_.clear(); //Raquel added to solve problem of less and less result files as generations go



	/*
	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 ....\n";}

	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;



}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, %rbx
leaq 0x1520(%rdi), %r14
movl 0x13e8(%rdi), %esi
leaq 0x48(%rsp), %r15
movq %r15, %rdi
callq 0x9f56b
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x5a115
leaq 0x66da4(%rip), %rdx     # 0xdd9b7
leaq 0x68(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x3336c
leaq 0x10(%rsp), %rdi
callq 0x295a0
leaq 0x48(%rsp), %rdi
callq 0x295a0
leaq 0xc8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x28dd0
leaq 0xc8(%rsp), %rdi
callq 0x9ec28
movl %eax, %ebp
leaq 0xc8(%rsp), %rdi
callq 0x295a0
testl %ebp, %ebp
jne 0x76c9b
leaq 0x5f228(%rip), %rsi     # 0xd5e97
leaq 0xa8(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x3338b
leaq 0xa8(%rsp), %rdi
callq 0x9f543
leaq 0xa8(%rsp), %rdi
callq 0x295a0
movq 0x15c8(%rbx), %r12
subq 0x15c0(%rbx), %r12
cmpl $0x0, 0x1614(%rbx)
je 0x76d5f
movq 0xef2db(%rip), %rdi     # 0x165f98
leaq 0x66948(%rip), %rsi     # 0xdd60c
callq 0x289f0
movl 0x13e8(%rbx), %esi
movq %rax, %rdi
callq 0x286e0
leaq 0x66942(%rip), %rsi     # 0xdd620
movq %rax, %rdi
callq 0x289f0
movl 0x1620(%rbx), %esi
movq %rax, %rdi
callq 0x29290
leaq 0x6692c(%rip), %rsi     # 0xdd627
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
movl 0x1614(%rbx), %eax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
cmpl $0x3, %eax
jb 0x76d6d
movq 0xef26d(%rip), %rdi     # 0x165f98
leaq 0x66903(%rip), %rsi     # 0xdd635
callq 0x289f0
movq 0x1c58(%rbx), %rsi
movq %rax, %rdi
callq 0x286e0
leaq 0x66910(%rip), %rsi     # 0xdd65d
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
jmp 0x76d6d
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x1b70(%rbx), %rdi
callq 0x8f982
leaq 0x1c30(%rbx), %rdi
callq 0x8f982
leaq 0x18c0(%rbx), %rdi
movq %rdi, 0x40(%rsp)
callq 0x8f9b0
leaq 0x1890(%rbx), %rdi
movq %rdi, 0x38(%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 %eax, %ebp
cmpl $0x2, 0x1614(%rbx)
movl %eax, %eax
movq %rax, 0x8(%rsp)
jbe 0x76e01
movq 0xef1b8(%rip), %rdi     # 0x165f98
leaq 0x66894(%rip), %rsi     # 0xdd67b
callq 0x289f0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
shrq $0x3, %r12
leaq 0x1db8(%rbx), %rax
movq %rax, 0x30(%rsp)
callq 0x280b0
movq %rax, 0x1db8(%rbx)
xorl %r15d, %r15d
leaq 0x48(%rsp), %r14
movq 0x8(%rsp), %rax
cmpq %rax, 0x18b8(%rbx)
jae 0x76ecf
movl 0x1628(%rbx), %eax
cmpq %rax, 0x1848(%rbx)
jae 0x76e78
cmpl %ebp, %r15d
jae 0x76e78
incl %r15d
movl %r15d, %edi
movl %r12d, %esi
callq 0x9ec1e
movl %eax, %r13d
movl %r15d, %edi
movl %r12d, %esi
callq 0x9ec14
movq %rbx, %rdi
movl %r13d, %esi
movl %eax, %edx
xorl %ecx, %ecx
callq 0x76502
jmp 0x76e37
movq %r14, %rdi
movq %rbx, %rsi
callq 0x770c4
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x8f082
movq %r14, %rdi
callq 0x5b268
cmpl $0x5, 0x1614(%rbx)
jb 0x76e25
movl 0x18e8(%rbx), %esi
movq 0xef0ea(%rip), %rdi     # 0x165f98
callq 0x286e0
movq %rax, %rdi
leaq 0x667cb(%rip), %rsi     # 0xdd688
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
jmp 0x76e25
cmpl $0x2, 0x1614(%rbx)
jb 0x76f99
movq 0xef0b5(%rip), %rdi     # 0x165f98
leaq 0x667b2(%rip), %rsi     # 0xdd69c
callq 0x289f0
movq %rax, %r14
movq 0x30(%rsp), %rdi
callq 0x864e6
movq %r14, %rdi
callq 0x290f0
leaq 0x667b0(%rip), %rsi     # 0xdd6bb
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
movq 0xef076(%rip), %rdi     # 0x165f98
leaq 0x6679c(%rip), %rsi     # 0xdd6c5
callq 0x289f0
leaq 0x1660(%rbx), %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x28910
movq %rax, %rdi
callq 0x284a0
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
callq 0x28dd0
leaq 0x88(%rsp), %rdi
callq 0x9e511
movl %eax, %ebp
leaq 0x88(%rsp), %rdi
callq 0x295a0
movq 0xef01d(%rip), %rdi     # 0x165f98
leaq 0x66752(%rip), %rsi     # 0xdd6d4
callq 0x289f0
movq %rax, %rdi
movl %ebp, %esi
callq 0x29290
movq %rax, %rdi
callq 0x284a0
movq 0x40(%rsp), %rdi
callq 0x8f9b0
movq 0x38(%rsp), %rdi
callq 0x8f9b0
cmpl $0x3, 0x1614(%rbx)
jb 0x76fc9
movq 0xeefdb(%rip), %rdi     # 0x165f98
leaq 0x66729(%rip), %rsi     # 0xdd6ed
callq 0x289f0
movl 0x1620(%rbx), %eax
addl $-0x3, %eax
cmpq %rax, 0x19f8(%rbx)
ja 0x77007
incl 0x13e8(%rbx)
leaq 0x10(%rsp), %rdi
callq 0x5b268
leaq 0x68(%rsp), %rdi
callq 0x295a0
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x728ee
leaq 0x66701(%rip), %rsi     # 0xdd717
leaq 0xe8(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x33402
leaq 0xe8(%rsp), %rsi
movq %rbx, %rdi
callq 0x6aed2
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
jmp 0x7705f
jmp 0x770a4
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0x77073
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x295a0
jmp 0x770a7
jmp 0x770a4
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x295a0
jmp 0x770b1
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x295a0
jmp 0x7708c
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
jmp 0x770b6
jmp 0x770a4
jmp 0x77099
jmp 0x770a4
movq %rax, %rbx
jmp 0x770b1
jmp 0x770a4
jmp 0x770a4
jmp 0x770a4
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5b268
leaq 0x68(%rsp), %rdi
callq 0x295a0
movq %rbx, %rdi
callq 0x292f0
nop