#include <iostream>

using namespace std;

long layer_sizes[] = {56l * 56 * 96,  28l * 28 * 96,  27l * 27 * 256,
                      13l * 13 * 256, 13l * 12 * 384, 13l * 12 * 384,
                      13l * 13 * 256, 6l * 6 * 256};
int num_layers = 8;

int main() {
  cudaEvent_t start, stop;
  cudaEventCreate(&start);
  cudaEventCreate(&stop);

  cudaEventRecord(start);
  // long size_to_alloc = layer_sizes[0];

  for (int j = 0; j < num_layers; j++) {
    long size_to_alloc = layer_sizes[j];
    int num_pieces = 8;
    void *p[num_pieces];
    for (int i = 0; i < num_pieces; i++) {
      cudaMallocHost(&p[i], size_to_alloc / num_pieces);
    }
    for (int i = 0; i < num_pieces; i++) {
      cudaFreeHost(p[i]);
    }
  }
  cudaEventRecord(stop);
  cudaEventSynchronize(stop);
  float milli;
  cudaEventElapsedTime(&milli, start, stop);
  cout << "allocating and freeing pieces(ms): " << milli << endl;

  void *p_bulk;

  cudaEventRecord(start);
  for (int j = 0; j < num_layers; j++) {
    long size_to_alloc = layer_sizes[j];
    cudaMallocHost(&p_bulk, size_to_alloc);
    cudaFreeHost(p_bulk);
  }
  cudaEventRecord(stop);
  cudaEventSynchronize(stop);
  cudaEventElapsedTime(&milli, start, stop);
  cout << "allocating and freeing bulk(ms): " << milli << endl;
}
	.file	"tmpxft_0032bec8_00000000-6_host_copy_time.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
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"allocating and freeing pieces(ms): "
.LC1:
	.string	"allocating and freeing bulk(ms): "
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%r15
	.cfi_offset 15, -24
	leaq	layer_sizes(%rip), %r15
	pushq	%r14
	leaq	-80(%rbp), %rdi
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	.cfi_offset 14, -32
	.cfi_offset 13, -40
	.cfi_offset 12, -48
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	subq	$72, %rsp
	movq	%fs:40, %rax
	movq	%rax, -56(%rbp)
	xorl	%eax, %eax
	call	cudaEventCreate@PLT
	leaq	-72(%rbp), %rdi
	call	cudaEventCreate@PLT
	movq	-80(%rbp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
.L3:
	cmpl	%ebx, num_layers(%rip)
	jle	.L17
	movq	%rsp, -104(%rbp)
	movq	(%r15,%rbx,8), %rax
.L4:
	movq	-104(%rbp), %rcx
	cmpq	%rcx, %rsp
	je	.L5
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L4
.L5:
	subq	$64, %rsp
	orq	$0, 56(%rsp)
	movl	$8, %ecx
	cqto
	movq	%rsp, %r13
	idivq	%rcx
	movq	%r13, %r14
	leaq	64(%r13), %r12
	movq	%rax, -112(%rbp)
.L7:
	movq	-112(%rbp), %rsi
	movq	%r14, %rdi
	addq	$8, %r14
	call	cudaMallocHost@PLT
	cmpq	%r12, %r14
	jne	.L7
	xorl	%r12d, %r12d
.L8:
	movq	0(%r13,%r12,8), %rdi
	incq	%r12
	call	cudaFreeHost@PLT
	cmpq	$8, %r12
	jne	.L8
	incq	%rbx
	movq	-104(%rbp), %rsp
	jmp	.L3
.L17:
	movq	-72(%rbp), %rdi
	xorl	%esi, %esi
	leaq	-84(%rbp), %r13
	xorl	%ebx, %ebx
	leaq	_ZSt4cout(%rip), %r12
	leaq	layer_sizes(%rip), %r14
	call	cudaEventRecord@PLT
	movq	-72(%rbp), %rdi
	call	cudaEventSynchronize@PLT
	movq	-72(%rbp), %rdx
	movq	-80(%rbp), %rsi
	movq	%r13, %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC0(%rip), %rsi
	movq	%r12, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	cvtss2sd	-84(%rbp), %xmm0
	movq	%rax, %rdi
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movq	-80(%rbp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
.L10:
	cmpl	%ebx, num_layers(%rip)
	jle	.L18
	movq	(%r14,%rbx,8), %rsi
	leaq	-64(%rbp), %rdi
	incq	%rbx
	call	cudaMallocHost@PLT
	movq	-64(%rbp), %rdi
	call	cudaFreeHost@PLT
	jmp	.L10
.L18:
	movq	-72(%rbp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	-72(%rbp), %rdi
	call	cudaEventSynchronize@PLT
	movq	-72(%rbp), %rdx
	movq	-80(%rbp), %rsi
	movq	%r13, %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC1(%rip), %rsi
	movq	%r12, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	cvtss2sd	-84(%rbp), %xmm0
	movq	%rax, %rdi
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movq	-56(%rbp), %rax
	subq	%fs:40, %rax
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	leaq	-40(%rbp), %rsp
	xorl	%eax, %eax
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE3635:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3661:
	.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
.LFE3661:
	.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
	.globl	num_layers
	.data
	.align 4
	.type	num_layers, @object
	.size	num_layers, 4
num_layers:
	.long	8
	.globl	layer_sizes
	.align 32
	.type	layer_sizes, @object
	.size	layer_sizes, 64
layer_sizes:
	.quad	301056
	.quad	75264
	.quad	186624
	.quad	43264
	.quad	59904
	.quad	59904
	.quad	43264
	.quad	9216
	.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;

long layer_sizes[] = {56l * 56 * 96,  28l * 28 * 96,  27l * 27 * 256,
                      13l * 13 * 256, 13l * 12 * 384, 13l * 12 * 384,
                      13l * 13 * 256, 6l * 6 * 256};
int num_layers = 8;

int main() {
  hipEvent_t start, stop;
  hipEventCreate(&start);
  hipEventCreate(&stop);

  hipEventRecord(start);
  // long size_to_alloc = layer_sizes[0];

  for (int j = 0; j < num_layers; j++) {
    long size_to_alloc = layer_sizes[j];
    int num_pieces = 8;
    void *p[num_pieces];
    for (int i = 0; i < num_pieces; i++) {
      hipHostMalloc(&p[i], size_to_alloc / num_pieces, hipHostMallocDefault);
    }
    for (int i = 0; i < num_pieces; i++) {
      hipHostFree(p[i]);
    }
  }
  hipEventRecord(stop);
  hipEventSynchronize(stop);
  float milli;
  hipEventElapsedTime(&milli, start, stop);
  cout << "allocating and freeing pieces(ms): " << milli << endl;

  void *p_bulk;

  hipEventRecord(start);
  for (int j = 0; j < num_layers; j++) {
    long size_to_alloc = layer_sizes[j];
    hipHostMalloc(&p_bulk, size_to_alloc, hipHostMallocDefault);
    hipHostFree(p_bulk);
  }
  hipEventRecord(stop);
  hipEventSynchronize(stop);
  hipEventElapsedTime(&milli, start, stop);
  cout << "allocating and freeing bulk(ms): " << milli << endl;
}
	.text
	.file	"host_copy_time.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	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	subq	$40, %rsp
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	leaq	-56(%rbp), %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	leaq	-48(%rbp), %rdi
	callq	hipEventCreate
	movq	(%rbx), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	cmpl	$0, num_layers(%rip)
	jle	.LBB0_7
# %bb.1:                                # %.lr.ph.preheader
	xorl	%r14d, %r14d
.LBB0_2:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_3 Depth 2
                                        #     Child Loop BB0_5 Depth 2
	movq	%rsp, %r15
	movq	layer_sizes(,%r14,8), %rax
	movq	%rsp, %r12
	addq	$-64, %r12
	movq	%r12, %rsp
	leaq	7(%rax), %rbx
	testq	%rax, %rax
	cmovnsq	%rax, %rbx
	sarq	$3, %rbx
	xorl	%r13d, %r13d
.LBB0_3:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leaq	(%r12,%r13), %rdi
	movq	%rbx, %rsi
	xorl	%edx, %edx
	callq	hipHostMalloc
	addq	$8, %r13
	cmpq	$64, %r13
	jne	.LBB0_3
# %bb.4:                                # %.preheader.preheader
                                        #   in Loop: Header=BB0_2 Depth=1
	xorl	%ebx, %ebx
.LBB0_5:                                # %.preheader
                                        #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	(%r12,%rbx,8), %rdi
	callq	hipHostFree
	incq	%rbx
	cmpq	$8, %rbx
	jne	.LBB0_5
# %bb.6:                                #   in Loop: Header=BB0_2 Depth=1
	movq	%r15, %rsp
	incq	%r14
	movslq	num_layers(%rip), %rax
	cmpq	%rax, %r14
	jl	.LBB0_2
.LBB0_7:                                # %._crit_edge
	movq	-48(%rbp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	-48(%rbp), %rdi
	callq	hipEventSynchronize
	movq	-56(%rbp), %rsi
	movq	-48(%rbp), %rdx
	leaq	-60(%rbp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	movl	$_ZSt4cout, %edi
	movl	$.L.str, %esi
	movl	$35, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	cvtss2sd	(%rbx), %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
	movq	-56(%rbp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	cmpl	$0, num_layers(%rip)
	jle	.LBB0_10
# %bb.8:                                # %.lr.ph27.preheader
	xorl	%r14d, %r14d
	leaq	-72(%rbp), %rbx
.LBB0_9:                                # %.lr.ph27
                                        # =>This Inner Loop Header: Depth=1
	movq	layer_sizes(,%r14,8), %rsi
	movq	%rbx, %rdi
	xorl	%edx, %edx
	callq	hipHostMalloc
	movq	-72(%rbp), %rdi
	callq	hipHostFree
	incq	%r14
	movslq	num_layers(%rip), %rax
	cmpq	%rax, %r14
	jl	.LBB0_9
.LBB0_10:                               # %._crit_edge28
	movq	-48(%rbp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	-48(%rbp), %rdi
	callq	hipEventSynchronize
	movq	-56(%rbp), %rsi
	movq	-48(%rbp), %rdx
	leaq	-60(%rbp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	movl	$_ZSt4cout, %edi
	movl	$.L.str.1, %esi
	movl	$33, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx), %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
	xorl	%eax, %eax
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa %rsp, 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	layer_sizes,@object             # @layer_sizes
	.data
	.globl	layer_sizes
	.p2align	4, 0x0
layer_sizes:
	.quad	301056                          # 0x49800
	.quad	75264                           # 0x12600
	.quad	186624                          # 0x2d900
	.quad	43264                           # 0xa900
	.quad	59904                           # 0xea00
	.quad	59904                           # 0xea00
	.quad	43264                           # 0xa900
	.quad	9216                            # 0x2400
	.size	layer_sizes, 64

	.type	num_layers,@object              # @num_layers
	.globl	num_layers
	.p2align	2, 0x0
num_layers:
	.long	8                               # 0x8
	.size	num_layers, 4

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"allocating and freeing pieces(ms): "
	.size	.L.str, 36

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"allocating and freeing bulk(ms): "
	.size	.L.str.1, 34

	.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
