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

__device__
AntGPU::AntGPU(int initialLocation, int matrixDim, curandState_t randState) :
	position(initialLocation),
	possibleLocations(new int[matrixDim]),
	goodnessNumerators(new double[matrixDim]),
	possibleLocationsLastIndex(matrixDim - 1),
	m_randomState(randState)
{
}

__device__
void AntGPU::Venture(int* route, const double* distanceMatrix, const double* pheromoneMatrix, int matrixDim, double alpha, double beta)
{
	while (possibleLocationsLastIndex >= 0)
	{
		int nextHop = SelectNextHop(distanceMatrix, pheromoneMatrix, matrixDim, alpha, beta);
		GoTo(nextHop, route, distanceMatrix, matrixDim);
	}
	//printf("Distance Traveled: %f\n", distance + distanceMatrix[position]);
	route[matrixDim] = route[0];
	distance += distanceMatrix[route[matrixDim - 1] * matrixDim + route[0]];
}

__device__
int AntGPU::SelectNextHop(const double* distance_matrix, const double* pheromoneMatrix, int matrixDim, double alpha, double beta)
{
	double denominator = 0;
	for (int i = 0; i <= possibleLocationsLastIndex; ++i)
	{
		int possiblePosition = possibleLocations[i];
		double goodnessNumerator = pow(pheromoneMatrix[position * matrixDim + possiblePosition], alpha) * pow(1.0 / distance_matrix[position * matrixDim + possiblePosition], beta);

		goodnessNumerators[possiblePosition] = goodnessNumerator;
		denominator += goodnessNumerator;
	}
	
	double sum = 0;
	double random = curand_uniform_double(&m_randomState);

	for (int i = 0; i <= possibleLocationsLastIndex; ++i)
	{
		int possiblePosition = possibleLocations[i];
		double numerator = goodnessNumerators[possiblePosition];
		double probability = numerator / denominator;
		if (random <= sum + probability)
		{
			possibleLocationsNextIndex = i;
			return possiblePosition;
		}
		sum += probability;
	}
	return -1;
	
}

__device__
void AntGPU::GoTo(int next, int* route, const double* distanceMatrix, int matrixDim)
{
	route[routeIndex++] = next;
	possibleLocations[possibleLocationsNextIndex] = possibleLocations[possibleLocationsLastIndex--];
	distance += distanceMatrix[position * matrixDim + next];
	position = next;
}

__device__
void AntGPU::Reset(int* route, int initialLocation, int matrixDim)
{
	routeIndex = 0;
	distance = 0;
	position = initialLocation;
	possibleLocationsLastIndex = matrixDim - 1;
	for (int i = 0; i < matrixDim; ++i) { possibleLocations[i] = i; }
	route[routeIndex++] = initialLocation;
	possibleLocations[initialLocation] = possibleLocations[possibleLocationsLastIndex--];
}



	.file	"tmpxft_00316f4e_00000000-6_AntGPU.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2250:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2250:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.align 2
	.globl	_ZN6AntGPUC2Eii17curandStateXORWOW
	.type	_ZN6AntGPUC2Eii17curandStateXORWOW, @function
_ZN6AntGPUC2Eii17curandStateXORWOW:
.LFB2242:
	.cfi_startproc
	endbr64
	movabsq	$2305843009213693950, %rax
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	xorl	%r9d, %r9d
	movslq	%edx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$16, %rsp
	.cfi_def_cfa_offset 48
	movl	%r9d, (%rdi)
	movl	%esi, 4(%rdi)
	cmpq	%r12, %rax
	jb	.L3
	movq	%rdi, %rbx
	leaq	0(,%r12,4), %rdi
	movq	%r12, %rbp
	call	_Znam@PLT
	decl	%ebp
	leaq	0(,%r12,8), %rdi
	movq	%rax, 8(%rbx)
	xorl	%eax, %eax
	movl	%ebp, 16(%rbx)
	movl	%eax, 20(%rbx)
	call	_Znam@PLT
	leaq	32(%rbx), %rdi
	leaq	48(%rsp), %rsi
	xorl	%edx, %edx
	movq	%rax, 24(%rbx)
	movl	$12, %ecx
	rep movsl
	movq	$0x000000000, 80(%rbx)
	movq	%rdx, 8(%rsp)
	movq	8(%rsp), %rdi
	addq	$16, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	free@PLT
.L3:
	.cfi_restore_state
	call	__cxa_throw_bad_array_new_length@PLT
	.cfi_endproc
.LFE2242:
	.size	_ZN6AntGPUC2Eii17curandStateXORWOW, .-_ZN6AntGPUC2Eii17curandStateXORWOW
	.globl	_ZN6AntGPUC1Eii17curandStateXORWOW
	.set	_ZN6AntGPUC1Eii17curandStateXORWOW,_ZN6AntGPUC2Eii17curandStateXORWOW
	.align 2
	.globl	_ZN6AntGPU7VentureEPiPKdS2_idd
	.type	_ZN6AntGPU7VentureEPiPKdS2_idd, @function
_ZN6AntGPU7VentureEPiPKdS2_idd:
.LFB2244:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2244:
	.size	_ZN6AntGPU7VentureEPiPKdS2_idd, .-_ZN6AntGPU7VentureEPiPKdS2_idd
	.align 2
	.globl	_ZN6AntGPU13SelectNextHopEPKdS1_idd
	.type	_ZN6AntGPU13SelectNextHopEPKdS1_idd, @function
