/*****************************************************************************
 * A microbenchmark to test the performance of varying memory copy operations
 * including different sizes and different sources and destinations
 ****************************************************************************/

#include <stdio.h>
#include <stdint.h>
#include <string.h>
extern "C" {
    void m5_dumpreset_stats(uint64_t workid, uint64_t threadid);
}

int main(int argc, char** argv) {

    unsigned minimum_elements = 2;
    unsigned maximum_elements = 16 * 1024 * 1024;

    for (int index = 0; index < argc; ++index) {
        if (strcmp(argv[index], "-m") == 0) {
            if (argc > index+1) {
                minimum_elements = atoi(argv[++index]);
            } else {
                printf("ERROR: Must specify minimum array size to -m option\n");
                exit(0);
            }
        } else if (strcmp(argv[index], "-M") == 0) {
            if (argc > index+1) {
                maximum_elements = atoi(argv[++index]);
            } else {
                printf("ERROR: Must specify maximum array size to -M option\n");
                exit(0);
            }
        }
    }

    bool power_two = true;
    unsigned *array = (unsigned*) malloc(maximum_elements * sizeof(unsigned));
    unsigned *d_array;
    cudaMalloc(&d_array, maximum_elements * sizeof(unsigned));

    printf("Testing copy host-to-device:\nSize:\n");
    m5_dumpreset_stats(0, 0);
    for (unsigned i = minimum_elements; i <= maximum_elements;) {
        printf("%d\n", i);
        cudaMemcpy(d_array, array, i * sizeof(unsigned), cudaMemcpyHostToDevice);
        m5_dumpreset_stats(0, 0);
        if (power_two) {
            if (i > 1) {
                i = 3 * i / 2;
                power_two = false;
            } else {
                i = 2;
            }
        } else {
            i = 4 * i / 3;
            power_two = true;
        }
    }

    printf("\n\nTesting copy device-to-host:\nSize:\n");
    m5_dumpreset_stats(0, 0);
    for (unsigned i = minimum_elements; i <= maximum_elements;) {
        printf("%d\n", i);
        cudaMemcpy(array, d_array, i * sizeof(unsigned), cudaMemcpyDeviceToHost);
        m5_dumpreset_stats(0, 0);
        if (power_two) {
            if (i > 1) {
                i = 3 * i / 2;
                power_two = false;
            } else {
                i = 2;
            }
        } else {
            i = 4 * i / 3;
            power_two = true;
        }
    }
    printf("\n");
    free(array);
    cudaFree(d_array);

    return 0;
}

	.file	"tmpxft_00385ebf_00000000-6_memcpy_load.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"-m"
.LC1:
	.string	"ERROR: Must specify minimum array size to -m option\n"
.LC2:
	.string	"-M"
.LC3:
	.string	"ERROR: Must specify maximum array size to -M option\n"
.LC4:
	.string	"Testing copy host-to-device:\nSize:\n"
.LC5:
	.string	"%d\n"
.LC6:
	.string	"\n\nTesting copy device-to-host:\nSize:\n"
