//
// Created by kindr on 2021/5/8.
//

#include "multiKernelConcurrent.cuh"
#include "../../common/utils.cuh"
#include <cstdio>

const int N = 1 << 25;

__global__
void math_kernel1(int n) {
    double sum = 0;
    for (int i = 0; i < n; i++) sum += tan(0.1) * tan(0.1);
    printf("sum=%g\n", sum);
}

__global__
void math_kernel2(int n) {
    double sum = 0;
    for (int i = 0; i < n; i++) sum += tan(0.1) * tan(0.1);
    printf("sum=%g\n", sum);
}

void multiKernelConcurrent() {

    int n_stream = 4;
    size_t nStreamBytes = n_stream * sizeof(cudaStream_t);
    auto *stream = static_cast<cudaStream_t *>(malloc(nStreamBytes));
    for (int i = 0; i < n_stream; i++) {
        cudaStreamCreate(&stream[i]);
    }
    CHECK(cudaGetLastError());

    for (int i = 0; i < n_stream; i++) {
        math_kernel1<<<1, 1, 0, stream[i]>>>(N);
        math_kernel2<<<1, 1, 0, stream[i]>>>(N);
    }

    for (int i = 0; i < n_stream; i++) {
        math_kernel1<<<1, 1>>>(N);
        math_kernel2<<<1, 1>>>(N);
    }

    cudaDeviceSynchronize();
    CHECK(cudaGetLastError());

    for (int i = 0; i < n_stream; i++) {
        cudaStreamDestroy(stream[i]);
    }

    free(stream);
}

	.file	"tmpxft_00230d02_00000000-6_multiKernelConcurrent.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3791:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3791:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z31__device_stub__Z12math_kernel1ii
	.type	_Z31__device_stub__Z12math_kernel1ii, @function
