#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>
using namespace std;

float timeMemory(bool pinned, bool toDevice)
{
  const int count = 1 << 20;
  const int iterations = 1 << 6;
  const int size = count * sizeof(int);
  cudaEvent_t start, end;
  int *h, *d;
  float elapsed;
  cudaError_t status;

  cudaEventCreate(&start);
  cudaEventCreate(&end);

  cudaMalloc(&d, size);
  if (pinned)
    cudaHostAlloc(&h, size, cudaHostAllocDefault);
  else
    h = new int[count];

  cudaEventRecord(start);

  for (int i = 0; i < iterations; i++)
  {
    if (toDevice)
      status = cudaMemcpy(d, h, size, cudaMemcpyHostToDevice);
    else
      status = cudaMemcpy(h, d, size, cudaMemcpyDeviceToHost);
  }

  cudaEventRecord(end);
  cudaEventSynchronize(end);
  cudaEventElapsedTime(&elapsed, start, end);

  if (pinned)
    cudaFreeHost(h);
  else
    delete [] h;

  cudaFree(d);
  cudaEventDestroy(start);
  cudaEventDestroy(end);

  return elapsed;
}

int main()
{
  cout << "From device, paged memory:\t" << timeMemory(false, false) << endl;
  cout << "To device,   paged memory:\t" << timeMemory(false, true) << endl;
  cout << "From device, pinned memory:\t" << timeMemory(true, false) << endl;
  cout << "To device,   pinned memory:\t" << timeMemory(true, true) << endl;

  getchar();
  return 0;
}
	.file	"tmpxft_0027297e_00000000-6_kernel.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	_Z10timeMemorybb
	.type	_Z10timeMemorybb, @function