_ZN6AntGPU13SelectNextHopEPKdS1_idd:
.LFB2245:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2245:
	.size	_ZN6AntGPU13SelectNextHopEPKdS1_idd, .-_ZN6AntGPU13SelectNextHopEPKdS1_idd
	.align 2
	.globl	_ZN6AntGPU4GoToEiPiPKdi
	.type	_ZN6AntGPU4GoToEiPiPKdi, @function
_ZN6AntGPU4GoToEiPiPKdi:
.LFB2246:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2246:
	.size	_ZN6AntGPU4GoToEiPiPKdi, .-_ZN6AntGPU4GoToEiPiPKdi
	.align 2
	.globl	_ZN6AntGPU5ResetEPiii
	.type	_ZN6AntGPU5ResetEPiii, @function
_ZN6AntGPU5ResetEPiii:
.LFB2247:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2247:
	.size	_ZN6AntGPU5ResetEPiii, .-_ZN6AntGPU5ResetEPiii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"precalc_xorwow_matrix"
.LC2:
	.string	"precalc_xorwow_offset_matrix"
.LC3:
	.string	"mrg32k3aM1"
.LC4:
	.string	"mrg32k3aM2"
.LC5:
	.string	"mrg32k3aM1SubSeq"
.LC6:
	.string	"mrg32k3aM2SubSeq"
.LC7:
	.string	"mrg32k3aM1Seq"
.LC8:
	.string	"mrg32k3aM2Seq"
.LC9:
	.string	"__cr_lgamma_table"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2273:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	movl	$102400, %r9d
	xorl	%r8d, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC1(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	movq	%rdx, %rcx
	leaq	_ZL21precalc_xorwow_matrix(%rip), %rsi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterVar@PLT
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC2(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$102400, %r9d
	leaq	_ZL28precalc_xorwow_offset_matrix(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%rcx
	.cfi_def_cfa_offset 24
	popq	%rsi
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC3(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$2304, %r9d
	leaq	_ZL10mrg32k3aM1(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%rdi
	.cfi_def_cfa_offset 24
	popq	%r8
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC4(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$2304, %r9d
	leaq	_ZL10mrg32k3aM2(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%r9
	.cfi_def_cfa_offset 24
	popq	%r10
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC5(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$2016, %r9d
	leaq	_ZL16mrg32k3aM1SubSeq(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%r11
	.cfi_def_cfa_offset 24
	popq	%rax
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC6(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$2016, %r9d
	leaq	_ZL16mrg32k3aM2SubSeq(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC7(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$2304, %r9d
	leaq	_ZL13mrg32k3aM1Seq(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%rcx
	.cfi_def_cfa_offset 24
	popq	%rsi
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC8(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$2304, %r9d
	leaq	_ZL13mrg32k3aM2Seq(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%rdi
	.cfi_def_cfa_offset 24
	popq	%r8
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$1
	.cfi_def_cfa_offset 32
	leaq	.LC9(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movl	$72, %r9d
	movq	%rdx, %rcx
	leaq	_ZL17__cr_lgamma_table(%rip), %rsi
	call	__cudaRegisterVar@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	popq	%r9
	.cfi_def_cfa_offset 24
	popq	%r10
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2273:
	.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
	.local	_ZL17__cr_lgamma_table
	.comm	_ZL17__cr_lgamma_table,72,32
	.local	_ZL13mrg32k3aM2Seq
	.comm	_ZL13mrg32k3aM2Seq,2304,32
	.local	_ZL13mrg32k3aM1Seq
	.comm	_ZL13mrg32k3aM1Seq,2304,32
	.local	_ZL16mrg32k3aM2SubSeq
	.comm	_ZL16mrg32k3aM2SubSeq,2016,32
	.local	_ZL16mrg32k3aM1SubSeq
	.comm	_ZL16mrg32k3aM1SubSeq,2016,32
	.local	_ZL10mrg32k3aM2
	.comm	_ZL10mrg32k3aM2,2304,32
	.local	_ZL10mrg32k3aM1
	.comm	_ZL10mrg32k3aM1,2304,32
	.local	_ZL28precalc_xorwow_offset_matrix
	.comm	_ZL28precalc_xorwow_offset_matrix,102400,32
	.local	_ZL21precalc_xorwow_matrix
	.comm	_ZL21precalc_xorwow_matrix,102400,32
	.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>


#include <hiprand/hiprand_kernel.h>

class AntGPU
{
	int routeIndex{};
	int position{};
	int* possibleLocations{};
	int possibleLocationsLastIndex{};
	int possibleLocationsNextIndex{};
	double* goodnessNumerators{};

	hiprandState_t m_randomState{};

public:
	__device__
		AntGPU(int initialLocation, int matrixDim, hiprandState_t randState);

	AntGPU() = default;

	__device__
		void Venture(int* route, const double* distanceMatrix, const double* pheromoneMatrix, int matrixDim, double alpha, double beta);

	__device__
		void Reset(int* route, int initialLocation, int matrixDim);

	double distance = 0;
private:
	__device__
		int SelectNextHop(const double* distance_matrix, const double* pheromoneMatrix, int matrixDim, double alpha, double beta);

	__device__
		void GoTo(int next, int* route, const double* distanceMatrix, int matrixDim);
};


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