#include <cuda.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define STOP 0
#define START 1

/* Play with the following two values */
#define NB 1000000L //Size of array (long integer)
#define MANY 200L    //Number of transfers

/* (over-)Simple chronometer function */
void chrono (int kind, float *time) {
  static clock_t counts;
  if (kind == START) {
    *time = 0.0;
    counts = clock();
    return;
  }
  if (kind == STOP) {
    *time = ((float)(clock()-counts))/((float)CLOCKS_PER_SEC);
  }
}

int main () {
  float *ptr, *gpu_ptr;
  cudaError_t err;
  float time, number_of_Gbytes;
  long i, j;
  
  /* Dynamic allocations below */
  //  Allocate ptr on host below
    ptr = (float *)malloc(NB * sizeof(float));
  // Allocate gpu_ptr on device below
 err = cudaMalloc(&gpu_ptr,sizeof(float)*NB);

  /* Some error handling */
  if (ptr == NULL) {
    printf ("Not enough memory on host\n");
    exit (1);
  }
  if (err != 0) {
    printf  ("Pb allocating memory on device. Reason:\n");
    printf ("%s\n", cudaGetErrorString (err));
    exit (1);
  }

  /* Are the following two lines necessary ? */
  for (i = 0; i < NB; i++) 
    ptr[i] = 9.0;

  /* Transfer loop below */
  chrono (START, &time);
  for (j = 0; j < MANY; j++) {
    // perform memory transfer here
//    cudaMemcpy(gpu_ptr,ptr,sizeof(float)*NB,cudaMemcpyHostToDevice);
    cudaMemcpy(gpu_ptr,ptr,sizeof(float)*NB,cudaMemcpyDeviceToHost);
  }
  chrono (STOP, &time);

  /* Output results */
  number_of_Gbytes = (MANY*(float)sizeof(float)*NB/1024./1024./1024.);
  printf ("%f Gbytes transfered in %f seconds\n", number_of_Gbytes, time);
  printf ("Bandwidth = %f Gb/s\n", number_of_Gbytes / time);

  /* Clean the place */
  cudaFree (gpu_ptr);
  free (ptr);
  return 0;
}
  

	.file	"tmpxft_002e23ad_00000000-6_bandwidth.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2031:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2031:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z6chronoiPf
	.type	_Z6chronoiPf, @function
_Z6chronoiPf:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rsi, %rbx
	cmpl	$1, %edi
	jne	.L3
	movl	$0x00000000, (%rsi)
	call	clock@PLT
	movq	%rax, _ZZ6chronoiPfE6counts(%rip)
	jmp	.L2
.L3:
	testl	%edi, %edi
	jne	.L2
	call	clock@PLT
	subq	_ZZ6chronoiPfE6counts(%rip), %rax
	cvtsi2ssq	%rax, %xmm0
	divss	.LC1(%rip), %xmm0
	movss	%xmm0, (%rbx)
.L2:
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z6chronoiPf, .-_Z6chronoiPf
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC2:
	.string	"Not enough memory on host\n"
.LC3:
	.string	"Pb allocating memory on device. Reason:\n"
.LC4:
	.string	"%s\n"
.LC7:
	.string	"%f Gbytes transfered in %f seconds\n"
.LC9:
	.string	"Bandwidth = %f Gb/s\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	$4000000, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	subq	$56, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	leaq	32(%rsp), %rdi
	movl	$4000000, %esi
	movq	%rax, %rbx
	call	cudaMalloc@PLT
	testq	%rbx, %rbx
	jne	.L8
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L17
.L8:
	movss	.LC5(%rip), %xmm0
	movl	%eax, %ebp
	xorl	%edx, %edx
	testl	%eax, %eax
	je	.L9
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%ebp, %edi
	call	cudaGetErrorString@PLT
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L17:
	movl	$1, %edi
	call	exit@PLT
