#include "utilities.cuh"


cudaError_t arrayMalloc(void*** array, int length, size_t size)
{
	cudaError_t cudaStatus;

	for (int i = 0; i < length; i++)
	{
		cudaStatus = cudaMalloc(array[i], size);
		if (cudaStatus != cudaSuccess)
		{
			fprintf(stderr, "cudaMalloc failed!\n");
			goto Error;
		}
	}
Error:
	for (size_t i = 0; i < length; i++)
	{
		cudaFree(array[i]);
	}

	return cudaStatus;
}

cudaError_t arraycpyHtoD(void*** array_d, void*** array_h, int length, size_t size)
{
	cudaError_t cudaStatus;

	for (int i = 0; i < length; i++)
	{
		cudaStatus = cudaMemcpy(*array_d[i], *array_h[i], size, cudaMemcpyHostToDevice);
		if (cudaStatus != cudaSuccess)
		{
			fprintf(stderr, "cudaMemcpy HostToDevice of array %d failed!\n", i);
			fprintf(stderr, "Reasons for failure : %s\n", cudaGetErrorString(cudaStatus));
			goto Error;
		}
	}
Error:
	for (size_t i = 0; i < length; i++)
	{
		cudaFree(array_d[i]);
	}

	return cudaStatus;
}

cudaError_t arraycpyDtoH(void*** array_h, void*** array_d, int length, size_t size)
{
	cudaError_t cudaStatus;

	for (int i = 0; i < length; i++)
	{
		cudaStatus = cudaMemcpy(*array_h[i], *array_d[i], size, cudaMemcpyDeviceToHost);
		if (cudaStatus != cudaSuccess)
		{
			fprintf(stderr, "cudaMemcpy DeviceToHost of array %d failed!\n", i);
			goto Error;
		}
	}
Error:
	for (size_t i = 0; i < length; i++)
	{
		cudaFree(array_d[i]);
	}

	return cudaStatus;
}

cudaError_t oneMalloc(void** a_d, size_t size)
{
	cudaError_t cudaStatus;
	cudaStatus = cudaMalloc((void**)&a_d, size );
	if (cudaStatus != cudaSuccess) {
		fprintf(stderr, "cudaMalloc failed!\n");
	}
	return cudaStatus;
}

cudaError_t onecpyHtoD(void* dev_a, void* a, size_t size)
{
	cudaError_t cudaStatus;
	cudaStatus = cudaMemcpy(dev_a, a, size, cudaMemcpyHostToDevice);
	if (cudaStatus != cudaSuccess) {
		fprintf(stderr, "cudaMemcpy failed!\n");
	}
	return cudaStatus;
}

cudaError_t onecpyDtoH(void* a, void* dev_a, size_t size)
{
	cudaError_t cudaStatus;
	cudaStatus = cudaMemcpy(a, dev_a, size, cudaMemcpyDeviceToHost);
	if (cudaStatus != cudaSuccess) {
		fprintf(stderr, "cudaMemcpy failed!\n");
	}
	return cudaStatus;
}

cudaError_t oneSetdevice()
{
	cudaError_t cudaStatus;
	cudaStatus = cudaSetDevice(0);
	if (cudaStatus != cudaSuccess) {
		fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?\n");
	}
	return cudaStatus;
}

cudaError_t oneLastError()
{
	cudaError_t cudaStatus;
	cudaStatus = cudaGetLastError();
	if (cudaStatus != cudaSuccess) {
		fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
	}
	return cudaStatus;
}

// cudaDeviceSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.

cudaError_t oneCudaDeviceSync()
{
	cudaError_t cudaStatus;
	cudaStatus = cudaDeviceSynchronize();
	if (cudaStatus != cudaSuccess) {
		fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
	}
	return cudaStatus;
}
	.file	"tmpxft_002e69e5_00000000-6_utilities.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2038:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2038:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"cudaMalloc failed!\n"
	.text
	.globl	_Z11arrayMallocPPPvim
	.type	_Z11arrayMallocPPPvim, @function
