#include <cuda_runtime.h>
#include <stdio.h>
#include <sys/time.h>

inline double seconds() {
  struct timeval tp;
  struct timezone tzp;
  int i = gettimeofday(&tp, &tzp);
  return ((double)tp.tv_sec + (double)tp.tv_usec * 1.e-6);
}

int total_size = 1024 * 1024;  // 1MB

void test(int size) {
  double iStart, iElaps;
  int num = total_size / size;
  float *d[num];

  iStart = seconds();
  for (int i = 0; i < num; i++) {
    cudaMalloc((float **)&d[i], size);
  }
  iElaps = seconds() - iStart;
  printf("cudaMalloc(%d) x %d Time elapsed %f sec\n", size, num, iElaps);

  iStart = seconds();
  for (int i = 0; i < num; i++) {
    cudaFree(d[i]);
  }
  iElaps = seconds() - iStart;
  printf("cudaFree(%d) x %d Time elapsed %f sec\n", size, num, iElaps);
}

int main(int argc, char **argv) {
  printf("%s Starting...\n", argv[0]);

  // set up device
  int dev = 0;
  cudaDeviceProp deviceProp;
  cudaGetDeviceProperties(&deviceProp, dev);
  printf("Using Device %d: %s\n", dev, deviceProp.name);
  cudaSetDevice(dev);

  int size = atoi(argv[1]);
  test(size);

  return (0);
}
	.file	"tmpxft_0024ae6d_00000000-6_cudaMallocBenchmark.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.text._Z7secondsv,"axG",@progbits,_Z7secondsv,comdat
	.weak	_Z7secondsv
	.type	_Z7secondsv, @function
_Z7secondsv:
.LFB2027:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rsi
	leaq	8(%rsp), %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	16(%rsp), %xmm0
	mulsd	.LC0(%rip), %xmm0
	cvtsi2sdq	8(%rsp), %xmm1
	addsd	%xmm1, %xmm0
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L3
	call	__stack_chk_fail@PLT
.L3:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z7secondsv, .-_Z7secondsv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"cudaMalloc(%d) x %d Time elapsed %f sec\n"
.LC2:
	.string	"cudaFree(%d) x %d Time elapsed %f sec\n"
	.text
	.globl	_Z4testi
	.type	_Z4testi, @function
_Z4testi:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	.cfi_offset 15, -24
	.cfi_offset 14, -32
	.cfi_offset 13, -40
	.cfi_offset 12, -48
	movl	%edi, %r12d
	pushq	%rbx
	subq	$40, %rsp
	.cfi_offset 3, -56
	movq	%fs:40, %rax
	movq	%rax, -56(%rbp)
	movl	total_size(%rip), %eax
	movq	%rsp, %rcx
	cltd
	idivl	%edi
	cltq
	leaq	15(,%rax,8), %rdx
	movq	%rax, %rbx
	movq	%rdx, %rax
	andq	$-4096, %rdx
	andq	$-16, %rax
	subq	%rdx, %rcx
.L7:
	cmpq	%rcx, %rsp
	je	.L8
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L7
.L8:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L9
	orq	$0, -8(%rsp,%rax)
.L9:
	call	_Z7secondsv
	movslq	%r12d, %rax
	movq	%rsp, %r14
	xorl	%r13d, %r13d
	movq	%rax, -80(%rbp)
	movq	%r14, %r15
	movsd	%xmm0, -72(%rbp)
.L10:
	cmpl	%ebx, %r13d
	jge	.L19
	movq	-80(%rbp), %rsi
	movq	%r15, %rdi
	incl	%r13d
	addq	$8, %r15
	call	cudaMalloc@PLT
	jmp	.L10
.L19:
	call	_Z7secondsv
	subsd	-72(%rbp), %xmm0
	movl	%ebx, %ecx
	movl	%r12d, %edx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	xorl	%r13d, %r13d
	call	__printf_chk@PLT
	call	_Z7secondsv
	movsd	%xmm0, -72(%rbp)
.L12:
	cmpl	%r13d, %ebx
	jle	.L20
	movq	(%r14,%r13,8), %rdi
	incq	%r13
	call	cudaFree@PLT
	jmp	.L12
.L20:
	call	_Z7secondsv
	subsd	-72(%rbp), %xmm0
	movl	%ebx, %ecx
	movl	%r12d, %edx
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	movq	-56(%rbp), %rax
	subq	%fs:40, %rax
	je	.L14
	call	__stack_chk_fail@PLT
.L14:
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z4testi, .-_Z4testi
	.section	.rodata.str1.1
.LC3:
	.string	"%s Starting...\n"
