/*!
	\file arrays.cu

	\author Andrew Kerr <arkerr@gatech.edu>

	\brief tests implementation of cudaMallocArray(), among other things

	\date Feb 12, 2010
*/

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

//////////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////////

bool testMemcpy(bool verbose) {
	bool passed = true;
	int width = 1024, height = 512;
	int errors = 0;

	cudaChannelFormatDesc channel = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);

	cudaArray *cuArray;
	cudaMallocArray(&cuArray, &channel, width, height);

	srand(7);

	size_t bytes = sizeof(float) * width * height;
	float *hostSource = new float[width * height];
	float *hostDest = new float[width * height];
	for (int j = 0; j < height; j++) {
		float *ptr = hostSource + j * width;
		float *dstPtr = hostDest + j * width;
		for (int i = 0; i < width; i++) {
			float x = (float)( (rand() % 1024) / 125.0f);
			ptr[i] = x;
			dstPtr[i] = -1.0f;
		}
	}

	cudaMemcpyToArray(cuArray, 0, 0, hostSource, bytes, cudaMemcpyHostToDevice);
	cudaMemcpyFromArray(hostDest, cuArray, 0, 0, bytes, cudaMemcpyDeviceToHost);

	for (int j = 0; j < height && errors < 5; j++) {
		float *srcPtr = hostSource + j * width;
		float *dstPtr = hostDest + j * width;
		for (int i = 0; i < width && errors < 5; i++) {
			float expected = srcPtr[i];
			float got = dstPtr[i];
			if (fabs(expected - got) > 0.001f) {
				++errors;
				if (verbose) {
					printf("ERROR: (%d, %d) - expected %f, got %f\n", i, j, expected, got); 
					fflush(stdout);
				}
			}
		}
	}

	cudaFreeArray(cuArray);

	delete [] hostSource;
	delete [] hostDest;

	return passed;
}

//////////////////////////////////////////////////////////////////////////////////////////////////