_Z11arrayMallocPPPvim:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movq	%rdx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movslq	%esi, %rbx
.L3:
	cmpl	%r12d, %ebx
	jle	.L12
	movq	0(%r13,%r12,8), %rdi
	movq	%r14, %rsi
	incq	%r12
	call	cudaMalloc@PLT
	movl	%eax, %ebp
	testl	%eax, %eax
	je	.L3
	movq	stderr(%rip), %rdi
	leaq	.LC0(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L4
.L12:
	xorl	%ebp, %ebp
.L4:
	xorl	%r12d, %r12d
.L6:
	cmpq	%rbx, %r12
	jnb	.L13
	movq	0(%r13,%r12,8), %rdi
	incq	%r12
	call	cudaFree@PLT
	jmp	.L6
.L13:
	popq	%rbx
	.cfi_def_cfa_offset 40
	movl	%ebp, %eax
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z11arrayMallocPPPvim, .-_Z11arrayMallocPPPvim
	.section	.rodata.str1.1
.LC1:
	.string	"cudaMemcpy HostToDevice of array %d failed!\n"
.LC2:
	.string	"Reasons for failure : %s\n"
	.text
	.globl	_Z12arraycpyHtoDPPPvS1_im
	.type	_Z12arraycpyHtoDPPPvS1_im, @function
_Z12arraycpyHtoDPPPvS1_im:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rcx, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rsi, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movslq	%edx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	pushq	%r8
	.cfi_def_cfa_offset 64
.L15:
	cmpl	%ebx, %r12d
	jle	.L22
	movq	(%r14,%rbx,8), %rsi
	movq	0(%r13,%rbx,8), %rax
	movl	$1, %ecx
	movq	%r15, %rdx
	movq	(%rsi), %rsi
	movq	(%rax), %rdi
	call	cudaMemcpy@PLT
	movl	%eax, %ebp
	leaq	1(%rbx), %rax
	testl	%ebp, %ebp
	je	.L20
	movq	stderr(%rip), %rdi
	movl	%ebx, %ecx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC1(%rip), %rdx
	call	__fprintf_chk@PLT
	movl	%ebp, %edi
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC2(%rip), %rdx
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L16
.L20:
	movq	%rax, %rbx
	jmp	.L15
.L22:
	xorl	%ebp, %ebp
.L16:
	xorl	%ebx, %ebx
.L18:
	cmpq	%r12, %rbx
	jnb	.L23
	movq	0(%r13,%rbx,8), %rdi
	incq	%rbx
	call	cudaFree@PLT
	jmp	.L18
.L23:
	popq	%rdx
	.cfi_def_cfa_offset 56
	movl	%ebp, %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
.LFE2028:
	.size	_Z12arraycpyHtoDPPPvS1_im, .-_Z12arraycpyHtoDPPPvS1_im
	.section	.rodata.str1.1
.LC3:
	.string	"cudaMemcpy DeviceToHost of array %d failed!\n"
	.text
	.globl	_Z12arraycpyDtoHPPPvS1_im
	.type	_Z12arraycpyDtoHPPPvS1_im, @function
_Z12arraycpyDtoHPPPvS1_im:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rcx, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rdi, %r14
	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
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movslq	%edx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	pushq	%r8
	.cfi_def_cfa_offset 64
.L25:
	cmpl	%ebx, %ebp
	jle	.L32
	movq	0(%r13,%rbx,8), %rsi
	movq	(%r14,%rbx,8), %rax
	movl	$2, %ecx
	movq	%r15, %rdx
	movq	(%rsi), %rsi
	movq	(%rax), %rdi
	call	cudaMemcpy@PLT
	movl	%eax, %r12d
	leaq	1(%rbx), %rax
	testl	%r12d, %r12d
	je	.L30
	movq	stderr(%rip), %rdi
	movl	%ebx, %ecx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC3(%rip), %rdx
	call	__fprintf_chk@PLT
	jmp	.L26
.L30:
	movq	%rax, %rbx
	jmp	.L25
.L32:
	xorl	%r12d, %r12d
.L26:
	xorl	%ebx, %ebx
.L28:
	cmpq	%rbp, %rbx
	jnb	.L33
	movq	0(%r13,%rbx,8), %rdi
	incq	%rbx
	call	cudaFree@PLT
	jmp	.L28
.L33:
	popq	%rdx
	.cfi_def_cfa_offset 56
	movl	%r12d, %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
.LFE2029:
	.size	_Z12arraycpyDtoHPPPvS1_im, .-_Z12arraycpyDtoHPPPvS1_im
	.globl	_Z9oneMallocPPvm
	.type	_Z9oneMallocPPvm, @function
_Z9oneMallocPPvm:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	movq	%rdi, 8(%rsp)
	leaq	8(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L34
	movq	stderr(%rip), %rdi
	leaq	.LC0(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L34:
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	movl	%ebx, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z9oneMallocPPvm, .-_Z9oneMallocPPvm
	.section	.rodata.str1.1
.LC4:
	.string	"cudaMemcpy failed!\n"
	.text
	.globl	_Z10onecpyHtoDPvS_m
	.type	_Z10onecpyHtoDPvS_m, @function
_Z10onecpyHtoDPvS_m:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L40
	movq	stderr(%rip), %rdi
	leaq	.LC4(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L40:
	movl	%ebx, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z10onecpyHtoDPvS_m, .-_Z10onecpyHtoDPvS_m
	.globl	_Z10onecpyDtoHPvS_m
	.type	_Z10onecpyDtoHPvS_m, @function
_Z10onecpyDtoHPvS_m:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L46
	movq	stderr(%rip), %rdi
	leaq	.LC4(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L46:
	movl	%ebx, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z10onecpyDtoHPvS_m, .-_Z10onecpyDtoHPvS_m
	.section	.rodata.str1.1
.LC5:
	.string	"cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?\n"
	.text
	.globl	_Z12oneSetdevicev
	.type	_Z12oneSetdevicev, @function
_Z12oneSetdevicev:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	xorl	%edi, %edi
	call	cudaSetDevice@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L52
	movq	stderr(%rip), %rdi
	leaq	.LC5(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L52:
	movl	%ebx, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2033:
	.size	_Z12oneSetdevicev, .-_Z12oneSetdevicev
	.section	.rodata.str1.1
.LC6:
	.string	"addKernel launch failed: %s\n"
	.text
	.globl	_Z12oneLastErrorv
	.type	_Z12oneLastErrorv, @function
_Z12oneLastErrorv:
.LFB2034:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	call	cudaGetLastError@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L58
	movl	%eax, %edi
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC6(%rip), %rdx
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L58:
	movl	%ebx, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2034:
	.size	_Z12oneLastErrorv, .-_Z12oneLastErrorv
	.section	.rodata.str1.1
.LC7:
	.string	"cudaDeviceSynchronize returned error code %d after launching addKernel!\n"
	.text
	.globl	_Z17oneCudaDeviceSyncv
	.type	_Z17oneCudaDeviceSyncv, @function
_Z17oneCudaDeviceSyncv:
.LFB2035:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	call	cudaDeviceSynchronize@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L64
	movq	stderr(%rip), %rdi
	movl	%eax, %ecx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC7(%rip), %rdx
	call	__fprintf_chk@PLT
.L64:
	movl	%ebx, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2035:
	.size	_Z17oneCudaDeviceSyncv, .-_Z17oneCudaDeviceSyncv
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2061:
	.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
.LFE2061:
	.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>

hipError_t arrayMalloc(void*** array, int length, size_t size);
hipError_t arraycpyHtoD(void*** array_d, void*** array_h, int length, size_t size);
hipError_t arraycpyDtoH(void*** array_h, void*** array_d, int length, size_t size);
hipError_t oneMalloc(void** a_d, size_t size);
hipError_t onecpyHtoD(void* dev_a, void* a, size_t size);
hipError_t onecpyDtoH(void* a, void* dev_a, size_t size);
hipError_t oneSetdevice();
hipError_t oneLastError();
hipError_t oneCudaDeviceSync();

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