#include <stdio.h>

__global__ void initFun(int *nf) {
    int n = threadIdx.x + blockIdx.x * blockDim.x;
    nf[n] *= 10;
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        fprintf(stderr, "USAGE: main <num_of_devices> " 
                   "<device_indices>\n");
        return -1;
    }

    int *info_devs = (int *) calloc(argc - 2, sizeof(int));

    info_devs[0] = atoi(argv[1]);
    for (int i = 1; i < argc - 2; i++) {
        info_devs[i] = atoi(argv[i + 1]);
    }

    int N = atoi(argv[4]);
    float elapsed_time;

    //printf("%d\n", N);

    cudaEvent_t start, stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);

    cudaStream_t* streams;
    int **nfd = (int **) calloc(info_devs[0], sizeof(int *));
    int **nfh = (int **) calloc(info_devs[0], sizeof(int *));

    streams = (cudaStream_t *) calloc(info_devs[0], sizeof(cudaStream_t));

    cudaEventRecord(start, 0);

    for (int i = 0; i < info_devs[0]; i++) {
        cudaSetDevice(info_devs[i + 1]);
        cudaStreamCreate(&streams[i]);

        cudaMalloc((void **) &nfd[i], (N / info_devs[0]) * sizeof(int));
        cudaMallocHost((void **) &nfh[i], (N / info_devs[0]) * sizeof(int));
     
        for (int n = 0; n < N / info_devs[0]; n++)
            nfh[i][n] = n + i * N / info_devs[0];

        cudaMemcpyAsync(nfd[i], nfh[i], (N / info_devs[0]) * sizeof(int),
                    cudaMemcpyHostToDevice, streams[i]);
       
        initFun <<< N / info_devs[0] / 32, 32, 0, streams[i] >>>(nfd[i]);

        cudaMemcpyAsync(nfh[i], nfd[i], (N / info_devs[0]) * sizeof(int),
                    cudaMemcpyDeviceToHost, streams[i]);
    }

    for (int i = 0; i < info_devs[0]; i++) {
        cudaSetDevice(info_devs[i + 1]);
        cudaStreamSynchronize(streams[i]);
        
        //for (int n = 0; n < N / info_devs[0]; n++)
            //fprintf(stderr, "nfh[%d][%d] = %d\n", i, n, nfh[i][n]);
    }

    cudaSetDevice(info_devs[1]);
    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);
    cudaEventElapsedTime(&elapsed_time, start, stop);
    printf("%f\n", elapsed_time);

    for (int i = 0; i < info_devs[0]; i++) {   
        cudaStreamDestroy(streams[i]);
        cudaFree(nfd[i]);
        cudaFreeHost(nfh[i]);
        cudaDeviceReset();
    }

    return 0;
}
	.file	"tmpxft_002e5f56_00000000-6_lab7_cuda.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
	.globl	_Z26__device_stub__Z7initFunPiPi
	.type	_Z26__device_stub__Z7initFunPiPi, @function