int main(int argc, char *arg[]) {

	bool result = testMemcpy(true);

	if (result) {
		printf("Test PASSED\n");
	}
	else {
		printf("Test FAILED\n");
	}

	return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////


	.file	"tmpxft_00251d39_00000000-6_arrays.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2031:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2031:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC4:
	.string	"ERROR: (%d, %d) - expected %f, got %f\n"
	.text
	.globl	_Z10testMemcpyb
	.type	_Z10testMemcpyb, @function
_Z10testMemcpyb:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$2, %r9d
	xorl	%r8d, %r8d
	xorl	%ecx, %ecx
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	xorl	%edx, %edx
	movl	$32, %esi
	movl	%edi, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	xorl	%r13d, %r13d
	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	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	leaq	52(%rsp), %rbx
	movq	%rbx, %rdi
	call	cudaCreateChannelDesc@PLT
	movq	%rbx, %rsi
	xorl	%r8d, %r8d
	movl	$512, %ecx
	movl	$1024, %edx
	leaq	40(%rsp), %rdi
	call	cudaMallocArray@PLT
	movl	$7, %edi
	call	srand@PLT
	movl	$2097152, %edi
	call	_Znam@PLT
	movl	$2097152, %edi
	movq	%rax, %rbp
	call	_Znam@PLT
	movq	%rax, %rbx
.L4:
	leaq	0(,%r13,4), %r15
	xorl	%r12d, %r12d
	leaq	0(%rbp,%r15), %rsi
	addq	%rbx, %r15
.L3:
	movq	%rsi, (%rsp)
	call	rand@PLT
	movl	$1024, %ecx
	movq	(%rsp), %rsi
	movl	$0xbf800000, (%r15,%r12)
	cltd
	idivl	%ecx
	cvtsi2ssl	%edx, %xmm0
	divss	.LC0(%rip), %xmm0
	movss	%xmm0, (%rsi,%r12)
	addq	$4, %r12
	cmpq	$4096, %r12
	jne	.L3
	addq	$1024, %r13
	cmpq	$524288, %r13
	jne	.L4
	movq	40(%rsp), %rdi
	movl	$1, %r9d
	movq	%rbp, %rcx
	xorl	%edx, %edx
	movl	$2097152, %r8d
	xorl	%esi, %esi
	call	cudaMemcpyToArray@PLT
	movq	40(%rsp), %rsi
	xorl	%ecx, %ecx
	xorl	%edx, %edx
	movl	$2097152, %r8d
	movl	$2, %r9d
	movq	%rbx, %rdi
	call	cudaMemcpyFromArray@PLT
	xorl	%r8d, %r8d
	xorl	%r10d, %r10d
.L11:
	movq	%r8, %r12
	movl	%r8d, (%rsp)
	salq	$12, %r12
	movl	%r8d, 16(%rsp)
	leaq	(%rbx,%r12), %rax
	leaq	0(%rbp,%r12), %r15
	xorl	%r12d, %r12d
	movq	%rax, 8(%rsp)
.L9:
	movq	8(%rsp), %rax
	movss	(%r15,%r12,4), %xmm0
	movl	%r12d, %r13d
	movss	(%rax,%r12,4), %xmm1
	movaps	%xmm0, %xmm2
	subss	%xmm1, %xmm2
	andps	.LC2(%rip), %xmm2
	comiss	.LC3(%rip), %xmm2
	jbe	.L5
	testb	%r14b, %r14b
	jne	.L7
.L8:
	incl	%r10d
	jmp	.L5
.L7:
	movl	16(%rsp), %ecx
	movl	%r12d, %edx
	leaq	.LC4(%rip), %rsi
	movb	$2, %al
	movl	$2, %edi
	movq	%r8, 24(%rsp)
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	movl	%r10d, 20(%rsp)
	call	__printf_chk@PLT
	movq	stdout(%rip), %rdi
	call	fflush@PLT
	movl	20(%rsp), %r10d
	movq	24(%rsp), %r8
	jmp	.L8
.L5:
	cmpl	$4, %r10d
	setle	%al
	incl	%r13d
	incq	%r12
	cmpl	$1023, %r13d
	jg	.L15
	testb	%al, %al
	jne	.L9
.L15:
	movl	(%rsp), %r15d
	incq	%r8
	incl	%r15d
	cmpl	$511, %r15d
	jg	.L16
	testb	%al, %al
	jne	.L11
.L16:
	movq	40(%rsp), %rdi
	call	cudaFreeArray@PLT
	movq	%rbp, %rdi
	call	_ZdaPv@PLT
	movq	%rbx, %rdi
	call	_ZdaPv@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	movb	$1, %al
	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	_Z10testMemcpyb, .-_Z10testMemcpyb
	.section	.rodata.str1.1
.LC5:
	.string	"Test PASSED\n"
.LC6:
	.string	"Test FAILED\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rcx
	.cfi_def_cfa_offset 16
	movl	$1, %edi
	call	_Z10testMemcpyb
	leaq	.LC5(%rip), %rsi
	testb	%al, %al
	jne	.L33
	leaq	.LC6(%rip), %rsi
.L33:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2054:
	.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
.LFE2054:
	.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
.LC0:
	.long	1123680256
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC2:
	.long	2147483647
	.long	0
	.long	0
	.long	0
	.section	.rodata.cst4
	.align 4
.LC3:
	.long	981668463
	.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:

/*!
	\file arrays.cu

	\author Andrew Kerr <arkerr@gatech.edu>

	\brief tests implementation of cudaMallocArray(), among other things

	\date Feb 12, 2010
*/


#include <hip/hip_runtime.h>
#include <stdlib.h>
#include <stdio.h>

//////////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////////

bool testMemcpy(bool verbose) {
	bool passed = true;
	int width = 1024, height = 512;
	int errors = 0;

	hipChannelFormatDesc channel = hipCreateChannelDesc(32, 0, 0, 0, hipChannelFormatKindFloat);

	hipArray *cuArray;
	hipMallocArray(&cuArray, &channel, width, height);

	srand(7);

	size_t bytes = sizeof(float) * width * height;
	float *hostSource = new float[width * height];
	float *hostDest = new float[width * height];
	for (int j = 0; j < height; j++) {
		float *ptr = hostSource + j * width;
		float *dstPtr = hostDest + j * width;
		for (int i = 0; i < width; i++) {
			float x = (float)( (rand() % 1024) / 125.0f);
			ptr[i] = x;
			dstPtr[i] = -1.0f;
		}
	}

	hipMemcpyToArray(cuArray, 0, 0, hostSource, bytes, hipMemcpyHostToDevice);
	hipMemcpyFromArray(hostDest, cuArray, 0, 0, bytes, hipMemcpyDeviceToHost);

	for (int j = 0; j < height && errors < 5; j++) {
		float *srcPtr = hostSource + j * width;
		float *dstPtr = hostDest + j * width;
		for (int i = 0; i < width && errors < 5; i++) {
			float expected = srcPtr[i];
			float got = dstPtr[i];
			if (fabs(expected - got) > 0.001f) {
				++errors;
				if (verbose) {
					printf("ERROR: (%d, %d) - expected %f, got %f\n", i, j, expected, got); 
					fflush(stdout);
				}
			}
		}
	}

	hipFreeArray(cuArray);

	delete [] hostSource;
	delete [] hostDest;

	return passed;
}

//////////////////////////////////////////////////////////////////////////////////////////////////

int main(int argc, char *arg[]) {

	bool result = testMemcpy(true);

	if (result) {
		printf("Test PASSED\n");
	}
	else {
		printf("Test FAILED\n");
	}

	return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////


	.text
	.file	"arrays.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z10testMemcpyb
.LCPI0_0:
	.long	0x42fa0000                      # float 125
.LCPI0_2:
	.long	0x3a83126f                      # float 0.00100000005
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI0_1:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.text
	.globl	_Z10testMemcpyb
	.type	_Z10testMemcpyb,@function
_Z10testMemcpyb:                        # @_Z10testMemcpyb
	.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	$56, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%edi, %ebx
	leaq	36(%rsp), %r14
	xorl	%r12d, %r12d
	movq	%r14, %rdi
	movl	$32, %esi
	xorl	%edx, %edx
	xorl	%ecx, %ecx
	xorl	%r8d, %r8d
	movl	$2, %r9d
	callq	hipCreateChannelDesc
	leaq	8(%rsp), %rdi
	movl	$1024, %edx                     # imm = 0x400
	movl	$512, %ecx                      # imm = 0x200
	movq	%r14, %rsi
	xorl	%r8d, %r8d
	callq	hipMallocArray
	movl	$7, %edi
	callq	srand
	movl	$2097152, %edi                  # imm = 0x200000
	callq	_Znam
	movq	%rax, %r15
	movl	$2097152, %edi                  # imm = 0x200000
	callq	_Znam
	movl	$4096, %r14d                    # imm = 0x1000
	movq	%r15, 24(%rsp)                  # 8-byte Spill
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movq	%rax, %r13
.LBB0_1:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_2 Depth 2
	xorl	%ebp, %ebp
.LBB0_2:                                #   Parent Loop BB0_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	movss	.LCPI0_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
                                        # kill: def $eax killed $eax def $rax
	leal	1023(%rax), %ecx
	testl	%eax, %eax
	cmovnsl	%eax, %ecx
	andl	$-1024, %ecx                    # imm = 0xFC00
	subl	%ecx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	divss	%xmm1, %xmm0
	movss	%xmm0, (%r15,%rbp,4)
	movl	$-1082130432, (%r13,%rbp,4)     # imm = 0xBF800000
	incq	%rbp
	cmpq	$1024, %rbp                     # imm = 0x400
	jne	.LBB0_2
# %bb.3:                                #   in Loop: Header=BB0_1 Depth=1
	incq	%r12
	addq	%r14, %r13
	addq	%r14, %r15
	cmpq	$512, %r12                      # imm = 0x200
	jne	.LBB0_1
# %bb.4:
	movq	8(%rsp), %rdi
	xorl	%r12d, %r12d
	movl	$2097152, %r8d                  # imm = 0x200000
	xorl	%esi, %esi
	xorl	%edx, %edx
	movq	24(%rsp), %r14                  # 8-byte Reload
	movq	%r14, %rcx
	movl	$1, %r9d
	callq	hipMemcpyToArray
	movq	8(%rsp), %rsi
	movl	$2097152, %r8d                  # imm = 0x200000
	movq	16(%rsp), %r15                  # 8-byte Reload
	movq	%r15, %rdi
	xorl	%edx, %edx
	xorl	%ecx, %ecx
	movl	$2, %r9d
	callq	hipMemcpyFromArray
	movaps	.LCPI0_1(%rip), %xmm3           # xmm3 = [NaN,NaN,NaN,NaN]
	movss	.LCPI0_2(%rip), %xmm4           # xmm4 = mem[0],zero,zero,zero
	xorl	%ebp, %ebp
.LBB0_5:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_6 Depth 2
	xorl	%r13d, %r13d
.LBB0_6:                                #   Parent Loop BB0_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%r14,%r13,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	(%r15,%r13,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	movaps	%xmm0, %xmm2
	subss	%xmm1, %xmm2
	andps	%xmm3, %xmm2
	ucomiss	%xmm4, %xmm2
	jbe	.LBB0_9
# %bb.7:                                #   in Loop: Header=BB0_6 Depth=2
	incl	%ebp
	testb	%bl, %bl
	je	.LBB0_9
# %bb.8:                                #   in Loop: Header=BB0_6 Depth=2
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	movl	$.L.str, %edi
	movl	%r13d, %esi
	movl	%r12d, %edx
	movb	$2, %al
	callq	printf
	movq	stdout(%rip), %rdi
	callq	fflush
	movss	.LCPI0_2(%rip), %xmm4           # xmm4 = mem[0],zero,zero,zero
	movaps	.LCPI0_1(%rip), %xmm3           # xmm3 = [NaN,NaN,NaN,NaN]
.LBB0_9:                                #   in Loop: Header=BB0_6 Depth=2
	cmpq	$1022, %r13                     # imm = 0x3FE
	ja	.LBB0_10
# %bb.13:                               #   in Loop: Header=BB0_6 Depth=2
	incq	%r13
	cmpl	$5, %ebp
	jl	.LBB0_6
.LBB0_10:                               #   in Loop: Header=BB0_5 Depth=1
	cmpq	$510, %r12                      # imm = 0x1FE
	ja	.LBB0_12
# %bb.11:                               #   in Loop: Header=BB0_5 Depth=1
	incq	%r12
	movl	$4096, %eax                     # imm = 0x1000
	addq	%rax, %r15
	addq	%rax, %r14
	cmpl	$5, %ebp
	jl	.LBB0_5
.LBB0_12:
	movq	8(%rsp), %rdi
	callq	hipFreeArray
	movq	24(%rsp), %rdi                  # 8-byte Reload
	callq	_ZdaPv
	movq	16(%rsp), %rdi                  # 8-byte Reload
	callq	_ZdaPv
	movb	$1, %al
	addq	$56, %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
.Lfunc_end0:
	.size	_Z10testMemcpyb, .Lfunc_end0-_Z10testMemcpyb
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movl	$1, %edi
	callq	_Z10testMemcpyb
	movl	$.Lstr, %edi
	callq	puts@PLT
	xorl	%eax, %eax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"ERROR: (%d, %d) - expected %f, got %f\n"
	.size	.L.str, 39

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Test PASSED"
	.size	.Lstr, 12

	.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
