/*
   Write a CUDA program that includes a host function to compute the square of a N dimensional square matrix, N being 16
*/

// Author: Naoki Atkins

#include <stdio.h>
#include <cuda.h>
#include <time.h>
#include <stdlib.h>
#include <limits.h>
#define N 16

__host__ int generator(int rank){
        int rand_num;
srand(time(NULL)+rank);
      rand_num = rand();
    if (rand_num<0) {
      rand_num=abs(rand_num);
    }
    else if(rand_num<10){
      rand_num+=10;
    }
    else if( rand_num>=100){
      rand_num = rand_num%100;
    }

    rand_num=rand_num*10;
    rand_num+=1000;

    rand_num+=rank;

    return rand_num;
    }

__host__ void computeSquare(int* matrix, int* result, int matrixsize) {
        int offset;
  for (unsigned jj = 0; jj < matrixsize; ++jj) {
    for (unsigned kk = 0; kk < matrixsize; ++kk) {
        offset = jj * N + kk;
      result[offset] += matrix[offset] * matrix[offset];
    }
  }
}

int main(int argc, char** argv) {
        // Matrix
        int i, j;
        int offset;
        int *mat = (int *)malloc(N * N * sizeof(int));

        printf("Generating a random matrix...\n");
        for (i = 0; i < N; ++i) {
                for (j = 0; j < N; ++j) {
                        offset = i * N + j;
                        mat[offset] = generator(rand() % 100);
                        printf("%d ", mat[offset]);
                }
                printf("\n");
        }

        // Square the matrix
        int *squareMat = (int *)malloc(N * N * sizeof(int));

        printf("Calculating the square...\n");
        computeSquare(mat, squareMat, N);

        for (i = 0; i < N; ++i) {
                for (j = 0; j < N; ++j) {
                        offset = i * N + j;
                        printf("%d ", squareMat[offset]);
                }
                printf("\n");
        }

        return 0;
}

	.file	"tmpxft_00344bb5_00000000-6_prob2.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z9generatori
	.type	_Z9generatori, @function
_Z9generatori:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	%edi, %ebx
	xorl	%edi, %edi
	call	time@PLT
	leal	(%rbx,%rax), %edi
	call	srand@PLT
	call	rand@PLT
	movl	%eax, %edx
	testl	%eax, %eax
	jns	.L3
	negl	%edx
	jmp	.L4
.L3:
	cmpl	$9, %eax
	jg	.L5
	addl	$10, %edx
	jmp	.L4
.L5:
	cmpl	$99, %eax
	jle	.L4
	movl	$100, %ecx
	cltd
	idivl	%ecx
.L4:
	imull	$10, %edx, %edx
	leal	1000(%rdx,%rbx), %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z9generatori, .-_Z9generatori
	.globl	_Z13computeSquarePiS_i
	.type	_Z13computeSquarePiS_i, @function
_Z13computeSquarePiS_i:
.LFB2028:
	.cfi_startproc
	endbr64
	xorl	%r10d, %r10d
	xorl	%r9d, %r9d
.L9:
	cmpl	%edx, %r9d
	je	.L13
	xorl	%ecx, %ecx
.L10:
	leal	(%rcx,%r10), %eax
	incl	%ecx
	cltq
	movl	(%rdi,%rax,4), %r8d
	imull	%r8d, %r8d
	addl	%r8d, (%rsi,%rax,4)
	cmpl	%edx, %ecx
	jne	.L10
	incl	%r9d
	addl	$16, %r10d
	jmp	.L9
.L13:
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z13computeSquarePiS_i, .-_Z13computeSquarePiS_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Generating a random matrix...\n"
.LC1:
	.string	"%d "
.LC2:
	.string	"\n"
.LC3:
	.string	"Calculating the square...\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$1024, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	leaq	.LC1(%rip), %r13
	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	malloc@PLT
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rbx
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L15:
	movq	%r12, %r14
	xorl	%ebp, %ebp
	salq	$6, %r14
	addq	%rbx, %r14
