#include "GpuFocalProcessing.cuh"
#include "GpuTimer.cuh"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <cmath>

using namespace winGpu;

__global__ void applyFocalOpGpu(FocalRasterGpu rasterInput, FocalRasterGpu rasterOutput, FocalKernelGpu kernel, int rowIter)
{
	int h = blockDim.x * blockIdx.x + threadIdx.x + rowIter;
	int w = blockDim.y * blockIdx.y + threadIdx.y;
	if (rasterInput.height <= h || rasterInput.width <= w)
	{
		return;
	}

	if (rasterInput(h, w) == rasterInput.defaultValue)
	{
		rasterOutput(h, w) = rasterInput(h, w);
		return;
	}
	double sum = 0.0;
	for (int i = 0; i < kernel.sideSize; ++i)
	{
		for (int j = 0; j < kernel.sideSize; ++j)
		{
			pixel value = rasterInput(h + (i - kernel.midSize), w + (j - kernel.midSize));
			if (value == rasterInput.defaultValue)
			{
				rasterOutput(h, w) = rasterInput(h, w);
				return;
			}
			sum += kernel[i][j] * value;
		}
	}
	if (sum <= 0)
	{
		sum = 0.0;
	}
	rasterOutput(h, w) = (pixel)sum;
}

double winGpu::doFocalOpGpu(pixel* input, int height, int width, pixel* output, std::vector<double> matrix)
{
	//  Rater   
	FocalRasterGpu rasterInput;
	rasterInput.height = height;
	rasterInput.width = width;
	rasterInput.data = 0;

	//  Rater   
	FocalRasterGpu rasterOutput;
	rasterOutput.height = height;
	rasterOutput.width = width;
	rasterOutput.data = 0;

	//  Kernel    
	FocalKernelGpu kernelTemp;
	kernelTemp.sideSize = (int)std::sqrt(matrix.size());
	kernelTemp.ker = matrix.data();
	kernelTemp.midSize = kernelTemp.sideSize / 2;

	FocalKernelGpu kernel;
	kernel.sideSize = kernelTemp.sideSize;
	kernel.midSize = kernelTemp.midSize;
	kernel.ker = 0;

	cudaSetDevice(0);
	cudaMalloc((void**)&rasterInput.data, rasterInput.size());
	cudaMalloc((void**)&rasterOutput.data, rasterOutput.size());
	cudaMalloc((void**)&kernel.ker, kernel.size());
	cudaMemcpy(rasterInput.data, input, rasterInput.size(), cudaMemcpyHostToDevice);
	cudaMemcpy(kernel.ker, kernelTemp.ker, kernel.size(), cudaMemcpyHostToDevice);

	const size_t maxAvaliableCoords = 8000000;
	int countRowsPerIter = maxAvaliableCoords / width;
	int countIter = height / countRowsPerIter + 1;
	const size_t size = width * countRowsPerIter;
	dim3 threadsPerBlock(16, 16);
	dim3 numBlocks(countRowsPerIter / threadsPerBlock.x + 1, width / threadsPerBlock.y + 1);

	float time;
	GPU_TIMER_START;
	for (int i = 0; i < countIter; i++)
	{
		int rowIter = i * countRowsPerIter;
		applyFocalOpGpu << <numBlocks, threadsPerBlock >> > (rasterInput, rasterOutput, kernel, rowIter);
		cudaDeviceSynchronize();
		int k = 5;
	}
	GPU_TIMER_STOP(time);

	cudaMemcpy(output, rasterOutput.data, rasterOutput.size(), cudaMemcpyDeviceToHost);

	cudaFree(rasterInput.data);
	cudaFree(rasterOutput.data);
	cudaFree(kernel.ker);

	return (double)time;
}

	.file	"tmpxft_0020dc57_00000000-6_GpuFocalProcessing.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2908:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2908:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z82__device_stub__Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEiRN6winGpu14FocalRasterGpuES1_RNS_14FocalKernelGpuEi
	.type	_Z82__device_stub__Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEiRN6winGpu14FocalRasterGpuES1_RNS_14FocalKernelGpuEi, @function
