#include "3d-test.cuh"
#include<iostream>
#include<stdio.h>

__host__ __device__
 void scalingFunction(int array[]) {
        

    for(int i=0; i<8; i++) {
        array[i] = array[i] * 2.0;
    }


}

__host__ __device__
void  distributeFunction(pencilComputation& p1,int x,int y){
  
      for(int z=0; z<8; z++) {
        p1.outputMatrix[x][y][z] = p1.pencilVector[z];
      }

}

__host__ __device__
void tOPrint(int i,int j,int k) {

 //pencilComputation p2;  
  printf("The value of i:%d,  j:%d, inputMatrix:%d, pencilVector:%d",i,j);//,p2.inputMatrix[i][j][k],p2.pencilVector[k]);

}

__global__ 
void pencilComputationFunction(pencilComputation& p1){
 
//pencilComputation p1;
//  auto p1 = *p1_in; // dereference the pointer
  p1.DStats("At start of pencilComputationFunction call");

  int i = threadIdx.x ;//+ blockIdx.x * blockDim.x;
  int j = threadIdx.y ; // + blockIdx.y * blockDim.y;
  int z = threadIdx.z;
  for(int k=0; k<8 ; k++) {
    p1.pencilVector[k] = p1.inputMatrix[i][j][k];
  }
  scalingFunction(p1.pencilVector);
  p1.DStats("After scalingFunction in pencilComputationFunction call");
  distributeFunction(p1,i,j);
  p1.DStats("At END of pencilComputationFunction call");
// printf("**************************************\n");

}

void pencilComputation::launcher(){
    
//
//    for(int i=0; i<2; i++) {
//      for(int j=0; j<2; j++) {
//          for(int k=0; k<2; k++) {
//              inputMatrix[i][j][k] = 10;
//          }
//      }
//  }

    dim3 grid(1,1,1);
    dim3 block(2,2,2);

   // #pragma acc enter data copyin(inputMatrix) copyout(outputMatrix)
    //pencilComputationFunction<<<grid,block>>>(*this);
    pencilComputationFunction<<<1,1>>>(*this);
   // pencilComputationFunction<<<1,1>>>();
    cudaDeviceSynchronize();
   

}

__host__ 
void pencilComputation::CStats(const char* message) {
  // Get sum, average and num-nonzero for each of inputMatrix & outputMatrix:
  printf("[CPU] Stats: %s\n", message);
  printf("               Sum      Average      NNZ\n");
  int sumInput = 0;
  int sumOutput = 0;
  int nnzInput = 0;
  int nnzOutput = 0;
  for (int i = 0; i < matrix_size; i++) {
    for (int j = 0; j < matrix_size; j++) {
      for (int k = 0; k < matrix_size; k++) {
        if (inputMatrix[i][j][k] != 0) { nnzInput++; }
        if (outputMatrix[i][j][k] != 0) { nnzOutput++; }
        sumInput += inputMatrix[i][j][k];
        sumOutput += outputMatrix[i][j][k];
      }
    }
  }

  double averageInput  = sumInput  / (double)(matrix_size * matrix_size * matrix_size);
  double averageOutput = sumOutput / (double)(matrix_size * matrix_size * matrix_size);
  printf("  inputMatrix    %d       %g           %d\n", sumInput, averageInput, nnzInput);
  printf("  outputMatrix   %d       %g           %d\n", sumOutput, averageOutput, nnzOutput);

  int sumVector = 0;
  int nnzVector = 0;
  for (int i = 0; i < (matrix_size * matrix_size * matrix_size); i++) {
    sumVector += pencilVector[i];
    if (pencilVector[i] != 0) { nnzVector++; }
  }
  double averageVector = sumVector / (double)(matrix_size * matrix_size * matrix_size);
  printf("  pencilVector   %d       %g           %d\n", sumVector, averageVector, nnzVector);
  printf("\n");
}

__device__
void pencilComputation::DStats(const char* message) {
  // Get sum, average and num-nonzero for each of inputMatrix & outputMatrix:
  printf("[Device] Matrix Stats: %s\n", message);
  printf("               Sum      Average      NNZ\n");
  int sumInput = 0;
  int sumOutput = 0;
  int nnzInput = 0;
  int nnzOutput = 0;
  for (int i = 0; i < matrix_size; i++) {
    for (int j = 0; j < matrix_size; j++) {
      for (int k = 0; k < matrix_size; k++) {
        if (inputMatrix[i][j][k] != 0) { nnzInput++; }
        if (outputMatrix[i][j][k] != 0) { nnzOutput++; }
        sumInput += inputMatrix[i][j][k];
        sumOutput += outputMatrix[i][j][k];
      }
    }
  }

  double averageInput  = sumInput  / (double)(matrix_size * matrix_size * matrix_size);
  double averageOutput = sumOutput / (double)(matrix_size * matrix_size * matrix_size);
  printf("  inputMatrix    %d       %g           %d\n", sumInput, averageInput, nnzInput);
  printf("  outputMatrix   %d       %g           %d\n", sumOutput, averageOutput, nnzOutput);
  int sumVector = 0;
  int nnzVector = 0;
  for (int i = 0; i < (matrix_size * matrix_size * matrix_size); i++) {
    sumVector += pencilVector[i];
    if (pencilVector[i] != 0) { nnzVector++; }
  }
  double averageVector = sumVector / (double)(matrix_size * matrix_size * matrix_size);
  printf("  pencilVector   %d       %g           %d\n", sumVector, averageVector, nnzVector);
  printf("\n");
}

	.file	"tmpxft_0023c499_00000000-6_3d-test.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3643:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3643:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z15scalingFunctionPi
	.type	_Z15scalingFunctionPi, @function
