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

namespace SIMPLE
{
	__device__
		Ant::Ant(int initialLocation, int matrixDim, curandState_t randState) :
		visitedIndex(0),
		isVisited(new bool[matrixDim]),
		position(initialLocation),
		goodnessNumerators(new double[matrixDim]),
		m_randomState(randState)
	{
	}

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

	__device__
		int Ant::SelectNextHop(const double* distance_matrix, const double* pheromoneMatrix, int matrixDim, double alpha, double beta)
	{
		double denominator = 0;
		for (int i = 0; i < matrixDim; ++i)
		{
			if (isVisited[i]) { continue; }
			int possiblePosition = i;

			double goodnessNumerator = pow(pheromoneMatrix[position * matrixDim + possiblePosition], alpha) * pow(1.0 / distance_matrix[position * matrixDim + possiblePosition], beta);

			goodnessNumerators[possiblePosition] = goodnessNumerator;
			denominator += goodnessNumerator;
		}

		//New
		/*
		for (int i = 0; i < matrixDim; ++i)
		{
			if (isVisited[i]) { continue; }
			goodnessNumerators[i] /= denominator;
		}

		double random = curand_uniform_double(&m_randomState);

		for (int i = 0; i < matrixDim; ++i)
		{
			if (isVisited[i]) { continue; }
			random -= goodnessNumerators[i];
			if (random <= 0) { return i; }
			
		}
		return -1;
		*/
		

		double sum = 0;
		double random = curand_uniform_double(&m_randomState);
		//printf("Random is %f\n", random);
		
		for (int i = 0; i < matrixDim; ++i)
		{
			if (isVisited[i]) { continue; }

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

	__device__
		void Ant::GoTo(int next, int* route, const double* distanceMatrix, int matrixDim)
	{
		route[visitedIndex++] = next;
		isVisited[next] = true;
		distance += distanceMatrix[position * matrixDim + next];
		position = next;
	}

	__device__
		void Ant::Reset(int* route, int initialLocation, int matrixDim)
	{
		visitedIndex = 0;
		distance = 0;
		position = initialLocation;
		for (int i = 0; i < matrixDim; ++i) { isVisited[i] = false; }
		isVisited[position] = true;
		route[visitedIndex++] = initialLocation;
	}


}



	.file	"tmpxft_002e2e3f_00000000-6_AntSimple.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	_ZN6SIMPLE3AntC2Eii17curandStateXORWOW
	.type	_ZN6SIMPLE3AntC2Eii17curandStateXORWOW, @function
_ZN6SIMPLE3AntC2Eii17curandStateXORWOW:
.LFB2242:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	xorl	%r9d, %r9d
	movl	%esi, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movslq	%edx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	%rdi, %rbx
	subq	$16, %rsp
	.cfi_def_cfa_offset 48
	movl	%r9d, (%rdi)
	movq	%rbp, %rdi
	call	_Znam@PLT
	movl	%r12d, 16(%rbx)
	movq	%rax, 8(%rbx)
	movabsq	$1152921504606846975, %rax
	cmpq	%rbp, %rax
	jb	.L3
	leaq	0(,%rbp,8), %rdi
	call	_Znam@PLT
	leaq	32(%rbx), %rdi
	leaq	48(%rsp), %rsi
	movl	$12, %ecx
	movq	%rax, 24(%rbx)
	xorl	%eax, %eax
	rep movsl
	movq	$0x000000000, 80(%rbx)
	movq	%rax, 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	_ZN6SIMPLE3AntC2Eii17curandStateXORWOW, .-_ZN6SIMPLE3AntC2Eii17curandStateXORWOW
	.globl	_ZN6SIMPLE3AntC1Eii17curandStateXORWOW
	.set	_ZN6SIMPLE3AntC1Eii17curandStateXORWOW,_ZN6SIMPLE3AntC2Eii17curandStateXORWOW
	.align 2
	.globl	_ZN6SIMPLE3Ant7VentureEPiPKdS3_idd
	.type	_ZN6SIMPLE3Ant7VentureEPiPKdS3_idd, @function
_ZN6SIMPLE3Ant7VentureEPiPKdS3_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	_ZN6SIMPLE3Ant7VentureEPiPKdS3_idd, .-_ZN6SIMPLE3Ant7VentureEPiPKdS3_idd
	.align 2
	.globl	_ZN6SIMPLE3Ant13SelectNextHopEPKdS2_idd
	.type	_ZN6SIMPLE3Ant13SelectNextHopEPKdS2_idd, @function
_ZN6SIMPLE3Ant13SelectNextHopEPKdS2_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	_ZN6SIMPLE3Ant13SelectNextHopEPKdS2_idd, .-_ZN6SIMPLE3Ant13SelectNextHopEPKdS2_idd
	.align 2
	.globl	_ZN6SIMPLE3Ant4GoToEiPiPKdi
	.type	_ZN6SIMPLE3Ant4GoToEiPiPKdi, @function
_ZN6SIMPLE3Ant4GoToEiPiPKdi:
.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	_ZN6SIMPLE3Ant4GoToEiPiPKdi, .-_ZN6SIMPLE3Ant4GoToEiPiPKdi
	.align 2
	.globl	_ZN6SIMPLE3Ant5ResetEPiii
	.type	_ZN6SIMPLE3Ant5ResetEPiii, @function
_ZN6SIMPLE3Ant5ResetEPiii:
.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	_ZN6SIMPLE3Ant5ResetEPiii, .-_ZN6SIMPLE3Ant5ResetEPiii
	.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>

namespace SIMPLE
{
	class Ant
	{
		int visitedIndex{};
		bool* isVisited{};
		int position{};

		double* goodnessNumerators{};

		hiprandState_t m_randomState{};

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

		Ant() = 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	"AntSimple.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
