#pragma once
// #include "../utils/__utils.h"
// #include "../expressions/expressions.h"
#include <iostream>
using namespace std;
// template <typename val_type>
template <typename val_type, std::size_t dim>
class device_tensor1
{
public:
	val_type* d_data_;

	device_tensor1()
	// : d_data_{new val_type[dim]}
	{
		// cudaMalloc((void**) &(this.d_data_), sizeof(val_type)*dim);
	}
	device_tensor1(device_tensor1 const& d_rhs_)
	// : d_data_{new val_type[dim]}
	{
		cudaMalloc((void**) &d_data_, sizeof(val_type)*dim);
		// DEBUG_MSG("device_tensor1 copy constructor is called");
		// std::memcpy(d_data_, rhs_.d_data_, sizeof(d_data_)*dim);
		cudaMemcpy(d_data_, d_rhs_.d_data_ , dim, cudaMemcpyDeviceToDevice);
	}
	device_tensor1& operator=(device_tensor1 const& d_rhs_){
		// DEBUG_MSG("device_tensor1 copy assignment operator is called");
		if(this != &d_rhs_){
	        // std::memcpy(d_data_, rhs_.d_data_, sizeof(d_data_)*dim);		
			cudaMemcpy(d_data_, d_rhs_.d_data_ , dim, cudaMemcpyDeviceToDevice);
		}
		return *this;
	}
    device_tensor1(device_tensor1&& d_rhs_) noexcept    
    // : d_data_{new val_type[dim]}
    {
		cudaMalloc((void**) &d_data_, sizeof(val_type)*dim);
    	std::swap(d_data_, d_rhs_.d_data_);
    	// DEBUG_MSG("device_tensor1 move constructor is called");

    }
    device_tensor1& operator=(device_tensor1&& d_rhs_) noexcept{
    	// DEBUG_MSG("device_tensor1 move assignment operator is called");
    	if(this != &d_rhs_){
    		std::swap(d_data_, d_rhs_.d_data_);
    	}
    	return *this;
    }
	~device_tensor1(){
		if (d_data_!=nullptr){
		    cudaFree(d_data_);
		    d_data_=nullptr;
		}
	}


};

int main(int argc, char const *argv[])
{
	device_tensor1<float,3> test ;
	/* code */
	return 0;
}
	.file	"tmpxft_00293c1c_00000000-6_test_tensor.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3644:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3644:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3641:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA3641
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movq	8(%rsp), %rdi
	testq	%rdi, %rdi
	je	.L3
	call	cudaFree@PLT
.L3:
	xorl	%eax, %eax
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3641:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA3641:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE3641-.LLSDACSB3641
.LLSDACSB3641:
.LLSDACSE3641:
	.section	.text.startup
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3667:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3667:
	.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
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.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 "../utils/__utils.h"
// #include "../expressions/expressions.h"
#include <iostream>
using namespace std;
// template <typename val_type>
template <typename val_type, std::size_t dim>
class device_tensor1
{
public:
	val_type* d_data_;

