// File: /workspace/github_cpp_makefile_star2_O1/flastest[P]HeterogeneousParallelDijkstra/BGL_Dijkstra.cc
// Function: parallel_dijkstra(boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, boost::no_property, boost::property<boost::edge_weight_t, int, boost::no_property>, boost::no_property, boost::listS> const&, unsigned long const&, std::map<unsigned long, unsigned long, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, unsigned long>>>&, int)
distance_map_t parallel_dijkstra(const graph_t &graph, 
						const vertex_descriptor &source, 
						predecessor_map_t &predecessors,
						int NUM_THREADS)
{

	debug_file_stream.open("debuglog"+std::to_string(NUM_THREADS)+".txt");
	//all the threads, pretty self explanatory
	vector<thread> threads;

	mutex_map_t distance_mutexes;

	//I need a new pqueue for all the offers.
	offer_pqueue_t offer_pq;

	//let's the mutex list for the predecessors
	mutex_map_t predecessor_mutexes;

	//this keeps track of the threads that are done
	vector<bool> done(NUM_THREADS, false); 

	distance_map_t distances = initialize_distances_from_source_and_mutex_maps(graph, distance_mutexes, predecessor_mutexes);


	//for using BGL's graph_t (adjacency table):
	//https://www.boost.org/doc/libs/1_60_0/libs/graph/doc/adjacency_list.html

	//set the source node's distance from source to zero
	distances[source] = 0;

	const auto weight = get(edge_weight, graph);

	//now to enqueue source's neighbors in the pqueue
	offer_pq.push(offer_t(source,0.0f));
	auto [start, finish] = out_edges(source,graph);
	for(;start != finish; start++)
	{
		if (DEBUG) std::cout << "adding an offer from source to "<<target(*start,graph)<<" of distance "<<get(weight, *start)<<std::endl;
		offer_t offer(target(*start,graph),get(weight, *start));
		offer_pq.push(offer);
	}


	//ok now all the threads gotta do their things. 
	for (int thread_id = 0; thread_id < NUM_THREADS;)
	{
		threads.push_back(
			std::thread([&, thread_id](){
				parallel_dijkstra_thread(
					done,
					thread_id,
					offer_pq,
					distances,
					distance_mutexes,
					predecessor_mutexes,
					predecessors,
					graph);}));
		thread_id++;
	}

	//join threads!
	for(int thread = 0; thread < NUM_THREADS; thread++)
	{
		threads[thread].join();
	}
	debug_file_stream.close();
	return distances;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rcx, 0x100(%rsp)
movq %rdx, %r13
movq %rsi, 0x58(%rsp)
movq %rdi, %rbp
movl %r8d, %ebx
negl %ebx
cmovsl %r8d, %ebx
movl $0x1, %r14d
cmpl $0xa, %ebx
jb 0xd5ad
movl $0x4, %r14d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %ebx, %ecx
cmpl $0x63, %ecx
jbe 0xd5a4
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0xd5aa
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0xd5ad
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r14d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0xd571
addl $-0x3, %r14d
jmp 0xd5ad
addl $-0x2, %r14d
jmp 0xd5ad
decl %r14d
movl %r8d, 0xc(%rsp)
movl %r8d, %r15d
shrl $0x1f, %r15d
leal (%r14,%r15), %esi
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x30(%rsp), %r12
movq %r12, %rdi
movl $0x2d, %edx
callq 0x32b0
addq (%r12), %r15
movq %r15, %rdi
movl %r14d, %esi
movl %ebx, %edx
callq 0x8993
leaq 0x51fd(%rip), %rcx      # 0x127ed
movl $0x8, %r8d
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3290
leaq 0x90(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xd632
movq %rdx, 0x80(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x90(%rsp)
jmp 0xd639
movups (%rcx), %xmm0
movups %xmm0, (%r14)
movq 0x8(%rax), %rdx
leaq 0x80(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x5197(%rip), %rsi      # 0x127f6
callq 0x3440
leaq 0xc0(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xd694
movq %rdx, 0xb0(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xc0(%rsp)
jmp 0xd69b
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
leaq 0xb0(%rsp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0xabe7(%rip), %rdi      # 0x182a8
movl $0x10, %edx
callq 0x3110
movq 0xb0(%rsp), %rdi
cmpq %r15, %rdi
je 0xd6e8
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3250
movq 0x80(%rsp), %rdi
cmpq %r14, %rdi
je 0xd705
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x3250
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0xd721
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3250
xorl %eax, %eax
movq %rax, 0xf0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xe0(%rsp)
leaq 0xb8(%rsp), %rcx
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
movq %rax, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
leaq 0x88(%rsp), %rcx
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
movslq 0xc(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x30(%rsp), %rdi
leaq 0x18(%rsp), %rcx
callq 0xdf52
leaq 0xb0(%rsp), %rdx
leaq 0x80(%rsp), %rcx
movq %rbp, %rdi
movq 0x58(%rsp), %rsi
callq 0xba8d
movq %rbp, %rdi
movq %r13, %rsi
callq 0xdbd2
movq %rbp, 0x10(%rsp)
xorl %ecx, %ecx
movl %ecx, (%rax)
movq (%r13), %rax
leaq 0x20(%rsp), %rsi
movq %rax, (%rsi)
movl %ecx, 0x8(%rsi)
leaq 0x60(%rsp), %rdi
callq 0xde68
movq (%r13), %rax
movq 0x58(%rsp), %rcx
movq 0x18(%rcx), %rcx
shlq $0x5, %rax
leaq (%rcx,%rax), %r13
movq (%rcx,%rax), %rbp
cmpq %r13, %rbp
je 0xd8b7
movq 0xa7cd(%rip), %r14      # 0x17fd0
leaq 0x60(%rsp), %r12
leaq 0x20(%rsp), %rbx
cmpb $0x1, 0xaa65(%rip)      # 0x18279
jne 0xd885
movl $0x1f, %edx
movq %r14, %rdi
leaq 0x4fd6(%rip), %rsi      # 0x127fb
callq 0x32a0
movq 0x10(%rbp), %rsi
movq %r14, %rdi
callq 0x3190
movq %rax, %r15
movl $0xd, %edx
movq %rax, %rdi
leaq 0x4fd3(%rip), %rsi      # 0x1281b
callq 0x32a0
movq 0x18(%rbp), %rax
movl (%rax), %esi
movq %r15, %rdi
callq 0x33e0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq 0x10(%rbp), %rax
movq 0x18(%rbp), %rcx
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
cvtsi2ssl (%rcx), %xmm0
movss %xmm0, 0x28(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0xdfba
movq (%rbp), %rbp
cmpq %r13, %rbp
jne 0xd80d
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rbp
jle 0xd987
xorl %r15d, %r15d
leaq 0xa49f(%rip), %r13      # 0x17d70
leaq 0x18(%rsp), %rbx
leaq 0x20(%rsp), %r14
leaq 0xe0(%rsp), %r12
movq $0x0, 0x18(%rsp)
movl $0x48, %edi
callq 0x3240
movq %r13, (%rax)
movl %r15d, 0x8(%rax)
leaq 0x30(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x60(%rsp), %rcx
movq %rcx, 0x18(%rax)
movq %rbp, 0x20(%rax)
leaq 0xb0(%rsp), %rcx
movq %rcx, 0x28(%rax)
leaq 0x80(%rsp), %rcx
movq %rcx, 0x30(%rax)
movq 0x100(%rsp), %rcx
movq %rcx, 0x38(%rax)
movq 0x58(%rsp), %rcx
movq %rcx, 0x40(%rax)
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x3220
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xd962
movq (%rdi), %rax
callq *0x8(%rax)
movq %r12, %rdi
movq %rbx, %rsi
callq 0xf17e
cmpq $0x0, 0x18(%rsp)
jne 0xdaf5
incl %r15d
cmpl %r15d, 0xc(%rsp)
jne 0xd8e3
movl 0xc(%rsp), %eax
testl %eax, %eax
jle 0xd9b1
movl %eax, %ebx
shlq $0x3, %rbx
xorl %r14d, %r14d
movq 0xe0(%rsp), %rdi
addq %r14, %rdi
callq 0x33c0
addq $0x8, %r14
cmpq %r14, %rbx
jne 0xd998
leaq 0xa8f0(%rip), %rdi      # 0x182a8
callq 0x3300
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xd9ed
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movl %eax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movl %eax, 0x48(%rsp)
movq %rax, 0x50(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xe458
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0xda11
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
leaq 0xb0(%rsp), %rdi
callq 0xe458
leaq 0xe0(%rsp), %rdi
callq 0xe05a
movq %rbp, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xdafa
jmp 0xdb03
jmp 0xdafa
movq %rax, %rbx
jmp 0xdb13
movq %rax, %rbx
jmp 0xdb43
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
cmpq %r15, %rdi
je 0xda84
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3250
jmp 0xda84
movq %rax, %rbx
movq 0x80(%rsp), %rdi
cmpq %r14, %rdi
je 0xdaa6
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x3250
jmp 0xdaa6
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0xdb81
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3250
jmp 0xdb81
jmp 0xdb03
jmp 0xdb03
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xdae2
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, 0x20(%rsp)
jmp 0xdb06
cmpq $0x0, 0x18(%rsp)
je 0xdb03
callq 0x30a0
movq %rax, %rbx
jmp 0xdb0b
jmp 0xdb03
jmp 0xdb03
movq %rax, %rbx
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0x8ca2
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xdb43
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movl %eax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movl %eax, 0x48(%rsp)
movq %rax, 0x50(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xe458
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0xdb67
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
leaq 0xb0(%rsp), %rdi
callq 0xe458
leaq 0xe0(%rsp), %rdi
callq 0xe05a
movq %rbx, %rdi
callq 0x3400
nop