#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include <device_launch_parameters.h>
#include <iostream>
#include <memory>

#include "floyd_warshall.cuh"

__global__ void floyd_warshall_buffer(int* in_mat, int* in_mat_t,
const int* in_x, const int* in_y) {
	const int dx = threadIdx.x;
	const int dy = threadIdx.y;
	const int mx = gridDim.x;
	const int my = gridDim.y;

	int position = dy * mx + dx;
	int position_t = dx * my + dy;
	float val1 = in_mat[position];
	float val2 = in_x[dx] + in_y[dy];
	in_mat[position] = (val1 < val2) ? val1 : val2;
	in_mat_t[position_t] = (val1 < val2) ? val1 : val2;
}

int main() {
	const int n = 3;
	const int infinite = std::numeric_limits<int>::max();

	int* graph = (int*)malloc(n * n * sizeof(int));
	graph[0] = 0;
	graph[1] = infinite;
	graph[2] = 1;
	graph[3] = 12;
	graph[4] = 0;
	graph[5] = 12;
	graph[6] = 1;
	graph[7] = 25;
	graph[8] = 0;

	int* graph_t = (int*)malloc(n * n * sizeof(int));
	graph_t[0] = 0;
	graph_t[1] = 12;
	graph_t[2] = 1;
	graph_t[3] = infinite;
	graph_t[4] = 0;
	graph_t[5] = 25;
	graph_t[6] = 1;
	graph_t[7] = 12;
	graph_t[8] = 0;

	int* path = (int*)calloc(n * n, sizeof(int));

	int* graph_d;
	int* graph_t_d;
	int* in_x_d;
	int* in_y_d;
	cudaMalloc(&graph_d, n * n * sizeof(int));
	cudaMalloc(&graph_t_d, n * n * sizeof(int));
	cudaMalloc(&in_x_d, n * sizeof(int));
	cudaMalloc(&in_y_d, n * sizeof(int));
	cudaMemcpy(graph_d, graph, n * n * sizeof(int), cudaMemcpyHostToDevice);
	cudaMemcpy(graph_t_d, graph_t, n * n * sizeof(int), cudaMemcpyHostToDevice);

	const dim3 grid = {n, n, 1};
	const int block = 9;

	for (int i = 0; i < n; ++i) {
		// Copy in in_x_d the i line from graph_d. 
		cudaMemcpy(in_x_d, &graph_d[i * n], n * sizeof(int), cudaMemcpyDeviceToDevice);
		// Copy in in_y_d the i line from graph_t_d. 
		cudaMemcpy(in_y_d, &graph_t_d[i * n], n * sizeof(int), cudaMemcpyDeviceToDevice);
		floyd_warshall_buffer<<<grid, block>>>(graph_d, graph_t_d, in_x_d, in_y_d);
	}

	cudaMemcpy(path, graph_d, n * n * sizeof(int), cudaMemcpyDeviceToHost);

	for (int x = 0; x < n; ++x)
	{
		for (int y = 0; y < n; ++y)
		{
				std::cout << path[x * n + y];
				std::cout << " ";
		}
		std::cout << "\n";
	}

	// Free memory.
	cudaFree(graph_t_d);
	cudaFree(graph_d);
	cudaFree(in_x_d);
	cudaFree(in_y_d);
	free(graph);
	free(graph_t);
	return EXIT_SUCCESS;
}

	.file	"tmpxft_00269c79_00000000-6_floyd_warshall.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB4282:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE4282:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z49__device_stub__Z21floyd_warshall_bufferPiS_PKiS1_PiS_PKiS1_
	.type	_Z49__device_stub__Z21floyd_warshall_bufferPiS_PKiS1_PiS_PKiS1_, @function
