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

// Change the code here:
// This should be changed to GPU kernel definition
void dot(int numElements, const float3* a, const float3* b, float* c)
{
    for (int i = 0; i < numElements; i++)
    {
        c[i] = a[i].x*b[i].x + a[i].y*b[i].y + a[i].z*b[i].z;
    }
}

int main()
{
    int numElements = 10000;

    float3* a = (float3*)calloc(numElements, sizeof(float3));
    float3* b = (float3*)calloc(numElements, sizeof(float3));
    float* c = (float*)calloc(numElements, sizeof(float));

    srand(1214134);
    for (int i = 0; i < numElements; i++)
    {
        a[i].x = float(rand())/float(RAND_MAX + 1.0);
        a[i].y = float(rand())/float(RAND_MAX + 1.0);
        a[i].z = float(rand())/float(RAND_MAX + 1.0);

        b[i].x = float(rand())/float(RAND_MAX + 1.0);
        b[i].y = float(rand())/float(RAND_MAX + 1.0);
        b[i].z = float(rand())/float(RAND_MAX + 1.0);
    }

    // Insert your code here:
    // 1. Create GPU device buffers
    // 2. Copy input data from host to device (vectors a and b)
    // 3. Change the CPU function call to the GPU kernel call
    dot(numElements, a, b, c);
    // 4. Copy the result back (vector c)

    for (int i = 0; i < std::min(10, numElements); i++)
    {
        printf("%f*%f + %f*%f + %f*%f = %f\n", a[i].x, b[i].x, a[i].y, b[i].y, a[i].z, b[i].z, c[i]);
    }
    printf("...\n");

    free(a);
    free(b);
    free(c);

    // Free GPU memory here
    
    return 0;
}

	.file	"tmpxft_0038298a_00000000-6_vector_dot_product_gpu.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2312:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2312:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z3dotiPK6float3S1_Pf
	.type	_Z3dotiPK6float3S1_Pf, @function
_Z3dotiPK6float3S1_Pf:
.LFB2307:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	xorl	%r8d, %r8d
.L3:
	cmpl	%r8d, %edi
	jle	.L6
	movss	(%rsi,%rax), %xmm0
	movss	4(%rsi,%rax), %xmm1
	mulss	(%rdx,%rax), %xmm0
	mulss	4(%rdx,%rax), %xmm1
	addss	%xmm1, %xmm0
	movss	8(%rsi,%rax), %xmm1
	mulss	8(%rdx,%rax), %xmm1
	addq	$12, %rax
	addss	%xmm1, %xmm0
	movss	%xmm0, (%rcx,%r8,4)
	incq	%r8
	jmp	.L3
.L6:
	ret
	.cfi_endproc
.LFE2307:
	.size	_Z3dotiPK6float3S1_Pf, .-_Z3dotiPK6float3S1_Pf
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"%f*%f + %f*%f + %f*%f = %f\n"
.LC2:
	.string	"...\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2308:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$12, %esi
	movl	$10000, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	pushq	%rcx
	.cfi_def_cfa_offset 64
	call	calloc@PLT
	movl	$12, %esi
	movl	$10000, %edi
	movq	%rax, %rbp
	call	calloc@PLT
	movl	$4, %esi
	movl	$10000, %edi
	movq	%rax, %rbx
	call	calloc@PLT
	movl	$1214134, %edi
	movq	%rax, %r13
	call	srand@PLT
.L8:
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	movss	%xmm0, 0(%rbp,%r12)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	movss	%xmm0, 4(%rbp,%r12)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	movss	%xmm0, 8(%rbp,%r12)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	movss	%xmm0, (%rbx,%r12)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	movss	%xmm0, 4(%rbx,%r12)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	movss	%xmm0, 8(%rbx,%r12)
	addq	$12, %r12
	cmpq	$120000, %r12
	jne	.L8
	movq	%r13, %rcx
	movq	%rbx, %rdx
	movq	%rbp, %rsi
	movl	$10000, %edi
	call	_Z3dotiPK6float3S1_Pf
	movq	%r13, %r14
	xorl	%r12d, %r12d
	leaq	.LC1(%rip), %r15
