// File: /workspace/github_cpp_makefile_star2_O3/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
movl 0x13e8(%rdi), %esi
leaq 0xe8(%rsp), %r14
movq %r14, %rdi
callq 0xbfa49
movq 0x1520(%rbx), %rcx
movq 0x1528(%rbx), %r8
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22a80
leaq 0x48(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x8cde9
movq %rdx, 0x38(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x48(%rsp)
jmp 0x8cdf2
movupd (%rcx), %xmm0
movupd %xmm0, (%r14)
movq 0x8(%rax), %rdx
leaq 0x38(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x72a42(%rip), %rsi     # 0xff857
callq 0x233b0
leaq 0x28(%rsp), %rsi
movq %rsi, -0x10(%rsi)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x8ce41
movq %rdx, 0x18(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x28(%rsp)
jmp 0x8ce49
movupd (%rcx), %xmm0
movupd %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x20(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x8ce78
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x229c0
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8ce99
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x229c0
leaq 0x98(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0x3213c
leaq 0x88(%rsp), %rdi
callq 0xbe9a8
movl %eax, %ebp
movq 0x88(%rsp), %rdi
cmpq %r14, %rdi
je 0x8ceeb
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x229c0
testl %ebp, %ebp
jne 0x8cf36
leaq 0x6b051(%rip), %rsi     # 0xf7f47
leaq 0xa8(%rsp), %rdi
leaq 0x18(%rsp), %rdx
callq 0x31944
leaq 0xa8(%rsp), %rdi
callq 0xbfa1f
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8cf36
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0x15c8(%rbx), %rbp
subq 0x15c0(%rbx), %rbp
cmpl $0x0, 0x1614(%rbx)
je 0x8d050
movq 0xe9050(%rip), %rdi     # 0x175fa8
leaq 0x7254d(%rip), %rsi     # 0xff4ac
movl $0x13, %edx
callq 0x22ab0
movl 0x13e8(%rbx), %esi
movq 0xe9032(%rip), %rdi     # 0x175fa8
callq 0x22650
movq %rax, %r14
leaq 0x7253b(%rip), %rsi     # 0xff4c0
movl $0x6, %edx
movq %rax, %rdi
callq 0x22ab0
movl 0x1620(%rbx), %esi
movq %r14, %rdi
callq 0x231e0
movq %rax, %r14
leaq 0x7251d(%rip), %rsi     # 0xff4c7
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
cmpl $0x3, 0x1614(%rbx)
jb 0x8d050
movq 0xe8fba(%rip), %rdi     # 0x175fa8
leaq 0x724e0(%rip), %rsi     # 0xff4d5
movl $0x27, %edx
callq 0x22ab0
movq 0x1c58(%rbx), %rsi
movq 0xe8f9b(%rip), %rdi     # 0x175fa8
callq 0x22650
movq %rax, %r14
leaq 0x724e1(%rip), %rsi     # 0xff4fd
movl $0x1d, %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 0xafaaa
leaq 0x1c30(%rbx), %rdi
callq 0xafaaa
leaq 0x18c0(%rbx), %r14
movq %r14, %rdi
callq 0xafad8
leaq 0x1890(%rbx), %r15
movq %r15, %rdi
callq 0xafad8
movl 0x1620(%rbx), %edi
movq 0x15c8(%rbx), %rdx
subq 0x15c0(%rbx), %rdx
shrq $0x3, %rdx
leal -0x1(%rdx), %esi
callq 0xbe989
movl %eax, %r12d
movq %r15, 0x58(%rsp)
movq %r14, 0x60(%rsp)
cmpl $0x2, 0x1614(%rbx)
jbe 0x8d14f
movq $0x0, (%rsp)
movq 0xe8ed9(%rip), %rdi     # 0x175fa8
leaq 0x72445(%rip), %rsi     # 0xff51b
movl $0xc, %edx
xorl %r13d, %r13d
callq 0x22ab0
movl %r12d, %esi
movq $0x0, (%rsp)
movq 0xe8eb3(%rip), %rdi     # 0x175fa8
xorl %r13d, %r13d
movq %rsi, 0x8(%rsp)
callq 0x22650
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movq $0x0, (%rsp)
xorl %r13d, %r13d
movl $0xa, %esi
callq 0x22940
movq $0x0, (%rsp)
movsbl %al, %esi
xorl %r13d, %r13d
movq %r14, %rdi
callq 0x22050
movq $0x0, (%rsp)
xorl %r13d, %r13d
movq %rax, %rdi
callq 0x22660
jmp 0x8d157
movl %r12d, %eax
movq %rax, 0x8(%rsp)
callq 0x220b0
movq %rax, 0x1db8(%rbx)
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
cmpq %rax, 0x18b8(%rbx)
jae 0x8d26f
shrq $0x3, %rbp
xorl %r13d, %r13d
movq $0x0, (%rsp)
movq (%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r13, %r15
movl 0x1628(%rbx), %eax
cmpq %rax, 0x1848(%rbx)
jae 0x8d1d2
cmpl %r12d, %r14d
jae 0x8d1d2
incl %r14d
movl %r14d, %edi
movl %ebp, %esi
callq 0xbe99e
movl %eax, %r13d
movl %r14d, %edi
movl %ebp, %esi
callq 0xbe994
movq %rbx, %rdi
movl %r13d, %esi
movl %eax, %edx
xorl %ecx, %ecx
callq 0x8bf54
jmp 0x8d193
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x8d5d8
movq 0x38(%rsp), %r13
movq 0x48(%rsp), %rax
movq %rax, (%rsp)
testq %r15, %r15
je 0x8d202
movq 0x10(%rsp), %rsi
subq %r15, %rsi
movq %r15, %rdi
callq 0x229c0
cmpl $0x5, 0x1614(%rbx)
jb 0x8d25b
movl 0x18e8(%rbx), %esi
movq 0xe8d90(%rip), %rdi     # 0x175fa8
callq 0x22650
movq %rax, %r15
movl $0x13, %edx
movq %rax, %rdi
leaq 0x722f9(%rip), %rsi     # 0xff528
callq 0x22ab0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r15, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movq 0x8(%rsp), %rax
cmpq %rax, 0x18b8(%rbx)
jb 0x8d187
jmp 0x8d27a
movq $0x0, (%rsp)
xorl %r13d, %r13d
cmpl $0x2, 0x1614(%rbx)
jb 0x8d3f5
movq 0xe8d1a(%rip), %rdi     # 0x175fa8
leaq 0x722a7(%rip), %rsi     # 0xff53c
movl $0x1e, %edx
callq 0x22ab0
callq 0x220b0
subq 0x1db8(%rbx), %rax
cvtsi2sd %rax, %xmm0
divsd 0x70d68(%rip), %xmm0    # 0xfe020
movq 0xe8ce9(%rip), %rdi     # 0x175fa8
callq 0x23060
movq %rax, %r14
leaq 0x7228d(%rip), %rsi     # 0xff55b
movl $0x9, %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
movq 0xe8c9f(%rip), %rdi     # 0x175fa8
leaq 0x72255(%rip), %rsi     # 0xff565
movl $0xe, %edx
callq 0x22ab0
movq 0x1660(%rbx), %rsi
movq 0x1668(%rbx), %rdx
movq 0xe8c79(%rip), %rdi     # 0x175fa8
callq 0x22ab0
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
leaq 0x78(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x1660(%rbx), %rsi
movq 0x1668(%rbx), %rdx
addq %rsi, %rdx
leaq 0x68(%rsp), %rdi
callq 0x3213c
leaq 0x68(%rsp), %rdi
callq 0xbdd32
movl %eax, %ebp
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x8d3a5
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0xe8bfc(%rip), %rdi     # 0x175fa8
leaq 0x721c1(%rip), %rsi     # 0xff574
movl $0x18, %edx
callq 0x22ab0
movq 0xe8be4(%rip), %rdi     # 0x175fa8
movl %ebp, %esi
callq 0x231e0
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 0x60(%rsp), %rdi
callq 0xafad8
movq 0x58(%rsp), %rdi
callq 0xafad8
cmpl $0x3, 0x1614(%rbx)
jb 0x8d42a
movq 0xe8b8f(%rip), %rdi     # 0x175fa8
leaq 0x7216d(%rip), %rsi     # 0xff58d
movl $0x29, %edx
callq 0x22ab0
movl 0x1620(%rbx), %eax
addl $-0x3, %eax
cmpq %rax, 0x19f8(%rbx)
ja 0x8d484
incl 0x13e8(%rbx)
testq %r13, %r13
je 0x8d456
movq (%rsp), %rsi
subq %r13, %rsi
movq %r13, %rdi
callq 0x229c0
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x8d472
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x229c0
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x86314
leaq 0xd8(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x72118(%rip), %rsi     # 0xff5b7
leaq 0x72172(%rip), %rdx     # 0xff618
leaq 0xc8(%rsp), %rdi
callq 0x321d2
leaq 0xc8(%rsp), %rsi
movq %rbx, %rdi
callq 0x784c2
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
cmpq %r14, %rdi
je 0x8d58f
movq 0xd8(%rsp), %rsi
jmp 0x8d51a
jmp 0x8d58c
movq %rax, %rbx
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8d5b4
movq 0xb8(%rsp), %rsi
jmp 0x8d53e
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x8d58f
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x8d58f
jmp 0x8d583
movq %rax, %rbx
movq 0x88(%rsp), %rdi
cmpq %r14, %rdi
je 0x8d5b4
movq 0x98(%rsp), %rsi
incq %rsi
jmp 0x8d5af
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x8d562
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x8d562
movq %rax, %rbx
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8d5d0
movq 0xf8(%rsp), %rsi
jmp 0x8d5c8
jmp 0x8d583
jmp 0x8d583
jmp 0x8d58c
movq %rax, %rbx
jmp 0x8d5b4
jmp 0x8d58c
jmp 0x8d594
movq %rax, %rbx
movq %r13, %r15
jmp 0x8d5a0
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq %rax, (%rsp)
testq %r15, %r15
je 0x8d5b4
movq (%rsp), %rsi
subq %r15, %rsi
movq %r15, %rdi
callq 0x229c0
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x8d5d0
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x229c0
movq %rbx, %rdi
callq 0x23240