_Z26__device_stub__Z7initFunPiPi:
.LFB2052:
	.cfi_startproc
	endbr64
	subq	$104, %rsp
	.cfi_def_cfa_offset 112
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %rcx
	leaq	16(%rsp), %rdx
	leaq	44(%rsp), %rsi
	leaq	32(%rsp), %rdi
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 40(%rsp)
	movq	%rax, 80(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	24(%rsp)
	.cfi_def_cfa_offset 120
	leaq	_Z7initFunPi(%rip), %rdi
	pushq	24(%rsp)
	.cfi_def_cfa_offset 128
	movq	60(%rsp), %rcx
	movl	68(%rsp), %r8d
	movq	48(%rsp), %rsi
	movl	56(%rsp), %edx
	leaq	96(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 120
	popq	%rdx
	.cfi_def_cfa_offset 112
.L2:
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$104, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2052:
	.size	_Z26__device_stub__Z7initFunPiPi, .-_Z26__device_stub__Z7initFunPiPi
	.globl	_Z7initFunPi
	.type	_Z7initFunPi, @function
_Z7initFunPi:
.LFB2053:
	.cfi_startproc
	endbr64
	jmp	_Z26__device_stub__Z7initFunPiPi
	.cfi_endproc
.LFE2053:
	.size	_Z7initFunPi, .-_Z7initFunPi
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"USAGE: main <num_of_devices> <device_indices>\n"
.LC1:
	.string	"%f\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
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$104, %rsp
	.cfi_def_cfa_offset 160
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	cmpl	$1, %edi
	jg	.L9
	movq	stderr(%rip), %rdi
	leaq	.LC0(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	orl	$-1, %eax
	jmp	.L8
.L9:
	leal	-2(%rdi), %ebp
	movq	%rsi, %r12
	movl	$4, %esi
	xorl	%r13d, %r13d
	movslq	%ebp, %rdi
	call	calloc@PLT
	movq	8(%r12), %rdi
	movq	%rax, %rbx
	call	atoi@PLT
	movl	%eax, (%rbx)
.L11:
	incq	%r13
	cmpl	%r13d, %ebp
	jle	.L24
	movq	8(%r12,%r13,8), %rdi
	call	atoi@PLT
	movl	%eax, (%rbx,%r13,4)
	jmp	.L11
.L24:
	movq	32(%r12), %rdi
	xorl	%r15d, %r15d
	call	atoi@PLT
	leaq	48(%rsp), %rdi
	movl	%eax, 40(%rsp)
	call	cudaEventCreate@PLT
	leaq	56(%rsp), %rdi
	call	cudaEventCreate@PLT
	movslq	(%rbx), %rbp
	movl	$8, %esi
	movq	%rbp, %rdi
	movl	%ebp, 16(%rsp)
	call	calloc@PLT
	movq	%rbp, %rdi
	movl	$8, %esi
	movq	%rax, 24(%rsp)
	call	calloc@PLT
	movq	%rbp, %rdi
	movl	$8, %esi
	movq	%rax, 32(%rsp)
	call	calloc@PLT
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	movq	%rax, %r13
	call	cudaEventRecord@PLT
	xorl	%eax, %eax
	movq	24(%rsp), %r12
	movq	%r13, %rbp
	movl	%eax, 44(%rsp)
	movq	32(%rsp), %r14
.L13:
	cmpl	%r15d, 16(%rsp)
	jle	.L25
	movl	4(%rbx,%r15,4), %edi
	call	cudaSetDevice@PLT
	movq	%rbp, %rdi
	call	cudaStreamCreate@PLT
	movl	40(%rsp), %eax
	movq	%r12, %rdi
	cltd
	idivl	16(%rsp)
	movl	%eax, 20(%rsp)
	cltq
	salq	$2, %rax
	movq	%rax, %rsi
	movq	%rax, 8(%rsp)
	call	cudaMalloc@PLT
	movq	8(%rsp), %rsi
	movq	%r14, %rdi
	call	cudaMallocHost@PLT
	movq	(%r14), %rsi
	xorl	%ecx, %ecx
.L14:
	cmpl	%ecx, 20(%rsp)
	jle	.L26
	movl	44(%rsp), %eax
	cltd
	idivl	16(%rsp)
	addl	%ecx, %eax
	movl	%eax, (%rsi,%rcx,4)
	incq	%rcx
	jmp	.L14
.L26:
	movq	0(%rbp), %r8
	movq	8(%rsp), %rdx
	movl	$1, %ecx
	movq	(%r12), %rdi
	call	cudaMemcpyAsync@PLT
	movl	$134217729, %eax
	movl	$32, %ecx
	xorl	%r8d, %r8d
	salq	$5, %rax
	movq	0(%rbp), %r9
	movl	$1, 84(%rsp)
	movq	%rax, 76(%rsp)
	movl	20(%rsp), %eax
	cltd
	idivl	%ecx
	movl	$134217729, %edx
	movl	$1, %ecx
	salq	$5, %rdx
	movl	%eax, 64(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 68(%rsp)
	movq	64(%rsp), %rdi
	movl	72(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L16
	movq	(%r12), %rdi
	call	_Z26__device_stub__Z7initFunPiPi
.L16:
	movq	0(%rbp), %r8
	movq	8(%rsp), %rdx
	incq	%r15
	addq	$8, %rbp
	movq	(%r12), %rsi
	movq	(%r14), %rdi
	movl	$2, %ecx
	addq	$8, %r12
	addq	$8, %r14
	call	cudaMemcpyAsync@PLT
	movl	40(%rsp), %edx
	addl	%edx, 44(%rsp)
	jmp	.L13
.L25:
	xorl	%ebp, %ebp
.L18:
	cmpl	%ebp, 16(%rsp)
	jle	.L27
	movl	4(%rbx,%rbp,4), %edi
	call	cudaSetDevice@PLT
	movq	0(%r13,%rbp,8), %rdi
	incq	%rbp
	call	cudaStreamSynchronize@PLT
	jmp	.L18
.L27:
	movl	4(%rbx), %edi
	xorl	%ebx, %ebx
	call	cudaSetDevice@PLT
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	56(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	leaq	76(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	76(%rsp), %xmm0
	call	__printf_chk@PLT
.L20:
	cmpl	%ebx, 16(%rsp)
	jle	.L28
	movq	0(%r13,%rbx,8), %rdi
	call	cudaStreamDestroy@PLT
	movq	24(%rsp), %rax
	movq	(%rax,%rbx,8), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rax
	movq	(%rax,%rbx,8), %rdi
	incq	%rbx
	call	cudaFreeHost@PLT
	call	cudaDeviceReset@PLT
	jmp	.L20
.L28:
	xorl	%eax, %eax
.L8:
	movq	88(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L22
	call	__stack_chk_fail@PLT
.L22:
	addq	$104, %rsp
	.cfi_def_cfa_offset 56
	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
	.section	.rodata.str1.1
.LC2:
	.string	"_Z7initFunPi"
	.section	.text.startup
	.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
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC2(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z7initFunPi(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	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
	.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>

__global__ void initFun(int *nf) {
    int n = threadIdx.x + blockIdx.x * blockDim.x;
    nf[n] *= 10;
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        fprintf(stderr, "USAGE: main <num_of_devices> " 
                   "<device_indices>\n");
        return -1;
    }

    int *info_devs = (int *) calloc(argc - 2, sizeof(int));

    info_devs[0] = atoi(argv[1]);
    for (int i = 1; i < argc - 2; i++) {
        info_devs[i] = atoi(argv[i + 1]);
    }

    int N = atoi(argv[4]);
    float elapsed_time;

    //printf("%d\n", N);

    hipEvent_t start, stop;
    hipEventCreate(&start);
    hipEventCreate(&stop);

    hipStream_t* streams;
    int **nfd = (int **) calloc(info_devs[0], sizeof(int *));
    int **nfh = (int **) calloc(info_devs[0], sizeof(int *));

    streams = (hipStream_t *) calloc(info_devs[0], sizeof(hipStream_t));

    hipEventRecord(start, 0);

    for (int i = 0; i < info_devs[0]; i++) {
        hipSetDevice(info_devs[i + 1]);
        hipStreamCreate(&streams[i]);

        hipMalloc((void **) &nfd[i], (N / info_devs[0]) * sizeof(int));
        hipHostMalloc((void **) &nfh[i], (N / info_devs[0]) * sizeof(int), hipHostMallocDefault);
     
        for (int n = 0; n < N / info_devs[0]; n++)
            nfh[i][n] = n + i * N / info_devs[0];

        hipMemcpyAsync(nfd[i], nfh[i], (N / info_devs[0]) * sizeof(int),
                    hipMemcpyHostToDevice, streams[i]);
       
        initFun <<< N / info_devs[0] / 32, 32, 0, streams[i] >>>(nfd[i]);

        hipMemcpyAsync(nfh[i], nfd[i], (N / info_devs[0]) * sizeof(int),
                    hipMemcpyDeviceToHost, streams[i]);
    }

    for (int i = 0; i < info_devs[0]; i++) {
        hipSetDevice(info_devs[i + 1]);
        hipStreamSynchronize(streams[i]);
        
        //for (int n = 0; n < N / info_devs[0]; n++)
            //fprintf(stderr, "nfh[%d][%d] = %d\n", i, n, nfh[i][n]);
    }

    hipSetDevice(info_devs[1]);
    hipEventRecord(stop, 0);
    hipEventSynchronize(stop);
    hipEventElapsedTime(&elapsed_time, start, stop);
    printf("%f\n", elapsed_time);

    for (int i = 0; i < info_devs[0]; i++) {   
        hipStreamDestroy(streams[i]);
        hipFree(nfd[i]);
        hipHostFree(nfh[i]);
        hipDeviceReset();
    }

    return 0;
}
	.text
	.file	"lab7_cuda.hip"
	.globl	_Z22__device_stub__initFunPi    # -- Begin function _Z22__device_stub__initFunPi
	.type	_Z22__device_stub__initFunPi,@function
_Z22__device_stub__initFunPi:           # @_Z22__device_stub__initFunPi
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$64, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	movq	%rsp, %rbx
	movq	%rax, (%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z7initFunPi, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$80, %rsp
	.cfi_adjust_cfa_offset -80
	popq	%rbx
	.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
	retq
.Lfunc_end0:
	.size	_Z22__device_stub__initFunPi, .Lfunc_end0-_Z22__device_stub__initFunPi
	.cfi_endproc
                                        # -- End function
	.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
	subq	$104, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$1, %edi
	jle	.LBB1_1
# %bb.2:
	movq	%rsi, %rbx
	movl	%edi, %r15d
	leal	-2(%r15), %r14d
	movl	$4, %esi
	movq	%r14, %rdi
	callq	calloc
	movq	%rax, %r13
	movq	8(%rbx), %rdi
	callq	atoi
	movl	%eax, 8(%rsp)                   # 4-byte Spill
	movl	%eax, (%r13)
	cmpl	$4, %r15d
	jb	.LBB1_5
# %bb.3:                                # %.lr.ph.preheader
	decq	%r14
	xorl	%r15d, %r15d
.LBB1_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	16(%rbx,%r15,8), %rdi
	callq	atoi
	movl	%eax, 4(%r13,%r15,4)
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB1_4
.LBB1_5:                                # %._crit_edge
	movq	32(%rbx), %rdi
	callq	atoi
	movl	%eax, 12(%rsp)                  # 4-byte Spill
	leaq	88(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	leaq	16(%rsp), %rdi
	callq	hipEventCreate
	movl	8(%rsp), %ebp                   # 4-byte Reload
	movslq	%ebp, %r14
	movl	$8, %esi
	movq	%r14, %rdi
	callq	calloc
	movq	%rax, %r12
	movl	$8, %esi
	movq	%r14, %rdi
	callq	calloc
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movl	$8, %esi
	movq	%r14, %rdi
	callq	calloc
	movq	%rax, %r14
	movq	(%rbx), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	%ebp, %r15d
	testl	%ebp, %ebp
	movq	%r14, %rbp
	jle	.LBB1_16
# %bb.6:                                # %.lr.ph99.preheader
	movq	%r15, 24(%rsp)                  # 8-byte Spill
	movabsq	$4294967296, %rax               # imm = 0x100000000
	xorl	%r15d, %r15d
	addq	$32, %rax
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	movq	%r12, 64(%rsp)                  # 8-byte Spill
	movq	%r13, 56(%rsp)                  # 8-byte Spill
	movq	%rbp, 48(%rsp)                  # 8-byte Spill
.LBB1_7:                                # %.lr.ph99
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_9 Depth 2
	movl	4(%r13,%r15,4), %edi
	callq	hipSetDevice
	leaq	(,%r15,8), %rbx
	addq	%rbp, %rbx
	movq	%rbx, %rdi
	callq	hipStreamCreate
	leaq	(%r12,%r15,8), %rdi
	movl	12(%rsp), %eax                  # 4-byte Reload
	cltd
	idivl	8(%rsp)                         # 4-byte Folded Reload
	movl	%eax, %r13d
	movslq	%eax, %r12
	leaq	(,%r12,4), %rbp
	movq	%rdi, 80(%rsp)                  # 8-byte Spill
	movq	%rbp, %rsi
	callq	hipMalloc
	movq	32(%rsp), %r14                  # 8-byte Reload
	leaq	(%r14,%r15,8), %rdi
	movq	%rdi, 72(%rsp)                  # 8-byte Spill
	movq	%rbp, %rsi
	xorl	%edx, %edx
	callq	hipHostMalloc
	movq	(%r14,%r15,8), %rsi
	testl	%r12d, %r12d
	jle	.LBB1_10
# %bb.8:                                # %.lr.ph95
                                        #   in Loop: Header=BB1_7 Depth=1
	movl	12(%rsp), %eax                  # 4-byte Reload
	imull	%r15d, %eax
	cltd
	idivl	8(%rsp)                         # 4-byte Folded Reload
                                        # kill: def $eax killed $eax def $rax
	movl	%r13d, %ecx
	xorl	%edx, %edx
.LBB1_9:                                #   Parent Loop BB1_7 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leal	(%rax,%rdx), %edi
	movl	%edi, (%rsi,%rdx,4)
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB1_9
.LBB1_10:                               # %._crit_edge96
                                        #   in Loop: Header=BB1_7 Depth=1
	movq	80(%rsp), %r14                  # 8-byte Reload
	movq	(%r14), %rdi
	movq	(%rbx), %r8
	movq	%rbp, %rdx
	movl	$1, %ecx
	callq	hipMemcpyAsync
	leal	31(%r13), %edi
	testl	%r13d, %r13d
	cmovnsl	%r13d, %edi
	sarl	$5, %edi
	movq	(%rbx), %r9
	movabsq	$4294967296, %rax               # imm = 0x100000000
	orq	%rax, %rdi
	movl	$1, %esi
	movq	40(%rsp), %rdx                  # 8-byte Reload
	movl	$1, %ecx
	xorl	%r8d, %r8d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_12
# %bb.11:                               #   in Loop: Header=BB1_7 Depth=1
	movq	(%r14), %rdi
	callq	_Z22__device_stub__initFunPi
.LBB1_12:                               #   in Loop: Header=BB1_7 Depth=1
	incq	%r15
	movq	72(%rsp), %rax                  # 8-byte Reload
	movq	(%rax), %rdi
	movq	(%r14), %rsi
	movq	(%rbx), %r8
	movq	%rbp, %rdx
	movl	$2, %ecx
	callq	hipMemcpyAsync
	cmpq	24(%rsp), %r15                  # 8-byte Folded Reload
	movq	64(%rsp), %r12                  # 8-byte Reload
	movq	56(%rsp), %r13                  # 8-byte Reload
	movq	48(%rsp), %rbp                  # 8-byte Reload
	jne	.LBB1_7
# %bb.13:                               # %.preheader
	cmpl	$0, 8(%rsp)                     # 4-byte Folded Reload
	movq	24(%rsp), %r15                  # 8-byte Reload
	jle	.LBB1_16
# %bb.14:                               # %.lr.ph101.preheader
	xorl	%ebx, %ebx
.LBB1_15:                               # %.lr.ph101
                                        # =>This Inner Loop Header: Depth=1
	leaq	1(%rbx), %r14
	movl	4(%r13,%rbx,4), %edi
	callq	hipSetDevice
	movq	(%rbp,%rbx,8), %rdi
	callq	hipStreamSynchronize
	movq	%r14, %rbx
	cmpq	%r14, %r15
	jne	.LBB1_15
.LBB1_16:                               # %._crit_edge102
	movl	4(%r13), %edi
	callq	hipSetDevice
	movq	16(%rsp), %rdi
	xorl	%ebx, %ebx
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	16(%rsp), %rdi
	callq	hipEventSynchronize
	movq	88(%rsp), %rsi
	movq	16(%rsp), %rdx
	leaq	100(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventElapsedTime
	cvtss2sd	(%r14), %xmm0
	movl	$.L.str.1, %edi
	movb	$1, %al
	callq	printf
	cmpl	$0, 8(%rsp)                     # 4-byte Folded Reload
	movq	32(%rsp), %r13                  # 8-byte Reload
	jle	.LBB1_19
# %bb.17:                               # %.lr.ph105.preheader
	xorl	%r14d, %r14d
.LBB1_18:                               # %.lr.ph105
                                        # =>This Inner Loop Header: Depth=1
	movq	(%rbp,%r14,8), %rdi
	callq	hipStreamDestroy
	movq	(%r12,%r14,8), %rdi
	callq	hipFree
	movq	(%r13,%r14,8), %rdi
	callq	hipHostFree
	callq	hipDeviceReset
	incq	%r14
	cmpq	%r14, %r15
	jne	.LBB1_18
.LBB1_19:
	movl	%ebx, %eax
	addq	$104, %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
.LBB1_1:
	.cfi_def_cfa_offset 160
	movq	stderr(%rip), %rcx
	movl	$.L.str, %edi
	movl	$46, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$-1, %ebx
	jmp	.LBB1_19
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z7initFunPi, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB3_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z7initFunPi,@object            # @_Z7initFunPi
	.section	.rodata,"a",@progbits
	.globl	_Z7initFunPi
	.p2align	3, 0x0
_Z7initFunPi:
	.quad	_Z22__device_stub__initFunPi
	.size	_Z7initFunPi, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"USAGE: main <num_of_devices> <device_indices>\n"
	.size	.L.str, 47

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

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z7initFunPi"
	.size	.L__unnamed_1, 13

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.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 _Z22__device_stub__initFunPi
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z7initFunPi
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z7initFunPi            ; -- Begin function _Z7initFunPi
	.globl	_Z7initFunPi
	.p2align	8
	.type	_Z7initFunPi,@function
_Z7initFunPi:                           ; @_Z7initFunPi
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x14
	s_load_b64 s[0:1], s[0:1], 0x0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s2, v[0:1]
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	v_add_co_u32 v0, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	global_load_b32 v2, v[0:1], off
	s_waitcnt vmcnt(0)
	v_mul_lo_u32 v2, v2, 10
	global_store_b32 v[0:1], v2, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z7initFunPi
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 264
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 3
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z7initFunPi, .Lfunc_end0-_Z7initFunPi
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 116
; NumSgprs: 18
; NumVgprs: 3
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 3
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.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_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         12
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         16
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         20
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         22
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         24
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         26
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         28
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         30
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         48
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         72
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 264
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z7initFunPi
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z7initFunPi.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     3
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