_Z10timeMemorybb:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movl	%esi, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movl	%edi, %ebx
	subq	$48, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	16(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	32(%rsp), %rdi
	movl	$4194304, %esi
	call	cudaMalloc@PLT
	testb	%bl, %bl
	je	.L3
	leaq	24(%rsp), %rdi
	xorl	%edx, %edx
	movl	$4194304, %esi
	call	cudaHostAlloc@PLT
	jmp	.L4
.L3:
	movl	$4194304, %edi
	call	_Znam@PLT
	movq	%rax, 24(%rsp)
.L4:
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	movl	$64, %ebp
	call	cudaEventRecord@PLT
.L7:
	movq	32(%rsp), %rax
	movq	24(%rsp), %rdi
	testb	%r12b, %r12b
	je	.L5
	movq	%rdi, %rsi
	movl	$1, %ecx
	movl	$4194304, %edx
	movq	%rax, %rdi
	jmp	.L16
.L5:
	movl	$2, %ecx
	movl	$4194304, %edx
	movq	%rax, %rsi
.L16:
	call	cudaMemcpy@PLT
	decl	%ebp
	jne	.L7
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	16(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rsi
	leaq	4(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	testb	%bl, %bl
	movq	24(%rsp), %rdi
	je	.L8
	call	cudaFreeHost@PLT
	jmp	.L9
.L8:
	testq	%rdi, %rdi
	je	.L9
	call	_ZdaPv@PLT
.L9:
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	8(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	16(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movss	4(%rsp), %xmm0
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$48, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3635:
	.size	_Z10timeMemorybb, .-_Z10timeMemorybb
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"From device, paged memory:\t"
.LC1:
	.string	"To device,   paged memory:\t"
.LC2:
	.string	"From device, pinned memory:\t"
.LC3:
	.string	"To device,   pinned memory:\t"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3636:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC0(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	leaq	_ZSt4cout(%rip), %rbx
	pushq	%rax
	.cfi_def_cfa_offset 32
	movq	%rbx, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	xorl	%esi, %esi
	xorl	%edi, %edi
	movq	%rax, %rbp
	call	_Z10timeMemorybb
	movq	%rbp, %rdi
	cvtss2sd	%xmm0, %xmm0
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movq	%rbx, %rdi
	leaq	.LC1(%rip), %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	$1, %esi
	xorl	%edi, %edi
	movq	%rax, %rbp
	call	_Z10timeMemorybb
	movq	%rbp, %rdi
	cvtss2sd	%xmm0, %xmm0
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movq	%rbx, %rdi
	leaq	.LC2(%rip), %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	xorl	%esi, %esi
	movl	$1, %edi
	movq	%rax, %rbp
	call	_Z10timeMemorybb
	movq	%rbp, %rdi
	cvtss2sd	%xmm0, %xmm0
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movq	%rbx, %rdi
	leaq	.LC3(%rip), %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	$1, %esi
	movl	$1, %edi
	movq	%rax, %rbx
	call	_Z10timeMemorybb
	movq	%rbx, %rdi
	cvtss2sd	%xmm0, %xmm0
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	call	getchar@PLT
	popq	%rdx
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.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 <iostream>
using namespace std;

float timeMemory(bool pinned, bool toDevice)
{
  const int count = 1 << 20;
  const int iterations = 1 << 6;
  const int size = count * sizeof(int);
  hipEvent_t start, end;
  int *h, *d;
  float elapsed;
  hipError_t status;

  hipEventCreate(&start);
  hipEventCreate(&end);

  hipMalloc(&d, size);
  if (pinned)
    hipHostAlloc(&h, size, hipHostMallocDefault);
  else
    h = new int[count];

  hipEventRecord(start);

  for (int i = 0; i < iterations; i++)
  {
    if (toDevice)
      status = hipMemcpy(d, h, size, hipMemcpyHostToDevice);
    else
      status = hipMemcpy(h, d, size, hipMemcpyDeviceToHost);
  }

  hipEventRecord(end);
  hipEventSynchronize(end);
  hipEventElapsedTime(&elapsed, start, end);

  if (pinned)
    hipHostFree(h);
  else
    delete [] h;

  hipFree(d);
  hipEventDestroy(start);
  hipEventDestroy(end);

  return elapsed;
}

int main()
{
  cout << "From device, paged memory:\t" << timeMemory(false, false) << endl;
  cout << "To device,   paged memory:\t" << timeMemory(false, true) << endl;
  cout << "From device, pinned memory:\t" << timeMemory(true, false) << endl;
  cout << "To device,   pinned memory:\t" << timeMemory(true, true) << endl;

  getchar();
  return 0;
}
	.text
	.file	"kernel.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z10timeMemorybb                # -- Begin function _Z10timeMemorybb
	.type	_Z10timeMemorybb,@function
_Z10timeMemorybb:                       # @_Z10timeMemorybb
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$48, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebp
	movl	%edi, %ebx
	leaq	32(%rsp), %rdi
	callq	hipEventCreate
	leaq	16(%rsp), %rdi
	callq	hipEventCreate
	leaq	24(%rsp), %rdi
	movl	$4194304, %esi                  # imm = 0x400000
	callq	hipMalloc
	testl	%ebx, %ebx
	je	.LBB0_2
# %bb.1:
	leaq	8(%rsp), %rdi
	movl	$4194304, %esi                  # imm = 0x400000
	xorl	%edx, %edx
	callq	hipHostAlloc
	jmp	.LBB0_3
.LBB0_2:
	movl	$4194304, %edi                  # imm = 0x400000
	callq	_Znam
	movq	%rax, 8(%rsp)
.LBB0_3:
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	$64, %r14d
.LBB0_4:                                # =>This Inner Loop Header: Depth=1
	testb	%bpl, %bpl
	je	.LBB0_6
# %bb.5:                                #   in Loop: Header=BB0_4 Depth=1
	movq	24(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	$4194304, %edx                  # imm = 0x400000
	movl	$1, %ecx
	jmp	.LBB0_7
.LBB0_6:                                #   in Loop: Header=BB0_4 Depth=1
	movq	8(%rsp), %rdi
	movq	24(%rsp), %rsi
	movl	$4194304, %edx                  # imm = 0x400000
	movl	$2, %ecx
.LBB0_7:                                #   in Loop: Header=BB0_4 Depth=1
	callq	hipMemcpy
	decl	%r14d
	jne	.LBB0_4
# %bb.8:
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	16(%rsp), %rdi
	callq	hipEventSynchronize
	movq	32(%rsp), %rsi
	movq	16(%rsp), %rdx
	leaq	44(%rsp), %rdi
	callq	hipEventElapsedTime
	movq	8(%rsp), %rdi
	testb	%bl, %bl
	je	.LBB0_10
# %bb.9:
	callq	hipHostFree
	jmp	.LBB0_12
.LBB0_10:
	testq	%rdi, %rdi
	je	.LBB0_12
# %bb.11:
	callq	_ZdaPv
.LBB0_12:
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipEventDestroy
	movq	16(%rsp), %rdi
	callq	hipEventDestroy
	movss	44(%rsp), %xmm0                 # xmm0 = mem[0],zero,zero,zero
	addq	$48, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z10timeMemorybb, .Lfunc_end0-_Z10timeMemorybb
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	$_ZSt4cout, %edi
	movl	$.L.str, %esi
	movl	$27, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	xorl	%edi, %edi
	xorl	%esi, %esi
	callq	_Z10timeMemorybb
	cvtss2sd	%xmm0, %xmm0
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertIdEERSoT_
	movq	%rax, %rbx
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%rbx, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	$_ZSt4cout, %edi
	movl	$.L.str.1, %esi
	movl	$27, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	xorl	%edi, %edi
	movl	$1, %esi
	callq	_Z10timeMemorybb
	cvtss2sd	%xmm0, %xmm0
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertIdEERSoT_
	movq	%rax, %rbx
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%rbx, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	$_ZSt4cout, %edi
	movl	$.L.str.2, %esi
	movl	$28, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	$1, %edi
	xorl	%esi, %esi
	callq	_Z10timeMemorybb
	cvtss2sd	%xmm0, %xmm0
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertIdEERSoT_
	movq	%rax, %rbx
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%rbx, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	$_ZSt4cout, %edi
	movl	$.L.str.3, %esi
	movl	$28, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	$1, %edi
	movl	$1, %esi
	callq	_Z10timeMemorybb
	cvtss2sd	%xmm0, %xmm0
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertIdEERSoT_
	movq	%rax, %rbx
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%rbx, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	callq	getchar
	xorl	%eax, %eax
	popq	%rbx
	.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	"From device, paged memory:\t"
	.size	.L.str, 28

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"To device,   paged memory:\t"
	.size	.L.str.1, 28

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"From device, pinned memory:\t"
	.size	.L.str.2, 29

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"To device,   pinned memory:\t"
	.size	.L.str.3, 29

	.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 _ZSt4cout
	.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
