// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %rbx
movq %rdi, 0x10(%rsp)
movq %rsi, 0x50(%rdi)
movq 0x15c8(%rsi), %r14
subq 0x15c0(%rsi), %r14
shrq $0x3, %r14
imull 0x1620(%rsi), %r14d
cmpb $0x1, 0x1631(%rsi)
movq %rsi, %rdi
jne 0x5fd4e
leaq 0x1730(%rbx), %rdi
leaq 0x70737(%rip), %rsi     # 0xd03d0
callq 0x3357b
testb %al, %al
jne 0x5fcc4
movl $0x1730, %edi           # imm = 0x1730
movq 0x10(%rsp), %rax
addq 0x50(%rax), %rdi
leaq 0x70708(%rip), %rsi     # 0xd03bf
callq 0x3357b
testb %al, %al
je 0x5fd45
xorl %edi, %edi
xorl %esi, %esi
callq 0x28e50
movq 0x10628c(%rip), %rdi    # 0x165f60
leaq 0x58(%rsp), %rsi
callq 0x28b90
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rax
cmpl $0x3, 0x1614(%rax)
jb 0x5fd0b
movq 0x1062a1(%rip), %rdi    # 0x165f98
leaq 0x79a73(%rip), %rsi     # 0xd9771
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
movq 0x10624e(%rip), %rdi    # 0x165f60
leaq 0x38(%rsp), %rsi
callq 0x28e40
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rax
cmpl $0x3, 0x1614(%rax)
jb 0x5fe50
movq 0x10625f(%rip), %rdi    # 0x165f98
leaq 0x79a49(%rip), %rsi     # 0xd9789
jmp 0x5fe43
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rdi
addq $0x1730, %rdi           # imm = 0x1730
leaq 0x70670(%rip), %rsi     # 0xd03cc
callq 0x3357b
testb %al, %al
je 0x5fe72
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rax
cmpl $0x3, 0x1614(%rax)
jb 0x5fd96
movq 0x106216(%rip), %rdi    # 0x165f98
leaq 0x79a1b(%rip), %rsi     # 0xd97a4
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
xorl %edi, %edi
xorl %esi, %esi
callq 0x28e50
movq 0x1061ba(%rip), %rdi    # 0x165f60
leaq 0x58(%rsp), %rsi
callq 0x28b90
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rax
cmpl $0x3, 0x1614(%rax)
jb 0x5fddd
movq 0x1061cf(%rip), %rdi    # 0x165f98
leaq 0x799a1(%rip), %rsi     # 0xd9771
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
movq 0x10617c(%rip), %rdi    # 0x165f60
leaq 0x38(%rsp), %rsi
callq 0x28e40
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rax
cmpl $0x3, 0x1614(%rax)
jb 0x5fe50
movq 0x106191(%rip), %rbx    # 0x165f98
leaq 0x799bc(%rip), %rsi     # 0xd97ca
movq %rbx, %rdi
callq 0x289f0
movl 0x38(%rsp), %esi
movq %rax, %rdi
callq 0x29290
leaq 0x799ad(%rip), %rsi     # 0xd97d6
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
leaq 0x79949(%rip), %rsi     # 0xd9789
movq %rbx, %rdi
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rax
movb $0x1, 0x13f8(%rax)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
incl %r14d
movq 0x10(%rsp), %rax
addq $0x68, %rax
movq %rax, 0x28(%rsp)
cmpb $0x1, 0x50(%rbx)
jne 0x5ffda
movq 0x106104(%rip), %rdi    # 0x165f98
leaq 0x79954(%rip), %rsi     # 0xd97ef
callq 0x289f0
xorl %ebp, %ebp
leaq 0x58(%rsp), %r12
leaq 0x38(%rsp), %rbx
leaq 0x30(%rsp), %r15
cmpl %r14d, %ebp
ja 0x5fe50
pushq $0x20
popq %rdi
callq 0x28a10
movq %rax, %r13
movq %r12, %rdi
movl %ebp, %esi
callq 0x9f56b
movq 0x58(%rsp), %rsi
movl $0x1a4, 0x38(%rsp)      # imm = 0x1A4
movl $0x3e8, %ecx            # imm = 0x3E8
movq %r13, %rdi
pushq $0x64
popq %rdx
movq %rbx, %r8
callq 0x61b68
movq %r13, 0x30(%rsp)
movq %r12, %rdi
callq 0x295a0
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x61bf4
incl %ebp
jmp 0x5feb1
movq %rdx, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movq %r12, %rdi
callq 0x295a0
jmp 0x5ff27
movq %rdx, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movq %r13, %rdi
pushq $0x20
popq %rsi
callq 0x28a50
jmp 0x5ff3e
movq %rdx, 0x18(%rsp)
movq %rax, 0x8(%rsp)
cmpl $0x1, 0x18(%rsp)
jne 0x600bd
movq 0x8(%rsp), %rdi
callq 0x28c90
movq %r12, %rdi
movq %rax, %rsi
callq 0x61bb2
movq 0x8(%rsp), %rdi
callq 0x283c0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x9f56b
movq 0x38(%rsp), %rdi
callq 0x63e0e
movq %rbx, %rdi
callq 0x295a0
pushq $0x20
popq %rdi
callq 0x28a10
movq %rax, %r13
movq %rbx, %rdi
movl %ebp, %esi
callq 0x9f56b
movq 0x38(%rsp), %rsi
movl $0x1a4, 0x24(%rsp)      # imm = 0x1A4
movl $0x3e8, %ecx            # imm = 0x3E8
movq %r13, %rdi
pushq $0x64
popq %rdx
leaq 0x24(%rsp), %r8
callq 0x61b68
movq %r13, 0x30(%rsp)
movq %rbx, %rdi
callq 0x295a0
movq %r12, %rdi
callq 0x6448c
callq 0x29150
jmp 0x5fef8
movq 0x105fb7(%rip), %rdi    # 0x165f98
leaq 0x7982f(%rip), %rsi     # 0xd9817
callq 0x289f0
xorl %ebx, %ebx
pushq $0x20
popq %r15
leaq 0x58(%rsp), %r12
leaq 0x38(%rsp), %r13
cmpl %r14d, %ebx
ja 0x5fe50
movq %r15, %rdi
callq 0x28a10
movq %rax, %rbp
movq %r12, %rdi
movl %ebx, %esi
callq 0x9f56b
movq 0x58(%rsp), %rsi
movq %rbp, %rdi
callq 0x61c12
movq %r12, %rdi
callq 0x295a0
movq %rbp, 0x38(%rsp)
movq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x61bf4
incl %ebx
jmp 0x5fffd
movq %rax, 0x8(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x295a0
jmp 0x6005c
movq %rax, 0x8(%rsp)
pushq $0x20
popq %rsi
movq %r13, %rdi
callq 0x28a50
jmp 0x60081
jmp 0x6007c
movq %rax, 0x8(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x295a0
jmp 0x60081
movq %rax, 0x8(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x6448c
callq 0x29150
jmp 0x600bd
jmp 0x60094
movq %rax, %rdi
callq 0x3325c
movq %rax, 0x8(%rsp)
jmp 0x600b2
movq %rax, 0x8(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x295a0
pushq $0x20
popq %rsi
movq %rbp, %rdi
callq 0x28a50
movq 0x8(%rsp), %rdi
callq 0x292f0
nop