.L16:
	call	rand@PLT
	movl	$100, %ecx
	cltd
	idivl	%ecx
	movl	%edx, %edi
	call	_Z9generatori
	movq	%r13, %rsi
	movl	$2, %edi
	movl	%eax, (%r14,%rbp,4)
	movl	%eax, %edx
	incq	%rbp
	xorl	%eax, %eax
	call	__printf_chk@PLT
	cmpq	$16, %rbp
	jne	.L16
	leaq	.LC2(%rip), %r14
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%r12
	movq	%r14, %rsi
	call	__printf_chk@PLT
	cmpq	$16, %r12
	jne	.L15
	movl	$1024, %edi
	xorl	%r12d, %r12d
	call	malloc@PLT
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rbp
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$16, %edx
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	call	_Z13computeSquarePiS_i
.L18:
	movq	%r12, %r15
	xorl	%ebx, %ebx
	salq	$6, %r15
	addq	%rbp, %r15
.L19:
	movl	(%r15,%rbx,4), %edx
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%rbx
	call	__printf_chk@PLT
	cmpq	$16, %rbx
	jne	.L19
	movq	%r14, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%r12
	call	__printf_chk@PLT
	cmpq	$16, %r12
	jne	.L18
	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
.LFE2029:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.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
.LFE2055:
	.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:

/*
   Write a CUDA program that includes a host function to compute the square of a N dimensional square matrix, N being 16
*/

// Author: Naoki Atkins

#include <stdio.h>
#include <hip/hip_runtime.h>
#include <time.h>
#include <stdlib.h>
#include <limits.h>
#define N 16

__host__ int generator(int rank){
        int rand_num;
srand(time(NULL)+rank);
      rand_num = rand();
    if (rand_num<0) {
      rand_num=abs(rand_num);
    }
    else if(rand_num<10){
      rand_num+=10;
    }
    else if( rand_num>=100){
      rand_num = rand_num%100;
    }

    rand_num=rand_num*10;
    rand_num+=1000;

    rand_num+=rank;

    return rand_num;
    }

__host__ void computeSquare(int* matrix, int* result, int matrixsize) {
        int offset;
  for (unsigned jj = 0; jj < matrixsize; ++jj) {
    for (unsigned kk = 0; kk < matrixsize; ++kk) {
        offset = jj * N + kk;
      result[offset] += matrix[offset] * matrix[offset];
    }
  }
}

int main(int argc, char** argv) {
        // Matrix
        int i, j;
        int offset;
        int *mat = (int *)malloc(N * N * sizeof(int));

        printf("Generating a random matrix...\n");
        for (i = 0; i < N; ++i) {
                for (j = 0; j < N; ++j) {
                        offset = i * N + j;
                        mat[offset] = generator(rand() % 100);
                        printf("%d ", mat[offset]);
                }
                printf("\n");
        }

        // Square the matrix
        int *squareMat = (int *)malloc(N * N * sizeof(int));

        printf("Calculating the square...\n");
        computeSquare(mat, squareMat, N);

        for (i = 0; i < N; ++i) {
                for (j = 0; j < N; ++j) {
                        offset = i * N + j;
                        printf("%d ", squareMat[offset]);
                }
                printf("\n");
        }

        return 0;
}

	.text
	.file	"prob2.hip"
	.globl	_Z9generatori                   # -- Begin function _Z9generatori
	.type	_Z9generatori,@function
_Z9generatori:                          # @_Z9generatori
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%edi, %ebx
	xorl	%edi, %edi
	callq	time
	leal	(%rax,%rbx), %edi
	callq	srand
	callq	rand
                                        # kill: def $eax killed $eax def $rax
	testl	%eax, %eax
	js	.LBB0_1
# %bb.2:
	cmpl	$9, %eax
	ja	.LBB0_4
# %bb.3:
	addl	$10, %eax
	jmp	.LBB0_6
.LBB0_1:
	negl	%eax
	jmp	.LBB0_6
.LBB0_4:
	cmpl	$100, %eax
	jb	.LBB0_6
# %bb.5:
	movl	%eax, %ecx
	imulq	$1374389535, %rcx, %rcx         # imm = 0x51EB851F
	shrq	$37, %rcx
	imull	$100, %ecx, %ecx
	subl	%ecx, %eax