_Z15scalingFunctionPi:
.LFB3635:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L3:
	cvtsi2sdl	(%rdi,%rax,4), %xmm0
	addsd	%xmm0, %xmm0
	cvttsd2sil	%xmm0, %edx
	movl	%edx, (%rdi,%rax,4)
	incq	%rax
	cmpq	$8, %rax
	jne	.L3
	ret
	.cfi_endproc
.LFE3635:
	.size	_Z15scalingFunctionPi, .-_Z15scalingFunctionPi
	.globl	_Z18distributeFunctionR17pencilComputationii
	.type	_Z18distributeFunctionR17pencilComputationii, @function
_Z18distributeFunctionR17pencilComputationii:
.LFB3636:
	.cfi_startproc
	endbr64
	movslq	%esi, %rsi
	movslq	%edx, %rdx
	leaq	64(%rdi), %rax
	addq	%rsi, %rsi
	leaq	4(%rsi,%rdx), %rdx
	movq	%rax, %rsi
	leaq	(%rdi,%rdx,8), %rdi
	movl	$32, %edx
	jmp	memmove@PLT
	.cfi_endproc
.LFE3636:
	.size	_Z18distributeFunctionR17pencilComputationii, .-_Z18distributeFunctionR17pencilComputationii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"The value of i:%d,  j:%d, inputMatrix:%d, pencilVector:%d"
	.text
	.globl	_Z7tOPrintiii
	.type	_Z7tOPrintiii, @function
_Z7tOPrintiii:
.LFB3637:
	.cfi_startproc
	endbr64
	movl	%edi, %edx
	movl	%esi, %ecx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC0(%rip), %rsi
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE3637:
	.size	_Z7tOPrintiii, .-_Z7tOPrintiii
	.section	.rodata.str1.1
.LC1:
	.string	"[CPU] Stats: %s\n"
.LC2:
	.string	"               Sum      Average      NNZ\n"
.LC4:
	.string	"  inputMatrix    %d       %g           %d\n"
.LC5:
	.string	"  outputMatrix   %d       %g           %d\n"
.LC6:
	.string	"  pencilVector   %d       %g           %d\n"
.LC7:
	.string	"\n"
	.text
	.align 2
	.globl	_ZN17pencilComputation6CStatsEPKc
	.type	_ZN17pencilComputation6CStatsEPKc, @function
_ZN17pencilComputation6CStatsEPKc:
.LFB3639:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movq	%rsi, %rdx
	xorl	%eax, %eax
	leaq	.LC1(%rip), %rsi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rdi, %rbp
	movl	$2, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rcx
	.cfi_def_cfa_offset 48
	call	__printf_chk@PLT
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbp, %rsi
	leaq	32(%rbp), %r10
	xorl	%ecx, %ecx
	xorl	%edx, %edx
.L9:
	movq	%rsi, %rax
	movl	$2, %r9d
.L14:
	movl	(%rax), %r8d
	movl	32(%rax), %edi
	cmpl	$1, %r8d
	sbbl	$-1, %ecx
	cmpl	$1, %edi
	sbbl	$-1, %r12d
	addl	%edx, %r8d
	movl	4(%rax), %edx
	addl	%ebx, %edi
	movl	36(%rax), %ebx
	cmpl	$1, %edx
	sbbl	$-1, %ecx
	cmpl	$1, %ebx
	sbbl	$-1, %r12d
	addl	%r8d, %edx
	addl	%edi, %ebx
	addq	$8, %rax
	decl	%r9d
	je	.L31
	movl	$1, %r9d
	jmp	.L14
.L31:
	addq	$16, %rsi
	cmpq	%rsi, %r10
	jne	.L9
	cvtsi2sdl	%ebx, %xmm0
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	mulsd	.LC3(%rip), %xmm0
	movq	%xmm0, %r14
	cvtsi2sdl	%edx, %xmm0
	mulsd	.LC3(%rip), %xmm0
	call	__printf_chk@PLT
	movl	%r12d, %ecx
	movl	%ebx, %edx
	movq	%r14, %xmm0
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	xorl	%eax, %eax
	xorl	%ecx, %ecx
	xorl	%edx, %edx