.LC4:
	.string	"Using Device %d: %s\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	$2, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rsi, %rbx
	subq	$1048, %rsp
	.cfi_def_cfa_offset 1072
	movq	(%rsi), %rdx
	leaq	.LC3(%rip), %rsi
	movq	%fs:40, %rax
	movq	%rax, 1032(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rbp
	call	__printf_chk@PLT
	xorl	%esi, %esi
	movq	%rbp, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	xorl	%edx, %edx
	movq	%rbp, %rcx
	movl	$2, %edi
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	cudaSetDevice@PLT
	movq	8(%rbx), %rdi
	call	atoi@PLT
	movl	%eax, %edi
	call	_Z4testi
	movq	1032(%rsp), %rax
	subq	%fs:40, %rax
	je	.L22
	call	__stack_chk_fail@PLT
.L22:
	addq	$1048, %rsp
	.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
.LFE2029:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.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
.LFE2055:
	.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	total_size
	.data
	.align 4
	.type	total_size, @object
	.size	total_size, 4
total_size:
	.long	1048576
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	-1598689907
	.long	1051772663
	.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 <sys/time.h>

inline double seconds() {
  struct timeval tp;
  struct timezone tzp;
  int i = gettimeofday(&tp, &tzp);
  return ((double)tp.tv_sec + (double)tp.tv_usec * 1.e-6);
}

int total_size = 1024 * 1024;  // 1MB

void test(int size) {
  double iStart, iElaps;
  int num = total_size / size;
  float *d[num];

  iStart = seconds();
  for (int i = 0; i < num; i++) {
    hipMalloc((float **)&d[i], size);
  }
  iElaps = seconds() - iStart;
  printf("cudaMalloc(%d) x %d Time elapsed %f sec\n", size, num, iElaps);

  iStart = seconds();
  for (int i = 0; i < num; i++) {
    hipFree(d[i]);
  }
  iElaps = seconds() - iStart;
  printf("cudaFree(%d) x %d Time elapsed %f sec\n", size, num, iElaps);
}

int main(int argc, char **argv) {
  printf("%s Starting...\n", argv[0]);

  // set up device
  int dev = 0;
  hipDeviceProp_t deviceProp;
  hipGetDeviceProperties(&deviceProp, dev);
  printf("Using Device %d: %s\n", dev, deviceProp.name);
  hipSetDevice(dev);

  int size = atoi(argv[1]);
  test(size);

  return (0);
}
	.text
	.file	"cudaMallocBenchmark.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z4testi
.LCPI0_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.text
	.globl	_Z4testi
	.type	_Z4testi,@function
_Z4testi:                               # @_Z4testi
	.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
	movl	total_size(%rip), %eax
	cltd
	movl	%edi, -44(%rbp)                 # 4-byte Spill
	idivl	%edi
	movl	%eax, %ebx
	leaq	15(,%rbx,8), %rax
	andq	$-16, %rax
	movq	%rsp, %r13
	subq	%rax, %r13
	movq	%r13, %rsp
	leaq	-80(%rbp), %r15
	leaq	-64(%rbp), %rsi
	movq	%r15, %rdi
	callq	gettimeofday
	cvtsi2sdq	(%r15), %xmm0
	cvtsi2sdq	8(%r15), %xmm1
	mulsd	.LCPI0_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, -56(%rbp)                # 8-byte Spill
	testl	%ebx, %ebx
	jle	.LBB0_3
# %bb.1:                                # %.lr.ph
	movslq	-44(%rbp), %r15                 # 4-byte Folded Reload
	movq	%rbx, %r14
	movq	%r13, %r12
.LBB0_2:                                # =>This Inner Loop Header: Depth=1
	movq	%r12, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	addq	$8, %r12
	decq	%r14
	jne	.LBB0_2
.LBB0_3:                                # %._crit_edge
	leaq	-80(%rbp), %r15
	leaq	-64(%rbp), %rsi
	movq	%r15, %rdi
	callq	gettimeofday
	xorps	%xmm1, %xmm1
	cvtsi2sdq	(%r15), %xmm1
	xorps	%xmm0, %xmm0
	cvtsi2sdq	8(%r15), %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	-56(%rbp), %xmm0                # 8-byte Folded Reload
	movl	$.L.str, %edi
	movl	-44(%rbp), %esi                 # 4-byte Reload
	movl	%ebx, %edx
	movb	$1, %al
	callq	printf
	leaq	-64(%rbp), %rsi
	movq	%r15, %rdi
	callq	gettimeofday
	movq	(%r15), %r12
	movq	8(%r15), %r15
	testl	%ebx, %ebx
	jle	.LBB0_6
# %bb.4:                                # %.lr.ph24.preheader
	xorl	%r14d, %r14d
.LBB0_5:                                # %.lr.ph24
                                        # =>This Inner Loop Header: Depth=1
	movq	(%r13,%r14,8), %rdi
	callq	hipFree
	incq	%r14
	cmpq	%r14, %rbx
	jne	.LBB0_5
.LBB0_6:                                # %._crit_edge25
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r15, %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sd	%r12, %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, -56(%rbp)                # 8-byte Spill
	leaq	-80(%rbp), %r15
	leaq	-64(%rbp), %rsi
	movq	%r15, %rdi
	callq	gettimeofday
	xorps	%xmm1, %xmm1
	cvtsi2sdq	(%r15), %xmm1
	xorps	%xmm0, %xmm0
	cvtsi2sdq	8(%r15), %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	-56(%rbp), %xmm0                # 8-byte Folded Reload
	movl	$.L.str.1, %edi
	movl	-44(%rbp), %esi                 # 4-byte Reload
	movl	%ebx, %edx
	movb	$1, %al
	callq	printf
	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	_Z4testi, .Lfunc_end0-_Z4testi
	.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	$1480, %rsp                     # imm = 0x5C8
	.cfi_def_cfa_offset 1504
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rsi, %rbx
	movq	(%rsi), %rsi
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	leaq	8(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	movl	$.L.str.3, %edi
	xorl	%esi, %esi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	printf
	xorl	%edi, %edi
	callq	hipSetDevice
	movq	8(%rbx), %rdi
	callq	atoi
	movl	%eax, %edi
	callq	_Z4testi
	xorl	%eax, %eax
	addq	$1480, %rsp                     # imm = 0x5C8
	.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	total_size,@object              # @total_size
	.data
	.globl	total_size
	.p2align	2, 0x0
total_size:
	.long	1048576                         # 0x100000
	.size	total_size, 4

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"cudaMalloc(%d) x %d Time elapsed %f sec\n"
	.size	.L.str, 41

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"cudaFree(%d) x %d Time elapsed %f sec\n"
	.size	.L.str.1, 39

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%s Starting...\n"
	.size	.L.str.2, 16

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Using Device %d: %s\n"
	.size	.L.str.3, 21

	.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
