// File: /workspace/github_cpp_makefile_star2_O3/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, 0x30(%rsp)
movq %rdi, %rbp
movl %r8d, %ebx
negl %ebx
cmovsl %r8d, %ebx
movl $0x1, %r14d
cmpl $0xa, %ebx
jb 0xd380
movl $0x4, %r14d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %ebx, %ecx
cmpl $0x63, %ecx
jbe 0xd377
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0xd37d
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0xd380
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r14d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0xd344
addl $-0x3, %r14d
jmp 0xd380
addl $-0x2, %r14d
jmp 0xd380
decl %r14d
movl %r8d, 0xc(%rsp)
movl %r8d, %r15d
shrl $0x1f, %r15d
leal (%r14,%r15), %esi
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x38(%rsp), %r12
movq %r12, %rdi
movl $0x2d, %edx
callq 0x32b0
addq (%r12), %r15
movq %r15, %rdi
movl %r14d, %esi
movl %ebx, %edx
callq 0x883b
leaq 0x546a(%rip), %rcx      # 0x1282d
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 0xd405
movq %rdx, 0x80(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x90(%rsp)
jmp 0xd40c
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 0x5404(%rip), %rsi      # 0x12836
callq 0x3440
leaq 0xc0(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xd467
movq %rdx, 0xb0(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xc0(%rsp)
jmp 0xd46e
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 0xae14(%rip), %rdi      # 0x182a8
movl $0x10, %edx
callq 0x3110
movq 0xb0(%rsp), %rdi
cmpq %r15, %rdi
je 0xd4bb
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3250
movq 0x80(%rsp), %rdi
cmpq %r14, %rdi
je 0xd4d8
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x3250
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0xd4f4
movq 0x48(%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 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rcx
callq 0xdce6
leaq 0xb0(%rsp), %rdx
leaq 0x80(%rsp), %rcx
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
callq 0xb830
movq %rbp, %rdi
movq %r13, %rsi
callq 0xd966
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 0xdbfa
movq (%r13), %rax
movq 0x30(%rsp), %rcx
movq 0x18(%rcx), %rcx
shlq $0x5, %rax
leaq (%rcx,%rax), %r13
movq (%rcx,%rax), %rbp
cmpq %r13, %rbp
je 0xd68a
movq 0xa9fa(%rip), %r14      # 0x17fd0
leaq 0x60(%rsp), %r12
leaq 0x20(%rsp), %rbx
cmpb $0x1, 0xac92(%rip)      # 0x18279
jne 0xd658
movl $0x1f, %edx
movq %r14, %rdi
leaq 0x5243(%rip), %rsi      # 0x1283b
callq 0x32a0
movq 0x10(%rbp), %rsi
movq %r14, %rdi
callq 0x3190
movq %rax, %r15
movl $0xd, %edx
movq %rax, %rdi
leaq 0x5240(%rip), %rsi      # 0x1285b
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 0xdd4e
movq (%rbp), %rbp
cmpq %r13, %rbp
jne 0xd5e0
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rbp
jle 0xd784
xorl %r15d, %r15d
leaq 0xa6cc(%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 0x38(%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 0x30(%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 0xd735
movq (%rdi), %rax
callq *0x8(%rax)
movq %r12, %rdi
movq %rbx, %rsi
callq 0xeede
cmpq $0x0, 0x18(%rsp)
jne 0xd8a8
incl %r15d
cmpl %r15d, 0xc(%rsp)
jne 0xd6b6
movl 0xc(%rsp), %eax
testl %eax, %eax
jle 0xd784
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 0xd76b
leaq 0xab1d(%rip), %rdi      # 0x182a8
callq 0x3300
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0xd7a7
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
leaq 0x80(%rsp), %rdi
callq 0xe1e6
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0xd7cb
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
leaq 0xb0(%rsp), %rdi
callq 0xe1e6
leaq 0xe0(%rsp), %rdi
callq 0xdde8
movq %rbp, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xd884
jmp 0xd8b1
jmp 0xd884
movq %rax, %rbx
jmp 0xd8c1
movq %rax, %rbx
jmp 0xd8d8
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
cmpq %r15, %rdi
je 0xd83b
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3250
jmp 0xd83b
movq %rax, %rbx
movq 0x80(%rsp), %rdi
cmpq %r14, %rdi
je 0xd85d
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x3250
jmp 0xd85d
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0xd916
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3250
jmp 0xd916
jmp 0xd8b1
movq %rax, %rbx
jmp 0xd8b9
jmp 0xd8b1
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xd8b4
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xd8b4
cmpq $0x0, 0x18(%rsp)
je 0xd8b1
callq 0x30a0
jmp 0xd8b1
jmp 0xd8b1
movq %rax, %rbx
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0x8b50
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0xd8d8
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
leaq 0x80(%rsp), %rdi
callq 0xe1e6
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0xd8fc
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
leaq 0xb0(%rsp), %rdi
callq 0xe1e6
leaq 0xe0(%rsp), %rdi
callq 0xdde8
movq %rbx, %rdi
callq 0x3400