#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>


/*
Instruções
COMPILAR -->  nvcc 2DstencilGPUSharedMemoryBlankBorderTimeSpaceSharingOpencvKarma.cu -o go `pkg-config --cflags --libs opencv` -w
EXECUTAR --> ./go DOMAIN_DIMS STENCIL_ORDER SPACE_TIME_BLOCK_TIMES BLOCK_DIM_X BLOCK_DIM_Y
*/

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <string>

using namespace std;

//===> CONSTANTES karma model <===//
#ifndef MODEL_WIDTH
#define MODEL_WIDTH 96
#endif

#ifndef BLOCK_TIMES
#define BLOCK_TIMES 1
#endif

#define Eh 3.0f
#define En 1.0f
#define Re 0.6f
#define tauE 5.0f
#define tauN 250.0f
#define gam 0.001f
#define East 1.5415f
#define DT 0.05f
#define DX (12.0f / MODEL_WIDTH)

#define MODELSIZE_X (MODEL_WIDTH)
#define MODELSIZE_Y (MODEL_WIDTH)
#define MODELSIZE_Z 1
#define MODELSIZE2D ( MODELSIZE_X*MODELSIZE_Y )

#ifndef BLOCKDIM_X
#define BLOCKDIM_X 32
#endif

#ifndef BLOCKDIM_Y
#define BLOCKDIM_Y 32
#endif

#define BLOCKDIM_Z 1
#define BLOCKDIM2D ( BLOCKDIM_X*BLOCKDIM_Y )

//==> CUDA GRID <==//
#define GRIDDIM_X ( ( MODELSIZE_X / BLOCKDIM_X ) + ( ( MODELSIZE_X % BLOCKDIM_X ) > 0 ) )
#define GRIDDIM_Y ( ( MODELSIZE_Y / BLOCKDIM_Y ) + ( ( MODELSIZE_Y % BLOCKDIM_Y ) > 0 ) )
#define GRIDDIM_Z 1

#define SHARED_TAM ((BLOCKDIM_X + (2 * BLOCK_TIMES)) * (BLOCKDIM_Y + (2 * BLOCK_TIMES)))
#define SHARED_DX (BLOCKDIM_X + (2 * BLOCK_TIMES))
#define SHARED_DY (BLOCKDIM_Y + (2 * BLOCK_TIMES))
/*
Função somente da GPU que recebe os parametros para o calculo de um stencil
d_e - dado de entrada
d_r - dado de saida
d_v - campo que deve ser atualizado
c_coeff - variável utilizada para armazenar o valores dos coeficcientes do stencil (utilizada apenas na versão com stencil simples usado anteriormente)
X - Y - Dimensões das estruturas de entrada
k - ordem do stencil
x -y - posição do centro do stencil na estrutura de entrada
GX - Dimensão horizontal da estrutura do dado de saída
Gx - Gy posição do centro do stencil na estrutura de saida
*/
__forceinline__ __device__ void _2Dstencil_(float *d_e, float *d_r, float *d_v, int X, int x, int y, int GX, int Gx, int Gy)
{
    int h_e_i = x + (y * (X));
    float temp = d_e[h_e_i];
    
    float rv = d_v[h_e_i];


    float Rn = (1.0f / (1.0f - expf(-Re))) - rv;
    float p = (temp > En) * 1.0f;
    float dv = (Rn * p - (1.0f - p) * rv) / tauN;
    float Dn = rv * rv;
    float hE = (1.0f - tanh(temp - Eh)) * temp * temp / 2.0f;
    float du = (((East - Dn) * hE) - temp) / tauE;

    float xlapr = d_e[(x + 1) + ((y) * (X))] - temp;
    float xlapl = temp - d_e[(x - 1) + ((y) * (X))];
    float xlapf = d_e[(x) + ((y + 1) * (X))] - temp;
    float xlapb = temp - d_e[(x) + ((y - 1) * (X))];

    float lap = xlapr - xlapl + xlapf - xlapb;
   
    temp = (temp + (du * DT) + (lap * DT * gam / (DX * DX)));

    d_v[h_e_i] = rv + dv * DT;
    h_e_i = Gx + ((Gy) * (GX));
    d_r[h_e_i] = temp;
}
/*
função chamada pelo host que controla as cópias e a ordem do calculo dos stencils bem como a carga para cada thread
, MODELSIZE_X, MODELSIZE_Y, BLOCK_TIMES
 int X, int Y, int times
*/
__global__ void _2Dstencil_global(float *d_e, float *d_r, float *d_v)
{

    int x, y; //,h_e_i,h_r_i,Xs,Ys,Dx,Dy;
    x = threadIdx.x + (blockIdx.x * BLOCKDIM_X);
    y = threadIdx.y + (blockIdx.y * BLOCKDIM_Y);
    extern __shared__ float sharedOrig[];

    int blockThreadIndex = threadIdx.x + threadIdx.y * BLOCKDIM_X;

    float * shared = sharedOrig;
    float * sharedRes = shared + SHARED_TAM;
    float * sharedV = sharedRes + SHARED_TAM; 
    /*
    Copia o Tile de memória compartilhada necessária para a configuração de tempo desejada
    Stride é utilizado pois a quantidade de elementos a serem copiados é sempre maior que a quantidade de threads
    As bordas
    */
    for (int stride = blockThreadIndex; stride < SHARED_TAM; stride += (BLOCKDIM_X * BLOCKDIM_Y))
    {
        int sharedIdxX = stride % SHARED_DX;
        int sharedIdxY = int(stride / SHARED_DX);
        int globalIdxX = (blockIdx.x * BLOCKDIM_X) + sharedIdxX - BLOCK_TIMES;
        int globalIdxY = (blockIdx.y * BLOCKDIM_Y) + sharedIdxY - BLOCK_TIMES;
        int globalIdx = globalIdxX + (-1*globalIdxX)*(globalIdxX < 0) - (globalIdxX-MODELSIZE_X+1)*(globalIdxX >= MODELSIZE_X)  +  (globalIdxY + (-1*globalIdxY)*(globalIdxY < 0) - (globalIdxY-MODELSIZE_Y+1)*(globalIdxY >= MODELSIZE_Y)) * MODELSIZE_X;
       
        shared[stride] = d_e[globalIdx];
        sharedV[stride] = d_v[globalIdx];
    }

    __syncthreads();

    /*
    Envia pra ser calculado todos os elementos além do ultimo instante de tempo
    */
    for (int t = 1; t < BLOCK_TIMES; t++)
    {
        int tDx = BLOCKDIM_X + ((BLOCK_TIMES - t) * 2);
        int tDy = BLOCKDIM_Y + ((BLOCK_TIMES - t) * 2);
        int tk2 = (t);
        int tSharedTam = tDx * tDy;
        for (int stride = blockThreadIndex; stride < tSharedTam; stride += (BLOCKDIM_X * BLOCKDIM_Y))
        {
            int tempX = (stride % tDx) + tk2;
            int tempY = (int(stride / tDx)) + tk2;
            _2Dstencil_(shared, sharedRes, sharedV, SHARED_DX, tempX, tempY, SHARED_DX, tempX, tempY);
        }

        float * temp = shared;
        shared = sharedRes;
        sharedRes = temp;
        __syncthreads();
    }
    /*
    Envia pra ser calculado todos os elementos do ultimo instante de tempo
   */
    _2Dstencil_(shared, d_r, sharedV, SHARED_DX, ((x%(BLOCKDIM_X))+BLOCK_TIMES), ((y%(BLOCKDIM_Y))+BLOCK_TIMES), MODELSIZE_X, x, y);
    
     int globalIdx = x + y * MODELSIZE_X;
     int sharedIdx = ((x%(BLOCKDIM_X))+BLOCK_TIMES) + ((y%(BLOCKDIM_Y))+BLOCK_TIMES)*SHARED_DX;
     d_v[globalIdx] = sharedV[sharedIdx];
}