	device_tensor1()
	// : d_data_{new val_type[dim]}
	{
		// cudaMalloc((void**) &(this.d_data_), sizeof(val_type)*dim);
	}
	device_tensor1(device_tensor1 const& d_rhs_)
	// : d_data_{new val_type[dim]}
	{
		hipMalloc((void**) &d_data_, sizeof(val_type)*dim);
		// DEBUG_MSG("device_tensor1 copy constructor is called");
		// std::memcpy(d_data_, rhs_.d_data_, sizeof(d_data_)*dim);
		hipMemcpy(d_data_, d_rhs_.d_data_ , dim, hipMemcpyDeviceToDevice);
	}
	device_tensor1& operator=(device_tensor1 const& d_rhs_){
		// DEBUG_MSG("device_tensor1 copy assignment operator is called");
		if(this != &d_rhs_){
	        // std::memcpy(d_data_, rhs_.d_data_, sizeof(d_data_)*dim);		
			hipMemcpy(d_data_, d_rhs_.d_data_ , dim, hipMemcpyDeviceToDevice);
		}
		return *this;
	}
    device_tensor1(device_tensor1&& d_rhs_) noexcept    
    // : d_data_{new val_type[dim]}
    {
		hipMalloc((void**) &d_data_, sizeof(val_type)*dim);
    	std::swap(d_data_, d_rhs_.d_data_);
    	// DEBUG_MSG("device_tensor1 move constructor is called");

    }
    device_tensor1& operator=(device_tensor1&& d_rhs_) noexcept{
    	// DEBUG_MSG("device_tensor1 move assignment operator is called");
    	if(this != &d_rhs_){
    		std::swap(d_data_, d_rhs_.d_data_);
    	}
    	return *this;
    }
	~device_tensor1(){
		if (d_data_!=nullptr){
		    hipFree(d_data_);
		    d_data_=nullptr;
		}
	}


};

int main(int argc, char const *argv[])
{
	device_tensor1<float,3> test ;
	/* code */
	return 0;
}
	.text
	.file	"test_tensor.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rsp, %rdi
	callq	_ZN14device_tensor1IfLm3EED2Ev
	xorl	%eax, %eax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.section	.text._ZN14device_tensor1IfLm3EED2Ev,"axG",@progbits,_ZN14device_tensor1IfLm3EED2Ev,comdat
	.weak	_ZN14device_tensor1IfLm3EED2Ev  # -- Begin function _ZN14device_tensor1IfLm3EED2Ev
	.p2align	1, 0x90
	.type	_ZN14device_tensor1IfLm3EED2Ev,@function
_ZN14device_tensor1IfLm3EED2Ev:         # @_ZN14device_tensor1IfLm3EED2Ev
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
	testq	%rdi, %rdi
	je	.LBB1_3
# %bb.1:
.Ltmp0:
	callq	hipFree
.Ltmp1:
# %bb.2:
	movq	$0, (%rbx)
.LBB1_3:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB1_4:
	.cfi_def_cfa_offset 16
.Ltmp2:
	movq	%rax, %rdi
	callq	__clang_call_terminate
.Lfunc_end1:
	.size	_ZN14device_tensor1IfLm3EED2Ev, .Lfunc_end1-_ZN14device_tensor1IfLm3EED2Ev
	.cfi_endproc
	.section	.gcc_except_table._ZN14device_tensor1IfLm3EED2Ev,"aG",@progbits,_ZN14device_tensor1IfLm3EED2Ev,comdat
	.p2align	2, 0x0
GCC_except_table1:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	3                               # @TType Encoding = udata4
	.uleb128 .Lttbase0-.Lttbaseref0
.Lttbaseref0:
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 1 <<
	.uleb128 .Ltmp1-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp1
	.uleb128 .Ltmp2-.Lfunc_begin0           #     jumps to .Ltmp2
	.byte	1                               #   On action: 1
.Lcst_end0:
	.byte	1                               # >> Action Record 1 <<
                                        #   Catch TypeInfo 1
	.byte	0                               #   No further actions
	.p2align	2, 0x0
                                        # >> Catch TypeInfos <<
	.long	0                               # TypeInfo 1
.Lttbase0:
	.p2align	2, 0x0
                                        # -- End function
	.section	.text.__clang_call_terminate,"axG",@progbits,__clang_call_terminate,comdat
	.hidden	__clang_call_terminate          # -- Begin function __clang_call_terminate
	.weak	__clang_call_terminate
	.type	__clang_call_terminate,@function
__clang_call_terminate:                 # @__clang_call_terminate
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__cxa_begin_catch
	callq	_ZSt9terminatev
.Lfunc_end2:
	.size	__clang_call_terminate, .Lfunc_end2-__clang_call_terminate
	.cfi_endproc
                                        # -- End function
	.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 __gxx_personality_v0
	.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