.L9:
	movss	%xmm0, (%rbx,%rdx,4)
	incq	%rdx
	cmpq	$1000000, %rdx
	jne	.L9
	movl	$0x00000000, 28(%rsp)
	movl	$200, %ebp
	call	clock@PLT
	movq	%rax, _ZZ6chronoiPfE6counts(%rip)
.L10:
	movq	32(%rsp), %rdi
	movl	$2, %ecx
	movl	$4000000, %edx
	movq	%rbx, %rsi
	call	cudaMemcpy@PLT
	decq	%rbp
	jne	.L10
	xorl	%edi, %edi
	leaq	28(%rsp), %rsi
	call	_Z6chronoiPf
	movss	28(%rsp), %xmm2
	movl	$2, %edi
	movb	$2, %al
	movsd	.LC6(%rip), %xmm0
	leaq	.LC7(%rip), %rsi
	cvtss2sd	%xmm2, %xmm1
	movss	%xmm2, 12(%rsp)
	call	__printf_chk@PLT
	movss	12(%rsp), %xmm2
	movl	$2, %edi
	movb	$1, %al
	movss	.LC8(%rip), %xmm0
	leaq	.LC9(%rip), %rsi
	divss	%xmm2, %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L11
	call	__stack_chk_fail@PLT
.L11:
	addq	$56, %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
.LFE2028:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2054:
	.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
.LFE2054:
	.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
	.local	_ZZ6chronoiPfE6counts
	.comm	_ZZ6chronoiPfE6counts,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC1:
	.long	1232348160
	.align 4
.LC5:
	.long	1091567616
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC6:
	.long	0
	.long	1072158596
	.section	.rodata.cst4
	.align 4
.LC8:
	.long	1061076000
	.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 <time.h>
#include <stdlib.h>
#include <stdio.h>
#define STOP 0
#define START 1

/* Play with the following two values */
#define NB 1000000L //Size of array (long integer)
#define MANY 200L    //Number of transfers

/* (over-)Simple chronometer function */
void chrono (int kind, float *time) {
  static clock_t counts;
  if (kind == START) {
    *time = 0.0;
    counts = clock();
    return;
  }
  if (kind == STOP) {
    *time = ((float)(clock()-counts))/((float)CLOCKS_PER_SEC);
  }
}

int main () {
  float *ptr, *gpu_ptr;
  hipError_t err;
  float time, number_of_Gbytes;
  long i, j;
  
  /* Dynamic allocations below */
  //  Allocate ptr on host below
    ptr = (float *)malloc(NB * sizeof(float));
  // Allocate gpu_ptr on device below
 err = hipMalloc(&gpu_ptr,sizeof(float)*NB);

  /* Some error handling */
  if (ptr == NULL) {
    printf ("Not enough memory on host\n");
    exit (1);
  }
  if (err != 0) {
    printf  ("Pb allocating memory on device. Reason:\n");
    printf ("%s\n", hipGetErrorString (err));
    exit (1);
  }

  /* Are the following two lines necessary ? */
  for (i = 0; i < NB; i++) 
    ptr[i] = 9.0;

  /* Transfer loop below */
  chrono (START, &time);
  for (j = 0; j < MANY; j++) {
    // perform memory transfer here
//    cudaMemcpy(gpu_ptr,ptr,sizeof(float)*NB,cudaMemcpyHostToDevice);
    hipMemcpy(gpu_ptr,ptr,sizeof(float)*NB,hipMemcpyDeviceToHost);
  }
  chrono (STOP, &time);

  /* Output results */
  number_of_Gbytes = (MANY*(float)sizeof(float)*NB/1024./1024./1024.);
  printf ("%f Gbytes transfered in %f seconds\n", number_of_Gbytes, time);
  printf ("Bandwidth = %f Gb/s\n", number_of_Gbytes / time);

  /* Clean the place */
  hipFree (gpu_ptr);
  free (ptr);
  return 0;
}
  

	.text
	.file	"bandwidth.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z6chronoiPf