int main(int argc, char *argv[])
{
    /*
    Declarações e valores padroes
    */
    //float *h_e, *h_r, *h_v;
    bool resultado = false;
    float *h_e, *h_v;
    float *d_e, *d_r, *d_v;
    int sharedSize;
    int globalTimes = 1;

    /*
    Obtenção dos parâmetros de entrada
    */
    if (argc > 1)
    {
        globalTimes = atoi(argv[1]);
    }

    if(argc > 2)
    {
        resultado = atoi(argv[2])==1;
    }


    /*
    Allocações de memória e configuração dos blocos e grid
    */
    dim3 grid_dim(GRIDDIM_X,GRIDDIM_Y,GRIDDIM_Z);
    dim3 block_dim(BLOCKDIM_X,BLOCKDIM_Y,BLOCKDIM_Z);
    sharedSize = SHARED_TAM * sizeof(float) * 3;
    h_e = (float *)malloc(MODELSIZE2D*sizeof(float));
    h_v = (float *)malloc(MODELSIZE2D*sizeof(float));
    cudaMalloc(&d_e, MODELSIZE2D*sizeof(float));
    cudaMalloc(&d_r, MODELSIZE2D*sizeof(float));
    cudaMalloc(&d_v, MODELSIZE2D*sizeof(float));

//Copia os dados do campo e envia para a GPU e inicializa o dominio de entrada

        


    FILE *arq;
    arq = fopen("entrada.txt", "rt");
    for (int i = 0; i < MODELSIZE_X; i++)
        for (int j = 0; j < MODELSIZE_Y; j++)
        {
            h_v[i + j * MODELSIZE_X] =0.5f;
            int temp;
            fscanf(arq," %d",&temp);
            h_e[i + j * MODELSIZE_X] = temp;
        }

    fclose(arq);
    cudaMemcpy(d_v, h_v, MODELSIZE2D*sizeof(float), cudaMemcpyHostToDevice);
   
    /* 
    Copy vectors from host memory to device memory
    Copia os dados da entrada de volta a GPU
        */
    cudaMemcpy(d_e, h_e, MODELSIZE2D*sizeof(float), cudaMemcpyHostToDevice);
    
    /*
    Começa o Timer
    */
    cudaEvent_t start, stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    cudaEventRecord(start, 0);

    /******************
    *** Kernel Call ***
    *******************/
    //_3Dstencil_global<<<blks,th_p_blk>>>(d_e,d_r,X,Y,Z);
    /*
    Executa o kernel
    */
    for(int i=0; i<globalTimes/BLOCK_TIMES; i ++)
    {
        _2Dstencil_global<<<grid_dim, block_dim, sharedSize>>>(d_e, d_r, d_v);
        float * temp = d_e;
        d_e = d_r;
        d_r = temp;
    }
    

    /*
    Identifica possíveis erros
    */
    cudaError_t err = cudaSuccess;
    err = cudaGetLastError();
    if (err != cudaSuccess)
    {
        printf ("-1");
        cudaFree(d_e);
        cudaFree(d_r);
        cudaFree(d_v);
        std::free(h_e);
        std::free(h_v);
        fprintf(stderr, "Failed to launch _2Dstencil_global kernel (error code %s)!\n", cudaGetErrorString(err));
        return 0;
    }
    /******************
    *** Kernel Call ***
    *******************/

    cudaDeviceSynchronize();
    /*
    Para o Timer
    */
    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);
    float elapsedTime;
    cudaEventElapsedTime(&elapsedTime, start, stop);
    cudaEventDestroy(start);
    cudaEventDestroy(stop);

    //printf("X %d || Y %d \nBX %d || BY %d \n",X,Y,BX,BY);
    //printf ("[%d,%.5f], sharedSize %d  CPUMem = %d bytes  GPUMen = %d bytes\n", times,elapsedTime,sharedSize,size*2,size*3);
    //printf("GPU elapsed time: %f s (%f milliseconds)\n", (elapsedTime/1000.0), elapsedTime);
    printf ("%.5f",elapsedTime);
    /*
    Copia o resultado de volta para o CPU
    */
    cudaMemcpy(h_e, d_e, MODELSIZE2D*sizeof(float), cudaMemcpyDeviceToHost);
    /*
    Copia o resultado para a imagem de visualização
    A estrutura de 
    */
    if(resultado)
    {
        arq = fopen("resultado.txt", "wt");
        for (int i = 0; i < MODELSIZE_X; i++)
        {
            for (int j = 0; j < MODELSIZE_Y; j++)
            {
                fprintf(arq," %6.4f",h_e[i+j*MODELSIZE_X]);
            }
            fprintf(arq,"\n");
        }
        fclose(arq);
    }
    
        

    cudaFree(d_e);
    cudaFree(d_r);
    cudaFree(d_v);
    std::free(h_e);
    std::free(h_v);

    return 0;
} /* main */

	.file	"tmpxft_003ae415_00000000-6_2DstencilGPUSharedMemoryKarmaSpaceTimeBlocking.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3783:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3783:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z41__device_stub__Z17_2Dstencil_globalPfS_S_PfS_S_
	.type	_Z41__device_stub__Z17_2Dstencil_globalPfS_S_PfS_S_, @function
