#include <cstdio>
#include <cstdlib>
#include <time.h>

#include "cuda_timer.cuh"

#define SafeTimerCall(err) __safeTimerCall(err, __FILE__, __LINE__)

inline void __safeTimerCall(cudaError err, const char *file, const int line) {
#pragma warning(push)
#pragma warning(disable: 4127) Prevent warning on do-while(0);
  do {
    if (cudaSuccess != err) {
      fprintf(stderr, "CudaTimer failed at %s:%i : %s\n", file, line, cudaGetErrorString(err));
      exit(-1);
    }
  } while (0);
#pragma warning(pop)
  return;
}

CudaTimer::CudaTimer() {
  SafeTimerCall(cudaEventCreate(&_begEvent));
  SafeTimerCall(cudaEventCreate(&_endEvent));
  return;
}

CudaTimer::~CudaTimer() {
  SafeTimerCall(cudaEventDestroy(_begEvent));
  SafeTimerCall(cudaEventDestroy(_endEvent));
  return;
}

void CudaTimer::start() {
  SafeTimerCall(cudaEventRecord(_begEvent, 0));
  return;
}

void CudaTimer::stop() {
  SafeTimerCall(cudaEventRecord(_endEvent, 0));
  return;
}

float CudaTimer::value() {
  SafeTimerCall(cudaEventSynchronize(_endEvent));
  float timeVal;
  SafeTimerCall(cudaEventElapsedTime(&timeVal, _begEvent, _endEvent));
  return timeVal / CLOCKS_PER_SEC;
}
	.file	"tmpxft_0029a1fd_00000000-6_cuda_timer.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2039:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2039:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata._Z15__safeTimerCall9cudaErrorPKci.str1.1,"aMS",@progbits,1
.LC0:
	.string	"CudaTimer failed at %s:%i : %s\n"
	.section	.text._Z15__safeTimerCall9cudaErrorPKci,"axG",@progbits,_Z15__safeTimerCall9cudaErrorPKci,comdat
	.weak	_Z15__safeTimerCall9cudaErrorPKci
	.type	_Z15__safeTimerCall9cudaErrorPKci, @function
_Z15__safeTimerCall9cudaErrorPKci:
.LFB2027:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L2
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rsi, %rbx
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	movl	%edx, 12(%rsp)
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movq	%rbx, %rcx
	movl	12(%rsp), %r8d
	movq	%rax, %r9
	leaq	.LC0(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L2:
	.cfi_def_cfa_offset 8
	.cfi_restore 3
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z15__safeTimerCall9cudaErrorPKci, .-_Z15__safeTimerCall9cudaErrorPKci
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/coder-net/course-project-ca/master/gpu_mm/cuda_timer.cu"
	.text
	.align 2
	.globl	_ZN9CudaTimerC2Ev
	.type	_ZN9CudaTimerC2Ev, @function
_ZN9CudaTimerC2Ev:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC1(%rip), %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rdi, %rbx
	pushq	%rax
	.cfi_def_cfa_offset 32
	call	cudaEventCreate@PLT
	movq	%rbp, %rsi
	movl	$23, %edx
	movl	%eax, %edi
	call	_Z15__safeTimerCall9cudaErrorPKci
	leaq	8(%rbx), %rdi
	call	cudaEventCreate@PLT
	popq	%rcx
	.cfi_def_cfa_offset 24
	movq	%rbp, %rsi
	popq	%rbx
	.cfi_def_cfa_offset 16
	movl	%eax, %edi
	movl	$24, %edx
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	_Z15__safeTimerCall9cudaErrorPKci
	.cfi_endproc
.LFE2029:
	.size	_ZN9CudaTimerC2Ev, .-_ZN9CudaTimerC2Ev
	.globl	_ZN9CudaTimerC1Ev
	.set	_ZN9CudaTimerC1Ev,_ZN9CudaTimerC2Ev
	.align 2
	.globl	_ZN9CudaTimerD2Ev
	.type	_ZN9CudaTimerD2Ev, @function
_ZN9CudaTimerD2Ev:
.LFB2032:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA2032
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC1(%rip), %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rdi, %rbx
	pushq	%rax
	.cfi_def_cfa_offset 32
	movq	(%rdi), %rdi
	call	cudaEventDestroy@PLT
	movq	%rbp, %rsi
	movl	$29, %edx
	movl	%eax, %edi
	call	_Z15__safeTimerCall9cudaErrorPKci
	movq	8(%rbx), %rdi
	call	cudaEventDestroy@PLT
	movl	$30, %edx
	movq	%rbp, %rsi
	movl	%eax, %edi
	call	_Z15__safeTimerCall9cudaErrorPKci
	popq	%rdx
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA2032:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE2032-.LLSDACSB2032
.LLSDACSB2032:
.LLSDACSE2032:
	.text
	.size	_ZN9CudaTimerD2Ev, .-_ZN9CudaTimerD2Ev
	.globl	_ZN9CudaTimerD1Ev
	.set	_ZN9CudaTimerD1Ev,_ZN9CudaTimerD2Ev
	.align 2
	.globl	_ZN9CudaTimer5startEv
	.type	_ZN9CudaTimer5startEv, @function
_ZN9CudaTimer5startEv:
.LFB2034:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	(%rdi), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	$35, %edx
	leaq	.LC1(%rip), %rsi
	popq	%rcx
	.cfi_def_cfa_offset 8
	movl	%eax, %edi
	jmp	_Z15__safeTimerCall9cudaErrorPKci
	.cfi_endproc
.LFE2034:
	.size	_ZN9CudaTimer5startEv, .-_ZN9CudaTimer5startEv
	.align 2
	.globl	_ZN9CudaTimer4stopEv
	.type	_ZN9CudaTimer4stopEv, @function
_ZN9CudaTimer4stopEv:
.LFB2035:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	8(%rdi), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	$40, %edx
	leaq	.LC1(%rip), %rsi
	popq	%rcx
	.cfi_def_cfa_offset 8
	movl	%eax, %edi
	jmp	_Z15__safeTimerCall9cudaErrorPKci
	.cfi_endproc
.LFE2035:
	.size	_ZN9CudaTimer4stopEv, .-_ZN9CudaTimer4stopEv
	.align 2
	.globl	_ZN9CudaTimer5valueEv
	.type	_ZN9CudaTimer5valueEv, @function
_ZN9CudaTimer5valueEv:
.LFB2036:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC1(%rip), %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rdi, %rbx
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	movq	8(%rdi), %rdi
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	call	cudaEventSynchronize@PLT
	movl	$45, %edx
	movq	%rbp, %rsi
	movl	%eax, %edi
	call	_Z15__safeTimerCall9cudaErrorPKci
	movq	8(%rbx), %rdx
	movq	(%rbx), %rsi
	leaq	4(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movl	$47, %edx
	movq	%rbp, %rsi
	movl	%eax, %edi
	call	_Z15__safeTimerCall9cudaErrorPKci
	movss	4(%rsp), %xmm0
	divss	.LC2(%rip), %xmm0
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L17
	call	__stack_chk_fail@PLT
.L17:
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2036:
	.size	_ZN9CudaTimer5valueEv, .-_ZN9CudaTimer5valueEv
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2062:
	.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
.LFE2062:
	.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.cst4,"aM",@progbits,4
	.align 4
.LC2:
	.long	1232348160
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.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>


class CudaTimer
{
private:
  hipEvent_t _begEvent;
  hipEvent_t _endEvent;

public:
  CudaTimer();
  ~CudaTimer();
  void start();
  void stop();
  float value();
};

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