#include<cuda.h>
#include<math.h>
#include<iostream>

__global__ void lag_matrix_kernel(const double* X1, const double* X2, double* lag_matrix, double* lag_x1, 
                                double* y_label, double bias, int rows, int cols, int p){
    // now the i-th elem: i = threadsIdx + blockIdx*blockDim
    int idx = threadIdx.x + blockIdx.x * blockDim.x; // idx of row
    if(idx<rows){ 
    	// fill in the lag_matrix
        for(int i = 0; i<p; i++){
    	    lag_matrix[idx + i*rows] = X1[i+idx];
    	}
        for(int j = 0; j<p; j++){
            lag_matrix[idx+(j+p)*rows] = X2[j+idx];
        }
        lag_matrix[(cols-1)*rows + idx] = bias; // fill in the bias
        // fill in the lag_x1
        for(int i = 0; i<p; i++){
            lag_x1[idx + i*rows] = X1[i+idx];
        }
        lag_x1[p*rows + idx] = bias;
        // fill in the label
        y_label[idx] = X1[idx+p];
    }
    return;
}

// 
void gen_lag_matrix(const double* x1, const double* x2, double* lag_matrix, double* lag_x1, double* y_label, 
                    double bias, int n, int p, int rows, int cols, int threads_per_block){
    // we want to let each thread fill out each row
    int num_blocks = (rows+threads_per_block - 1)/threads_per_block;

    double * d_x1;
    double * d_x2;
    double * d_matrix_lag = new double[rows*cols];
    double * d_x1_lag = new double[rows*(p+1)];
    double * d_label = new double[rows];

    //std::cout << A[nsq-] << std::endl;
    //std::cout << B[nsq-] << std::endl; 

    cudaMallocManaged((void**)&d_x1, sizeof(double)*n);
    cudaMallocManaged((void**)&d_x2, sizeof(double)*n);
    cudaMallocManaged((void**)&d_matrix_lag, sizeof(double)*(rows*cols));
    cudaMallocManaged((void**)&d_x1_lag, sizeof(double)*(rows*(p+1)));
    cudaMallocManaged((void**)&d_label, sizeof(double)*rows);

    cudaMemcpy(d_x1, x1, sizeof(double)*n, cudaMemcpyHostToDevice);
    cudaMemcpy(d_x2, x2, sizeof(double)*n, cudaMemcpyHostToDevice);

    
    // timing 
    
    cudaEvent_t start;
    cudaEvent_t stop;   
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    
    cudaEventRecord(start);

    lag_matrix_kernel<<<num_blocks,threads_per_block>>>(d_x1, d_x2, d_matrix_lag, d_x1_lag, d_label, bias, rows, cols, p);
    cudaDeviceSynchronize();

    cudaEventRecord(stop);
    cudaEventSynchronize(stop);

    // Get the elapsed time in milliseconds
    float ms;
    cudaEventElapsedTime(&ms, start, stop);

    // bring the result back  -- dont need for task1
    cudaMemcpy(lag_matrix, d_matrix_lag, (rows*cols)*sizeof(double), cudaMemcpyDeviceToHost);
    cudaMemcpy(lag_x1, d_x1_lag, (rows*(p+1))*sizeof(double), cudaMemcpyDeviceToHost);
    cudaMemcpy(y_label, d_label, rows*sizeof(double), cudaMemcpyDeviceToHost);

    cudaFree(d_x1);
    cudaFree(d_x2);
    cudaFree(d_matrix_lag);
    cudaFree(d_x1_lag);
    cudaFree(d_label);

    std::cout << "Lagged matrix time: " << ms << " ms" << std::endl;

}

	.file	"tmpxft_0029d70a_00000000-6_gen_lag_matrix.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	_Z53__device_stub__Z17lag_matrix_kernelPKdS0_PdS1_S1_diiiPKdS0_PdS1_S1_diii
	.type	_Z53__device_stub__Z17lag_matrix_kernelPKdS0_PdS1_S1_diiiPKdS0_PdS1_S1_diii, @function
