// File: j-faria[P]kima/examples/trends/../../DNest4/code/SamplerImpl.h
// Function: DNest4::Sampler<RVmodel>::run(unsigned int)
void Sampler<ModelType>::run(unsigned int thin)
{
	// Set the thining of terminal output
	thin_print = thin;

#ifndef NO_THREADS
	// Set up threads and barrier
	// Delete if necessary (shouldn't be needed!)
	if(barrier != nullptr)
		delete barrier;
	for(auto& thread: threads)
	{
		if(thread != nullptr)
			delete thread;
	}

	// Create the barrier
	barrier = new Barrier(num_threads);

	// Create and launch threads
	for(size_t i=0; i<threads.size(); ++i)
	{
		// Function to run on each thread
		auto func = std::bind(&Sampler<ModelType>::run_thread, this, i);

		// Allocate and create the thread
		threads[i] = new std::thread(func);
	}

	// Join and de-allocate all threads and barrier
	for(auto& t: threads)
		t->join();

	// Delete dynamically allocated stuff and point to nullptr
	delete barrier;
	barrier = nullptr;
	for(auto& t: threads)
	{
		delete t;
		t = nullptr;
	}
#else
	for(size_t i=0; i<threads.size(); ++i) run_thread(i);
#endif

	// Save the sampler state to a file.
	std::fstream fout("sampler_state.txt", std::ios::out);
	print(fout);
	fout.close();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x320, %rsp            # imm = 0x320
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movl -0xc(%rbp), %ecx
movl %ecx, 0x4(%rax)
cmpq $0x0, 0x20(%rax)
je 0x6dba1
movq -0x2d8(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x2e0(%rbp)
cmpq $0x0, %rax
je 0x6db9f
movq -0x2e0(%rbp), %rdi
callq 0xa9460
movq -0x2e0(%rbp), %rdi
movl $0x68, %esi
callq 0x6510
jmp 0x6dba1
movq -0x2d8(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0xa9480
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0xa94b0
movq %rax, -0x28(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0xa94e0
testb $0x1, %al
jne 0x6dbdd
jmp 0x6dc34
leaq -0x20(%rbp), %rdi
callq 0xa9520
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
je 0x6dc27
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x2e8(%rbp)
cmpq $0x0, %rax
je 0x6dc25
movq -0x2e8(%rbp), %rdi
callq 0xa9540
movq -0x2e8(%rbp), %rdi
movl $0x8, %esi
callq 0x6510
jmp 0x6dc27
jmp 0x6dc29
leaq -0x20(%rbp), %rdi
callq 0xa9570
jmp 0x6dbca
movl $0x68, %edi
callq 0x6500
movq %rax, %rdi
movq -0x2d8(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x2f0(%rbp)
movl 0x28(%rax), %esi
callq 0xb0400
jmp 0x6dc5c
movq -0x2d8(%rbp), %rax
movq -0x2f0(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq $0x0, -0x48(%rbp)
movq -0x2d8(%rbp), %rdi
movq -0x48(%rbp), %rax
movq %rax, -0x2f8(%rbp)
addq $0x8, %rdi
callq 0xa9590
movq %rax, %rcx
movq -0x2f8(%rbp), %rax
cmpq %rcx, %rax
jae 0x6dd76
movq -0x2d8(%rbp), %rax
leaq 0x3b93e(%rip), %rcx     # 0xa95f0
movq %rcx, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movq %rax, -0x80(%rbp)
leaq -0x68(%rbp), %rdi
movq %rdi, -0x308(%rbp)
leaq -0x78(%rbp), %rsi
leaq -0x80(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0xa95b0
movl $0x8, %edi
callq 0x6500
movq -0x308(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x300(%rbp)
callq 0xa9a90
jmp 0x6dd03
movq -0x2d8(%rbp), %rdi
addq $0x8, %rdi
movq -0x48(%rbp), %rsi
callq 0xa9b50
movq -0x300(%rbp), %rcx
movq %rcx, (%rax)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x6dc76
movq -0x2f0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x68, %esi
callq 0x6510
jmp 0x6df50
movq -0x300(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x8, %esi
callq 0x6510
jmp 0x6df50
movq -0x2d8(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0xa9480
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
callq 0xa94b0
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xa94e0
testb $0x1, %al
jne 0x6ddc7
jmp 0x6ddf7
leaq -0x90(%rbp), %rdi
callq 0xa9520
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq (%rax), %rdi
callq 0x67d0
leaq -0x90(%rbp), %rdi
callq 0xa9570
jmp 0x6ddae
movq -0x2d8(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x310(%rbp)
cmpq $0x0, %rax
je 0x6de2c
movq -0x310(%rbp), %rdi
callq 0xa9460
movq -0x310(%rbp), %rdi
movl $0x68, %esi
callq 0x6510
movq -0x2d8(%rbp), %rax
movq $0x0, 0x20(%rax)
addq $0x8, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
callq 0xa9480
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
callq 0xa94b0
movq %rax, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0xa94e0
testb $0x1, %al
jne 0x6de85
jmp 0x6dee8
leaq -0xb0(%rbp), %rdi
callq 0xa9520
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x318(%rbp)
cmpq $0x0, %rax
je 0x6decc
movq -0x318(%rbp), %rdi
callq 0xa9540
movq -0x318(%rbp), %rdi
movl $0x8, %esi
callq 0x6510
movq -0xc0(%rbp), %rax
movq $0x0, (%rax)
leaq -0xb0(%rbp), %rdi
callq 0xa9570
jmp 0x6de6c
leaq 0x5d78d(%rip), %rsi     # 0xcb67c
leaq -0x2d0(%rbp), %rdi
movl $0x10, %edx
callq 0x6680
movq -0x2d8(%rbp), %rdi
leaq -0x2c0(%rbp), %rsi
callq 0xa9b70
jmp 0x6df15
leaq -0x2d0(%rbp), %rdi
callq 0x6060
jmp 0x6df23
leaq -0x2d0(%rbp), %rdi
callq 0x60f0
addq $0x320, %rsp            # imm = 0x320
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x2d0(%rbp), %rdi
callq 0x60f0
movq -0x38(%rbp), %rdi
callq 0x6830
nopl (%rax)