// File: /workspace/github_cpp_makefile_star2_O2/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 $0xf8, %rsp
movq %rcx, 0x48(%rsp)
movq %rdx, %r13
movq %rsi, 0x20(%rsp)
movq %rdi, %rbp
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
movl %r8d, 0xc(%rsp)
movl %r8d, %esi
callq 0x6a11
leaq 0x5126(%rip), %rsi      # 0xfc3b
leaq 0x70(%rsp), %rdi
movq %rbx, %rdx
callq 0xb08f
leaq 0x511b(%rip), %rdx      # 0xfc44
leaq 0xa0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x6953
leaq 0xc77e(%rip), %rdi      # 0x172c0
leaq 0xa0(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x3130
leaq 0xa0(%rsp), %rdi
callq 0x3110
leaq 0x70(%rsp), %rdi
callq 0x3110
leaq 0xd0(%rsp), %rdi
callq 0x3110
xorps %xmm0, %xmm0
andq $0x0, 0x40(%rsp)
leaq 0xa8(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x60(%rsp)
leaq 0x78(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movaps %xmm0, 0x50(%rsp)
movslq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0xd0(%rsp), %rdi
leaq 0x28(%rsp), %rcx
callq 0xb166
leaq 0xa0(%rsp), %rdx
leaq 0x70(%rsp), %rcx
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
callq 0x9e16
movq %rbp, %rdi
movq %r13, %rsi
callq 0xaf02
andl $0x0, (%rax)
movq (%r13), %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
andl $0x0, 0x8(%rsi)
leaq 0x50(%rsp), %rdi
callq 0xb11a
movq (%r13), %r14
shlq $0x5, %r14
movq 0x20(%rsp), %rax
addq 0x18(%rax), %r14
leaq 0x502a(%rip), %r12      # 0xfc69
leaq 0x50(%rsp), %rbx
leaq 0x10(%rsp), %r13
movq %r14, %r15
movq (%r15), %r15
cmpq %r14, %r15
je 0xacc1
cmpb $0x1, 0xc636(%rip)      # 0x17291
jne 0xac9d
movq 0xc364(%rip), %rdi      # 0x16fc8
leaq 0x4fde(%rip), %rsi      # 0xfc49
callq 0x3270
movq 0x10(%r15), %rsi
movq %rax, %rdi
callq 0x31c0
movq %rax, %rdi
movq %r12, %rsi
callq 0x3270
movq 0x18(%r15), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3410
movq %rax, %rdi
callq 0x3140
movq 0x10(%r15), %rax
movq 0x18(%r15), %rcx
movq %rax, 0x10(%rsp)
cvtsi2ssl (%rcx), %xmm0
movss %xmm0, 0x18(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0xb1ca
jmp 0xac4c
movq %rbp, %r13
xorl %ebp, %ebp
movl 0xc(%rsp), %ebx
testl %ebx, %ebx
cmovlel %ebp, %ebx
leaq 0x28(%rsp), %r12
leaq 0x10(%rsp), %r14
leaq 0x30(%rsp), %r15
cmpl %ebp, %ebx
je 0xad7d
andq $0x0, 0x28(%rsp)
pushq $0x48
popq %rdi
callq 0x3280
leaq 0xc065(%rip), %rcx      # 0x16d60
movq %rcx, (%rax)
movl %ebp, 0x8(%rax)
leaq 0xd0(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x50(%rsp), %rcx
movq %rcx, 0x18(%rax)
movq %r13, 0x20(%rax)
leaq 0xa0(%rsp), %rcx
movq %rcx, 0x28(%rax)
leaq 0x70(%rsp), %rcx
movq %rcx, 0x30(%rax)
movq 0x48(%rsp), %rcx
movq %rcx, 0x38(%rax)
movq 0x20(%rsp), %rcx
movq %rcx, 0x40(%rax)
movq %rax, 0x10(%rsp)
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x3260
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xad63
movq (%rdi), %rax
callq *0x8(%rax)
movq %r15, %rdi
movq %r12, %rsi
callq 0xc6f6
movq %r12, %rdi
callq 0xb1e0
incl %ebp
jmp 0xacde
movl %ebx, %ebx
shlq $0x3, %rbx
xorl %r14d, %r14d
movq %r13, %rbp
cmpq %r14, %rbx
je 0xada1
movq 0x30(%rsp), %rdi
addq %r14, %rdi
callq 0x33f0
addq $0x8, %r14
jmp 0xad89
leaq 0xc518(%rip), %rdi      # 0x172c0
callq 0x3330
leaq 0xd0(%rsp), %rdi
callq 0xb1ee
leaq 0x70(%rsp), %rdi
callq 0xb706
leaq 0x50(%rsp), %rdi
callq 0xb71a
leaq 0xa0(%rsp), %rdi
callq 0xb706
leaq 0x30(%rsp), %rdi
callq 0xb1fe
movq %rbp, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xae71
jmp 0xae71
jmp 0xae71
movq %rax, %r14
jmp 0xae7c
movq %rax, %r14
jmp 0xae89
movq %rax, %r14
leaq 0xa0(%rsp), %rdi
callq 0x3110
jmp 0xae1f
movq %rax, %r14
leaq 0x70(%rsp), %rdi
callq 0x3110
jmp 0xae2e
movq %rax, %r14
leaq 0xd0(%rsp), %rdi
callq 0x3110
jmp 0xaeb4
jmp 0xae71
jmp 0xae71
movq %rax, %r14
jmp 0xae68
movq %rax, %r14
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xae68
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xae68
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0xb1e0
movq %r13, %rbp
jmp 0xae74
jmp 0xae71
jmp 0xae71
movq %rax, %r14
movq %rbp, %rdi
callq 0x70ae
leaq 0xd0(%rsp), %rdi
callq 0xb1ee
leaq 0x70(%rsp), %rdi
callq 0xb706
leaq 0x50(%rsp), %rdi
callq 0xb71a
leaq 0xa0(%rsp), %rdi
callq 0xb706
leaq 0x30(%rsp), %rdi
callq 0xb1fe
movq %r14, %rdi
callq 0x3420