
#include <cuda.h>
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

void inizializzazioneHostArray(int *h_array,int n){
        int i;
        for(i=0;i<n;i++)
                *(h_array+i)=i;
}

int main(int argc,char *argv[]){

int n;
int *h_array, *d_array;

if(argc==1)
                n=20;
else sscanf(argv[1],"%d",&n);


//alloco host array
h_array=(int *)malloc(n*sizeof( int ));
//alloco device array
cudaMalloc((void **)&d_array,n*sizeof(int));

//inizializzo host array
inizializzazioneHostArray(h_array,n);

//trasferisco i dati da host a device
cudaMemcpy(d_array,h_array,n*sizeof(int),cudaMemcpyHostToDevice);


//alloco array per ricevere di nuovo
int *h_array2=(int *)malloc(n*sizeof(int));
cudaMemcpy(h_array,d_array,n*sizeof(int),cudaMemcpyDeviceToHost);

//stampa
int i;
for(i=0;i<n;i++)
{
        printf("%d - %d",h_array[i],h_array[i]);
        printf("\n");
}



//dealloco memoria dell'heap sull'host
free((void *)h_array);
free((void *)h_array2);
//dealloco memoria heap della ram scheda video
}
	.file	"tmpxft_002daa75_00000000-6_esempio1.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3639:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3639:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z25inizializzazioneHostArrayPii
	.type	_Z25inizializzazioneHostArrayPii, @function
_Z25inizializzazioneHostArrayPii:
.LFB3635:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L3:
	cmpl	%eax, %esi
	jle	.L6
	movl	%eax, (%rdi,%rax,4)
	incq	%rax
	jmp	.L3
.L6:
	ret
	.cfi_endproc
.LFE3635:
	.size	_Z25inizializzazioneHostArrayPii, .-_Z25inizializzazioneHostArrayPii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d"
.LC1:
	.string	"%d - %d"
.LC2:
	.string	"\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3636:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	decl	%edi
	jne	.L8
	movl	$20, 12(%rsp)
	jmp	.L9
.L8:
	movq	8(%rsi), %rdi
	leaq	12(%rsp), %rdx
	xorl	%eax, %eax
	leaq	.LC0(%rip), %rsi
	call	__isoc23_sscanf@PLT
.L9:
	movslq	12(%rsp), %rbp
	leaq	.LC1(%rip), %r12
	salq	$2, %rbp
	movq	%rbp, %rdi
	call	malloc@PLT
	movq	%rbp, %rsi
	leaq	16(%rsp), %rdi
	xorl	%ebp, %ebp
	movq	%rax, %rbx
	call	cudaMalloc@PLT
	movslq	12(%rsp), %rdx
	movq	%rbx, %rdi
	movl	$1, %ecx
	movl	%edx, %esi
	salq	$2, %rdx
	call	_Z25inizializzazioneHostArrayPii
	movq	16(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMemcpy@PLT
	movslq	12(%rsp), %rdx
	movq	16(%rsp), %rsi
	movq	%rbx, %rdi
	movl	$2, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
.L10:
	cmpl	%ebp, 12(%rsp)
	jle	.L14
	movl	(%rbx,%rbp,4), %edx
	movq	%r12, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%rbp
	movl	%edx, %ecx
	call	__printf_chk@PLT
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L10
.L14:
	movq	%rbx, %rdi
	call	free@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3636:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3662:
	.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
.LFE3662:
	.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:


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

using namespace std;

void inizializzazioneHostArray(int *h_array,int n){
        int i;
        for(i=0;i<n;i++)
                *(h_array+i)=i;
}

int main(int argc,char *argv[]){

int n;
int *h_array, *d_array;

if(argc==1)
                n=20;
else sscanf(argv[1],"%d",&n);


//alloco host array
h_array=(int *)malloc(n*sizeof( int ));
//alloco device array
hipMalloc((void **)&d_array,n*sizeof(int));

//inizializzo host array
inizializzazioneHostArray(h_array,n);

//trasferisco i dati da host a device
hipMemcpy(d_array,h_array,n*sizeof(int),hipMemcpyHostToDevice);


//alloco array per ricevere di nuovo
int *h_array2=(int *)malloc(n*sizeof(int));
hipMemcpy(h_array,d_array,n*sizeof(int),hipMemcpyDeviceToHost);

//stampa
int i;
for(i=0;i<n;i++)
{
        printf("%d - %d",h_array[i],h_array[i]);
        printf("\n");
}



//dealloco memoria dell'heap sull'host
free((void *)h_array);
free((void *)h_array2);
//dealloco memoria heap della ram scheda video
}
	.text
	.file	"esempio1.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z25inizializzazioneHostArrayPii # -- Begin function _Z25inizializzazioneHostArrayPii
	.type	_Z25inizializzazioneHostArrayPii,@function
_Z25inizializzazioneHostArrayPii:       # @_Z25inizializzazioneHostArrayPii
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%esi, %eax
	xorl	%ecx, %ecx
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%ecx, (%rdi,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB0_2
.LBB0_3:                                # %._crit_edge
	retq
.Lfunc_end0:
	.size	_Z25inizializzazioneHostArrayPii, .Lfunc_end0-_Z25inizializzazioneHostArrayPii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	cmpl	$1, %edi
	jne	.LBB1_2
# %bb.1:
	movl	$20, 12(%rsp)
	movl	$80, %r14d
	jmp	.LBB1_3
.LBB1_2:
	movq	8(%rsi), %rdi
	leaq	12(%rsp), %rbx
	movl	$.L.str, %esi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	__isoc23_sscanf
	movslq	(%rbx), %r14
	shlq	$2, %r14
.LBB1_3:
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, %rbx
	leaq	16(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movl	12(%rsp), %eax
	testl	%eax, %eax
	jle	.LBB1_6
# %bb.4:                                # %.lr.ph.preheader.i
	xorl	%ecx, %ecx
.LBB1_5:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movl	%ecx, (%rbx,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB1_5
.LBB1_6:                                # %_Z25inizializzazioneHostArrayPii.exit
	movq	16(%rsp), %rdi
	movslq	%eax, %rdx
	shlq	$2, %rdx
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	16(%rsp), %rsi
	movslq	12(%rsp), %rdx
	shlq	$2, %rdx
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	cmpl	$0, 12(%rsp)
	jle	.LBB1_9
# %bb.7:                                # %.lr.ph.preheader
	xorl	%r14d, %r14d
.LBB1_8:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rbx,%r14,4), %edx
	movl	$.L.str.1, %edi
	movl	%edx, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r14
	movslq	12(%rsp), %rax
	cmpq	%rax, %r14
	jl	.LBB1_8
.LBB1_9:                                # %._crit_edge
	movq	%rbx, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%d"
	.size	.L.str, 3

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"%d - %d"
	.size	.L.str.1, 8

	.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