.L17:
	movl	64(%rbp,%rax), %esi
	addl	%esi, %edx
	cmpl	$1, %esi
	sbbl	$-1, %ecx
	addq	$4, %rax
	cmpq	$32, %rax
	jne	.L17
	cvtsi2sdl	%edx, %xmm0
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	mulsd	.LC3(%rip), %xmm0
	call	__printf_chk@PLT
	popq	%rdx
	.cfi_def_cfa_offset 40
	leaq	.LC7(%rip), %rsi
	popq	%rbx
	.cfi_def_cfa_offset 32
	movl	$2, %edi
	popq	%rbp
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE3639:
	.size	_ZN17pencilComputation6CStatsEPKc, .-_ZN17pencilComputation6CStatsEPKc
	.align 2
	.globl	_ZN17pencilComputation6DStatsEPKc
	.type	_ZN17pencilComputation6DStatsEPKc, @function
_ZN17pencilComputation6DStatsEPKc:
.LFB3640:
	.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
.LFE3640:
	.size	_ZN17pencilComputation6DStatsEPKc, .-_ZN17pencilComputation6DStatsEPKc
	.globl	_Z63__device_stub__Z25pencilComputationFunctionR17pencilComputationP17pencilComputation
	.type	_Z63__device_stub__Z25pencilComputationFunctionR17pencilComputationP17pencilComputation, @function
_Z63__device_stub__Z25pencilComputationFunctionR17pencilComputationP17pencilComputation:
.LFB3665:
	.cfi_startproc
	endbr64
	subq	$104, %rsp
	.cfi_def_cfa_offset 112
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %rcx
	leaq	16(%rsp), %rdx
	leaq	44(%rsp), %rsi
	leaq	32(%rsp), %rdi
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 40(%rsp)
	movq	%rax, 80(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L34
	pushq	24(%rsp)
	.cfi_def_cfa_offset 120
	leaq	_Z25pencilComputationFunctionR17pencilComputation(%rip), %rdi
	pushq	24(%rsp)
	.cfi_def_cfa_offset 128
	movq	60(%rsp), %rcx
	movl	68(%rsp), %r8d
	movq	48(%rsp), %rsi
	movl	56(%rsp), %edx
	leaq	96(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 120
	popq	%rdx
	.cfi_def_cfa_offset 112
.L34:
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L36
	call	__stack_chk_fail@PLT
.L36:
	addq	$104, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3665:
	.size	_Z63__device_stub__Z25pencilComputationFunctionR17pencilComputationP17pencilComputation, .-_Z63__device_stub__Z25pencilComputationFunctionR17pencilComputationP17pencilComputation
	.globl	_Z25pencilComputationFunctionR17pencilComputation
	.type	_Z25pencilComputationFunctionR17pencilComputation, @function
_Z25pencilComputationFunctionR17pencilComputation:
.LFB3666:
	.cfi_startproc
	endbr64
	jmp	_Z63__device_stub__Z25pencilComputationFunctionR17pencilComputationP17pencilComputation
	.cfi_endproc
.LFE3666:
	.size	_Z25pencilComputationFunctionR17pencilComputation, .-_Z25pencilComputationFunctionR17pencilComputation
	.align 2
	.globl	_ZN17pencilComputation8launcherEv
	.type	_ZN17pencilComputation8launcherEv, @function
_ZN17pencilComputation8launcherEv:
.LFB3638:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rdi
	movl	$1, %ecx
	movl	$1, %esi
	movq	%rdi, %rdx
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L40
	movq	%rbx, %rdi
	call	_Z63__device_stub__Z25pencilComputationFunctionR17pencilComputationP17pencilComputation
.L40:
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	cudaDeviceSynchronize@PLT
	.cfi_endproc
.LFE3638:
	.size	_ZN17pencilComputation8launcherEv, .-_ZN17pencilComputation8launcherEv
	.section	.rodata.str1.1
.LC8:
	.string	"_Z25pencilComputationFunctionR17pencilComputation"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3668:
	.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	.LC8(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z25pencilComputationFunctionR17pencilComputation(%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
.LFE3668:
	.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
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC3:
	.long	0
	.long	1069547520
	.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:


#include <hip/hip_runtime.h>
#include<iostream>

// Size of our matrix:
#define matrix_size 2
#define initial_value 1


class pencilComputation {
  public:

    int inputMatrix[matrix_size][matrix_size][matrix_size] = { { {initial_value} } };
    int outputMatrix[matrix_size][matrix_size][matrix_size] = { { { 0 } } };
    int pencilVector[matrix_size * matrix_size * matrix_size] = { 0 };

    void launcher();

    void CStats(const char *message);
    void DStats(const char *message);
};


	.text
	.file	"3d-test.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.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