.L9:
	cvtss2sd	0(%rbp,%r12), %xmm0
	cvtss2sd	8(%rbx,%r12), %xmm5
	movb	$7, %al
	cvtss2sd	8(%rbp,%r12), %xmm4
	cvtss2sd	4(%rbx,%r12), %xmm3
	cvtss2sd	(%rbx,%r12), %xmm1
	movq	%r15, %rsi
	cvtss2sd	4(%rbp,%r12), %xmm2
	movl	$2, %edi
	addq	$12, %r12
	cvtss2sd	(%r14), %xmm6
	addq	$4, %r14
	call	__printf_chk@PLT
	cmpq	$120, %r12
	jne	.L9
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	%r13, %rdi
	call	free@PLT
	popq	%rdx
	.cfi_def_cfa_offset 56
	xorl	%eax, %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
.LFE2308:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2335:
	.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
.LFE2335:
	.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	805306368
	.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>
#include <stdlib.h>
#include <algorithm>

// Change the code here:
// This should be changed to GPU kernel definition
void dot(int numElements, const float3* a, const float3* b, float* c)
{
    for (int i = 0; i < numElements; i++)
    {
        c[i] = a[i].x*b[i].x + a[i].y*b[i].y + a[i].z*b[i].z;
    }
}

int main()
{
    int numElements = 10000;

    float3* a = (float3*)calloc(numElements, sizeof(float3));
    float3* b = (float3*)calloc(numElements, sizeof(float3));
    float* c = (float*)calloc(numElements, sizeof(float));

    srand(1214134);
    for (int i = 0; i < numElements; i++)
    {
        a[i].x = float(rand())/float(RAND_MAX + 1.0);
        a[i].y = float(rand())/float(RAND_MAX + 1.0);
        a[i].z = float(rand())/float(RAND_MAX + 1.0);

        b[i].x = float(rand())/float(RAND_MAX + 1.0);
        b[i].y = float(rand())/float(RAND_MAX + 1.0);
        b[i].z = float(rand())/float(RAND_MAX + 1.0);
    }

    // Insert your code here:
    // 1. Create GPU device buffers
    // 2. Copy input data from host to device (vectors a and b)
    // 3. Change the CPU function call to the GPU kernel call
    dot(numElements, a, b, c);
    // 4. Copy the result back (vector c)

    for (int i = 0; i < std::min(10, numElements); i++)
    {
        printf("%f*%f + %f*%f + %f*%f = %f\n", a[i].x, b[i].x, a[i].y, b[i].y, a[i].z, b[i].z, c[i]);
    }
    printf("...\n");

    free(a);
    free(b);
    free(c);

    // Free GPU memory here
    
    return 0;
}

	.text
	.file	"vector_dot_product_gpu.hip"
	.globl	_Z3dotiPK15HIP_vector_typeIfLj3EES2_Pf # -- Begin function _Z3dotiPK15HIP_vector_typeIfLj3EES2_Pf
	.type	_Z3dotiPK15HIP_vector_typeIfLj3EES2_Pf,@function
_Z3dotiPK15HIP_vector_typeIfLj3EES2_Pf: # @_Z3dotiPK15HIP_vector_typeIfLj3EES2_Pf
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jle	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%edi, %eax
	shlq	$2, %rax
	leaq	(%rax,%rax,2), %rax
	xorl	%edi, %edi
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movss	(%rsi,%rdi), %xmm0              # xmm0 = mem[0],zero,zero,zero
	mulss	(%rdx,%rdi), %xmm0
	movss	4(%rsi,%rdi), %xmm1             # xmm1 = mem[0],zero,zero,zero
	mulss	4(%rdx,%rdi), %xmm1
	addss	%xmm0, %xmm1
	movss	8(%rsi,%rdi), %xmm0             # xmm0 = mem[0],zero,zero,zero
	mulss	8(%rdx,%rdi), %xmm0
	addss	%xmm1, %xmm0
	movss	%xmm0, (%rcx)
	addq	$4, %rcx
	addq	$12, %rdi
	cmpq	%rdi, %rax
	jne	.LBB0_2