_Z49__device_stub__Z21floyd_warshall_bufferPiS_PKiS1_PiS_PKiS1_:
.LFB4304:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movq	%rcx, (%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	movq	%rsp, %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z21floyd_warshall_bufferPiS_PKiS1_(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L2:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4304:
	.size	_Z49__device_stub__Z21floyd_warshall_bufferPiS_PKiS1_PiS_PKiS1_, .-_Z49__device_stub__Z21floyd_warshall_bufferPiS_PKiS1_PiS_PKiS1_
	.globl	_Z21floyd_warshall_bufferPiS_PKiS1_
	.type	_Z21floyd_warshall_bufferPiS_PKiS1_, @function
_Z21floyd_warshall_bufferPiS_PKiS1_:
.LFB4305:
	.cfi_startproc
	endbr64
	jmp	_Z49__device_stub__Z21floyd_warshall_bufferPiS_PKiS1_PiS_PKiS1_
	.cfi_endproc
.LFE4305:
	.size	_Z21floyd_warshall_bufferPiS_PKiS1_, .-_Z21floyd_warshall_bufferPiS_PKiS1_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	" "
.LC1:
	.string	"\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB4279:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$36, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movabsq	$4294967305, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movabsq	$51539607552, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movabsq	$51539607553, %r12
	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
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$36, %edi
	movq	%rax, %rbp
	movl	$2147483647, %eax
	salq	$32, %rax
	movq	%r12, 8(%rbp)
	movq	%rax, 0(%rbp)
	movabsq	$107374182401, %rax
	movq	%rax, 24(%rbp)
	xorl	%eax, %eax
	movq	%r13, 16(%rbp)
	movl	%eax, 32(%rbp)
	call	malloc@PLT
	xorl	%edx, %edx
	movl	$4, %esi
	movl	$9, %edi
	movq	%r13, (%rax)
	movq	%rax, %rbx
	movabsq	$9223372032559808513, %rax
	movq	%rax, 8(%rbx)
	movl	$25, %eax
	salq	$32, %rax
	movl	%edx, 32(%rbx)
	movq	%r12, 24(%rbx)
	xorl	%r12d, %r12d
	movq	%rax, 16(%rbx)
	call	calloc@PLT
	movq	%rsp, %rdi
	movl	$36, %esi
	movq	%rax, %r13
	call	cudaMalloc@PLT
	leaq	8(%rsp), %rdi
	movl	$36, %esi
	call	cudaMalloc@PLT
	leaq	16(%rsp), %rdi
	movl	$12, %esi
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movl	$12, %esi
	call	cudaMalloc@PLT
	movq	(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rsi
	movl	$36, %edx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$36, %edx
	call	cudaMemcpy@PLT
	movl	$1, 40(%rsp)
	movabsq	$12884901891, %rax
	movq	%rax, 32(%rsp)
.L10:
	movq	(%rsp), %rsi
	movq	16(%rsp), %rdi
	movl	$3, %ecx
	movl	$12, %edx
	addq	%r12, %rsi
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rsi
	movq	24(%rsp), %rdi
	movl	$3, %ecx
	movl	$12, %edx
	addq	%r12, %rsi
	call	cudaMemcpy@PLT
	movq	32(%rsp), %rdi
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	40(%rsp), %esi
	movq	%r14, %rdx
	movl	$1, %ecx
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L9
	movq	24(%rsp), %rcx
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdi
	call	_Z49__device_stub__Z21floyd_warshall_bufferPiS_PKiS1_PiS_PKiS1_
.L9:
	addq	$12, %r12
	cmpq	$36, %r12
	jne	.L10
	movq	(%rsp), %rsi
	movl	$2, %ecx
	movq	%r13, %rdi
	xorl	%r14d, %r14d
	movl	$36, %edx
	call	cudaMemcpy@PLT
.L11:
	imulq	$12, %r14, %rcx
	xorl	%r12d, %r12d
	leaq	0(%r13,%rcx), %r15
.L12:
	movl	(%r15,%r12,4), %esi
	leaq	_ZSt4cout(%rip), %rdi
	incq	%r12
	call	_ZNSolsEi@PLT
	movl	$1, %edx
	leaq	.LC0(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
	cmpq	$3, %r12
	jne	.L12
	movl	$1, %edx
	leaq	.LC1(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	incq	%r14
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
	cmpq	$3, %r14
	jne	.L11
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L14
	call	__stack_chk_fail@PLT
.L14:
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	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
.LFE4279:
	.size	main, .-main
	.section	.rodata.str1.1
.LC2:
	.string	"_Z21floyd_warshall_bufferPiS_PKiS1_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB4307:
	.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	.LC2(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z21floyd_warshall_bufferPiS_PKiS1_(%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
.LFE4307:
	.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:

﻿#pragma once

#include <hip/hip_runtime.h>


__global__ void floyd_warshall_buffer(int* in_mat, int* in_mat_t,
const int* in_x, const int* in_y);

	.text
	.file	"floyd_warshall.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
