#include "cuda_part.cuh"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>

#ifdef __CUDACC__
#define KERNEL_ARGS2(grid,block) <<< grid , block >>>
#else
#define KERNEL_ARGS2(grid, block)
#endif


__global__
void cuda_run(void *arr, size_t pitch) {
	int id = ((int)blockIdx.x) * ((int)blockDim.x) + (int)(threadIdx.x);
	int x = id / 5;
	int y = id % 5;
	float a, b;

	if (id < 25) {
		float *row = (float *)((char *)arr + y * pitch);
		a = row[x];
		
		row[x] += 1;
		b = row[x];
		
		printf("bye %d %f %f\n", id, a, b);
	}
	

}


void setup(float **h_arr) {

	

	void *d_arr;
	size_t pitch;
	cudaError_t e;
	e = cudaMallocPitch(&d_arr, &pitch, 5 * sizeof(float), 5 );
	if (e != cudaSuccess) {
		std::cout << "Memalloc fail\n";
	}
	std::cout << pitch << "\n";

	e = cudaMemcpy2D(d_arr, pitch, *h_arr, 5 * sizeof(float), 5 * sizeof(float) , 5 , cudaMemcpyHostToDevice);
	//e = cudaMemset2D(d_arr, pitch, 1, 5 * sizeof(float), 5);
	if (e != cudaSuccess) {
		std::cout << "Memcpy fail\n";
	}

	cuda_run KERNEL_ARGS2(1, 64) (d_arr,pitch);

	e = cudaMemcpy2D(*h_arr, 5 * sizeof(float), d_arr, pitch, 5 * sizeof(float), 5 , cudaMemcpyDeviceToHost);
	if (e != cudaSuccess) {
		std::cout << "2nd Memcpy fail\n";
	}
	cudaFree(d_arr);

}
	.file	"tmpxft_00255bc4_00000000-6_cuda_part.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3638:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3638:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z28__device_stub__Z8cuda_runPvmPvm
	.type	_Z28__device_stub__Z8cuda_runPvmPvm, @function
_Z28__device_stub__Z8cuda_runPvmPvm:
.LFB3660:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movq	%rsi, (%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z8cuda_runPvm(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	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 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L2:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3660:
	.size	_Z28__device_stub__Z8cuda_runPvmPvm, .-_Z28__device_stub__Z8cuda_runPvmPvm
	.globl	_Z8cuda_runPvm
	.type	_Z8cuda_runPvm, @function
_Z8cuda_runPvm:
.LFB3661:
	.cfi_startproc
	endbr64
	jmp	_Z28__device_stub__Z8cuda_runPvmPvm
	.cfi_endproc
.LFE3661:
	.size	_Z8cuda_runPvm, .-_Z8cuda_runPvm
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Memalloc fail\n"
.LC1:
	.string	"\n"
.LC2:
	.string	"Memcpy fail\n"
.LC3:
	.string	"2nd Memcpy fail\n"
	.text
	.globl	_Z5setupPPf
	.type	_Z5setupPPf, @function
_Z5setupPPf:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	$5, %ecx
	movq	%rdi, %rbp
	movl	$20, %edx
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	leaq	_ZSt4cout(%rip), %rbx
	subq	$56, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rsi
	movq	%rsp, %rdi
	call	cudaMallocPitch@PLT
	testl	%eax, %eax
	je	.L9
	movl	$14, %edx
	leaq	.LC0(%rip), %rsi
	movq	%rbx, %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
.L9:
	movq	8(%rsp), %rsi
	movq	%rbx, %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	leaq	.LC1(%rip), %rsi
	movl	$1, %edx
	movq	%rax, %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
	movl	$20, %r8d
	movl	$5, %r9d
	movl	$20, %ecx
	pushq	%rsi
	.cfi_def_cfa_offset 88
	movq	0(%rbp), %rdx
	pushq	$1
	.cfi_def_cfa_offset 96
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdi
	call	cudaMemcpy2D@PLT
	popq	%rdi
	.cfi_def_cfa_offset 88
	popq	%r8
	.cfi_def_cfa_offset 80
	testl	%eax, %eax
	je	.L10
	movl	$12, %edx
	leaq	.LC2(%rip), %rsi
	movq	%rbx, %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
.L10:
	movl	$67108865, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1, %ecx
	salq	$6, %rdx
	movl	$1, %esi
	movabsq	$4294967297, %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L11
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdi
	call	_Z28__device_stub__Z8cuda_runPvmPvm
.L11:
	pushq	%rax
	.cfi_def_cfa_offset 88
	movq	0(%rbp), %rdi
	movl	$5, %r9d
	movl	$20, %r8d
	pushq	$2
	.cfi_def_cfa_offset 96
	movq	24(%rsp), %rcx
	movl	$20, %esi
	movq	16(%rsp), %rdx
	call	cudaMemcpy2D@PLT
	popq	%rdx
	.cfi_def_cfa_offset 88
	popq	%rcx
	.cfi_def_cfa_offset 80
	testl	%eax, %eax
	je	.L12
	movl	$16, %edx
	leaq	.LC3(%rip), %rsi
	movq	%rbx, %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
.L12:
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$56, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3635:
	.size	_Z5setupPPf, .-_Z5setupPPf
	.section	.rodata.str1.1
.LC4:
	.string	"_Z8cuda_runPvm"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3663:
	.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	.LC4(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z8cuda_runPvm(%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
.LFE3663:
	.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>


void setup(float **h_arr);
	.text
	.file	"cuda_part.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