.LBB0_6:
	leal	(%rax,%rax,4), %eax
	leal	(%rbx,%rax,2), %eax
	addl	$1000, %eax                     # imm = 0x3E8
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z9generatori, .Lfunc_end0-_Z9generatori
	.cfi_endproc
                                        # -- End function
	.globl	_Z13computeSquarePiS_i          # -- Begin function _Z13computeSquarePiS_i
	.type	_Z13computeSquarePiS_i,@function
_Z13computeSquarePiS_i:                 # @_Z13computeSquarePiS_i
	.cfi_startproc
# %bb.0:
	testl	%edx, %edx
	je	.LBB1_5
# %bb.1:                                # %.preheader.preheader
	xorl	%eax, %eax
	xorl	%ecx, %ecx
.LBB1_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_3 Depth 2
	xorl	%r8d, %r8d
.LBB1_3:                                #   Parent Loop BB1_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leal	(%rax,%r8), %r9d
	movslq	%r9d, %r9
	movl	(%rdi,%r9,4), %r10d
	imull	%r10d, %r10d
	addl	%r10d, (%rsi,%r9,4)
	incl	%r8d
	cmpl	%r8d, %edx
	jne	.LBB1_3
# %bb.4:                                #   in Loop: Header=BB1_2 Depth=1
	incl	%ecx
	addl	$16, %eax
	cmpl	%edx, %ecx
	jne	.LBB1_2
.LBB1_5:                                # %._crit_edge
	retq
.Lfunc_end1:
	.size	_Z13computeSquarePiS_i, .Lfunc_end1-_Z13computeSquarePiS_i
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function 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	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$1024, %edi                     # imm = 0x400
	callq	malloc
	movq	%rax, %rbx
	movl	$.Lstr, %edi
	callq	puts@PLT
	movq	%rbx, %r14
	xorl	%r15d, %r15d
.LBB2_1:                                # %.preheader23
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_2 Depth 2
	xorl	%r12d, %r12d
.LBB2_2:                                #   Parent Loop BB2_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	movslq	%eax, %rdi
	imulq	$1374389535, %rdi, %rax         # imm = 0x51EB851F
	movq	%rax, %rcx
	shrq	$63, %rcx
	sarq	$37, %rax
	addl	%ecx, %eax
	imull	$100, %eax, %eax
	subl	%eax, %edi
                                        # kill: def $edi killed $edi killed $rdi
	callq	_Z9generatori
	movl	%eax, (%r14,%r12,4)
	movl	$.L.str.1, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
	incq	%r12
	cmpq	$16, %r12
	jne	.LBB2_2
# %bb.3:                                #   in Loop: Header=BB2_1 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r15
	addq	$64, %r14
	cmpq	$16, %r15
	jne	.LBB2_1
# %bb.4:
	movl	$1024, %edi                     # imm = 0x400
	callq	malloc
	movq	%rax, %r14
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	xorl	%eax, %eax
	movq	%r14, %rcx
.LBB2_5:                                # %.preheader.i
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_6 Depth 2
	xorl	%edx, %edx
.LBB2_6:                                #   Parent Loop BB2_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%rbx,%rdx,4), %esi
	imull	%esi, %esi
	addl	%esi, (%rcx,%rdx,4)
	incq	%rdx
	cmpq	$16, %rdx
	jne	.LBB2_6
# %bb.7:                                #   in Loop: Header=BB2_5 Depth=1
	incq	%rax
	addq	$64, %rbx
	addq	$64, %rcx
	cmpq	$16, %rax
	jne	.LBB2_5
# %bb.8:                                # %.preheader.preheader
	xorl	%ebx, %ebx
.LBB2_9:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_10 Depth 2
	xorl	%r15d, %r15d
.LBB2_10:                               #   Parent Loop BB2_9 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%r14,%r15,4), %esi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r15
	cmpq	$16, %r15
	jne	.LBB2_10
# %bb.11:                               # %_Z13computeSquarePiS_i.exit
                                        #   in Loop: Header=BB2_9 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%rbx
	addq	$64, %r14
	cmpq	$16, %rbx
	jne	.LBB2_9
# %bb.12:
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"%d "
	.size	.L.str.1, 4

	.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	"Generating a random matrix..."
	.size	.Lstr, 30

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Calculating the square..."
	.size	.Lstr.1, 26

	.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
