#include <cuda.h>
#include <cuda_runtime_api.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdlib.h>

#define N         40
#define GAP       -2
#define MATCH      1
#define MISMATCH  -1

//#include "kernels.h"

using namespace std ;


__device__ volatile int g_mutex;
__device__ volatile int g_mutex_sync;

__device__ void __gpu_sync(int goalVal)
{
    //thread ID in a block
    int tid_in_block = threadIdx.x * blockDim.y + threadIdx.y;
    // only thread 0 is used for synchronization
    if (tid_in_block == 0)
    {
            atomicAdd((int *)&g_mutex_sync, 1);

            while(g_mutex_sync != goalVal)
            {
            //Do nothing here
            }        
            //g_mutex = 0 ;
    }
    
    //__threadfence();
    __syncthreads();

}

__global__ void calc_matrix(int *d_mat , int l1 , int l2)
{  
    int row = (blockIdx.y * blockDim.y) + threadIdx.y ; 

    int col = (blockIdx.x * blockDim.x) + threadIdx.x ;

    int offset = col + row * blockDim.x * gridDim.x ;

    __gpu_sync(1);

    d_mat[offset]       =  gridDim.x     ;

}



void cuda_error_check(cudaError_t err , const char *msg );

int main()
{
        const int NN = 64 ;
        const int  L1 = NN ;
        const int  L2 = NN ;
           

      //  int  mat[L1*L2];        // Dynamic Prog. Matrix

        int *mat = new int[L1*L2] ;
        

        //printf("\nSize : %d\n",sizeof(mat));
// #################################  Parallel #####################################################        
        int *d_mat ;
        
        cuda_error_check(cudaSetDevice(0) , "cudaSetDevice failed!" );


        cuda_error_check(cudaMalloc((void **)&d_mat       , L1 * L2 * sizeof(int)),"cudaMalloc Failed!");

        int BLOCK_SIZE = 16;
        int GRID_SIZE  = 1  ;

        dim3 dimBlock(BLOCK_SIZE , BLOCK_SIZE );
        dim3 dimGrid(GRID_SIZE , 1);

        calc_matrix<<< dimBlock , dimGrid >>> ( d_mat , L1 , L2 ) ;

        printf("\nKernel Complete Size : %d %d : %d ", L1 ,L2 , L1*L2 );

        cuda_error_check(cudaMemcpy(mat , d_mat ,L1* L2 * sizeof(int) , cudaMemcpyDeviceToHost),"cudaMemcpy D-H failed! 1");
        printf("\nCudamemcpy D-H Complete");

        cudaFree(d_mat);

       // cudaDeviceReset();

// #################################################################################################        

        printf("\nMatrix: \n");

        cout << "        ";
        for( int j = 0; j < L1; j++ )
        {
                //cout << seq_2[ j ] << "   ";
        }
        cout << "\n";

        for( int i = 0; i < L2; i++ )
        {
                if( i > 0 )
                {
                        //cout << seq_1[ i-1 ] << " ";
                }
                for( int j = 0; j < L1; j++ )
                {
                        cout.width(3);
                        cout << mat[i*L1 + j] << " ";
                }
                cout << endl;
        }

// #################################################################################################
        
}

void cuda_error_check(cudaError_t err , const char *msg )
{
    if(err != cudaSuccess)
    {
      printf("The error is %s, %s \n", cudaGetErrorString(err), msg );
      exit(1);
    }
}

	.file	"tmpxft_00309572_00000000-6_test4.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3894:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3894:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z10__gpu_synci
	.type	_Z10__gpu_synci, @function
_Z10__gpu_synci:
.LFB3889:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE3889:
	.size	_Z10__gpu_synci, .-_Z10__gpu_synci
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"The error is %s, %s \n"
	.text
	.globl	_Z16cuda_error_check9cudaErrorPKc
	.type	_Z16cuda_error_check9cudaErrorPKc, @function