_Z82__device_stub__Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEiRN6winGpu14FocalRasterGpuES1_RNS_14FocalKernelGpuEi:
.LFB2930:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movl	%ecx, 12(%rsp)
	leaq	32(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	12(%rsp), %rax
	movq	%rdi, 88(%rsp)
	leaq	40(%rsp), %rdi
	movq	%rsi, 96(%rsp)
	leaq	52(%rsp), %rsi
	movq	%rdx, 104(%rsp)
	leaq	24(%rsp), %rdx
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movl	$1, 48(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	32(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEi(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 160
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L2:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2930:
	.size	_Z82__device_stub__Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEiRN6winGpu14FocalRasterGpuES1_RNS_14FocalKernelGpuEi, .-_Z82__device_stub__Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEiRN6winGpu14FocalRasterGpuES1_RNS_14FocalKernelGpuEi
	.globl	_Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEi
	.type	_Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEi, @function
_Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEi:
.LFB2931:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	%edx, %ecx
	movq	%rdi, (%rsp)
	movq	%rsp, %rdx
	leaq	32(%rsp), %rdi
	movq	%rsi, 8(%rsp)
	leaq	56(%rsp), %rsi
	call	_Z82__device_stub__Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEiRN6winGpu14FocalRasterGpuES1_RNS_14FocalKernelGpuEi
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2931:
	.size	_Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEi, .-_Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEi
	.globl	_ZN6winGpu12doFocalOpGpuEPsiiS0_St6vectorIdSaIdEE
	.type	_ZN6winGpu12doFocalOpGpuEPsiiS0_St6vectorIdSaIdEE, @function
_ZN6winGpu12doFocalOpGpuEPsiiS0_St6vectorIdSaIdEE:
.LFB2902:
	.cfi_startproc
	endbr64
	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
	movl	%edx, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rcx, %rbp
	xorl	%ecx, %ecx
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%esi, %ebx
	subq	$184, %rsp
	.cfi_def_cfa_offset 240
	movq	(%r8), %r14
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	movl	%edx, 76(%rsp)
	movl	%edx, 100(%rsp)
	movq	8(%r8), %rdx
	movl	%esi, 72(%rsp)
	subq	%r14, %rdx
	movl	%esi, 96(%rsp)
	xorl	%esi, %esi
	sarq	$3, %rdx
	movl	$-655238927, 80(%rsp)
	movq	%rcx, 88(%rsp)
	movl	$-655238927, 104(%rsp)
	movq	%rsi, 112(%rsp)
	js	.L10
	cvtsi2sdq	%rdx, %xmm0
	jmp	.L11
.L10:
	movq	%rdx, %rax
	andl	$1, %edx
	shrq	%rax
	orq	%rdx, %rax
	cvtsi2sdq	%rax, %xmm0
	addsd	%xmm0, %xmm0
.L11:
	call	sqrt@PLT
	movl	$2, %ecx
	xorl	%edi, %edi
	movabsq	$68719476752, %r15
	cvttsd2sil	%xmm0, %eax
	cltd
	movl	%eax, 40(%rsp)
	idivl	%ecx
	movl	%eax, 44(%rsp)
	xorl	%eax, %eax
	movq	%rax, 48(%rsp)
	call	cudaSetDevice@PLT
	movl	76(%rsp), %esi
	imull	72(%rsp), %esi
	leaq	88(%rsp), %rdi
	movslq	%esi, %rsi
	addq	%rsi, %rsi
	call	cudaMalloc@PLT
	movl	96(%rsp), %esi
	imull	100(%rsp), %esi
	leaq	112(%rsp), %rdi
	movslq	%esi, %rsi
	addq	%rsi, %rsi
	call	cudaMalloc@PLT
	movl	40(%rsp), %esi
	leaq	48(%rsp), %rdi
	imull	%esi, %esi
	movslq	%esi, %rsi
	salq	$3, %rsi
	call	cudaMalloc@PLT
	movl	72(%rsp), %edx
	imull	76(%rsp), %edx
	movq	%r12, %rsi
	movq	88(%rsp), %rdi
	movl	$1, %ecx
	movslq	%edx, %rdx
	addq	%rdx, %rdx
	call	cudaMemcpy@PLT
	movl	40(%rsp), %edx
	movq	48(%rsp), %rdi
	movq	%r14, %rsi
	movl	$1, %ecx
	imull	%edx, %edx
	movslq	%edx, %rdx
	salq	$3, %rdx
	call	cudaMemcpy@PLT
	movslq	%r13d, %rsi
	xorl	%edx, %edx
	movl	$8000000, %eax
	shrl	$4, %r13d
	movq	%rsp, %rdi
	movl	$1, 24(%rsp)
	divq	%rsi
	movl	$1, 36(%rsp)
	incl	%r13d
	movl	%r13d, 32(%rsp)
	movq	%rax, %rcx
	movl	%ebx, %eax
	cltd
	movl	%ecx, %r12d
	idivl	%ecx
	shrl	$4, %ecx
	incl	%ecx
	movl	%ecx, 28(%rsp)
	movl	%eax, %ebx
	call	cudaEventCreate@PLT
	leaq	8(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	xorl	%r14d, %r14d
	xorl	%r13d, %r13d
.L12:
	cmpl	%r13d, %ebx
	jl	.L17
	movl	24(%rsp), %ecx
	movq	28(%rsp), %rdi
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	36(%rsp), %esi
	movq	%r15, %rdx
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L13
	movq	88(%rsp), %rax
	movups	72(%rsp), %xmm1
	leaq	56(%rsp), %rdx
	movl	%r14d, %ecx
	movups	96(%rsp), %xmm2
	movups	40(%rsp), %xmm3
	leaq	120(%rsp), %rdi
	leaq	144(%rsp), %rsi
	movq	%rax, 136(%rsp)
	movq	112(%rsp), %rax
	movups	%xmm1, 120(%rsp)
	movq	%rax, 160(%rsp)
	movups	%xmm2, 144(%rsp)
	movups	%xmm3, 56(%rsp)
	call	_Z82__device_stub__Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEiRN6winGpu14FocalRasterGpuES1_RNS_14FocalKernelGpuEi
.L13:
	call	cudaDeviceSynchronize@PLT
	incl	%r13d
	addl	%r12d, %r14d
	jmp	.L12
.L17:
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	8(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	8(%rsp), %rdx
	movq	(%rsp), %rsi
	leaq	144(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movl	96(%rsp), %edx
	imull	100(%rsp), %edx
	movq	%rbp, %rdi
	movq	112(%rsp), %rsi
	movl	$2, %ecx
	movslq	%edx, %rdx
	addq	%rdx, %rdx
	call	cudaMemcpy@PLT
	movq	88(%rsp), %rdi
	call	cudaFree@PLT
	movq	112(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	cvtss2sd	144(%rsp), %xmm0
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	addq	$184, %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
.LFE2902:
	.size	_ZN6winGpu12doFocalOpGpuEPsiiS0_St6vectorIdSaIdEE, .-_ZN6winGpu12doFocalOpGpuEPsiiS0_St6vectorIdSaIdEE
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEi"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2933:
	.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	_Z15applyFocalOpGpuN6winGpu14FocalRasterGpuES0_NS_14FocalKernelGpuEi(%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
.LFE2933:
	.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"

#include <stdio.h>
#include <vector>

typedef short int pixel;

namespace winGpu
{
	struct FocalKernelGpu
	{
		int sideSize;
		int midSize;
		double* ker;
		__host__ __device__ const double* operator[] (int i) const { return ker + i * sideSize; }
		__host__ __device__ double* operator[] (int i) { return ker + i * sideSize; }
		__host__ __device__ size_t size() const { return sideSize * sideSize * sizeof(double); }
	};

	struct FocalRasterGpu
	{
		int height, width;
		const pixel defaultValueConst = -9999;
		pixel defaultValue = -9999;
		pixel* data;
		__host__ __device__ pixel& operator() (int h, int w)
		{
			if (0 <= h && h < height &&
				0 <= w && w < width)
			{
				return *(data + h * width + w);
			}
			else
			{
				return defaultValue;
			}
		}
		__host__ __device__ const pixel& operator() (int h, int w) const
		{
			if (0 <= h && h < height &&
				0 <= w && w < width)
			{
				return *(data + h * width + w);
			}
			else
			{
				return defaultValueConst;
			}
		}
		__host__ __device__ size_t size() const { return height * width * sizeof(pixel); }
	};

	double doFocalOpGpu(pixel* input, int height, int width, pixel* output, std::vector<double> matrix);
}

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