// File: richardposner[P]BioNetFit2/src/code/Pheromones.cpp
// Function: Pheromones::init(Swarm*)
void Pheromones::init(Swarm *s) {
	swarm_ = s;
	unsigned int nSubPar= swarm_->options.swarmSize * swarm_->options.models.size()+1; //razi:number of subparticles, +1 since starts from 1

	// Using MPI
	if (swarm_->options.useCluster && (swarm_->options.clusterSoftware == "mpi" || swarm_->options.clusterSoftware == "slurm") ) {
		// Set up our MPI environment and communicator

		//std::cout<<"Pheromones Initialization: MPI\n";
		//env_ = new mpi::environment();
	    // Initialize the MPI environment
	    MPI_Init(NULL, NULL);
	    // Get the number of processes
	    int world_size;
	    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
	    if (swarm_->options.verbosity>=3) {

	    	cout << "Defined mpi environment" << endl;
	    }
//		world_ = new mpi::communicator();
		 // Get the rank of the process
		    int world_rank;
		    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

		    //cout << "My rank is " << world_rank << " and I have just started." << endl;
		    if (swarm_->options.verbosity>=3) {

		    	cout << "Defined world communicator" << endl;
		    }
		//  boost::mpi::environment env;
		 // boost::mpi::communicator world_;
		//scout << "Hello World! from process " << world_.rank() << endl;
	}
	else if (swarm_->options.clusterSoftware == "BNF2mpi"){
	    if (swarm_->options.verbosity>=3) {

			cout << "Detected BNF2mpi in Pheromones init()" << endl;
	    }
			//env_ = new mpi::environment();
		    MPI_Init(NULL, NULL);
			    // Get the number of processes
			    int world_size;
			    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
			    if (swarm_->options.verbosity>=3) {

			    	cout << "Defined mpi environment" << endl;
			    }
		//		world_ = new mpi::communicator();
				 // Get the rank of the process
				    int world_rank;
				    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

				    if (swarm_->options.verbosity>=3) {

				    	cout << "My rank is " << world_rank << "and I have just started." << endl;

				    	cout << "Defined world communicator" << endl;
				    }
				//  boost::mpi::environment env;
	}
	// Using IPC
	else {

		//std::cout << "init ipc" << std::endl;
		if (s->isMaster) {
			std::cout<<"Pheromones Initialization: IPC: Master\n";
			for (unsigned int i = 0; i <= nSubPar ; ++i) {
				//std::cout << "creating: " << std::toString(static_cast<long long int>(i)) << std::endl;
				message_queue *smq;

				try {
					smq = new message_queue(create_only, toString(i).c_str(), MAX_SWARM_NUM_MESSAGE, MAX_SWARM_MESSAGE_SIZE);
				} catch (boost::interprocess::interprocess_exception e) {
					message_queue::remove(toString(i).c_str());
					smq = new message_queue(create_only, toString(i).c_str(), MAX_SWARM_NUM_MESSAGE, MAX_SWARM_MESSAGE_SIZE);
				}
				smq_.push_back(smq);
			}
		}
		else {
			std::cout<<"Pheromones Initialization: IPC: Slave\n";
			for (unsigned int i = 0; i <= nSubPar; ++i) {
				message_queue *smq = new message_queue(open_only, toString(i).c_str());
				smq_.push_back(smq);
				//std::cout << "opening: " << std::toString(static_cast<long long int>(i)) << " with max size of " << smq_[i]->get_max_msg() << std::endl;
			}
		}
	}

	swarm_->commInit = true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, 0x50(%rax)
movq 0x50(%rax), %rcx
movslq 0x1620(%rcx), %rcx
movq %rcx, -0x138(%rbp)
movq 0x50(%rax), %rdi
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
movq -0x138(%rbp), %rcx
movq %rax, %rdx
movq -0x130(%rbp), %rax
imulq %rdx, %rcx
addq $0x1, %rcx
movl %ecx, -0x14(%rbp)
movq 0x50(%rax), %rax
testb $0x1, 0x1631(%rax)
je 0xccff0
movq -0x130(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x270, %rdi            # imm = 0x270
leaq 0x1078e9(%rip), %rsi    # 0x1d4805
callq 0x44930
testb $0x1, %al
jne 0xccf53
movq -0x130(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x270, %rdi            # imm = 0x270
leaq 0x1078af(%rip), %rsi    # 0x1d47f4
callq 0x44930
testb $0x1, %al
jne 0xccf53
jmp 0xccff0
xorl %eax, %eax
movl %eax, %esi
movq %rsi, %rdi
callq 0x2f7f0
movq 0x20802a(%rip), %rdi    # 0x2d4f90
leaq -0x18(%rbp), %rsi
callq 0x30f90
movq -0x130(%rbp), %rax
movq 0x50(%rax), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xccfa5
movq 0x207fc6(%rip), %rdi    # 0x2d4f50
leaq 0x111270(%rip), %rsi    # 0x1de201
callq 0x2ff50
movq %rax, %rdi
movq 0x208028(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
movq 0x207fe4(%rip), %rdi    # 0x2d4f90
leaq -0x1c(%rbp), %rsi
callq 0x311b0
movq -0x130(%rbp), %rax
movq 0x50(%rax), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xccfeb
movq 0x207f80(%rip), %rdi    # 0x2d4f50
leaq 0x111242(%rip), %rsi    # 0x1de219
callq 0x2ff50
movq %rax, %rdi
movq 0x207fe2(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xcd4f4
movq -0x130(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x270, %rdi            # imm = 0x270
leaq 0x1077f1(%rip), %rsi    # 0x1d4801
callq 0x44930
testb $0x1, %al
jne 0xcd01e
jmp 0xcd12d
movq -0x130(%rbp), %rax
movq 0x50(%rax), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xcd054
movq 0x207f17(%rip), %rdi    # 0x2d4f50
leaq 0x1111f4(%rip), %rsi    # 0x1de234
callq 0x2ff50
movq %rax, %rdi
movq 0x207f79(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
xorl %eax, %eax
movl %eax, %esi
movq %rsi, %rdi
callq 0x2f7f0
movq 0x207f29(%rip), %rdi    # 0x2d4f90
leaq -0x20(%rbp), %rsi
callq 0x30f90
movq -0x130(%rbp), %rax
movq 0x50(%rax), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xcd0a6
movq 0x207ec5(%rip), %rdi    # 0x2d4f50
leaq 0x11116f(%rip), %rsi    # 0x1de201
callq 0x2ff50
movq %rax, %rdi
movq 0x207f27(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
movq 0x207ee3(%rip), %rdi    # 0x2d4f90
leaq -0x24(%rbp), %rsi
callq 0x311b0
movq -0x130(%rbp), %rax
movq 0x50(%rax), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xcd128
movq 0x207e7f(%rip), %rdi    # 0x2d4f50
leaq 0x111182(%rip), %rsi    # 0x1de25a
callq 0x2ff50
movq %rax, %rdi
movl -0x24(%rbp), %esi
callq 0x2f800
movq %rax, %rdi
leaq 0x111174(%rip), %rsi    # 0x1de266
callq 0x2ff50
movq %rax, %rdi
movq 0x207ec7(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
movq 0x207e43(%rip), %rdi    # 0x2d4f50
leaq 0x111105(%rip), %rsi    # 0x1de219
callq 0x2ff50
movq %rax, %rdi
movq 0x207ea5(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xcd4f2
movq -0x10(%rbp), %rax
testb $0x1, 0x50(%rax)
je 0xcd3e9
movq 0x207e0e(%rip), %rdi    # 0x2d4f50
leaq 0x111136(%rip), %rsi    # 0x1de27f
callq 0x2ff50
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x14(%rbp), %eax
ja 0xcd3e4
movl $0x20, %edi
callq 0x2f3b0
movq %rax, -0x140(%rbp)
jmp 0xcd174
movb $0x1, -0x65(%rbp)
movl -0x28(%rbp), %esi
leaq -0x60(%rbp), %rdi
callq 0x17e460
jmp 0xcd186
leaq -0x60(%rbp), %rdi
callq 0x2f290
movq %rax, -0x148(%rbp)
leaq -0x64(%rbp), %rdi
callq 0xd05a0
jmp 0xcd1a1
movq -0x148(%rbp), %rsi
movq -0x140(%rbp), %rdi
movl $0x64, %edx
movl $0x3e8, %ecx            # imm = 0x3E8
leaq -0x64(%rbp), %r8
callq 0xd05c0
jmp 0xcd1c4
movq -0x140(%rbp), %rax
movb $0x0, -0x65(%rbp)
movq %rax, -0x30(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x311e8
jmp 0xcd33e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xcd22d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xcd212
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x311e8
testb $0x1, -0x65(%rbp)
jne 0xcd21a
jmp 0xcd22b
movq -0x140(%rbp), %rdi
movl $0x20, %esi
callq 0x2f2a0
jmp 0xcd22d
movl -0x3c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0xcd50f
movq -0x38(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x309c0
movq %rax, %rsi
leaq -0x98(%rbp), %rdi
callq 0xd0640
jmp 0xcd25e
movq -0x150(%rbp), %rdi
callq 0x2f160
movl -0x28(%rbp), %esi
leaq -0xb8(%rbp), %rdi
callq 0x17e460
jmp 0xcd27b
leaq -0xb8(%rbp), %rdi
callq 0x2f290
movq %rax, %rdi
callq 0xd06c0
jmp 0xcd291
leaq -0xb8(%rbp), %rdi
callq 0x311e8
movl $0x20, %edi
callq 0x2f3b0
movq %rax, -0x158(%rbp)
jmp 0xcd2b0
movb $0x1, -0xe5(%rbp)
movl -0x28(%rbp), %esi
leaq -0xe0(%rbp), %rdi
callq 0x17e460
jmp 0xcd2c8
leaq -0xe0(%rbp), %rdi
callq 0x2f290
movq %rax, -0x160(%rbp)
leaq -0xe4(%rbp), %rdi
callq 0xd05a0
jmp 0xcd2e9
movq -0x160(%rbp), %rsi
movq -0x158(%rbp), %rdi
movl $0x64, %edx
movl $0x3e8, %ecx            # imm = 0x3E8
leaq -0xe4(%rbp), %r8
callq 0xd05c0
jmp 0xcd30f
movq -0x158(%rbp), %rax
movb $0x0, -0xe5(%rbp)
movq %rax, -0x30(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x311e8
leaq -0x98(%rbp), %rdi
callq 0xd06e0
callq 0x304d0
movq -0x130(%rbp), %rdi
addq $0x68, %rdi
leaq -0x30(%rbp), %rsi
callq 0xd0720
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xcd155
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xcd3cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x311e8
jmp 0xcd3cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xcd3ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x311e8
testb $0x1, -0xe5(%rbp)
jne 0xcd3b9
jmp 0xcd3ca
movq -0x158(%rbp), %rdi
movl $0x20, %esi
callq 0x2f2a0
jmp 0xcd3cc
leaq -0x98(%rbp), %rdi
callq 0xd06e0
callq 0x304d0
jmp 0xcd3df
jmp 0xcd50f
jmp 0xcd4f0
movq 0x207b60(%rip), %rdi    # 0x2d4f50
leaq 0x110eb0(%rip), %rsi    # 0x1de2a7
callq 0x2ff50
movl $0x0, -0xec(%rbp)
movl -0xec(%rbp), %eax
cmpl -0x14(%rbp), %eax
ja 0xcd4ee
movl $0x20, %edi
callq 0x2f3b0
movq %rax, -0x168(%rbp)
movb $0x1, -0x121(%rbp)
movl -0xec(%rbp), %esi
leaq -0x120(%rbp), %rdi
callq 0x17e460
jmp 0xcd441
leaq -0x120(%rbp), %rdi
callq 0x2f290
movq -0x168(%rbp), %rdi
movq %rax, %rsi
callq 0xd0790
jmp 0xcd45e
movb $0x0, -0x121(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x311e8
movq -0x168(%rbp), %rax
movq -0x130(%rbp), %rdi
movq %rax, -0xf8(%rbp)
addq $0x68, %rdi
leaq -0xf8(%rbp), %rsi
callq 0xd0720
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xec(%rbp)
jmp 0xcd406
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xcd4d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x311e8
testb $0x1, -0x121(%rbp)
jne 0xcd4db
jmp 0xcd4ec
movq -0x168(%rbp), %rdi
movl $0x20, %esi
callq 0x2f2a0
jmp 0xcd50f
jmp 0xcd4f0
jmp 0xcd4f2
jmp 0xcd4f4
movq -0x130(%rbp), %rax
movq 0x50(%rax), %rax
movb $0x1, 0x13f8(%rax)
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x30170
movq %rax, %rdi
callq 0x43490