_Z16cuda_error_check9cudaErrorPKc:
.LFB3891:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L4
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rsi, %rbx
	call	cudaGetErrorString@PLT
	movl	$2, %edi
	movq	%rbx, %rcx
	leaq	.LC0(%rip), %rsi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L4:
	.cfi_def_cfa_offset 8
	.cfi_restore 3
	ret
	.cfi_endproc
.LFE3891:
	.size	_Z16cuda_error_check9cudaErrorPKc, .-_Z16cuda_error_check9cudaErrorPKc
	.globl	_Z33__device_stub__Z11calc_matrixPiiiPiii
	.type	_Z33__device_stub__Z11calc_matrixPiiiPiii, @function
_Z33__device_stub__Z11calc_matrixPiiiPiii:
.LFB3916:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %rcx
	leaq	32(%rsp), %rdi
	movl	%esi, 4(%rsp)
	leaq	44(%rsp), %rsi
	movl	%edx, (%rsp)
	leaq	16(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 40(%rsp)
	movq	%rax, 80(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L10
	pushq	24(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z11calc_matrixPiii(%rip), %rdi
	pushq	24(%rsp)
	.cfi_def_cfa_offset 144
	movq	60(%rsp), %rcx
	movl	68(%rsp), %r8d
	movq	48(%rsp), %rsi
	movl	56(%rsp), %edx
	leaq	96(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L10:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3916:
	.size	_Z33__device_stub__Z11calc_matrixPiiiPiii, .-_Z33__device_stub__Z11calc_matrixPiiiPiii
	.globl	_Z11calc_matrixPiii
	.type	_Z11calc_matrixPiii, @function
_Z11calc_matrixPiii:
.LFB3917:
	.cfi_startproc
	endbr64
	jmp	_Z33__device_stub__Z11calc_matrixPiiiPiii
	.cfi_endproc
.LFE3917:
	.size	_Z11calc_matrixPiii, .-_Z11calc_matrixPiii
	.section	.rodata.str1.1
.LC1:
	.string	"cudaSetDevice failed!"
.LC2:
	.string	"cudaMalloc Failed!"
.LC3:
	.string	"\nKernel Complete Size : %d %d : %d "
.LC4:
	.string	"cudaMemcpy D-H failed! 1"
.LC5:
	.string	"\nCudamemcpy D-H Complete"
.LC6:
	.string	"\nMatrix: \n"
.LC7:
	.string	"        "
.LC8:
	.string	"\n"
.LC9:
	.string	" "
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3890:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$16384, %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
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	_Znam@PLT
	xorl	%edi, %edi
	movq	%rax, %rbp
	call	cudaSetDevice@PLT
	leaq	.LC1(%rip), %rsi
	movl	%eax, %edi
	call	_Z16cuda_error_check9cudaErrorPKc
	leaq	8(%rsp), %rdi
	movl	$16384, %esi
	call	cudaMalloc@PLT
	leaq	.LC2(%rip), %rsi
	movl	%eax, %edi
	call	_Z16cuda_error_check9cudaErrorPKc
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1, %ecx
	movabsq	$4294967297, %rdx
	movl	$1, %esi
	movabsq	$68719476752, %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L16
	movq	8(%rsp), %rdi
	movl	$64, %edx
	movl	$64, %esi
	call	_Z33__device_stub__Z11calc_matrixPiiiPiii
.L16:
	movl	$4096, %r8d
	movl	$64, %ecx
	xorl	%eax, %eax
	xorl	%r12d, %r12d
	movl	$64, %edx
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	8(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbp, %rdi
	movl	$16384, %edx
	leaq	_ZSt4cout(%rip), %r13
	leaq	.LC9(%rip), %r15
	call	cudaMemcpy@PLT
	leaq	.LC4(%rip), %rsi
	movl	%eax, %edi
	call	_Z16cuda_error_check9cudaErrorPKc
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$8, %edx
	leaq	.LC7(%rip), %rsi
	movq	%r13, %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
	movl	$1, %edx
	leaq	.LC8(%rip), %rsi
	movq	%r13, %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
.L17:
	movq	%r12, %r14
	xorl	%ebx, %ebx
	salq	$8, %r14
	addq	%rbp, %r14
.L18:
	movl	(%r14,%rbx,4), %esi
	movq	%r13, %rdi
	movq	$3, 24+_ZSt4cout(%rip)
	incq	%rbx
	call	_ZNSolsEi@PLT
	movl	$1, %edx
	movq	%r15, %rsi
	movq	%rax, %rdi
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
	cmpq	$64, %rbx
	jne	.L18
	movq	%r13, %rdi
	incq	%r12
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	cmpq	$64, %r12
	jne	.L17
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L20
	call	__stack_chk_fail@PLT
.L20:
	addq	$56, %rsp
	.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
.LFE3890:
	.size	main, .-main
	.section	.rodata.str1.1
.LC10:
	.string	"_Z11calc_matrixPiii"
.LC11:
	.string	"g_mutex"
.LC12:
	.string	"g_mutex_sync"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3919:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC10(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z11calc_matrixPiii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC11(%rip), %rdx
	movl	$4, %r9d
	leaq	_ZL7g_mutex(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	call	__cudaRegisterVar@PLT
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC12(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$4, %r9d
	leaq	_ZL12g_mutex_sync(%rip), %rsi
	call	__cudaRegisterVar@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	popq	%rcx
	.cfi_def_cfa_offset 24
	popq	%rsi
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3919:
	.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
	.local	_ZL12g_mutex_sync
	.comm	_ZL12g_mutex_sync,4,4
	.local	_ZL7g_mutex
	.comm	_ZL7g_mutex,4,4
	.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 <hip/hip_runtime_api.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdlib.h>

#define N         40
#define GAP       -2
#define MATCH      1
#define MISMATCH  -1

//#include "kernels.h"

using namespace std ;


__device__ volatile int g_mutex;
__device__ volatile int g_mutex_sync;

__device__ void __gpu_sync(int goalVal)
{
    //thread ID in a block
    int tid_in_block = threadIdx.x * blockDim.y + threadIdx.y;
    // only thread 0 is used for synchronization
    if (tid_in_block == 0)
    {
            atomicAdd((int *)&g_mutex_sync, 1);

            while(g_mutex_sync != goalVal)
            {
            //Do nothing here
            }        
            //g_mutex = 0 ;
    }
    
    //__threadfence();
    __syncthreads();

}

__global__ void calc_matrix(int *d_mat , int l1 , int l2)
{  
    int row = (blockIdx.y * blockDim.y) + threadIdx.y ; 

    int col = (blockIdx.x * blockDim.x) + threadIdx.x ;

    int offset = col + row * blockDim.x * gridDim.x ;

    __gpu_sync(1);

    d_mat[offset]       =  gridDim.x     ;

}



void cuda_error_check(hipError_t err , const char *msg );

int main()
{
        const int NN = 64 ;
        const int  L1 = NN ;
        const int  L2 = NN ;
           

      //  int  mat[L1*L2];        // Dynamic Prog. Matrix

        int *mat = new int[L1*L2] ;
        

        //printf("\nSize : %d\n",sizeof(mat));
// #################################  Parallel #####################################################        
        int *d_mat ;
        
        cuda_error_check(hipSetDevice(0) , "hipSetDevice failed!" );


        cuda_error_check(hipMalloc((void **)&d_mat       , L1 * L2 * sizeof(int)),"hipMalloc Failed!");

        int BLOCK_SIZE = 16;
        int GRID_SIZE  = 1  ;

        dim3 dimBlock(BLOCK_SIZE , BLOCK_SIZE );
        dim3 dimGrid(GRID_SIZE , 1);

        calc_matrix<<< dimBlock , dimGrid >>> ( d_mat , L1 , L2 ) ;

        printf("\nKernel Complete Size : %d %d : %d ", L1 ,L2 , L1*L2 );

        cuda_error_check(hipMemcpy(mat , d_mat ,L1* L2 * sizeof(int) , hipMemcpyDeviceToHost),"hipMemcpy D-H failed! 1");
        printf("\nCudamemcpy D-H Complete");

        hipFree(d_mat);

       // cudaDeviceReset();

// #################################################################################################        

        printf("\nMatrix: \n");

        cout << "        ";
        for( int j = 0; j < L1; j++ )
        {
                //cout << seq_2[ j ] << "   ";
        }
        cout << "\n";

        for( int i = 0; i < L2; i++ )
        {
                if( i > 0 )
                {
                        //cout << seq_1[ i-1 ] << " ";
                }
                for( int j = 0; j < L1; j++ )
                {
                        cout.width(3);
                        cout << mat[i*L1 + j] << " ";
                }
                cout << endl;
        }

// #################################################################################################
        
}

void cuda_error_check(hipError_t err , const char *msg )
{
    if(err != hipSuccess)
    {
      printf("The error is %s, %s \n", hipGetErrorString(err), msg );
      exit(1);
    }
}

	.text
	.file	"test4.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z26__device_stub__calc_matrixPiii # -- Begin function _Z26__device_stub__calc_matrixPiii
	.type	_Z26__device_stub__calc_matrixPiii,@function
_Z26__device_stub__calc_matrixPiii:     # @_Z26__device_stub__calc_matrixPiii
	.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
	subq	$96, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	4(%rsp), %rcx
	movl	%esi, (%rcx)
	movq	%rsp, %rsi
	movl	%edx, (%rsi)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z11calc_matrixPiii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$112, %rsp
	.cfi_adjust_cfa_offset -112
	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_end0:
	.size	_Z26__device_stub__calc_matrixPiii, .Lfunc_end0-_Z26__device_stub__calc_matrixPiii
	.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	$16384, %edi                    # imm = 0x4000
	callq	_Znam
	movq	%rax, %rbx
	xorl	%edi, %edi
	callq	hipSetDevice
	movl	$.L.str, %esi
	movl	%eax, %edi
	callq	_Z16cuda_error_check10hipError_tPKc
	movq	%rsp, %rdi
	movl	$16384, %esi                    # imm = 0x4000
	callq	hipMalloc
	movl	$.L.str.1, %esi
	movl	%eax, %edi
	callq	_Z16cuda_error_check10hipError_tPKc
	movabsq	$68719476752, %rdi              # imm = 0x1000000010
	movabsq	$4294967297, %rdx               # imm = 0x100000001
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_2
# %bb.1:
	movq	(%rsp), %rdi
	movl	$64, %esi
	movl	$64, %edx
	callq	_Z26__device_stub__calc_matrixPiii
.LBB1_2:
	xorl	%r14d, %r14d
	movl	$.L.str.2, %edi
	movl	$64, %esi
	movl	$64, %edx
	movl	$4096, %ecx                     # imm = 0x1000
	xorl	%eax, %eax
	callq	printf
	movq	(%rsp), %rsi
	movl	$16384, %edx                    # imm = 0x4000
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movl	$.L.str.3, %esi
	movl	%eax, %edi
	callq	_Z16cuda_error_check10hipError_tPKc
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
	movq	(%rsp), %rdi
	callq	hipFree
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$_ZSt4cout, %r15d
	movl	$_ZSt4cout, %edi
	movl	$.L.str.6, %esi
	movl	$8, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	$_ZSt4cout, %edi
	movl	$.L.str.7, %esi
	movl	$1, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.LBB1_3:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
	xorl	%r12d, %r12d
.LBB1_4:                                #   Parent Loop BB1_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rax
	movq	$3, _ZSt4cout+16(%rax)
	movl	(%rbx,%r12,4), %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSolsEi
	movl	$.L.str.8, %esi
	movl	$1, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	incq	%r12
	cmpq	$64, %r12
	jne	.LBB1_4
# %bb.5:                                #   in Loop: Header=BB1_3 Depth=1
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%r15, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	incq	%r14
	addq	$256, %rbx                      # imm = 0x100
	cmpq	$64, %r14
	jne	.LBB1_3
# %bb.6:
	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_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z16cuda_error_check10hipError_tPKc # -- Begin function _Z16cuda_error_check10hipError_tPKc
	.type	_Z16cuda_error_check10hipError_tPKc,@function
_Z16cuda_error_check10hipError_tPKc:    # @_Z16cuda_error_check10hipError_tPKc
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jne	.LBB2_2
# %bb.1:
	retq
.LBB2_2:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rsi, %rbx
	callq	hipGetErrorString
	movl	$.L.str.9, %edi
	movq	%rax, %rsi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end2:
	.size	_Z16cuda_error_check10hipError_tPKc, .Lfunc_end2-_Z16cuda_error_check10hipError_tPKc
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB3_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB3_2:
	subq	$32, %rsp
	.cfi_adjust_cfa_offset 32
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z11calc_matrixPiii, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	addq	$32, %rsp
	.cfi_adjust_cfa_offset -32
	movl	$g_mutex, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movl	$4, %r9d
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	pushq	$0
	.cfi_adjust_cfa_offset 8
	pushq	$0
	.cfi_adjust_cfa_offset 8
	callq	__hipRegisterVar
	addq	$16, %rsp
	.cfi_adjust_cfa_offset -16
	movl	$g_mutex_sync, %esi
	movl	$.L__unnamed_3, %edx
	movl	$.L__unnamed_3, %ecx
	movl	$4, %r9d
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	pushq	$0
	.cfi_adjust_cfa_offset 8
	pushq	$0
	.cfi_adjust_cfa_offset 8
	callq	__hipRegisterVar
	addq	$16, %rsp
	.cfi_adjust_cfa_offset -16
	movl	$__hip_module_dtor, %edi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end3:
	.size	__hip_module_ctor, .Lfunc_end3-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB4_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB4_2:
	retq
.Lfunc_end4:
	.size	__hip_module_dtor, .Lfunc_end4-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	g_mutex,@object                 # @g_mutex
	.local	g_mutex
	.comm	g_mutex,4,4
	.type	g_mutex_sync,@object            # @g_mutex_sync
	.local	g_mutex_sync
	.comm	g_mutex_sync,4,4
	.type	_Z11calc_matrixPiii,@object     # @_Z11calc_matrixPiii
	.section	.rodata,"a",@progbits
	.globl	_Z11calc_matrixPiii
	.p2align	3, 0x0
_Z11calc_matrixPiii:
	.quad	_Z26__device_stub__calc_matrixPiii
	.size	_Z11calc_matrixPiii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"hipSetDevice failed!"
	.size	.L.str, 21

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"hipMalloc Failed!"
	.size	.L.str.1, 18

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"\nKernel Complete Size : %d %d : %d "
	.size	.L.str.2, 36

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"hipMemcpy D-H failed! 1"
	.size	.L.str.3, 24

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"\nCudamemcpy D-H Complete"
	.size	.L.str.4, 25

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"        "
	.size	.L.str.6, 9

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"\n"
	.size	.L.str.7, 2

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	" "
	.size	.L.str.8, 2

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"The error is %s, %s \n"
	.size	.L.str.9, 22

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z11calc_matrixPiii"
	.size	.L__unnamed_1, 20

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"g_mutex"
	.size	.L__unnamed_2, 8

	.type	.L__unnamed_3,@object           # @2
.L__unnamed_3:
	.asciz	"g_mutex_sync"
	.size	.L__unnamed_3, 13

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.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	"\nMatrix: "
	.size	.Lstr, 10

	.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 _Z26__device_stub__calc_matrixPiii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym g_mutex
	.addrsig_sym g_mutex_sync
	.addrsig_sym _Z11calc_matrixPiii
	.addrsig_sym _ZSt4cout
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z11calc_matrixPiii     ; -- Begin function _Z11calc_matrixPiii
	.globl	_Z11calc_matrixPiii
	.p2align	8
	.type	_Z11calc_matrixPiii,@function
_Z11calc_matrixPiii:                    ; @_Z11calc_matrixPiii
; %bb.0:
	s_clause 0x1
	s_load_b32 s3, s[0:1], 0x1c
	s_load_b32 s2, s[0:1], 0x10
	v_and_b32_e32 v1, 0x3ff, v0
	v_bfe_u32 v0, v0, 10, 10
	s_mov_b32 s5, exec_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	v_sub_nc_u32_e32 v3, 0, v0
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s4, s3, 16
	v_mul_u32_u24_e32 v2, s4, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_eq_u32_e64 v2, v3
	s_cbranch_execz .LBB0_5
; %bb.1:
	s_mov_b32 s7, exec_lo
	s_mov_b32 s6, exec_lo
	v_mbcnt_lo_u32_b32 v2, s7, 0
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_eq_u32_e32 0, v2
	s_cbranch_execz .LBB0_3
; %bb.2:
	s_bcnt1_i32_b32 s7, s7
	s_delay_alu instid0(SALU_CYCLE_1)
	v_dual_mov_b32 v2, 0 :: v_dual_mov_b32 v3, s7
	s_getpc_b64 s[8:9]
	s_add_u32 s8, s8, g_mutex_sync@rel32@lo+4
	s_addc_u32 s9, s9, g_mutex_sync@rel32@hi+12
	global_atomic_add_u32 v2, v3, s[8:9]
.LBB0_3:
	s_or_b32 exec_lo, exec_lo, s6
	s_mov_b32 s6, 0
	s_getpc_b64 s[8:9]
	s_add_u32 s8, s8, g_mutex_sync@rel32@lo+4
	s_addc_u32 s9, s9, g_mutex_sync@rel32@hi+12
.LBB0_4:                                ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_4) | instid1(SALU_CYCLE_1)
	v_dual_mov_b32 v2, s8 :: v_dual_mov_b32 v3, s9
	flat_load_b32 v2, v[2:3] glc dlc
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_cmp_eq_u32_e32 vcc_lo, 1, v2
	s_or_b32 s6, vcc_lo, s6
	s_and_not1_b32 exec_lo, exec_lo, s6
	s_cbranch_execnz .LBB0_4
.LBB0_5:                                ; %Flow9
	s_or_b32 exec_lo, exec_lo, s5
	v_mad_u64_u32 v[2:3], null, s15, s4, v[0:1]
	s_and_b32 s3, s3, 0xffff
	s_load_b64 s[0:1], s[0:1], 0x0
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	v_mad_u64_u32 v[3:4], null, v2, s2, s[14:15]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mad_u64_u32 v[4:5], null, v3, s3, v[1:2]
	v_mov_b32_e32 v2, s2
	v_ashrrev_i32_e32 v5, 31, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[4:5]
	v_add_co_u32 v0, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	global_store_b32 v[0:1], v2, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z11calc_matrixPiii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 272
		.amdhsa_user_sgpr_count 14
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 6
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z11calc_matrixPiii, .Lfunc_end0-_Z11calc_matrixPiii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 340
; NumSgprs: 18
; NumVgprs: 6
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 6
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.protected	g_mutex                 ; @g_mutex
	.type	g_mutex,@object
	.section	.bss,"aw",@nobits
	.globl	g_mutex
	.p2align	2, 0x0
g_mutex:
	.long	0                               ; 0x0
	.size	g_mutex, 4

	.protected	g_mutex_sync            ; @g_mutex_sync
	.type	g_mutex_sync,@object
	.globl	g_mutex_sync
	.p2align	2, 0x0
g_mutex_sync:
	.long	0                               ; 0x0
	.size	g_mutex_sync, 4

	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.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 g_mutex_sync
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .offset:         12
        .size:           4
        .value_kind:     by_value
      - .offset:         16
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         20
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         28
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         30
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         32
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         34
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         36
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         38
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         80
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z11calc_matrixPiii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z11calc_matrixPiii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     6
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