.LC7:
	.string	"\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rsi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	$16777216, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	$2, %ebx
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	movl	%edi, 4(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
.L3:
	movl	4(%rsp), %eax
	cmpl	%eax, %r12d
	jge	.L24
	movslq	%r12d, %rax
	leaq	.LC0(%rip), %rsi
	leal	1(%r12), %r14d
	movq	0(%r13,%rax,8), %rdi
	leaq	0(,%rax,8), %r15
	movq	%rdi, 8(%rsp)
	call	strcmp@PLT
	movq	8(%rsp), %rdi
	testl	%eax, %eax
	jne	.L4
	cmpl	%r14d, 4(%rsp)
	jle	.L5
	movq	8(%r13,%r15), %rdi
	call	atoi@PLT
	movl	%eax, %ebx
	jmp	.L6
.L5:
	leaq	.LC1(%rip), %rsi
	jmp	.L22
.L4:
	leaq	.LC2(%rip), %rsi
	call	strcmp@PLT
	testl	%eax, %eax
	jne	.L18
	cmpl	%r14d, 4(%rsp)
	jle	.L7
	movq	8(%r13,%r15), %rdi
	call	atoi@PLT
	movl	%eax, %ebp
	jmp	.L6
.L7:
	leaq	.LC3(%rip), %rsi
.L22:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
.L18:
	movl	%r12d, %r14d
.L6:
	leal	1(%r14), %r12d
	jmp	.L3
.L24:
	movl	%ebp, %r12d
	movb	$1, %r15b
	leaq	.LC5(%rip), %r14
	salq	$2, %r12
	movq	%r12, %rdi
	call	malloc@PLT
	movq	%r12, %rsi
	leaq	16(%rsp), %rdi
	movl	%ebx, %r12d
	movq	%rax, %r13
	call	cudaMalloc@PLT
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%esi, %esi
	xorl	%edi, %edi
	call	m5_dumpreset_stats@PLT
.L9:
	cmpl	%r12d, %ebp
	jb	.L25
	movl	%r12d, %edx
	movq	%r14, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	16(%rsp), %rdi
	movl	%r12d, %edx
	movq	%r13, %rsi
	salq	$2, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	xorl	%esi, %esi
	xorl	%edi, %edi
	call	m5_dumpreset_stats@PLT
	testb	%r15b, %r15b
	je	.L10
	cmpl	$1, %r12d
	jbe	.L19
	leal	(%r12,%r12,2), %r12d
	xorl	%r15d, %r15d
	shrl	%r12d
	jmp	.L9
.L10:
	sall	$2, %r12d
	movl	$3, %ecx
	xorl	%edx, %edx
	movb	$1, %r15b
	movl	%r12d, %eax
	divl	%ecx
	movl	%eax, %r12d
	jmp	.L9
.L19:
	movl	$2, %r12d
	jmp	.L9
.L25:
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%esi, %esi
	xorl	%edi, %edi
	leaq	.LC5(%rip), %r12
	call	m5_dumpreset_stats@PLT
.L13:
	cmpl	%ebx, %ebp
	jb	.L26
	movl	%ebx, %edx
	movq	%r12, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	16(%rsp), %rsi
	movl	%ebx, %edx
	movq	%r13, %rdi
	salq	$2, %rdx
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	xorl	%esi, %esi
	xorl	%edi, %edi
	call	m5_dumpreset_stats@PLT
	testb	%r15b, %r15b
	je	.L14
	cmpl	$1, %ebx
	jbe	.L20
	leal	(%rbx,%rbx,2), %ebx
	xorl	%r15d, %r15d
	shrl	%ebx
	jmp	.L13
.L14:
	sall	$2, %ebx
	movl	$3, %ecx
	xorl	%edx, %edx
	movb	$1, %r15b
	movl	%ebx, %eax
	divl	%ecx
	movl	%eax, %ebx
	jmp	.L13
.L20:
	movl	$2, %ebx
	jmp	.L13
.L26:
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L17
	call	__stack_chk_fail@PLT
.L17:
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2053:
	.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
.LFE2053:
	.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:

/*****************************************************************************
 * A microbenchmark to test the performance of varying memory copy operations
 * including different sizes and different sources and destinations
 ****************************************************************************/


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
extern "C" {
    void m5_dumpreset_stats(uint64_t workid, uint64_t threadid);
}

int main(int argc, char** argv) {

    unsigned minimum_elements = 2;
    unsigned maximum_elements = 16 * 1024 * 1024;

    for (int index = 0; index < argc; ++index) {
        if (strcmp(argv[index], "-m") == 0) {
            if (argc > index+1) {
                minimum_elements = atoi(argv[++index]);
            } else {
                printf("ERROR: Must specify minimum array size to -m option\n");
                exit(0);
            }
        } else if (strcmp(argv[index], "-M") == 0) {
            if (argc > index+1) {
                maximum_elements = atoi(argv[++index]);
            } else {
                printf("ERROR: Must specify maximum array size to -M option\n");
                exit(0);
            }
        }
    }

    bool power_two = true;
    unsigned *array = (unsigned*) malloc(maximum_elements * sizeof(unsigned));
    unsigned *d_array;
    hipMalloc(&d_array, maximum_elements * sizeof(unsigned));

    printf("Testing copy host-to-device:\nSize:\n");
    m5_dumpreset_stats(0, 0);
    for (unsigned i = minimum_elements; i <= maximum_elements;) {
        printf("%d\n", i);
        hipMemcpy(d_array, array, i * sizeof(unsigned), hipMemcpyHostToDevice);
        m5_dumpreset_stats(0, 0);
        if (power_two) {
            if (i > 1) {
                i = 3 * i / 2;
                power_two = false;
            } else {
                i = 2;
            }
        } else {
            i = 4 * i / 3;
            power_two = true;
        }
    }

    printf("\n\nTesting copy device-to-host:\nSize:\n");
    m5_dumpreset_stats(0, 0);
    for (unsigned i = minimum_elements; i <= maximum_elements;) {
        printf("%d\n", i);
        hipMemcpy(array, d_array, i * sizeof(unsigned), hipMemcpyDeviceToHost);
        m5_dumpreset_stats(0, 0);
        if (power_two) {
            if (i > 1) {
                i = 3 * i / 2;
                power_two = false;
            } else {
                i = 2;
            }
        } else {
            i = 4 * i / 3;
            power_two = true;
        }
    }
    printf("\n");
    free(array);
    hipFree(d_array);

    return 0;
}

	.text
	.file	"memcpy_load.hip"
	.globl	main                            # -- Begin function 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	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	pushq	%rax
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	testl	%edi, %edi
	jle	.LBB0_1
# %bb.7:                                # %.lr.ph.preheader
	movq	%rsi, %r15
	movl	%edi, %ebp
	movl	$16777216, %ebx                 # imm = 0x1000000
	xorl	%r13d, %r13d
	movl	$2, %r14d
.LBB0_8:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movslq	%r13d, %rax
	movq	(%r15,%rax,8), %r12
	movl	$.L.str, %esi
	movq	%r12, %rdi
	callq	strcmp
	testl	%eax, %eax
	je	.LBB0_9
# %bb.13:                               #   in Loop: Header=BB0_8 Depth=1
	movl	$.L.str.2, %esi
	movq	%r12, %rdi
	callq	strcmp
	testl	%eax, %eax
	jne	.LBB0_16
# %bb.14:                               #   in Loop: Header=BB0_8 Depth=1
	incl	%r13d
	cmpl	%ebp, %r13d
	jge	.LBB0_27
# %bb.15:                               #   in Loop: Header=BB0_8 Depth=1
	movslq	%r13d, %rax
	movq	(%r15,%rax,8), %rdi
	callq	atoi
	movl	%eax, %ebx
	jmp	.LBB0_16
.LBB0_9:                                #   in Loop: Header=BB0_8 Depth=1
	incl	%r13d
	cmpl	%ebp, %r13d
	jge	.LBB0_11
# %bb.10:                               #   in Loop: Header=BB0_8 Depth=1
	movslq	%r13d, %rax
	movq	(%r15,%rax,8), %rdi
	callq	atoi
	movl	%eax, %r14d
.LBB0_16:                               #   in Loop: Header=BB0_8 Depth=1
	incl	%r13d
	cmpl	%ebp, %r13d
	jl	.LBB0_8
	jmp	.LBB0_2
.LBB0_1:
	movl	$2, %r14d
	movl	$16777216, %ebx                 # imm = 0x1000000
.LBB0_2:                                # %._crit_edge
	movl	%ebx, %r12d
	shlq	$2, %r12
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %r15
	movq	%rsp, %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	movl	$.Lstr, %edi
	callq	puts@PLT
	xorl	%edi, %edi
	xorl	%esi, %esi
	callq	m5_dumpreset_stats
	movb	$1, %bpl
	cmpl	%ebx, %r14d
	ja	.LBB0_17
# %bb.3:                                # %.lr.ph68.preheader
	movb	$1, %bpl
	movl	$2863311531, %r13d              # imm = 0xAAAAAAAB
	movl	%r14d, %r12d
.LBB0_4:                                # %.lr.ph68
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.5, %edi
	movl	%r12d, %esi
	xorl	%eax, %eax
	callq	printf
	movq	(%rsp), %rdi
	movl	%r12d, %edx
	shlq	$2, %rdx
	movq	%r15, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	xorl	%edi, %edi
	xorl	%esi, %esi
	callq	m5_dumpreset_stats
	testb	$1, %bpl
	je	.LBB0_22
# %bb.5:                                #   in Loop: Header=BB0_4 Depth=1
	movl	$2, %eax
	cmpl	$2, %r12d
	jb	.LBB0_23
# %bb.6:                                #   in Loop: Header=BB0_4 Depth=1
	leal	(%r12,%r12,2), %eax
	shrl	%eax
	xorl	%ebp, %ebp
	jmp	.LBB0_23
.LBB0_22:                               #   in Loop: Header=BB0_4 Depth=1
	shll	$2, %r12d
	imulq	%r13, %r12
	shrq	$33, %r12
	movb	$1, %bpl
	movl	%r12d, %eax
.LBB0_23:                               #   in Loop: Header=BB0_4 Depth=1
	movl	%eax, %r12d
	cmpl	%ebx, %eax
	jbe	.LBB0_4
.LBB0_17:                               # %._crit_edge69
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	xorl	%edi, %edi
	xorl	%esi, %esi
	callq	m5_dumpreset_stats
	cmpl	%ebx, %r14d
	ja	.LBB0_26
# %bb.18:
	movl	$2863311531, %r12d              # imm = 0xAAAAAAAB
.LBB0_19:                               # %.lr.ph75
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.5, %edi
	movl	%r14d, %esi
	xorl	%eax, %eax
	callq	printf
	movq	(%rsp), %rsi
	movl	%r14d, %edx
	shlq	$2, %rdx
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	xorl	%edi, %edi
	xorl	%esi, %esi
	callq	m5_dumpreset_stats
	testb	$1, %bpl
	je	.LBB0_24
# %bb.20:                               #   in Loop: Header=BB0_19 Depth=1
	movl	$2, %eax
	cmpl	$2, %r14d
	jb	.LBB0_25
# %bb.21:                               #   in Loop: Header=BB0_19 Depth=1
	leal	(%r14,%r14,2), %eax
	shrl	%eax
	xorl	%ebp, %ebp
	jmp	.LBB0_25
.LBB0_24:                               #   in Loop: Header=BB0_19 Depth=1
	shll	$2, %r14d
	imulq	%r12, %r14
	shrq	$33, %r14
	movb	$1, %bpl
	movl	%r14d, %eax
.LBB0_25:                               #   in Loop: Header=BB0_19 Depth=1
	movl	%eax, %r14d
	cmpl	%ebx, %eax
	jbe	.LBB0_19
.LBB0_26:                               # %._crit_edge76
	movl	$10, %edi
	callq	putchar@PLT
	movq	%r15, %rdi
	callq	free
	movq	(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.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
.LBB0_11:
	.cfi_def_cfa_offset 64
	movl	$.Lstr.3, %edi
	jmp	.LBB0_12
.LBB0_27:
	movl	$.Lstr.2, %edi
.LBB0_12:
	callq	puts@PLT
	xorl	%edi, %edi
	callq	exit
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"-m"
	.size	.L.str, 3

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"-M"
	.size	.L.str.2, 3

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"%d\n"
	.size	.L.str.5, 4

	.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	"Testing copy host-to-device:\nSize:"
	.size	.Lstr, 35

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"\n\nTesting copy device-to-host:\nSize:"
	.size	.Lstr.1, 37

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"ERROR: Must specify maximum array size to -M option"
	.size	.Lstr.2, 52

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"ERROR: Must specify minimum array size to -m option"
	.size	.Lstr.3, 52

	.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
