#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <cuda_runtime.h>

#define N 1500000000

int cudaCheck(cudaError_t code) {
	if(code == cudaSuccess) {
		//printf("cudaSuccess\n");
		return 0;
	} else {
		printf("cudaCheck(): %s\n", cudaGetErrorString(cudaGetLastError()));
		return -1;
	}
}

int main() {
	void* host = malloc(N);
	if(host == NULL) {
		perror("malloc()");
		return 1;
	}
	
	void* device = NULL;
	cudaError_t ret = cudaMalloc(&device, N);
	if(cudaCheck(ret) < 0) {
		return 1;
	}
	
	unsigned long int diff = 0;
	struct timeb t_begin = {0}, t_end = {0};
	
	ftime(&t_begin);
	cudaMemcpy(host, device, N, cudaMemcpyDeviceToHost);
	ftime(&t_end);
	diff = (t_end.time * 1000 + t_end.millitm) - (t_begin.time * 1000 + t_begin.millitm);
	printf("bytes transferred from device to host: %lu\n", (unsigned long int)N);
	printf("time elapsed: %lu ms\n", diff);
	printf("speed: %f MB/s\n", ((unsigned long int)N) / (1000.0 * diff));
	
	putchar('\n');
	
	ftime(&t_begin);
	cudaMemcpy(device, host, N, cudaMemcpyHostToDevice);
	ftime(&t_end);
	diff = (t_end.time * 1000 + t_end.millitm) - (t_begin.time * 1000 + t_begin.millitm);
	printf("bytes transferred from host to device: %lu\n", (unsigned long int)N);
	printf("time elapsed: %lu ms\n", diff);
	printf("speed: %f MB/s\n", ((unsigned long int)N) / (1000.0 * diff));
	
	free(host);
	cudaFree(device);
	return 0;
}
	.file	"tmpxft_003118be_00000000-6_cuda_memcpy_bench.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
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"cudaCheck(): %s\n"
	.text
	.globl	_Z9cudaCheck9cudaError
	.type	_Z9cudaCheck9cudaError, @function
_Z9cudaCheck9cudaError:
.LFB2027:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L6
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	call	cudaGetErrorString@PLT
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	orl	$-1, %eax
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
.L6:
	xorl	%eax, %eax
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z9cudaCheck9cudaError, .-_Z9cudaCheck9cudaError
	.section	.rodata.str1.1
.LC1:
	.string	"malloc()"
.LC2:
	.string	"bytes transferred from device to host: %lu\n"
.LC3:
	.string	"time elapsed: %lu ms\n"
.LC6:
	.string	"speed: %f MB/s\n"
.LC7:
	.string	"bytes transferred from host to device: %lu\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	$1500000000, %edi
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	subq	$48, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	testq	%rax, %rax
	jne	.L13
	leaq	.LC1(%rip), %rdi
	call	perror@PLT
	jmp	.L14
.L13:
	movq	%rsp, %rdi
	movq	%rax, %rbp
	movl	$1500000000, %esi
	xorl	%eax, %eax
	movq	%rax, (%rsp)
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	_Z9cudaCheck9cudaError
	testl	%eax, %eax
	jns	.L15
.L14:
	movl	$1, %eax
	jmp	.L12
