// File: /workspace/github_cpp_makefile_star2_O1/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 0x7e83f
movq 0xf778c(%rip), %r14     # 0x175f90
leaq 0x81fcd(%rip), %rsi     # 0x1007d8
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
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0xc03b8
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 0x7e89c
movq %rdx, 0xd8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xe8(%rsp)
jmp 0x7e8a3
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 0xbf2c2
movl %eax, %ebp
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x7e8e6
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x229c0
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7e901
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x229c0
testl %ebp, %ebp
jne 0x7ea75
leaq 0x1520(%rbx), %rdx
leaq 0x79680(%rip), %rsi     # 0xf7f97
leaq 0x40(%rsp), %rdi
callq 0x319d8
movl 0x13e8(%rbx), %esi
movq %rsp, %rdi
callq 0xc03b8
leaq 0x50(%rsp), %r14
movq -0x10(%r14), %rcx
movq -0x8(%r14), %r8
movq 0x8(%rsp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %esi
cmpq %r14, %rcx
je 0x7e954
movq 0x50(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x7e973
leaq 0x10(%rsp), %rdi
movl $0xf, %esi
cmpq %rdi, -0x10(%rdi)
je 0x7e96e
movq 0x10(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x7e983
movq (%rsp), %rsi
leaq 0x40(%rsp), %rdi
callq 0x222b0
jmp 0x7e98f
movq %rsp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22a80
leaq 0x30(%rsp), %rsi
movq %rsi, 0x20(%rsp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
je 0x7e9b4
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x7e9ba
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x28(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7e9f2
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x7ea09
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x229c0
leaq 0x128(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
leaq 0x118(%rsp), %rdi
callq 0x32180
leaq 0x118(%rsp), %rdi
callq 0xc038e
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x7ea59
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x7ea75
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x229c0
cmpb $0x0, 0x13f0(%rbx)
jne 0x7eb03
movl 0x1620(%rbx), %edx
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x881a0
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x1a08(%rbx), %rax
movups 0x1a10(%rbx), %xmm0
movq (%r14), %rcx
movq %rcx, 0x1a08(%rbx)
movq 0x8(%r14), %rcx
movq %rcx, 0x1a10(%rbx)
movq 0x10(%r14), %rcx
movq %rcx, 0x1a18(%rbx)
movq (%rdi), %rcx
movq 0x8(%rdi), %rdx
movq 0x10(%rdi), %rsi
movq %rax, (%rdi)
movups %xmm0, 0x8(%rdi)
movq %rcx, (%r14)
movq %rdx, 0x8(%r14)
movq %rsi, 0x10(%r14)
callq 0xa7bb4
movq %r14, %rdi
callq 0xa7bb4
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
xorl %eax, %eax
movq %rax, 0x28(%rsp)
movb $0x0, 0x30(%rsp)
leaq 0x80(%rsp), %r14
movq %rax, 0x10(%r14)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movq 0xf745d(%rip), %r15     # 0x175f90
leaq 0x60(%rsp), %r12
movl $0x14, %edx
movq %r15, %rdi
leaq 0x81caf(%rip), %rsi     # 0x1007f6
callq 0x22ab0
movl 0x13e8(%rbx), %esi
movq %r15, %rdi
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 0x8d672
movq %rbx, %rdi
callq 0x8795e
movq %rbx, %rdi
callq 0x9c93c
movl 0x13e8(%rbx), %esi
movq %rsp, %r13
movq %r13, %rdi
callq 0xc03b8
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 0x7ebef
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x7ebf5
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 (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x7ec28
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x229c0
cmpb $0x1, 0x1646(%rbx)
jne 0x7ec3b
movq 0x40(%rsp), %rsi
callq 0x91f58
movq %rbx, %rdi
callq 0x8795e
movl %eax, %ebp
cmpl $0x3, 0x1614(%rbx)
jb 0x7ec98
movl $0x15, %edx
movq %r15, %rdi
leaq 0x81bae(%rip), %rsi     # 0x10080b
callq 0x22ab0
movzbl %bpl, %esi
movq %r15, %rdi
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 0xc03b8
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 0x7ecf2
movq %rdx, 0x60(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x70(%rsp)
jmp 0x7ecf8
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 0x81a5c(%rip), %rsi     # 0x100776
callq 0x233b0
leaq 0x10(%rsp), %rsi
movq %rsi, (%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x7ed45
movq %rdx, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
jmp 0x7ed4b
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x8(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x7ed7f
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x7eda4
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x229c0
leaq 0xb0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
leaq 0xa0(%rsp), %rdi
callq 0x32180
movq %rbx, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x92400
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x7ee02
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x229c0
testb %bpl, %bpl
jne 0x7eed7
cmpl $0x0, 0x1620(%rbx)
jle 0x7ee56
xorl %r13d, %r13d
incl %r13d
movl %r13d, 0x60(%rsp)
movq 0x88(%rsp), %rsi
cmpq 0x90(%rsp), %rsi
je 0x7ee42
movl %r13d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x88(%rsp)
jmp 0x7ee4d
movq %r14, %rdi
movq %r12, %rdx
callq 0xaf698
cmpl 0x1620(%rbx), %r13d
jl 0x7ee17
leaq 0xc0(%rsp), %rdi
movq %r14, %rsi
callq 0xa8934
movq %rbx, %rdi
leaq 0xc0(%rsp), %rsi
xorl %edx, %edx
callq 0x8905e
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x7ee95
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x229c0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x7eeb0
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7eecc
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x229c0
testb %bpl, %bpl
je 0x7eb38
jmp 0x7ef0e
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x7eef2
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7ef0e
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x7ef2b
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x229c0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x7ef47
movq 0x30(%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 0x7f122
movq 0x128(%rsp), %rsi
incq %rsi
jmp 0x7f11d
movq %rax, %rbx
jmp 0x7f122
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7efa8
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x7efa8
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7f13e
movq 0x50(%rsp), %rsi
jmp 0x7f136
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x7efe9
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x7efe9
movq %rax, %rbx
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0x7f12c
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x7f0c9
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
jmp 0x7f03f
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x7f0c9
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x7f0c9
jmp 0x7f102
movq %rax, %rbx
jmp 0x7f091
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x7f105
movq 0x10(%rsp), %rsi
jmp 0x7f0f8
movq %rax, %rbx
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x7f091
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x7f0bf
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x7f0bf
jmp 0x7f0bc
jmp 0x7f0c6
movq %rax, %rbx
leaq 0x50(%rsp), %r14
jmp 0x7f0e9
movq %rax, %rbx
leaq 0x50(%rsp), %r14
leaq 0x10(%rsp), %rax
movq (%rsp), %rdi
cmpq %rax, %rdi
je 0x7f0e9
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x229c0
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x7f105
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x229c0
jmp 0x7f105
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x7f122
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x229c0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x7f13e
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x229c0
movq %rbx, %rdi
callq 0x23240