_Z41__device_stub__Z17_2Dstencil_globalPfS_S_PfS_S_:
.LFB3805:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z17_2Dstencil_globalPfS_S_(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L2:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3805:
	.size	_Z41__device_stub__Z17_2Dstencil_globalPfS_S_PfS_S_, .-_Z41__device_stub__Z17_2Dstencil_globalPfS_S_PfS_S_
	.globl	_Z17_2Dstencil_globalPfS_S_
	.type	_Z17_2Dstencil_globalPfS_S_, @function
_Z17_2Dstencil_globalPfS_S_:
.LFB3806:
	.cfi_startproc
	endbr64
	jmp	_Z41__device_stub__Z17_2Dstencil_globalPfS_S_PfS_S_
	.cfi_endproc
.LFE3806:
	.size	_Z17_2Dstencil_globalPfS_S_, .-_Z17_2Dstencil_globalPfS_S_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"rt"
.LC1:
	.string	"entrada.txt"
.LC3:
	.string	" %d"
.LC4:
	.string	"-1"
.LC5:
	.string	"Failed to launch _2Dstencil_global kernel (error code %s)!\n"
.LC6:
	.string	"%.5f"
.LC7:
	.string	"wt"
.LC8:
	.string	"resultado.txt"
.LC9:
	.string	" %6.4f"
.LC10:
	.string	"\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3780:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	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	$120, %rsp
	.cfi_def_cfa_offset 176
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	cmpl	$1, %edi
	jle	.L23
	movl	%edi, %ebp
	movq	8(%rsi), %rdi
	movq	%rsi, %rbx
	xorl	%r14d, %r14d
	call	atoi@PLT
	movl	%eax, %r12d
	cmpl	$2, %ebp
	je	.L9
	movq	16(%rbx), %rdi
	call	atoi@PLT
	decl	%eax
	sete	%r14b
	jmp	.L9
.L23:
	movl	$1, %r12d
	xorl	%r14d, %r14d
.L9:
	movabsq	$12884901891, %rax
	movl	$36864, %edi
	movl	$1, 88(%rsp)
	movq	%rax, 80(%rsp)
	movabsq	$137438953504, %rax
	movq	%rax, 92(%rsp)
	movl	$1, 100(%rsp)
	call	malloc@PLT
	movl	$36864, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	leaq	40(%rsp), %rdi
	movl	$36864, %esi
	movq	%rax, %rbp
	call	cudaMalloc@PLT
	leaq	48(%rsp), %rdi
	movl	$36864, %esi
	call	cudaMalloc@PLT
	leaq	56(%rsp), %rdi
	movl	$36864, %esi
	call	cudaMalloc@PLT
	leaq	.LC1(%rip), %rdi
	leaq	.LC0(%rip), %rsi
	call	fopen@PLT
	xorl	%r8d, %r8d
	movq	%rax, %rdi
.L10:
	leaq	0(,%r8,4), %r13
	xorl	%r15d, %r15d
	leaq	0(%rbp,%r13), %r9
	addq	%rbx, %r13
.L11:
	movl	$0x3f000000, (%r9,%r15)
	leaq	72(%rsp), %rax
	leaq	.LC3(%rip), %rsi
	movq	%rax, (%rsp)
	movq	%rax, %rdx
	xorl	%eax, %eax
	movq	%r8, 24(%rsp)
	movq	%r9, 16(%rsp)
	movq	%rdi, 8(%rsp)
	call	__isoc23_fscanf@PLT
	cvtsi2ssl	72(%rsp), %xmm0
	movq	8(%rsp), %rdi
	movq	16(%rsp), %r9
	movq	24(%rsp), %r8
	movss	%xmm0, 0(%r13,%r15)
	addq	$384, %r15
	cmpq	$36864, %r15
	jne	.L11
	incq	%r8
	cmpq	$96, %r8
	jne	.L10
	call	fclose@PLT
	movq	56(%rsp), %rdi
	movq	%rbp, %rsi
	xorl	%r13d, %r13d
	movl	$1, %ecx
	movl	$36864, %edx
	call	cudaMemcpy@PLT
	movq	40(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$36864, %edx
	call	cudaMemcpy@PLT
	leaq	64(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
.L13:
	cmpl	%r13d, %r12d
	jle	.L31
	movl	100(%rsp), %ecx
	movq	92(%rsp), %rdx
	xorl	%r9d, %r9d
	movl	$13872, %r8d
	movq	80(%rsp), %rdi
	movl	88(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L14
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	movq	40(%rsp), %rdi
	call	_Z41__device_stub__Z17_2Dstencil_globalPfS_S_PfS_S_
.L14:
	movq	40(%rsp), %rax
	movq	48(%rsp), %rdx
	incl	%r13d
	movq	%rdx, 40(%rsp)
	movq	%rax, 48(%rsp)
	jmp	.L13
.L31:
	call	cudaGetLastError@PLT
	movl	%eax, %r12d
	testl	%eax, %eax
	je	.L16
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movl	%r12d, %edi
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC5(%rip), %rdx
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L17
.L16:
	call	cudaDeviceSynchronize@PLT
	movq	72(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	72(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	72(%rsp), %rdx
	movq	64(%rsp), %rsi
	leaq	36(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	64(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	72(%rsp), %rdi
	call	cudaEventDestroy@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	36(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	40(%rsp), %rsi
	movl	$2, %ecx
	movl	$36864, %edx
	movq	%rbx, %rdi
	call	cudaMemcpy@PLT
	testb	%r14b, %r14b
	je	.L18
	leaq	.LC7(%rip), %rsi
	leaq	.LC8(%rip), %rdi
	xorl	%r13d, %r13d
	call	fopen@PLT
	leaq	.LC9(%rip), %r15
	movq	%rax, (%rsp)
.L19:
	leaq	(%rbx,%r13,4), %r12
	xorl	%r14d, %r14d
.L20:
	movq	(%rsp), %rdi
	movq	%r15, %rdx
	movl	$2, %esi
	movb	$1, %al
	incl	%r14d
	cvtss2sd	(%r12), %xmm0
	addq	$384, %r12
	call	__fprintf_chk@PLT
	cmpl	$96, %r14d
	jne	.L20
	movq	(%rsp), %rdi
	leaq	.LC10(%rip), %rdx
	xorl	%eax, %eax
	incq	%r13
	movl	$2, %esi
	call	__fprintf_chk@PLT
	cmpq	$96, %r13
	jne	.L19
	movq	(%rsp), %rdi
	call	fclose@PLT
.L18:
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
.L17:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L22
	call	__stack_chk_fail@PLT
.L22:
	addq	$120, %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
.LFE3780:
	.size	main, .-main
	.section	.rodata.str1.1
.LC11:
	.string	"_Z17_2Dstencil_globalPfS_S_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3808:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 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	.LC11(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z17_2Dstencil_globalPfS_S_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3808:
	.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>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>


/*
Instruções
COMPILAR -->  nvcc 2DstencilGPUSharedMemoryBlankBorderTimeSpaceSharingOpencvKarma.cu -o go `pkg-config --cflags --libs opencv` -w
EXECUTAR --> ./go DOMAIN_DIMS STENCIL_ORDER SPACE_TIME_BLOCK_TIMES BLOCK_DIM_X BLOCK_DIM_Y
*/

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <string>

using namespace std;

//===> CONSTANTES karma model <===//
#ifndef MODEL_WIDTH
#define MODEL_WIDTH 96
#endif

#ifndef BLOCK_TIMES
#define BLOCK_TIMES 1
#endif

#define Eh 3.0f
#define En 1.0f
#define Re 0.6f
#define tauE 5.0f
#define tauN 250.0f
#define gam 0.001f
#define East 1.5415f
#define DT 0.05f
#define DX (12.0f / MODEL_WIDTH)

#define MODELSIZE_X (MODEL_WIDTH)
#define MODELSIZE_Y (MODEL_WIDTH)
#define MODELSIZE_Z 1
#define MODELSIZE2D ( MODELSIZE_X*MODELSIZE_Y )

#ifndef BLOCKDIM_X
#define BLOCKDIM_X 32
#endif

#ifndef BLOCKDIM_Y
#define BLOCKDIM_Y 32
#endif

#define BLOCKDIM_Z 1
#define BLOCKDIM2D ( BLOCKDIM_X*BLOCKDIM_Y )

//==> CUDA GRID <==//
#define GRIDDIM_X ( ( MODELSIZE_X / BLOCKDIM_X ) + ( ( MODELSIZE_X % BLOCKDIM_X ) > 0 ) )
#define GRIDDIM_Y ( ( MODELSIZE_Y / BLOCKDIM_Y ) + ( ( MODELSIZE_Y % BLOCKDIM_Y ) > 0 ) )
#define GRIDDIM_Z 1

#define SHARED_TAM ((BLOCKDIM_X + (2 * BLOCK_TIMES)) * (BLOCKDIM_Y + (2 * BLOCK_TIMES)))
#define SHARED_DX (BLOCKDIM_X + (2 * BLOCK_TIMES))
#define SHARED_DY (BLOCKDIM_Y + (2 * BLOCK_TIMES))
/*
Função somente da GPU que recebe os parametros para o calculo de um stencil
d_e - dado de entrada
d_r - dado de saida
d_v - campo que deve ser atualizado
c_coeff - variável utilizada para armazenar o valores dos coeficcientes do stencil (utilizada apenas na versão com stencil simples usado anteriormente)
X - Y - Dimensões das estruturas de entrada
k - ordem do stencil
x -y - posição do centro do stencil na estrutura de entrada
GX - Dimensão horizontal da estrutura do dado de saída
Gx - Gy posição do centro do stencil na estrutura de saida
*/
__forceinline__ __device__ void _2Dstencil_(float *d_e, float *d_r, float *d_v, int X, int x, int y, int GX, int Gx, int Gy)
{
    int h_e_i = x + (y * (X));
    float temp = d_e[h_e_i];
    
    float rv = d_v[h_e_i];


    float Rn = (1.0f / (1.0f - expf(-Re))) - rv;
    float p = (temp > En) * 1.0f;
    float dv = (Rn * p - (1.0f - p) * rv) / tauN;
    float Dn = rv * rv;
    float hE = (1.0f - tanh(temp - Eh)) * temp * temp / 2.0f;
    float du = (((East - Dn) * hE) - temp) / tauE;

    float xlapr = d_e[(x + 1) + ((y) * (X))] - temp;
    float xlapl = temp - d_e[(x - 1) + ((y) * (X))];
    float xlapf = d_e[(x) + ((y + 1) * (X))] - temp;
    float xlapb = temp - d_e[(x) + ((y - 1) * (X))];

    float lap = xlapr - xlapl + xlapf - xlapb;
   
    temp = (temp + (du * DT) + (lap * DT * gam / (DX * DX)));

    d_v[h_e_i] = rv + dv * DT;
    h_e_i = Gx + ((Gy) * (GX));
    d_r[h_e_i] = temp;
}
/*
função chamada pelo host que controla as cópias e a ordem do calculo dos stencils bem como a carga para cada thread
, MODELSIZE_X, MODELSIZE_Y, BLOCK_TIMES
 int X, int Y, int times
*/
__global__ void _2Dstencil_global(float *d_e, float *d_r, float *d_v)
{

    int x, y; //,h_e_i,h_r_i,Xs,Ys,Dx,Dy;
    x = threadIdx.x + (blockIdx.x * BLOCKDIM_X);
    y = threadIdx.y + (blockIdx.y * BLOCKDIM_Y);
    extern __shared__ float sharedOrig[];

    int blockThreadIndex = threadIdx.x + threadIdx.y * BLOCKDIM_X;

    float * shared = sharedOrig;
    float * sharedRes = shared + SHARED_TAM;
    float * sharedV = sharedRes + SHARED_TAM; 
    /*
    Copia o Tile de memória compartilhada necessária para a configuração de tempo desejada
    Stride é utilizado pois a quantidade de elementos a serem copiados é sempre maior que a quantidade de threads
    As bordas
    */
    for (int stride = blockThreadIndex; stride < SHARED_TAM; stride += (BLOCKDIM_X * BLOCKDIM_Y))
    {
        int sharedIdxX = stride % SHARED_DX;
        int sharedIdxY = int(stride / SHARED_DX);
        int globalIdxX = (blockIdx.x * BLOCKDIM_X) + sharedIdxX - BLOCK_TIMES;
        int globalIdxY = (blockIdx.y * BLOCKDIM_Y) + sharedIdxY - BLOCK_TIMES;
        int globalIdx = globalIdxX + (-1*globalIdxX)*(globalIdxX < 0) - (globalIdxX-MODELSIZE_X+1)*(globalIdxX >= MODELSIZE_X)  +  (globalIdxY + (-1*globalIdxY)*(globalIdxY < 0) - (globalIdxY-MODELSIZE_Y+1)*(globalIdxY >= MODELSIZE_Y)) * MODELSIZE_X;
       
        shared[stride] = d_e[globalIdx];
        sharedV[stride] = d_v[globalIdx];
    }

    __syncthreads();

    /*
    Envia pra ser calculado todos os elementos além do ultimo instante de tempo
    */
    for (int t = 1; t < BLOCK_TIMES; t++)
    {
        int tDx = BLOCKDIM_X + ((BLOCK_TIMES - t) * 2);
        int tDy = BLOCKDIM_Y + ((BLOCK_TIMES - t) * 2);
        int tk2 = (t);
        int tSharedTam = tDx * tDy;
        for (int stride = blockThreadIndex; stride < tSharedTam; stride += (BLOCKDIM_X * BLOCKDIM_Y))
        {
            int tempX = (stride % tDx) + tk2;
            int tempY = (int(stride / tDx)) + tk2;
            _2Dstencil_(shared, sharedRes, sharedV, SHARED_DX, tempX, tempY, SHARED_DX, tempX, tempY);
        }

        float * temp = shared;
        shared = sharedRes;
        sharedRes = temp;
        __syncthreads();
    }
    /*
    Envia pra ser calculado todos os elementos do ultimo instante de tempo
   */
    _2Dstencil_(shared, d_r, sharedV, SHARED_DX, ((x%(BLOCKDIM_X))+BLOCK_TIMES), ((y%(BLOCKDIM_Y))+BLOCK_TIMES), MODELSIZE_X, x, y);
    
     int globalIdx = x + y * MODELSIZE_X;
     int sharedIdx = ((x%(BLOCKDIM_X))+BLOCK_TIMES) + ((y%(BLOCKDIM_Y))+BLOCK_TIMES)*SHARED_DX;
     d_v[globalIdx] = sharedV[sharedIdx];
}

int main(int argc, char *argv[])
{
    /*
    Declarações e valores padroes
    */
    //float *h_e, *h_r, *h_v;
    bool resultado = false;
    float *h_e, *h_v;
    float *d_e, *d_r, *d_v;
    int sharedSize;
    int globalTimes = 1;

    /*
    Obtenção dos parâmetros de entrada
    */
    if (argc > 1)
    {
        globalTimes = atoi(argv[1]);
    }

    if(argc > 2)
    {
        resultado = atoi(argv[2])==1;
    }


    /*
    Allocações de memória e configuração dos blocos e grid
    */
    dim3 grid_dim(GRIDDIM_X,GRIDDIM_Y,GRIDDIM_Z);
    dim3 block_dim(BLOCKDIM_X,BLOCKDIM_Y,BLOCKDIM_Z);
    sharedSize = SHARED_TAM * sizeof(float) * 3;
    h_e = (float *)malloc(MODELSIZE2D*sizeof(float));
    h_v = (float *)malloc(MODELSIZE2D*sizeof(float));
    hipMalloc(&d_e, MODELSIZE2D*sizeof(float));
    hipMalloc(&d_r, MODELSIZE2D*sizeof(float));
    hipMalloc(&d_v, MODELSIZE2D*sizeof(float));

//Copia os dados do campo e envia para a GPU e inicializa o dominio de entrada

        


    FILE *arq;
    arq = fopen("entrada.txt", "rt");
    for (int i = 0; i < MODELSIZE_X; i++)
        for (int j = 0; j < MODELSIZE_Y; j++)
        {
            h_v[i + j * MODELSIZE_X] =0.5f;
            int temp;
            fscanf(arq," %d",&temp);
            h_e[i + j * MODELSIZE_X] = temp;
        }

    fclose(arq);
    hipMemcpy(d_v, h_v, MODELSIZE2D*sizeof(float), hipMemcpyHostToDevice);
   
    /* 
    Copy vectors from host memory to device memory
    Copia os dados da entrada de volta a GPU
        */
    hipMemcpy(d_e, h_e, MODELSIZE2D*sizeof(float), hipMemcpyHostToDevice);
    
    /*
    Começa o Timer
    */
    hipEvent_t start, stop;
    hipEventCreate(&start);
    hipEventCreate(&stop);
    hipEventRecord(start, 0);

    /******************
    *** Kernel Call ***
    *******************/
    //_3Dstencil_global<<<blks,th_p_blk>>>(d_e,d_r,X,Y,Z);
    /*
    Executa o kernel
    */
    for(int i=0; i<globalTimes/BLOCK_TIMES; i ++)
    {
        _2Dstencil_global<<<grid_dim, block_dim, sharedSize>>>(d_e, d_r, d_v);
        float * temp = d_e;
        d_e = d_r;
        d_r = temp;
    }
    

    /*
    Identifica possíveis erros
    */
    hipError_t err = hipSuccess;
    err = hipGetLastError();
    if (err != hipSuccess)
    {
        printf ("-1");
        hipFree(d_e);
        hipFree(d_r);
        hipFree(d_v);
        std::free(h_e);
        std::free(h_v);
        fprintf(stderr, "Failed to launch _2Dstencil_global kernel (error code %s)!\n", hipGetErrorString(err));
        return 0;
    }
    /******************
    *** Kernel Call ***
    *******************/

    hipDeviceSynchronize();
    /*
    Para o Timer
    */
    hipEventRecord(stop, 0);
    hipEventSynchronize(stop);
    float elapsedTime;
    hipEventElapsedTime(&elapsedTime, start, stop);
    hipEventDestroy(start);
    hipEventDestroy(stop);

    //printf("X %d || Y %d \nBX %d || BY %d \n",X,Y,BX,BY);
    //printf ("[%d,%.5f], sharedSize %d  CPUMem = %d bytes  GPUMen = %d bytes\n", times,elapsedTime,sharedSize,size*2,size*3);
    //printf("GPU elapsed time: %f s (%f milliseconds)\n", (elapsedTime/1000.0), elapsedTime);
    printf ("%.5f",elapsedTime);
    /*
    Copia o resultado de volta para o CPU
    */
    hipMemcpy(h_e, d_e, MODELSIZE2D*sizeof(float), hipMemcpyDeviceToHost);
    /*
    Copia o resultado para a imagem de visualização
    A estrutura de 
    */
    if(resultado)
    {
        arq = fopen("resultado.txt", "wt");
        for (int i = 0; i < MODELSIZE_X; i++)
        {
            for (int j = 0; j < MODELSIZE_Y; j++)
            {
                fprintf(arq," %6.4f",h_e[i+j*MODELSIZE_X]);
            }
            fprintf(arq,"\n");
        }
        fclose(arq);
    }
    
        

    hipFree(d_e);
    hipFree(d_r);
    hipFree(d_v);
    std::free(h_e);
    std::free(h_v);

    return 0;
} /* main */

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

                                        # End of file scope inline assembly
	.globl	_Z32__device_stub___2Dstencil_globalPfS_S_ # -- Begin function _Z32__device_stub___2Dstencil_globalPfS_S_
	.type	_Z32__device_stub___2Dstencil_globalPfS_S_,@function
_Z32__device_stub___2Dstencil_globalPfS_S_: # @_Z32__device_stub___2Dstencil_globalPfS_S_
	.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	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%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	$_Z17_2Dstencil_globalPfS_S_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	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	_Z32__device_stub___2Dstencil_globalPfS_S_, .Lfunc_end0-_Z32__device_stub___2Dstencil_globalPfS_S_
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.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	$88, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$2, %edi
	jl	.LBB1_1
# %bb.2:
	movq	%rsi, %rbx
	movl	%edi, %r14d
	movq	8(%rsi), %rdi
	callq	atoi
	cmpl	$2, %r14d
	movl	%eax, 36(%rsp)                  # 4-byte Spill
	je	.LBB1_3
# %bb.4:
	movq	16(%rbx), %rdi
	callq	atoi
	cmpl	$1, %eax
	sete	%al
	movl	%eax, 20(%rsp)                  # 4-byte Spill
	jmp	.LBB1_5
.LBB1_1:
	movl	$1, 36(%rsp)                    # 4-byte Folded Spill
.LBB1_3:
	movl	$0, 20(%rsp)                    # 4-byte Folded Spill
.LBB1_5:                                # %.thread
	movl	$36864, %edi                    # imm = 0x9000
	callq	malloc
	movq	%rax, %r14
	movl	$36864, %edi                    # imm = 0x9000
	callq	malloc
	movq	%rax, %rbx
	leaq	8(%rsp), %rdi
	movl	$36864, %esi                    # imm = 0x9000
	callq	hipMalloc
	leaq	24(%rsp), %rdi
	movl	$36864, %esi                    # imm = 0x9000
	callq	hipMalloc
	leaq	56(%rsp), %rdi
	movl	$36864, %esi                    # imm = 0x9000
	callq	hipMalloc
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %r15
	leaq	48(%rsp), %r12
	movq	%rbx, 72(%rsp)                  # 8-byte Spill
	movq	%r14, 64(%rsp)                  # 8-byte Spill
	xorl	%r13d, %r13d
.LBB1_6:                                # %.preheader61
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_7 Depth 2
	xorl	%ebp, %ebp
.LBB1_7:                                #   Parent Loop BB1_6 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	$1056964608, (%rbx,%rbp)        # imm = 0x3F000000
	movl	$.L.str.2, %esi
	movq	%r15, %rdi
	movq	%r12, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	xorps	%xmm0, %xmm0
	cvtsi2ssl	48(%rsp), %xmm0
	movss	%xmm0, (%r14,%rbp)
	addq	$384, %rbp                      # imm = 0x180
	cmpq	$36864, %rbp                    # imm = 0x9000
	jne	.LBB1_7
# %bb.8:                                #   in Loop: Header=BB1_6 Depth=1
	incq	%r13
	addq	$4, %r14
	addq	$4, %rbx
	cmpq	$96, %r13
	jne	.LBB1_6
# %bb.9:
	movq	%r15, %rdi
	callq	fclose
	movq	56(%rsp), %rdi
	movl	$36864, %edx                    # imm = 0x9000
	movq	72(%rsp), %r13                  # 8-byte Reload
	movq	%r13, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rdi
	movl	$36864, %edx                    # imm = 0x9000
	movq	64(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	48(%rsp), %r15
	movq	%r15, %rdi
	callq	hipEventCreate
	leaq	40(%rsp), %rdi
	callq	hipEventCreate
	movq	(%r15), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	36(%rsp), %ebp                  # 4-byte Reload
	testl	%ebp, %ebp
	jle	.LBB1_14
# %bb.10:
	movabsq	$12884901891, %r15              # imm = 0x300000003
	movabsq	$137438953504, %r12             # imm = 0x2000000020
.LBB1_11:                               # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$13872, %r8d                    # imm = 0x3630
	movq	%r15, %rdi
	movl	$1, %esi
	movq	%r12, %rdx
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_13
# %bb.12:                               #   in Loop: Header=BB1_11 Depth=1
	movq	8(%rsp), %rdi
	movq	24(%rsp), %rsi
	movq	56(%rsp), %rdx
	callq	_Z32__device_stub___2Dstencil_globalPfS_S_
.LBB1_13:                               #   in Loop: Header=BB1_11 Depth=1
	movq	8(%rsp), %rax
	movq	24(%rsp), %rcx
	movq	%rcx, 8(%rsp)
	movq	%rax, 24(%rsp)
	decl	%ebp
	jne	.LBB1_11
.LBB1_14:                               # %._crit_edge
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB1_15
# %bb.16:
	callq	hipDeviceSynchronize
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	40(%rsp), %rdi
	callq	hipEventSynchronize
	movq	48(%rsp), %rsi
	movq	40(%rsp), %rdx
	leaq	84(%rsp), %r15
	movq	%r15, %rdi
	callq	hipEventElapsedTime
	movq	48(%rsp), %rdi
	callq	hipEventDestroy
	movq	40(%rsp), %rdi
	callq	hipEventDestroy
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r15), %xmm0
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	movq	8(%rsp), %rsi
	movl	$36864, %edx                    # imm = 0x9000
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	cmpb	$0, 20(%rsp)                    # 1-byte Folded Reload
	je	.LBB1_22
# %bb.17:
	movl	$.L.str.6, %edi
	movl	$.L.str.7, %esi
	callq	fopen
	movq	%rax, %r15
	xorl	%r12d, %r12d
.LBB1_18:                               # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_19 Depth 2
	movq	$-36864, %r14                   # imm = 0xFFFF7000
.LBB1_19:                               #   Parent Loop BB1_18 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtss2sd	36864(%rbx,%r14), %xmm0
	movl	$.L.str.8, %esi
	movq	%r15, %rdi
	movb	$1, %al
	callq	fprintf
	addq	$384, %r14                      # imm = 0x180
	jne	.LBB1_19
# %bb.20:                               #   in Loop: Header=BB1_18 Depth=1
	movl	$10, %edi
	movq	%r15, %rsi
	callq	fputc@PLT
	incq	%r12
	addq	$4, %rbx
	cmpq	$96, %r12
	jne	.LBB1_18
# %bb.21:
	movq	%r15, %rdi
	callq	fclose
	movq	64(%rsp), %rbx                  # 8-byte Reload
.LBB1_22:
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	56(%rsp), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	free
	movq	%r13, %rdi
	callq	free
.LBB1_23:
	xorl	%eax, %eax
	addq	$88, %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
.LBB1_15:
	.cfi_def_cfa_offset 144
	movl	%eax, %ebp
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	56(%rsp), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	free
	movq	%r13, %rdi
	callq	free
	movq	stderr(%rip), %rbx
	movl	%ebp, %edi
	callq	hipGetErrorString
	movl	$.L.str.4, %esi
	movq	%rbx, %rdi
	movq	%rax, %rdx
	xorl	%eax, %eax
	callq	fprintf
	jmp	.LBB1_23
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.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:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z17_2Dstencil_globalPfS_S_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__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	.LBB3_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
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z17_2Dstencil_globalPfS_S_,@object # @_Z17_2Dstencil_globalPfS_S_
	.section	.rodata,"a",@progbits
	.globl	_Z17_2Dstencil_globalPfS_S_
	.p2align	3, 0x0
_Z17_2Dstencil_globalPfS_S_:
	.quad	_Z32__device_stub___2Dstencil_globalPfS_S_
	.size	_Z17_2Dstencil_globalPfS_S_, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"entrada.txt"
	.size	.L.str, 12

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"rt"
	.size	.L.str.1, 3

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	" %d"
	.size	.L.str.2, 4

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"-1"
	.size	.L.str.3, 3

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Failed to launch _2Dstencil_global kernel (error code %s)!\n"
	.size	.L.str.4, 60

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"%.5f"
	.size	.L.str.5, 5

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"resultado.txt"
	.size	.L.str.6, 14

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"wt"
	.size	.L.str.7, 3

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	" %6.4f"
	.size	.L.str.8, 7

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z17_2Dstencil_globalPfS_S_"
	.size	.L__unnamed_1, 28

	.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

	.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 _Z32__device_stub___2Dstencil_globalPfS_S_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z17_2Dstencil_globalPfS_S_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z17_2Dstencil_globalPfS_S_ ; -- Begin function _Z17_2Dstencil_globalPfS_S_
	.globl	_Z17_2Dstencil_globalPfS_S_
	.p2align	8
	.type	_Z17_2Dstencil_globalPfS_S_,@function
_Z17_2Dstencil_globalPfS_S_:            ; @_Z17_2Dstencil_globalPfS_S_
; %bb.0:
	s_clause 0x1
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[2:3], s[0:1], 0x10
	v_and_b32_e32 v1, 0x3ff, v0
	v_bfe_u32 v2, v0, 10, 10
	s_mov_b32 s0, exec_lo
                                        ; implicit-def: $sgpr1
                                        ; implicit-def: $sgpr8
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshl_add_u32 v0, v2, 5, v1
	v_cmpx_lt_u32_e32 0x483, v0
	s_xor_b32 s0, exec_lo, s0
; %bb.1:                                ; %.._crit_edge_crit_edge
	s_lshl_b32 s1, s15, 5
	s_lshl_b32 s8, s14, 5
                                        ; implicit-def: $vgpr0
; %bb.2:                                ; %Flow93
	s_or_saveexec_b32 s0, s0
	v_dual_mov_b32 v3, s1 :: v_dual_mov_b32 v4, s8
	s_xor_b32 exec_lo, exec_lo, s0
	s_cbranch_execz .LBB0_6
; %bb.3:                                ; %.lr.ph
	v_lshlrev_b32_e32 v3, 7, v2
	v_lshlrev_b32_e32 v4, 2, v1
	s_lshl_b32 s1, s14, 5
	s_lshl_b32 s8, s15, 5
	s_mov_b32 s9, 0
	s_delay_alu instid0(VALU_DEP_1)
	v_add3_u32 v3, v3, v4, 0
.LBB0_4:                                ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_and_b32_e32 v4, 0xffff, v0
	v_mul_u32_u24_e32 v4, 0xf0f1, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshrrev_b32_e32 v4, 21, v4
	v_mul_lo_u16 v5, v4, 34
	v_add_nc_u32_e32 v4, s8, v4
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_sub_nc_u16 v5, v0, v5
	v_add_nc_u32_e32 v6, -1, v4
	v_add_nc_u32_e32 v4, 0xffffffa0, v4
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_cmp_lt_i32_e32 vcc_lo, 0x5f, v6
	v_and_b32_e32 v5, 0xffff, v5
	v_max_i32_e32 v7, 0, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_cndmask_b32 v4, 0, v4 :: v_dual_add_nc_u32 v5, s1, v5
	v_sub_nc_u32_e32 v4, v7, v4
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_add_nc_u32_e32 v6, -1, v5
	v_sub_nc_u32_e32 v5, 0x60, v5
	v_mul_lo_u32 v4, 0x60, v4
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_4)
	v_cmp_lt_i32_e32 vcc_lo, 0x5f, v6
	v_max_i32_e32 v7, 0, v6
	v_cndmask_b32_e32 v5, 0, v5, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add3_u32 v4, v5, v7, v4
	v_ashrrev_i32_e32 v5, 31, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[4:5], 2, v[4:5]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v6, vcc_lo, s4, v4
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v7, vcc_lo, s5, v5, vcc_lo
	v_add_co_u32 v4, vcc_lo, s2, v4
	v_add_co_ci_u32_e32 v5, vcc_lo, s3, v5, vcc_lo
	global_load_b32 v6, v[6:7], off
	global_load_b32 v4, v[4:5], off
	v_add_nc_u32_e32 v5, 0x400, v0
	v_cmp_lt_u32_e32 vcc_lo, 0x83, v0
	s_waitcnt vmcnt(1)
	ds_store_b32 v3, v6
	s_waitcnt vmcnt(0)
	ds_store_b32 v3, v4 offset:9248
	v_dual_mov_b32 v0, v5 :: v_dual_add_nc_u32 v3, 0x1000, v3
	s_or_b32 s9, vcc_lo, s9
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s9
	s_cbranch_execnz .LBB0_4
; %bb.5:                                ; %Flow92
	s_or_b32 exec_lo, exec_lo, s9
	v_dual_mov_b32 v3, s8 :: v_dual_mov_b32 v4, s1
.LBB0_6:                                ; %Flow94
	s_or_b32 exec_lo, exec_lo, s0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v0, v4, v1
	v_add_nc_u32_e32 v3, v3, v2
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_ashrrev_i32_e32 v1, 31, v0
	v_ashrrev_i32_e32 v2, 31, v3
                                        ; implicit-def: $vgpr8
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_lshrrev_b32_e32 v1, 27, v1
	v_lshrrev_b32_e32 v2, 27, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v1, v0, v1
	v_add_nc_u32_e32 v2, v3, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_and_b32_e32 v1, 0xffffffe0, v1
	v_and_b32_e32 v2, 0xffffe0, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_sub_nc_u32_e32 v5, v0, v1
	v_sub_nc_u32_e32 v7, v3, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_i32_i24 v1, v7, 34, v5
	v_lshl_add_u32 v1, v1, 2, 4
	s_delay_alu instid0(VALU_DEP_1)
	v_add_nc_u32_e32 v2, 0, v1
	ds_load_b32 v4, v2 offset:136
	v_add3_u32 v2, 0, 0x2420, v1
	ds_load_b32 v1, v2 offset:136
	s_waitcnt lgkmcnt(1)
	v_add_f32_e32 v6, 0xc0400000, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ngt_f32_e64 s0, 0x3f200000, |v6|
	s_and_saveexec_b32 s1, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	s_xor_b32 s0, exec_lo, s1
	s_cbranch_execz .LBB0_8
; %bb.7:
	v_add_f32_e64 v8, |v6|, |v6|
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v9, 0x3fb8aa3b, v8
	v_cmp_ngt_f32_e32 vcc_lo, 0xc2ce8ed0, v8
	v_rndne_f32_e32 v10, v9
	v_fma_f32 v11, 0x3fb8aa3b, v8, -v9
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_sub_f32_e32 v9, v9, v10
	v_fmamk_f32 v11, v8, 0x32a5705f, v11
	v_cvt_i32_f32_e32 v10, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f32_e32 v9, v9, v11
	v_exp_f32_e32 v9, v9
	s_waitcnt_depctr 0xfff
	v_ldexp_f32 v9, v9, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cndmask_b32_e32 v9, 0, v9, vcc_lo
	v_cmp_nlt_f32_e32 vcc_lo, 0x42b17218, v8
	v_cndmask_b32_e32 v8, 0x7f800000, v9, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f32_e32 v8, 1.0, v8
	v_rcp_f32_e32 v8, v8
	s_waitcnt_depctr 0xfff
	v_fma_f32 v8, v8, -2.0, 1.0
.LBB0_8:                                ; %Flow
	s_or_saveexec_b32 s0, s0
	v_mul_i32_i24_e32 v7, 34, v7
	s_xor_b32 exec_lo, exec_lo, s0
; %bb.9:
	v_mul_f32_e32 v8, v6, v6
	s_mov_b32 s1, 0xbbbac73d
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_fmaak_f32 v9, s1, v8, 0x3ca908c9
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmaak_f32 v9, v8, v9, 0xbd5c1c4e
	v_fmaak_f32 v9, v8, v9, 0x3e088382
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmaak_f32 v9, v8, v9, 0xbeaaaa99
	v_mul_f32_e64 v9, |v6|, v9
	s_delay_alu instid0(VALU_DEP_1)
	v_fma_f32 v8, v8, v9, |v6|
; %bb.10:                               ; %_Z11_2Dstencil_PfS_S_iiiiii.exit
	s_or_b32 exec_lo, exec_lo, s0
	s_delay_alu instid0(VALU_DEP_1)
	v_bfi_b32 v6, 0x7fffffff, v8, v6
	v_cmp_lt_f32_e32 vcc_lo, 1.0, v4
	s_waitcnt lgkmcnt(0)
	v_dual_sub_f32 v10, 0x400dd8fe, v1 :: v_dual_lshlrev_b32 v7, 2, v7
	v_fma_f32 v11, -v1, v1, 0x3fc54fdf
	v_lshlrev_b32_e32 v5, 2, v5
	v_cndmask_b32_e64 v8, 0, 1.0, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_sub_f32 v6, 1.0, v6 :: v_dual_sub_f32 v9, 1.0, v8
	v_dual_mul_f32 v6, v4, v6 :: v_dual_mul_f32 v9, v1, v9
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v6, v4, v6
	v_fma_f32 v9, v10, v8, -v9
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v6, 0.5, v6
	v_div_scale_f32 v17, vcc_lo, v9, 0x437a0000, v9
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_4)
	v_fma_f32 v10, v11, v6, -v4
	v_add3_u32 v6, 0, v5, v7
	v_add3_u32 v7, 0, v7, v5
	v_div_scale_f32 v11, null, 0x437a0000, 0x437a0000, v9
	v_div_scale_f32 v12, null, 0x40a00000, 0x40a00000, v10
	ds_load_2addr_b32 v[5:6], v6 offset0:34 offset1:36
	ds_load_2addr_b32 v[7:8], v7 offset0:1 offset1:69
	v_rcp_f32_e32 v13, v11
	v_rcp_f32_e32 v14, v12
	s_waitcnt_depctr 0xfff
	v_fma_f32 v15, -v11, v13, 1.0
	v_fma_f32 v16, -v12, v14, 1.0
	s_delay_alu instid0(VALU_DEP_2)
	v_fmac_f32_e32 v13, v15, v13
	v_div_scale_f32 v15, s0, v10, 0x40a00000, v10
	s_waitcnt lgkmcnt(1)
	v_dual_sub_f32 v6, v6, v4 :: v_dual_sub_f32 v5, v4, v5
	v_fmac_f32_e32 v14, v16, v14
	s_waitcnt lgkmcnt(0)
	v_sub_f32_e32 v8, v8, v4
	v_dual_mul_f32 v16, v17, v13 :: v_dual_sub_f32 v7, v4, v7
	v_sub_f32_e32 v5, v6, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v19, -v11, v16, v17
	v_add_f32_e32 v8, v5, v8
	v_mul_f32_e32 v18, v15, v14
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v6, -v12, v18, v15
	v_fmac_f32_e32 v18, v6, v14
	v_mad_u64_u32 v[5:6], null, 0x60, v3, v[0:1]
	v_sub_f32_e32 v3, v8, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_mul_f32 v3, 0x3d4ccccd, v3 :: v_dual_fmac_f32 v16, v19, v13
	v_mul_f32_e32 v3, 0x3a83126f, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v0, -v11, v16, v17
	v_div_fmas_f32 v0, v0, v13, v16
	s_mov_b32 vcc_lo, s0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fixup_f32 v0, v0, 0x437a0000, v9
	v_fmac_f32_e32 v1, 0x3d4ccccd, v0
	v_fma_f32 v6, -v12, v18, v15
	ds_store_b32 v2, v1 offset:136
	v_div_fmas_f32 v7, v6, v14, v18
	v_ashrrev_i32_e32 v6, 31, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_div_fixup_f32 v7, v7, 0x40a00000, v10
	v_lshlrev_b64 v[5:6], 2, v[5:6]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fmac_f32_e32 v4, 0x3d4ccccd, v7
	v_add_co_u32 v7, vcc_lo, s6, v5
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_ci_u32_e32 v8, vcc_lo, s7, v6, vcc_lo
	v_fmamk_f32 v0, v3, 0x42800000, v4
	v_add_co_u32 v3, vcc_lo, s2, v5
	v_add_co_ci_u32_e32 v4, vcc_lo, s3, v6, vcc_lo
	global_store_b32 v[7:8], v0, off
	global_store_b32 v[3:4], v1, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z17_2Dstencil_globalPfS_S_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 24
		.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 20
		.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	_Z17_2Dstencil_globalPfS_S_, .Lfunc_end0-_Z17_2Dstencil_globalPfS_S_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1376
; NumSgprs: 18
; NumVgprs: 20
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 20
; 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
	.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_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 24
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z17_2Dstencil_globalPfS_S_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z17_2Dstencil_globalPfS_S_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     20
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
