#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdlib.h>
#include <stdio.h>
#include "cudaHelpers.cuh"

void waitForKernel() {
	CUDA_CHECK_RETURN(cudaGetLastError());
	CUDA_CHECK_RETURN(cudaDeviceSynchronize());
}

void resetDevice() {
	CUDA_CHECK_RETURN(cudaDeviceReset());
}

void setDevice(int device) {
	CUDA_CHECK_RETURN(cudaSetDevice(device));
}
	.file	"tmpxft_00205d4c_00000000-6_cudaHelpers.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2035:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2035:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Error: %s \n Error Code %d \n Error is at line %d, in file %s \n"
.LC1:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/peturssonstefan/LSHonGPU/master/LinearScan/cudaHelpers.cu"
	.text
	.globl	_Z13waitForKernelv
	.type	_Z13waitForKernelv, @function
_Z13waitForKernelv:
.LFB2030:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	call	cudaGetLastError@PLT
	testl	%eax, %eax
	je	.L3
	movl	%eax, %edi
	movl	%eax, 12(%rsp)
	call	cudaGetErrorString@PLT
	movl	$8, %r9d
	movq	%rax, %rcx
	leaq	.LC1(%rip), %rax
	pushq	%rdi
	.cfi_remember_state
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	jmp	.L6
.L3:
	.cfi_restore_state
	call	cudaDeviceSynchronize@PLT
	testl	%eax, %eax
	je	.L2
	movl	%eax, %edi
	movl	%eax, 12(%rsp)
	call	cudaGetErrorString@PLT
	movl	$9, %r9d
	movq	%rax, %rcx
	leaq	.LC1(%rip), %rax
	pushq	%rsi
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
.L6:
	movl	28(%rsp), %r8d
	movq	stderr(%rip), %rdi
	leaq	.LC0(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	popq	%rax
	.cfi_def_cfa_offset 40
	popq	%rdx
	.cfi_def_cfa_offset 32
	call	cudaDeviceReset@PLT
	orl	$-1, %edi
	call	exit@PLT
.L2:
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z13waitForKernelv, .-_Z13waitForKernelv
	.globl	_Z11resetDevicev
	.type	_Z11resetDevicev, @function
_Z11resetDevicev:
.LFB2031:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	call	cudaDeviceReset@PLT
	testl	%eax, %eax
	je	.L8
	movl	%eax, %edi
	movl	%eax, 12(%rsp)
	call	cudaGetErrorString@PLT
	movl	$13, %r9d
	movl	$2, %esi
	movq	stderr(%rip), %rdi
	movq	%rax, %rcx
	pushq	%rax
	.cfi_def_cfa_offset 40
	leaq	.LC1(%rip), %rax
	leaq	.LC0(%rip), %rdx
	pushq	%rax
	.cfi_def_cfa_offset 48
	movl	28(%rsp), %r8d
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	popq	%rdx
	.cfi_def_cfa_offset 40
	popq	%rcx
	.cfi_def_cfa_offset 32
	call	cudaDeviceReset@PLT
	orl	$-1, %edi
	call	exit@PLT
.L8:
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z11resetDevicev, .-_Z11resetDevicev
	.globl	_Z9setDevicei
	.type	_Z9setDevicei, @function
_Z9setDevicei:
.LFB2032:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	call	cudaSetDevice@PLT
	testl	%eax, %eax
	je	.L11
	movl	%eax, %edi
	movl	%eax, 12(%rsp)
	call	cudaGetErrorString@PLT
	movl	$17, %r9d
	movl	$2, %esi
	movq	stderr(%rip), %rdi
	movq	%rax, %rcx
	pushq	%rax
	.cfi_def_cfa_offset 40
	leaq	.LC1(%rip), %rax
	leaq	.LC0(%rip), %rdx
	pushq	%rax
	.cfi_def_cfa_offset 48
	movl	28(%rsp), %r8d
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	popq	%rdx
	.cfi_def_cfa_offset 40
	popq	%rcx
	.cfi_def_cfa_offset 32
	call	cudaDeviceReset@PLT
	orl	$-1, %edi
	call	exit@PLT
.L11:
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z9setDevicei, .-_Z9setDevicei
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2058:
	.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
.LFE2058:
	.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:

#pragma once

#include "hip/hip_runtime.h"

#include <stdlib.h>
#include <stdio.h>

#define CUDA_CHECK_RETURN(value){ \
	hipError_t _m_cudaStat = value; \
	if (_m_cudaStat != hipSuccess) { \
		fprintf(stderr, "Error: %s \n Error Code %d \n Error is at line %d, in file %s \n", hipGetErrorString(_m_cudaStat), _m_cudaStat, __LINE__ , __FILE__); \
		hipDeviceReset(); \
		exit(-1); \
	} \
}\

template <class T>
T* mallocArray(T* host_array, int arraySize, bool copyData = false) {
	T* dev_array;
	CUDA_CHECK_RETURN(hipMalloc((void**)&dev_array, arraySize * sizeof(T)));

	if (copyData)
		CUDA_CHECK_RETURN(hipMemcpy(dev_array, host_array, arraySize * sizeof(T), hipMemcpyHostToDevice));

	return dev_array;
}

template<class T>
void copyArrayToHost(T* destination, T* source, int size) {
	CUDA_CHECK_RETURN(hipMemcpy(destination, source, size * sizeof(T), hipMemcpyDeviceToHost));
}

template<class T>
void freeDeviceArray(T* dev_array) {
	CUDA_CHECK_RETURN(hipFree(dev_array));
}

void waitForKernel();

void resetDevice();

void setDevice(int device = 0);



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