// File: /workspace/github_cpp_makefile_star2_O3/richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::runSPSO()
void Swarm::runSPSO() {
	if (options.verbosity >= 3) {
		cout << "Running a synchronous PSO fit" << endl;
	}

	if (!checkIfFileExists(options.jobOutputDir + toString(currentGeneration))) {//Raquel: changed, subdir name was hardcoded to 1
		string createDirCmd = "mkdir " + options.jobOutputDir + toString(currentGeneration);//Raquel: changed, subdir name was hardcoded to 1
		runCommand(createDirCmd);
	}

	if (!resumingSavedSwarm) {
		// Generate all particles that will be present in the swarm
		populationTopology_ = generateTopology(options.swarmSize);
	}

	//saveSwarmState();
	string path; //Raquel added
	vector<unsigned int> finishedParticles;
	vector<unsigned int> particlestoProcess;

	bool stopCriteria = false;

	//int numFinishedParticles = 0;

	//unsigned int p = 1;
	//int mid; //Raquel added
	//int sp = 0; //Raquel added
	//int nModels = options.models.size(); //Raquel added


	while (!stopCriteria) {

		if (options.verbosity >= 0) {
			cout << "Generation (flock): " << currentGeneration << endl;
		}


/*
		while (finishedParticles.size() < options.swarmSize && p <= options.swarmSize) {
			if (runningParticles_.size() < options.parallelCount) {

				//Raquel Changed this to run the correct number of subparticles
				sp++; //Raquel added
				p = fcalcParID(sp, nModels);//Raquel added
				mid = fcalcMID(sp, nModels);//Raquel added
				launchSubParticle(p, mid, false);//Raquel added
				//launchParticle(p++);
			}
			cout << "RAQUEL: parallel count " << options.parallelCount << endl;
			cout << "RAQUEL: runningParticles_.size() " << runningParticles_.size() << endl;
			cout << "RAQUEL: options.swarmSize " << options.swarmSize << endl;
			cout << "RAQUEL : p " << p << endl;
			cout << "RAQUEL: finished " << finishedParticles.size() << endl;

			usleep(250000);

			vector<unsigned int> currFinishedParticles;
			cout << "Raquel before checkmastermessages" << endl;
			currFinishedParticles = checkMasterMessages();
			cout << "Raquel after checkmastermessages" << endl;


			if (currFinishedParticles.size()) {
				finishedParticles.insert(finishedParticles.end(), currFinishedParticles.begin(), currFinishedParticles.end());
			}




		}

	*/


		//sp=0;

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

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

			if (runningSubParticles_.size()< options.parallelCount) {//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);

			}
			// Check for any messages from particles
			usleep(10000);
			checkMasterMessages();

			numFinishedParticles = finishedParticles_.size();
		}

*/
			//numFinishedParticles = options.swarmSize;

				//cout << "RAQUEL: starting runGeneration" << endl;
				runGeneration();
				//usleep(250000);

				//cout << "RAQUEL: finished runGeneration" << endl;
				//cout << "RAQUEL: starting checkStopCriteria" << endl;
				stopCriteria = checkStopCriteria();
				//cout << "RAQUEL: STOP CRITERIA value" << stopCriteria << endl;
				//cout << "RAQUEL: starting saveSwarmState" << endl;

				saveSwarmState();
				//cout << "RAQUEL: finished saveSwarmState" << endl;

				string currentDirectory = options.jobOutputDir + toString(currentGeneration);
				if (options.deleteOldFiles) {
					//cout << "RAQUEL: starting cleanupFiles" << endl;

					cleanupFiles(currentDirectory.c_str());
					//cout << "RAQUEL: finished cleanupFiles" << endl;

				}



				// Check for stop criteria
				stopCriteria = checkStopCriteria();
				if (options.verbosity >= 3) {
					cout << "Stop Criteria value: " << stopCriteria << endl;
				}
				string outputPath = options.jobOutputDir + toString(currentGeneration - 1 ) + "_summary.txt";

				outputRunSummary(outputPath);


		if (!stopCriteria) {


			//for(int i=1; i <= options.swarmSize; i++){//
			//	for(int j=0; j<options.models.size(); j++){
			//		subparticleCurrParamSets_[i][j] = particleCurrParamSets_[i];
			//	}
			//}



			for(int i=1; i <= options.swarmSize; i++){//
							particlestoProcess.push_back(i);
			}

			// Process particles
			//cout << "Raquel before processParticlesPSO " << particlestoProcess.size() << " finished particles" << endl;
			processParticlesPSO(particlestoProcess, true);


			//cout << "Raquel After processParticlesPSO in the main loop" << endl;

			//cout << "RAQUEL FALSE STOP CRITERIA IN RUNSPSO" << endl;

		}




	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdi, %rbx
cmpl $0x3, 0x1614(%rdi)
jb 0x7e173
movq 0xf7e70(%rip), %r14     # 0x175fa8
leaq 0x82680(%rip), %rsi     # 0x1007bf
movl $0x1d, %edx
movq %r14, %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
movl 0x13e8(%rbx), %esi
movq %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 0xe8(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x7e1ce
movq %rdx, 0xd8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xe8(%rsp)
jmp 0x7e1d5
movups (%rcx), %xmm0
movups %xmm0, (%r14)
movq 0x8(%rax), %rdx
leaq 0xd8(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
callq 0xbe9a8
movl %eax, %ebp
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x7e218
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x229c0
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7e233
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x229c0
testl %ebp, %ebp
jne 0x7e3a7
leaq 0x1520(%rbx), %rdx
leaq 0x79cfe(%rip), %rsi     # 0xf7f47
leaq 0x40(%rsp), %rdi
callq 0x31944
movl 0x13e8(%rbx), %esi
leaq 0x20(%rsp), %rdi
callq 0xbfa49
leaq 0x50(%rsp), %r14
movq -0x10(%r14), %rcx
movq -0x8(%r14), %r8
movq 0x28(%rsp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %edi
cmpq %r14, %rcx
je 0x7e288
movq 0x50(%rsp), %rdi
movq 0x20(%rsp), %rsi
cmpq %rdi, %rax
jbe 0x7e2ab
leaq 0x30(%rsp), %r9
movl $0xf, %edi
cmpq %r9, %rsi
je 0x7e2a6
movq 0x30(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x7e2b7
leaq 0x40(%rsp), %rdi
callq 0x222b0
jmp 0x7e2c5
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22a80
leaq 0x10(%rsp), %rsi
movq %rsi, (%rsp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
je 0x7e2e8
movq %rdx, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
jmp 0x7e2ee
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x8(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7e326
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x7e33d
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x229c0
leaq 0x128(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
leaq 0x118(%rsp), %rdi
callq 0x3213c
leaq 0x118(%rsp), %rdi
callq 0xbfa1f
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x7e38c
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x229c0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x7e3a7
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x229c0
cmpb $0x0, 0x13f0(%rbx)
jne 0x7e410
movl 0x1620(%rbx), %edx
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x87756
movq 0x1a18(%rbx), %rax
movaps (%r14), %xmm0
movq 0x10(%r14), %rcx
movq %rcx, 0x1a18(%rbx)
movups 0x1a08(%rbx), %xmm1
movups %xmm0, 0x1a08(%rbx)
movq %rsp, %rdi
movaps %xmm1, (%rdi)
movq %rax, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movq $0x0, 0x10(%r14)
callq 0xa7156
movq %r14, %rdi
callq 0xa7156
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
leaq 0x80(%rsp), %r14
movq %rax, 0x10(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
leaq 0x60(%rsp), %r12
leaq 0xa0(%rsp), %r15
movl $0x14, %edx
movq 0xf7b57(%rip), %rdi     # 0x175fa8
leaq 0x82385(%rip), %rsi     # 0x1007dd
callq 0x22ab0
movl 0x13e8(%rbx), %esi
movq 0xf7b3e(%rip), %rdi     # 0x175fa8
callq 0x22650
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %rbp, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movq %rbx, %rdi
callq 0x8cd7e
movq %rbx, %rdi
callq 0x86f12
movq %rbx, %rdi
callq 0x9be92
movl 0x13e8(%rbx), %esi
leaq 0x20(%rsp), %r13
movq %r13, %rdi
callq 0xbfa49
movq 0x1520(%rbx), %rcx
movq 0x1528(%rbx), %r8
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22a80
leaq 0x50(%rsp), %rsi
movq %rsi, 0x40(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x7e506
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x7e50c
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x48(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x7e540
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x229c0
cmpb $0x1, 0x1646(%rbx)
jne 0x7e553
movq 0x40(%rsp), %rsi
callq 0x9163a
movq %rbx, %rdi
callq 0x86f12
movl %eax, %ebp
cmpl $0x3, 0x1614(%rbx)
jb 0x7e5b8
movl $0x15, %edx
movq 0xf7a36(%rip), %rdi     # 0x175fa8
leaq 0x82279(%rip), %rsi     # 0x1007f2
callq 0x22ab0
movzbl %bpl, %esi
movq 0xf7a1f(%rip), %rdi     # 0x175fa8
callq 0x230e0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r13, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movl 0x13e8(%rbx), %esi
decl %esi
leaq 0xf8(%rsp), %r13
movq %r13, %rdi
callq 0xbfa49
movq 0x1520(%rbx), %rcx
movq 0x1528(%rbx), %r8
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22a80
leaq 0x70(%rsp), %rsi
movq %rsi, 0x60(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x7e612
movq %rdx, 0x60(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x70(%rsp)
jmp 0x7e618
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x68(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r12, %rdi
leaq 0x82123(%rip), %rsi     # 0x10075d
callq 0x233b0
leaq 0x30(%rsp), %rsi
movq %rsi, 0x20(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x7e667
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x7e66d
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x28(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x7e6a1
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x7e6c6
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x229c0
leaq 0xb0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x3213c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x91ae2
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x7e71b
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x229c0
testb %bpl, %bpl
jne 0x7e7ea
cmpl $0x0, 0x1620(%rbx)
jle 0x7e76a
xorl %ebp, %ebp
incl %ebp
movl %ebp, 0x60(%rsp)
movq 0x88(%rsp), %rsi
cmpq 0x90(%rsp), %rsi
je 0x7e757
movl %ebp, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x88(%rsp)
jmp 0x7e762
movq %r14, %rdi
movq %r12, %rdx
callq 0xaeef8
cmpl 0x1620(%rbx), %ebp
jl 0x7e72f
leaq 0xc0(%rsp), %rdi
movq %r14, %rsi
callq 0xa8002
movq %rbx, %rdi
leaq 0xc0(%rsp), %rsi
xorl %edx, %edx
callq 0x886d0
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x7e7a9
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x229c0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x7e7c5
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7e445
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x7e445
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x7e806
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
leaq 0x10(%rsp), %rbx
je 0x7e827
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x7e844
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x229c0
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x7e85a
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x229c0
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x7ea24
movq 0x128(%rsp), %rsi
incq %rsi
jmp 0x7ea1f
movq %rax, %rbx
jmp 0x7ea24
movq %rax, %rbx
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7e8bb
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x7e8bb
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7ea3f
movq 0x50(%rsp), %rsi
jmp 0x7ea37
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x7e8fc
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x7e8fc
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0x7ea2d
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x7e9ca
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
jmp 0x7e952
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x7e9ca
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x7e9ca
jmp 0x7ea04
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x7ea07
movq 0x30(%rsp), %rsi
jmp 0x7e9fa
jmp 0x7e9c2
movq %rax, %rbx
jmp 0x7e9a1
movq %rax, %rbx
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x7e9a1
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x7e9e6
movq 0x108(%rsp), %rsi
jmp 0x7e9de
jmp 0x7e9c7
movq %rax, %rbx
jmp 0x7e9e6
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x7e9e6
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7ea07
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x7ea07
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x7ea24
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x229c0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x7ea3f
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x229c0
movq %rbx, %rdi
callq 0x23240
nop