_Z31__device_stub__Z12math_kernel1ii:
.LFB3813:
	.cfi_startproc
	endbr64
	subq	$104, %rsp
	.cfi_def_cfa_offset 112
	movl	%edi, 12(%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	12(%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	_Z12math_kernel1i(%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
.LFE3813:
	.size	_Z31__device_stub__Z12math_kernel1ii, .-_Z31__device_stub__Z12math_kernel1ii
	.globl	_Z12math_kernel1i
	.type	_Z12math_kernel1i, @function
_Z12math_kernel1i:
.LFB3814:
	.cfi_startproc
	endbr64
	jmp	_Z31__device_stub__Z12math_kernel1ii
	.cfi_endproc
.LFE3814:
	.size	_Z12math_kernel1i, .-_Z12math_kernel1i
	.globl	_Z31__device_stub__Z12math_kernel2ii
	.type	_Z31__device_stub__Z12math_kernel2ii, @function
_Z31__device_stub__Z12math_kernel2ii:
.LFB3815:
	.cfi_startproc
	endbr64
	subq	$104, %rsp
	.cfi_def_cfa_offset 112
	movl	%edi, 12(%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	12(%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	.L8
	pushq	24(%rsp)
	.cfi_def_cfa_offset 120
	leaq	_Z12math_kernel2i(%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
.L8:
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$104, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3815:
	.size	_Z31__device_stub__Z12math_kernel2ii, .-_Z31__device_stub__Z12math_kernel2ii
	.globl	_Z12math_kernel2i
	.type	_Z12math_kernel2i, @function
_Z12math_kernel2i:
.LFB3816:
	.cfi_startproc
	endbr64
	jmp	_Z31__device_stub__Z12math_kernel2ii
	.cfi_endproc
.LFE3816:
	.size	_Z12math_kernel2i, .-_Z12math_kernel2i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/KindRoach/LearnCuda/master/task/stream/multiKernelConcurrent.cu"
.LC1:
	.string	"Error: %s:%d, "
.LC2:
	.string	"code: %d, reason: %s\n"
	.text
	.globl	_Z21multiKernelConcurrentv
	.type	_Z21multiKernelConcurrentv, @function
_Z21multiKernelConcurrentv:
.LFB3788:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movl	$32, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	call	malloc@PLT
	movq	%rax, %rbx
	movq	%rax, %rdi
	call	cudaStreamCreate@PLT
	leaq	8(%rbx), %rdi
	call	cudaStreamCreate@PLT
	leaq	16(%rbx), %rdi
	call	cudaStreamCreate@PLT
	leaq	24(%rbx), %rdi
	call	cudaStreamCreate@PLT
	call	cudaGetLastError@PLT
	testl	%eax, %eax
	je	.L14
	movq	stderr(%rip), %rdi
	movl	%eax, %ebp
	xorl	%eax, %eax
	movl	$33, %r8d
	leaq	.LC0(%rip), %rcx
	leaq	.LC1(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%ebp, %edi
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	%ebp, %ecx
	leaq	.LC2(%rip), %rdx
	movq	%rax, %r8
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L14:
	movabsq	$4294967297, %rbp
	xorl	%r12d, %r12d
.L17:
	movq	(%rbx,%r12,8), %r9
	xorl	%r8d, %r8d
	movq	%rbp, %rdx
	movl	$1, %ecx
	movq	%rbp, %rdi
	movl	$1, %esi
	movq	%rbp, 20(%rsp)
	movl	$1, 28(%rsp)
	movq	%rbp, 8(%rsp)
	movl	$1, 16(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L15
	movl	$33554432, %edi
	call	_Z31__device_stub__Z12math_kernel1ii
.L15:
	movq	(%rbx,%r12,8), %r9
	xorl	%r8d, %r8d
	movq	%rbp, %rdx
	movl	$1, %ecx
	movq	%rbp, %rdi
	movl	$1, %esi
	movq	%rbp, 20(%rsp)
	movl	$1, 28(%rsp)
	movq	%rbp, 8(%rsp)
	movl	$1, 16(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L16
	movl	$33554432, %edi
	call	_Z31__device_stub__Z12math_kernel2ii
.L16:
	incq	%r12
	cmpq	$4, %r12
	jne	.L17
	movabsq	$4294967297, %rbp
.L20:
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%rbp, %rdx
	movl	$1, %ecx
	movq	%rbp, %rdi
	movl	$1, %esi
	movq	%rbp, 20(%rsp)
	movl	$1, 28(%rsp)
	movq	%rbp, 8(%rsp)
	movl	$1, 16(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L18
	movl	$33554432, %edi
	call	_Z31__device_stub__Z12math_kernel1ii
.L18:
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%rbp, %rdx
	movl	$1, %ecx
	movq	%rbp, %rdi
	movl	$1, %esi
	movq	%rbp, 20(%rsp)
	movl	$1, 28(%rsp)
	movq	%rbp, 8(%rsp)
	movl	$1, 16(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L19
	movl	$33554432, %edi
	call	_Z31__device_stub__Z12math_kernel2ii
.L19:
	decl	%r12d
	jne	.L20
	call	cudaDeviceSynchronize@PLT
	call	cudaGetLastError@PLT
	movl	%eax, %ebp
	testl	%eax, %eax
	je	.L21
	movq	stderr(%rip), %rdi
	movl	$46, %r8d
	xorl	%eax, %eax
	leaq	.LC0(%rip), %rcx
	leaq	.LC1(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%ebp, %edi
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	%ebp, %ecx
	leaq	.LC2(%rip), %rdx
	movq	%rax, %r8
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L21:
	xorl	%ebp, %ebp
.L22:
	movq	(%rbx,%rbp,8), %rdi
	incq	%rbp
	call	cudaStreamDestroy@PLT
	cmpq	$4, %rbp
	jne	.L22
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	free@PLT
	.cfi_endproc
.LFE3788:
	.size	_Z21multiKernelConcurrentv, .-_Z21multiKernelConcurrentv
	.section	.rodata.str1.1
.LC3:
	.string	"_Z12math_kernel2i"
.LC4:
	.string	"_Z12math_kernel1i"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3818:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -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	.LC3(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z12math_kernel2i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC4(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z12math_kernel1i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	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	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3818:
	.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:

//
// Created by kindr on 2021/5/8.
//

#ifndef LEARNCUDA_MULTIKERNELCONCURRENT_CUH
#define LEARNCUDA_MULTIKERNELCONCURRENT_CUH

__global__ void math_kernel1(int n);

__global__ void math_kernel2(int n);

void multiKernelConcurrent();

#endif //LEARNCUDA_MULTIKERNELCONCURRENT_CUH

	.text
	.file	"multiKernelConcurrent.hip"
	.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