_Z53__device_stub__Z17lag_matrix_kernelPKdS0_PdS1_S1_diiiPKdS0_PdS1_S1_diii:
.LFB3660:
	.cfi_startproc
	endbr64
	subq	$216, %rsp
	.cfi_def_cfa_offset 224
	movq	%rdi, 56(%rsp)
	leaq	80(%rsp), %rdi
	movq	%rsi, 48(%rsp)
	leaq	92(%rsp), %rsi
	movq	%rdx, 40(%rsp)
	leaq	64(%rsp), %rdx
	movq	%rcx, 32(%rsp)
	leaq	72(%rsp), %rcx
	movq	%r8, 24(%rsp)
	movl	%r9d, 12(%rsp)
	movsd	%xmm0, 16(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 200(%rsp)
	xorl	%eax, %eax
	leaq	56(%rsp), %rax
	movl	$1, 88(%rsp)
	movq	%rax, 128(%rsp)
	leaq	48(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	40(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 160(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 168(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 176(%rsp)
	leaq	224(%rsp), %rax
	movq	%rax, 184(%rsp)
	leaq	232(%rsp), %rax
	movq	%rax, 192(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 80(%rsp)
	movq	%rax, 92(%rsp)
	movl	$1, 100(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	72(%rsp)
	.cfi_def_cfa_offset 232
	leaq	_Z17lag_matrix_kernelPKdS0_PdS1_S1_diii(%rip), %rdi
	pushq	72(%rsp)
	.cfi_def_cfa_offset 240
	movq	108(%rsp), %rcx
	movl	116(%rsp), %r8d
	movq	96(%rsp), %rsi
	movl	104(%rsp), %edx
	leaq	144(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 232
	popq	%rdx
	.cfi_def_cfa_offset 224
.L2:
	movq	200(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$216, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3660:
	.size	_Z53__device_stub__Z17lag_matrix_kernelPKdS0_PdS1_S1_diiiPKdS0_PdS1_S1_diii, .-_Z53__device_stub__Z17lag_matrix_kernelPKdS0_PdS1_S1_diiiPKdS0_PdS1_S1_diii
	.globl	_Z17lag_matrix_kernelPKdS0_PdS1_S1_diii
	.type	_Z17lag_matrix_kernelPKdS0_PdS1_S1_diii, @function
_Z17lag_matrix_kernelPKdS0_PdS1_S1_diii:
.LFB3661:
	.cfi_startproc
	endbr64
	jmp	_Z53__device_stub__Z17lag_matrix_kernelPKdS0_PdS1_S1_diiiPKdS0_PdS1_S1_diii
	.cfi_endproc
.LFE3661:
	.size	_Z17lag_matrix_kernelPKdS0_PdS1_S1_diii, .-_Z17lag_matrix_kernelPKdS0_PdS1_S1_diii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Lagged matrix time: "
.LC1:
	.string	" ms"
	.text
	.globl	_Z14gen_lag_matrixPKdS0_PdS1_S1_diiiii
	.type	_Z14gen_lag_matrixPKdS0_PdS1_S1_diiiii, @function
_Z14gen_lag_matrixPKdS0_PdS1_S1_diiiii:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movabsq	$1152921504606846975, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$168, %rsp
	.cfi_def_cfa_offset 224
	movq	%rdx, 24(%rsp)
	movl	232(%rsp), %ebx
	movq	%rcx, 32(%rsp)
	movl	248(%rsp), %r12d
	movq	%r8, 40(%rsp)
	movsd	%xmm0, 48(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leal	-1(%rbx,%r12), %eax
	cltd
	idivl	%r12d
	movl	%eax, 60(%rsp)
	movl	240(%rsp), %eax
	imull	%ebx, %eax
	cltq
	cmpq	%rax, %r15
	jb	.L9
	salq	$3, %rax
	movq	%rdi, %r14
	movq	%rsi, %r13
	movslq	%r9d, %rbp
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	_Znam@PLT
	movq	%rax, 88(%rsp)
	movl	224(%rsp), %eax
	incl	%eax
	imull	%ebx, %eax
	cltq
	cmpq	%rax, %r15
	jnb	.L10
.L9:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L15
	call	__cxa_throw_bad_array_new_length@PLT
.L10:
	salq	$3, %rax
	movq	%rax, %rdi
	movq	%rax, 16(%rsp)
	call	_Znam@PLT
	movq	%rax, 96(%rsp)
	movslq	%ebx, %rax
	cmpq	%rax, %r15
	jb	.L9
	leaq	0(,%rax,8), %r15
	salq	$3, %rbp
	movq	%r15, %rdi
	call	_Znam@PLT
	leaq	72(%rsp), %rdi
	movl	$1, %edx
	movq	%rbp, %rsi
	movq	%rax, 104(%rsp)
	call	cudaMallocManaged@PLT
	leaq	80(%rsp), %rdi
	movl	$1, %edx
	movq	%rbp, %rsi
	call	cudaMallocManaged@PLT
	movq	8(%rsp), %rsi
	leaq	88(%rsp), %rdi
	movl	$1, %edx
	call	cudaMallocManaged@PLT
	movq	16(%rsp), %rsi
	leaq	96(%rsp), %rdi
	movl	$1, %edx
	call	cudaMallocManaged@PLT
	leaq	104(%rsp), %rdi
	movl	$1, %edx
	movq	%r15, %rsi
	call	cudaMallocManaged@PLT
	movq	72(%rsp), %rdi
	movq	%rbp, %rdx
	movq	%r14, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	80(%rsp), %rdi
	movq	%rbp, %rdx
	movq	%r13, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	112(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	120(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	112(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	60(%rsp), %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	movl	%r12d, 140(%rsp)
	movq	%rax, 144(%rsp)
	movq	140(%rsp), %rdx
	movl	%ecx, 128(%rsp)
	movl	148(%rsp), %ecx
	movq	%rax, 132(%rsp)
	movq	128(%rsp), %rdi
	movl	136(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L12
	movl	224(%rsp), %eax
	movl	%ebx, %r9d
	pushq	%rax
	.cfi_def_cfa_offset 232
	movl	248(%rsp), %eax
	pushq	%rax
	.cfi_def_cfa_offset 240
	movsd	64(%rsp), %xmm0
	movq	120(%rsp), %r8
	movq	112(%rsp), %rcx
	movq	104(%rsp), %rdx
	movq	96(%rsp), %rsi
	movq	88(%rsp), %rdi
	call	_Z53__device_stub__Z17lag_matrix_kernelPKdS0_PdS1_S1_diiiPKdS0_PdS1_S1_diii
	popq	%rax
	.cfi_def_cfa_offset 232
	popq	%rdx
	.cfi_def_cfa_offset 224
.L12:
	call	cudaDeviceSynchronize@PLT
	movq	120(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	120(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	120(%rsp), %rdx
	movq	112(%rsp), %rsi
	leaq	140(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	8(%rsp), %rdx
	movq	88(%rsp), %rsi
	movl	$2, %ecx
	movq	24(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rdx
	movq	96(%rsp), %rsi
	movl	$2, %ecx
	movq	32(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	104(%rsp), %rsi
	movq	40(%rsp), %rdi
	movq	%r15, %rdx
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	72(%rsp), %rdi
	call	cudaFree@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	88(%rsp), %rdi
	call	cudaFree@PLT
	movq	96(%rsp), %rdi
	call	cudaFree@PLT
	movq	104(%rsp), %rdi
	call	cudaFree@PLT
	leaq	.LC0(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	cvtss2sd	140(%rsp), %xmm0
	movq	%rax, %rdi
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	leaq	.LC1(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
.L15:
	call	__stack_chk_fail@PLT
.L13:
	addq	$168, %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
.LFE3635:
	.size	_Z14gen_lag_matrixPKdS0_PdS1_S1_diiiii, .-_Z14gen_lag_matrixPKdS0_PdS1_S1_diiiii
	.section	.rodata.str1.1
.LC2:
	.string	"_Z17lag_matrix_kernelPKdS0_PdS1_S1_diii"
	.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	.LC2(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z17lag_matrix_kernelPKdS0_PdS1_S1_diii(%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:

#ifndef LAGMAT_CUH
#define LAGMAT_CUH


__global__ void lag_matrix_kernel(const double* X1, const double* X2, double* lag_matrix,  double* lag_x1, 
								double* y_label, double bias, int rows, int cols, int p);

void gen_lag_matrix(const double* x1, const double* x2, double* lag_matrix,  double* lag_x1, double* y_label, 
					double bias, int n, int p, int rows, int cols, int threads_per_block);

#endif

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