.LCPI0_0:
	.long	0x49742400                      # float 1.0E+6
	.text
	.globl	_Z6chronoiPf
	.type	_Z6chronoiPf,@function
_Z6chronoiPf:                           # @_Z6chronoiPf
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rsi, %rbx
	testl	%edi, %edi
	je	.LBB0_3
# %bb.1:
	cmpl	$1, %edi
	jne	.LBB0_4
# %bb.2:
	movl	$0, (%rbx)
	callq	clock
	movq	%rax, _ZZ6chronoiPfE6counts(%rip)
	jmp	.LBB0_4
.LBB0_3:
	callq	clock
	subq	_ZZ6chronoiPfE6counts(%rip), %rax
	cvtsi2ss	%rax, %xmm0
	divss	.LCPI0_0(%rip), %xmm0
	movss	%xmm0, (%rbx)
.LBB0_4:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z6chronoiPf, .Lfunc_end0-_Z6chronoiPf
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0x49742400                      # float 1.0E+6
.LCPI1_2:
	.long	0x3f3ebc20                      # float 0.74505806
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI1_1:
	.quad	0x3fe7d78400000000              # double 0.74505805969238281
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.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	$16, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %rbp, -16
	movl	$4000000, %edi                  # imm = 0x3D0900
	callq	malloc
	movq	%rax, %rbx
	leaq	8(%rsp), %rdi
	movl	$4000000, %esi                  # imm = 0x3D0900
	callq	hipMalloc
	testq	%rbx, %rbx
	je	.LBB1_1
# %bb.3:
	movl	%eax, %ebp
	testl	%eax, %eax
	jne	.LBB1_9
# %bb.4:                                # %.preheader.preheader
	xorl	%eax, %eax
.LBB1_5:                                # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	movl	$1091567616, (%rbx,%rax,4)      # imm = 0x41100000
	incq	%rax
	cmpq	$1000000, %rax                  # imm = 0xF4240
	jne	.LBB1_5
# %bb.6:
	callq	clock
	movq	%rax, _ZZ6chronoiPfE6counts(%rip)
	movl	$200, %r14d
.LBB1_7:                                # =>This Inner Loop Header: Depth=1
	movq	8(%rsp), %rdi
	movl	$4000000, %edx                  # imm = 0x3D0900
	movq	%rbx, %rsi
	movl	$2, %ecx
	callq	hipMemcpy
	decq	%r14
	jne	.LBB1_7
# %bb.8:
	callq	clock
	subq	_ZZ6chronoiPfE6counts(%rip), %rax
	cvtsi2ss	%rax, %xmm0
	divss	.LCPI1_0(%rip), %xmm0
	movss	%xmm0, 4(%rsp)                  # 4-byte Spill
	cvtss2sd	%xmm0, %xmm1
	movsd	.LCPI1_1(%rip), %xmm0           # xmm0 = mem[0],zero
	movl	$.L.str.3, %edi
	movb	$2, %al
	callq	printf
	movss	.LCPI1_2(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	divss	4(%rsp), %xmm0                  # 4-byte Folded Reload
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$16, %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
.LBB1_1:
	.cfi_def_cfa_offset 48
	movl	$.Lstr.1, %edi
	jmp	.LBB1_2
.LBB1_9:
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	%ebp, %edi
	callq	hipGetErrorString
	movq	%rax, %rdi
.LBB1_2:
	callq	puts@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	_ZZ6chronoiPfE6counts,@object   # @_ZZ6chronoiPfE6counts
	.local	_ZZ6chronoiPfE6counts
	.comm	_ZZ6chronoiPfE6counts,8,8
	.type	.L.str.3,@object                # @.str.3
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.3:
	.asciz	"%f Gbytes transfered in %f seconds\n"
	.size	.L.str.3, 36

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Bandwidth = %f Gb/s\n"
	.size	.L.str.4, 21

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Pb allocating memory on device. Reason:"
	.size	.Lstr, 40

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Not enough memory on host"
	.size	.Lstr.1, 26

	.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