.LBB0_3:                                # %._crit_edge
	retq
.Lfunc_end0:
	.size	_Z3dotiPK15HIP_vector_typeIfLj3EES2_Pf, .Lfunc_end0-_Z3dotiPK15HIP_vector_typeIfLj3EES2_Pf
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0x30000000                      # float 4.65661287E-10
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$10000, %edi                    # imm = 0x2710
	movl	$12, %esi
	callq	calloc
	movq	%rax, %rbx
	movl	$10000, %edi                    # imm = 0x2710
	movl	$12, %esi
	callq	calloc
	movq	%rax, %r14
	movl	$10000, %edi                    # imm = 0x2710
	movl	$4, %esi
	callq	calloc
	movq	%rax, %r15
	movl	$1214134, %edi                  # imm = 0x1286B6
	callq	srand
	movl	$8, %r12d
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	.LCPI1_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	mulss	%xmm1, %xmm0
	movss	%xmm0, -8(%rbx,%r12)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	movss	%xmm0, -4(%rbx,%r12)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	movss	%xmm0, (%rbx,%r12)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	movss	%xmm0, -8(%r14,%r12)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	movss	%xmm0, -4(%r14,%r12)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	movss	%xmm0, (%r14,%r12)
	addq	$12, %r12
	cmpq	$120008, %r12                   # imm = 0x1D4C8
	jne	.LBB1_1
# %bb.2:                                # %.lr.ph.i.preheader
	movl	$2, %eax
	movq	%r15, %rcx
.LBB1_3:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movss	-8(%rbx,%rax,4), %xmm0          # xmm0 = mem[0],zero,zero,zero
	mulss	-8(%r14,%rax,4), %xmm0
	movss	-4(%rbx,%rax,4), %xmm1          # xmm1 = mem[0],zero,zero,zero
	mulss	-4(%r14,%rax,4), %xmm1
	addss	%xmm0, %xmm1
	movss	(%rbx,%rax,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	mulss	(%r14,%rax,4), %xmm0
	addss	%xmm1, %xmm0
	movss	%xmm0, (%rcx)
	addq	$3, %rax
	addq	$4, %rcx
	cmpq	$30002, %rax                    # imm = 0x7532
	jne	.LBB1_3
# %bb.4:                                # %_Z3dotiPK15HIP_vector_typeIfLj3EES2_Pf.exit.preheader
	movl	$2, %r12d
	movq	%r15, %r13
.LBB1_5:                                # %_Z3dotiPK15HIP_vector_typeIfLj3EES2_Pf.exit
                                        # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtss2sd	-8(%rbx,%r12,4), %xmm0
	xorps	%xmm1, %xmm1
	cvtss2sd	-8(%r14,%r12,4), %xmm1
	xorps	%xmm2, %xmm2
	cvtss2sd	-4(%rbx,%r12,4), %xmm2
	xorps	%xmm3, %xmm3
	cvtss2sd	-4(%r14,%r12,4), %xmm3
	xorps	%xmm4, %xmm4
	cvtss2sd	(%rbx,%r12,4), %xmm4
	xorps	%xmm5, %xmm5
	cvtss2sd	(%r14,%r12,4), %xmm5
	xorps	%xmm6, %xmm6
	cvtss2sd	(%r13), %xmm6
	movl	$.L.str, %edi
	movb	$7, %al
	callq	printf
	addq	$4, %r13
	addq	$3, %r12
	cmpq	$32, %r12
	jne	.LBB1_5
# %bb.6:
	movl	$.Lstr, %edi
	callq	puts@PLT
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	xorl	%eax, %eax
	popq	%rbx
	.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
	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	"%f*%f + %f*%f + %f*%f = %f\n"
	.size	.L.str, 28

	.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	"..."
	.size	.Lstr, 4

	.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
