
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "cuda.h"
#include "device_atomic_functions.h"

#include <stdio.h>
#include "Bfs.cuh"

__global__ void bfs_kernel(unsigned int* current_set, unsigned int* new_set,
	int current_set_size, int* current_set_size_new,
	Node* node_list, Edge* edge_list, int* color, int* cost, int level)
{
	// get tread number
	int tid = blockIdx.x * blockDim.x + threadIdx.x;

	for (int j = tid; j<current_set_size; j += blockDim.x*gridDim.x) {
		unsigned int index = current_set[j];// get one from the current set
		current_set[j] = 0;                 // make it not visited
		cost[index] = level;
		Node cur_node = node_list[index];
		// all adjacent vertices
		for (int i = cur_node.start; i < cur_node.start + cur_node.edge_num; i++)
		{
			unsigned int id = edge_list[i].dest;
			int old_color = atomicExch((int*)&color[id], BLACK);// visit adjacent node
			if (old_color == WHITE) { // was not visited
				int write_position = atomicAdd((int*) &(*current_set_size_new), 1);
				new_set[write_position] = id; // add to set of the next level
			}
		}
	}
}

void callBFSKernel(const unsigned int blocks,
	const unsigned int threadsPerBlock,
	unsigned int* current_set, unsigned int* new_set,
	int current_set_size, int* current_set_size_new,
	Node* node_list, Edge* edge_list, int* color, int* cost, int level)
{
	bfs_kernel << <blocks, threadsPerBlock >> > (current_set, new_set, current_set_size,
		current_set_size_new, node_list, edge_list, color, cost, level);
}


	.file	"tmpxft_002f41b6_00000000-6_Bfs.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z58__device_stub__Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_iPjS_iPiP6node_tP6edge_tS0_S0_i
	.type	_Z58__device_stub__Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_iPjS_iPiP6node_tP6edge_tS0_S0_i, @function
_Z58__device_stub__Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_iPjS_iPiP6node_tP6edge_tS0_S0_i:
.LFB2052:
	.cfi_startproc
	endbr64
	subq	$216, %rsp
	.cfi_def_cfa_offset 224
	movq	224(%rsp), %rax
	movq	%rdi, 56(%rsp)
	leaq	80(%rsp), %rdi
	movq	%rsi, 48(%rsp)
	leaq	92(%rsp), %rsi
	movq	%rax, 8(%rsp)
	movq	232(%rsp), %rax
	movl	%edx, 44(%rsp)
	leaq	64(%rsp), %rdx
	movq	%rcx, 32(%rsp)
	leaq	72(%rsp), %rcx
	movq	%r8, 24(%rsp)
	movq	%r9, 16(%rsp)
	movq	%rax, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 200(%rsp)
	xorl	%eax, %eax
	leaq	56(%rsp), %rax
	movl	$1, 88(%rsp)
	movq	%rax, 128(%rsp)
	leaq	48(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	44(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 160(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 168(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 176(%rsp)
	movq	%rsp, %rax
	movq	%rax, 184(%rsp)
	leaq	240(%rsp), %rax
	movq	%rax, 192(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 80(%rsp)
	movq	%rax, 92(%rsp)
	movl	$1, 100(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	72(%rsp)
	.cfi_def_cfa_offset 232
	leaq	_Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_i(%rip), %rdi
	pushq	72(%rsp)
	.cfi_def_cfa_offset 240
	movq	108(%rsp), %rcx
	movl	116(%rsp), %r8d
	movq	96(%rsp), %rsi
	movl	104(%rsp), %edx
	leaq	144(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 232
	popq	%rdx
	.cfi_def_cfa_offset 224
.L2:
	movq	200(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$216, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2052:
	.size	_Z58__device_stub__Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_iPjS_iPiP6node_tP6edge_tS0_S0_i, .-_Z58__device_stub__Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_iPjS_iPiP6node_tP6edge_tS0_S0_i
	.globl	_Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_i
	.type	_Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_i, @function
_Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_i:
.LFB2053:
	.cfi_startproc
	endbr64
	jmp	_Z58__device_stub__Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_iPjS_iPiP6node_tP6edge_tS0_S0_i
	.cfi_endproc
.LFE2053:
	.size	_Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_i, .-_Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_i
	.globl	_Z13callBFSKerneljjPjS_iPiP6node_tP6edge_tS0_S0_i
	.type	_Z13callBFSKerneljjPjS_iPiP6node_tP6edge_tS0_S0_i, @function
_Z13callBFSKerneljjPjS_iPiP6node_tP6edge_tS0_S0_i:
.LFB2027:
	.cfi_startproc
	endbr64
	movabsq	$4294967297, %rax
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%r9, %r12
	xorl	%r9d, %r9d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movl	%esi, 52(%rsp)
	movq	128(%rsp), %r13
	movq	%rax, 56(%rsp)
	movq	136(%rsp), %r14
	movq	%rax, 44(%rsp)
	movl	48(%rsp), %esi
	movq	%rdx, 8(%rsp)
	movq	52(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	movl	60(%rsp), %ecx
	movl	%edi, 40(%rsp)
	movq	40(%rsp), %rdi
	movl	%r8d, 28(%rsp)
	xorl	%r8d, %r8d
	movq	144(%rsp), %r15
	movq	152(%rsp), %rbx
	movl	160(%rsp), %ebp
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L8
	movl	%ebp, 144(%rsp)
	movq	%r14, %r9
	movq	%r13, %r8
	movq	%r12, %rcx
	movq	%rbx, 136(%rsp)
	movl	28(%rsp), %edx
	movq	%r15, 128(%rsp)
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdi
	addq	$72, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	_Z58__device_stub__Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_iPjS_iPiP6node_tP6edge_tS0_S0_i
.L8:
	.cfi_restore_state
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z13callBFSKerneljjPjS_iPiP6node_tP6edge_tS0_S0_i, .-_Z13callBFSKerneljjPjS_iPiP6node_tP6edge_tS0_S0_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_i"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC0(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z10bfs_kernelPjS_iPiP6node_tP6edge_tS0_S0_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2055:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#ifndef BFS_CUH
#define BFS_CUH

#define WHITE	0				/* not visited */
#define GREY	1				/* visiting */
#define BLACK	2				/* visited */
#define INF		2147483647		/* infinity distance */
#define MAX_LEVEL		20000	
#define THREAD_PER_BLOCK 512    /* can depend on number of nodes*/
#define BLOCK_NUM 256

typedef struct node_t
{
	unsigned int start;		/* starting index of edges */
	unsigned int edge_num;
} Node;

typedef struct edge_t
{
	unsigned int dest;			/* index of nodes */
	unsigned int cost;
} Edge;

void callBFSKernel(const unsigned int blocks,
	const unsigned int threadsPerBlock,
	unsigned int* current_set, unsigned int* new_set,
	int current_set_size, int* current_set_size_new,
	Node* node_list, Edge* edge_list, int* color, int* cost, int level);

#endif // BFS_CUH
	.text
	.file	"Bfs.hip"
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