.L15:
	leaq	8(%rsp), %r14
	xorps	%xmm0, %xmm0
	leaq	24(%rsp), %rbx
	movq	%r14, %rdi
	movups	%xmm0, 8(%rsp)
	leaq	.LC3(%rip), %r13
	movups	%xmm0, 24(%rsp)
	call	ftime@PLT
	movq	(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbp, %rdi
	movl	$1500000000, %edx
	call	cudaMemcpy@PLT
	movq	%rbx, %rdi
	call	ftime@PLT
	movzwl	32(%rsp), %edx
	movzwl	16(%rsp), %ecx
	leaq	.LC2(%rip), %rsi
	imulq	$1000, 24(%rsp), %rax
	movl	$2, %edi
	addq	%rdx, %rax
	imulq	$1000, 8(%rsp), %rdx
	addq	%rcx, %rdx
	subq	%rdx, %rax
	movl	$1500000000, %edx
	movq	%rax, %r12
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
	movq	%r12, %rdx
	movq	%r13, %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	testq	%r12, %r12
	js	.L17
	cvtsi2sdq	%r12, %xmm0
	jmp	.L18
.L17:
	movq	%r12, %rdx
	movq	%r12, %rax
	shrq	%rdx
	andl	$1, %eax
	orq	%rax, %rdx
	cvtsi2sdq	%rdx, %xmm0
	addsd	%xmm0, %xmm0
.L18:
	mulsd	.LC4(%rip), %xmm0
	movsd	.LC5(%rip), %xmm1
	leaq	.LC6(%rip), %r12
	movb	$1, %al
	movq	%r12, %rsi
	movl	$2, %edi
	divsd	%xmm0, %xmm1
	movaps	%xmm1, %xmm0
	call	__printf_chk@PLT
	movl	$10, %edi
	call	putchar@PLT
	movq	%r14, %rdi
	call	ftime@PLT
	movq	(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rsi
	movl	$1500000000, %edx
	call	cudaMemcpy@PLT
	movq	%rbx, %rdi
	call	ftime@PLT
	movzwl	32(%rsp), %eax
	movzwl	16(%rsp), %edx
	leaq	.LC7(%rip), %rsi
	imulq	$1000, 24(%rsp), %rbx
	movl	$2, %edi
	addq	%rax, %rbx
	imulq	$1000, 8(%rsp), %rax
	addq	%rdx, %rax
	movl	$1500000000, %edx
	subq	%rax, %rbx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
	movq	%rbx, %rdx
	movq	%r13, %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	testq	%rbx, %rbx
	js	.L19
	cvtsi2sdq	%rbx, %xmm0
	jmp	.L20
.L19:
	movq	%rbx, %rax
	andl	$1, %ebx
	shrq	%rax
	orq	%rbx, %rax
	cvtsi2sdq	%rax, %xmm0
	addsd	%xmm0, %xmm0
.L20:
	mulsd	.LC4(%rip), %xmm0
	movq	%r12, %rsi
	movl	$2, %edi
	movb	$1, %al
	movsd	.LC5(%rip), %xmm1
	divsd	%xmm0, %xmm1
	movaps	%xmm1, %xmm0
	call	__printf_chk@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	xorl	%eax, %eax
.L12:
	movq	40(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L21
	call	__stack_chk_fail@PLT
.L21:
	addq	$48, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.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
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC4:
	.long	0
	.long	1083129856
	.align 8
.LC5:
	.long	-1073741824
	.long	1104566795
	.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 <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <hip/hip_runtime.h>

#define N 1500000000

int cudaCheck(hipError_t code) {
	if(code == hipSuccess) {
		//printf("cudaSuccess\n");
		return 0;
	} else {
		printf("cudaCheck(): %s\n", hipGetErrorString(hipGetLastError()));
		return -1;
	}
}

int main() {
	void* host = malloc(N);
	if(host == NULL) {
		perror("malloc()");
		return 1;
	}
	
	void* device = NULL;
	hipError_t ret = hipMalloc(&device, N);
	if(cudaCheck(ret) < 0) {
		return 1;
	}
	
	unsigned long int diff = 0;
	struct timeb t_begin = {0}, t_end = {0};
	
	ftime(&t_begin);
	hipMemcpy(host, device, N, hipMemcpyDeviceToHost);
	ftime(&t_end);
	diff = (t_end.time * 1000 + t_end.millitm) - (t_begin.time * 1000 + t_begin.millitm);
	printf("bytes transferred from device to host: %lu\n", (unsigned long int)N);
	printf("time elapsed: %lu ms\n", diff);
	printf("speed: %f MB/s\n", ((unsigned long int)N) / (1000.0 * diff));
	
	putchar('\n');
	
	ftime(&t_begin);
	hipMemcpy(device, host, N, hipMemcpyHostToDevice);
	ftime(&t_end);
	diff = (t_end.time * 1000 + t_end.millitm) - (t_begin.time * 1000 + t_begin.millitm);
	printf("bytes transferred from host to device: %lu\n", (unsigned long int)N);
	printf("time elapsed: %lu ms\n", diff);
	printf("speed: %f MB/s\n", ((unsigned long int)N) / (1000.0 * diff));
	
	free(host);
	hipFree(device);
	return 0;
}
	.text
	.file	"cuda_memcpy_bench.hip"
	.globl	_Z9cudaCheck10hipError_t        # -- Begin function _Z9cudaCheck10hipError_t
	.type	_Z9cudaCheck10hipError_t,@function
_Z9cudaCheck10hipError_t:               # @_Z9cudaCheck10hipError_t
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	je	.LBB0_1
# %bb.2:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	hipGetLastError
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$-1, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
	retq
.LBB0_1:
	xorl	%eax, %eax
	retq
.Lfunc_end0:
	.size	_Z9cudaCheck10hipError_t, .Lfunc_end0-_Z9cudaCheck10hipError_t
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	1127219200                      # 0x43300000
	.long	1160773632                      # 0x45300000
	.long	0                               # 0x0
	.long	0                               # 0x0
.LCPI1_1:
	.quad	0x4330000000000000              # double 4503599627370496
	.quad	0x4530000000000000              # double 1.9342813113834067E+25
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI1_2:
	.quad	0x408f400000000000              # double 1000
.LCPI1_3:
	.quad	0x41d65a0bc0000000              # double 1.5E+9
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$48, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$1500000000, %edi               # imm = 0x59682F00
	callq	malloc
	testq	%rax, %rax
	je	.LBB1_1
# %bb.2:
	movq	%rax, %rbx
	leaq	8(%rsp), %rdi
	movq	$0, (%rdi)
	movl	$1500000000, %esi               # imm = 0x59682F00
	callq	hipMalloc
	movl	%eax, %edi
	callq	_Z9cudaCheck10hipError_t
	testl	%eax, %eax
	js	.LBB1_3
# %bb.4:
	xorps	%xmm0, %xmm0
	leaq	32(%rsp), %r14
	movaps	%xmm0, (%r14)
	leaq	16(%rsp), %r15
	movaps	%xmm0, (%r15)
	movq	%r14, %rdi
	callq	ftime
	movq	8(%rsp), %rsi
	movl	$1500000000, %edx               # imm = 0x59682F00
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%r15, %rdi
	callq	ftime
	movq	(%r15), %rax
	movzwl	8(%r15), %ecx
	movzwl	8(%r14), %edx
	subq	%rdx, %rcx
	subq	(%r14), %rax
	imulq	$1000, %rax, %r12               # imm = 0x3E8
	addq	%rcx, %r12
	xorl	%ebp, %ebp
	movl	$.L.str.2, %edi
	movl	$1500000000, %esi               # imm = 0x59682F00
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.3, %edi
	movq	%r12, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	%r12, %xmm0
	punpckldq	.LCPI1_0(%rip), %xmm0   # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
	subpd	.LCPI1_1(%rip), %xmm0
	movapd	%xmm0, %xmm1
	unpckhpd	%xmm0, %xmm1                    # xmm1 = xmm1[1],xmm0[1]
	addsd	%xmm0, %xmm1
	mulsd	.LCPI1_2(%rip), %xmm1
	movsd	.LCPI1_3(%rip), %xmm0           # xmm0 = mem[0],zero
	divsd	%xmm1, %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	movl	$10, %edi
	callq	putchar
	movq	%r14, %rdi
	callq	ftime
	movq	8(%rsp), %rdi
	movl	$1500000000, %edx               # imm = 0x59682F00
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	%r15, %rdi
	callq	ftime
	movq	(%r15), %rax
	movzwl	8(%r15), %ecx
	movzwl	8(%r14), %edx
	subq	%rdx, %rcx
	subq	(%r14), %rax
	imulq	$1000, %rax, %r14               # imm = 0x3E8
	addq	%rcx, %r14
	movl	$.L.str.5, %edi
	movl	$1500000000, %esi               # imm = 0x59682F00
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.3, %edi
	movq	%r14, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	%r14, %xmm0
	punpckldq	.LCPI1_0(%rip), %xmm0   # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
	subpd	.LCPI1_1(%rip), %xmm0
	movapd	%xmm0, %xmm1
	unpckhpd	%xmm0, %xmm1                    # xmm1 = xmm1[1],xmm0[1]
	addsd	%xmm0, %xmm1
	mulsd	.LCPI1_2(%rip), %xmm1
	movsd	.LCPI1_3(%rip), %xmm0           # xmm0 = mem[0],zero
	divsd	%xmm1, %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	movq	%rbx, %rdi
	callq	free
	movq	8(%rsp), %rdi
	callq	hipFree
	jmp	.LBB1_5
.LBB1_1:
	movl	$.L.str.1, %edi
	callq	perror
.LBB1_3:
	movl	$1, %ebp
.LBB1_5:
	movl	%ebp, %eax
	addq	$48, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.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	"cudaCheck(): %s\n"
	.size	.L.str, 17

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"malloc()"
	.size	.L.str.1, 9

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"bytes transferred from device to host: %lu\n"
	.size	.L.str.2, 44

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"time elapsed: %lu ms\n"
	.size	.L.str.3, 22

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"speed: %f MB/s\n"
	.size	.L.str.4, 16

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"bytes transferred from host to device: %lu\n"
	.size	.L.str.5, 44

	.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
