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

#include "convolution.cuh"

// #define USE_SHARED_MEM // Question 2 et 3
// #define USE_PITCH  // Question 3

void init_A(
	/* IN */
	noyau_t *noyau,
	int Ni,
	int Nj,
	/* OUT */
	float **A
	) {

    const float pasx = 1/float(Ni);
    const float pasy = 1/float(Nj);

    for(int i = noyau->R ; i < Ni+noyau->R ; i++) {
	    const float x = 2*M_PI*(0.5*pasx + (i-noyau->R)*pasx);

	    for(int j = noyau->R ; j < Nj+noyau->R ; j++) {
	        const float y = 2*M_PI*(0.5*pasy + (j-noyau->R)*pasy);
	        A[i][j] = sin(x)*cos(y);
	    }
    }
}

void periodic_bc(
	/* IN */
	noyau_t *noyau,
	int Ni,
	int Nj,
	/* INOUT */
	float **A
	) {

    /* WEST BOUNDARY UPDATE */
    for(int i = noyau->R ; i < Ni+noyau->R ; i++) {
	    for(int j = 0 ; j < noyau->R ; j++) {
	        A[i][j] = A[i][j+Nj];
	    }
    }

    /* EAST BOUNDARY UPDATE */
    for(int i = noyau->R ; i < Ni+noyau->R ; i++) {
	    for(int j = Nj+noyau->R ; j < Nj+2*noyau->R ; j++) {
	        A[i][j] = A[i][j-Nj];
	    }
    }

    /* SOUTH BOUNDARY UPDATE */
    for(int i = 0 ; i < noyau->R ; i++) {
	    for(int j = 0 ; j < Nj+2*noyau->R ; j++) {
	        A[i][j] = A[i+Ni][j];
	    }
    }

    /* NORTH BOUNDARY UPDATE */
    for(int i = Ni+noyau->R ; i < Ni+2*noyau->R ; i++) {
	    for(int j = 0 ; j < Nj+2*noyau->R ; j++) {
	        A[i][j] = A[i-Ni][j];
	    }
    }
}

void output(
	/* IN */
	noyau_t *noyau,
	int Ni,
	int Nj,
	float **A,
	const char *fichier
	) {
    
    FILE *fd = fopen(fichier, "w");

    const float pasx = 1/float(Ni);
    const float pasy = 1/float(Nj);

    for(int i = noyau->R ; i < Ni+noyau->R ; i++) {
	    const float x = 2*M_PI*(0.5*pasx + (i-noyau->R)*pasx);

	    for(int j = noyau->R ; j < Nj+noyau->R ; j++) {
	        const float y = 2*M_PI*(0.5*pasy + (j-noyau->R)*pasy);

	        fprintf(fd, "%.4e %.4e %.4e\n", x, y, A[i][j]);
	    }
	    fprintf(fd, "\n");
    }

    fclose(fd);
    printf("Ecriture fichier %s terminee.\n", fichier);
}

void check_noerr(int linenr) {
    cudaError_t err = cudaGetLastError();
    if (err != cudaSuccess) {
	    printf("Erreur ligne %d : %s \n", linenr, cudaGetErrorString(err));
	    abort();
    }
}

int main(int argc, char **argv) {
    int Ni, Nj;

    if (argc !=3) {
        printf("Usage: ./convolution Ni Nj\n");
        abort();
    }
    
    Ni = atoi(argv[1]);
    Nj = atoi(argv[2]);

    float *buf_A, *buf_B;
    float **A, **B;

    float *d_buf_A, *d_buf_B;
    size_t pitchA, pitchB;

    dim3 bThreads(DIM_BX, DIM_BY);
    dim3 gBlocs;

    gBlocs.x = (Ni + bThreads.x-1) / bThreads.x;
    gBlocs.y = (Nj + bThreads.y-1) / bThreads.y;

    printf("nthreads : (%d, %d)\n",  bThreads.x, bThreads.y);
    printf("nblocks  : (%d, %d)\n",  gBlocs.x, gBlocs.y);

    int Ni_2R, Nj_2R;
    noyau_t noyau;
    noyau_t *d_noyau; // pointeur noyau sur device

    d_noyau = init_noyau(&noyau, R_NOYAU);

    Ni_2R = Ni + 2*noyau.R;
    Nj_2R = Nj + 2*noyau.R;

    buf_A = (float*)malloc(Ni_2R*Nj_2R*sizeof(float));
    buf_B = (float*)malloc(Ni_2R*Nj_2R*sizeof(float));

    A = (float**)malloc(Ni_2R*sizeof(float*));
    B = (float**)malloc(Ni_2R*sizeof(float*));

    for(int i = 0 ; i < Ni_2R ; i++) {
	    A[i] = buf_A + i*Nj_2R;
	    B[i] = buf_B + i*Nj_2R;
    }

#ifdef USE_PITCH
    // A COMPLETER    
#else
    cudaMalloc(&d_buf_A, Ni_2R*Nj_2R*sizeof(float));
    cudaMalloc(&d_buf_B, Ni_2R*Nj_2R*sizeof(float));

    pitchA = Nj_2R*sizeof(float);
    pitchB = Nj_2R*sizeof(float);
#endif

    init_A(&noyau, Ni, Nj, A);
    output(&noyau, Ni, Nj, A, "A000");

    for(int iter = 1 ; iter <= 5 ; iter++) {
	    periodic_bc(&noyau, Ni, Nj, A);

	// Transfert CPU => GPU
#ifdef USE_PITCH
	// A COMPLETER
#else
	    cudaMemcpy(
		    d_buf_A, 
		    buf_A, 
		    Ni_2R*Nj_2R*sizeof(float), 
		    cudaMemcpyHostToDevice);
#endif
	    check_noerr(__LINE__);

	// Application operateur convolution
#ifdef USE_SHARED_MEM
	    convol_sh<<<gBlocs, bThreads>>>(
		    d_noyau, 
		    d_buf_A, pitchA,
		    Ni, Nj, 
		    d_buf_B, pitchB);
#else
	    convol_gl<<<gBlocs, bThreads>>>(
		    d_noyau, 
		    d_buf_A, pitchA,
		    Ni, Nj, 
		    d_buf_B, pitchB);
#endif
	    check_noerr(__LINE__);

	// Transfert GPU => CPU
#ifdef USE_PITCH
	// A COMPLETER
#else
	    cudaMemcpy(
		    buf_B,
		    d_buf_B, 
		    Ni_2R*Nj_2R*sizeof(float), 
		    cudaMemcpyDeviceToHost);
#endif
	    check_noerr(__LINE__);

	    float *buf_C;
	    buf_C = buf_A;
	    buf_A = buf_B;
	    buf_B = buf_C;

	    float **C;
	    C = A;
	    A = B;
	    B = C;
    }

    output(&noyau, Ni, Nj, A, "A001");

    free(buf_A);
    free(buf_B);
    free(A);
    free(B);

    free_noyau(&noyau, d_noyau);

    return 0;
}


	.file	"tmpxft_0023989b_00000000-6_main.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z6init_AP7noyau_tiiPPf
	.type	_Z6init_AP7noyau_tiiPPf, @function
_Z6init_AP7noyau_tiiPPf:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	cvtsi2ssl	%esi, %xmm1
	movq	%rcx, %r15
	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	$40, %rsp
	.cfi_def_cfa_offset 96
	movss	.LC0(%rip), %xmm0
	movslq	(%rdi), %r13
	movaps	%xmm0, %xmm2
	leal	(%rsi,%r13), %eax
	movq	%r13, %r14
	divss	%xmm1, %xmm2
	cvtsi2ssl	%edx, %xmm1
	movl	%eax, 20(%rsp)
	leal	(%rdx,%r13), %eax
	movl	%eax, 24(%rsp)
	leaq	0(,%r13,4), %r12
	divss	%xmm1, %xmm0
	movss	%xmm0, 8(%rsp)
.L3:
	cmpl	%r13d, 20(%rsp)
	jle	.L9
	movl	%r13d, %eax
	cvtss2sd	%xmm2, %xmm1
	movq	%r12, %rbp
	xorl	%ebx, %ebx
	mulsd	.LC1(%rip), %xmm1
	subl	%r14d, %eax
	cvtsi2ssl	%eax, %xmm0
	mulss	%xmm2, %xmm0
	cvtss2sd	%xmm0, %xmm0
	addsd	%xmm1, %xmm0
	mulsd	.LC2(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm3
	movss	%xmm3, 12(%rsp)
.L4:
	leal	(%rbx,%r14), %ecx
	cmpl	%ecx, 24(%rsp)
	jle	.L10
	movss	12(%rsp), %xmm0
	movss	%xmm2, 28(%rsp)
	call	sinf@PLT
	cvtss2sd	8(%rsp), %xmm1
	mulsd	.LC1(%rip), %xmm1
	movss	%xmm0, 16(%rsp)
	cvtsi2ssl	%ebx, %xmm0
	mulss	8(%rsp), %xmm0
	incl	%ebx
	cvtss2sd	%xmm0, %xmm0
	addsd	%xmm1, %xmm0
	mulsd	.LC2(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	call	cosf@PLT
	mulss	16(%rsp), %xmm0
	movq	(%r15,%r13,8), %rcx
	movss	28(%rsp), %xmm2
	movss	%xmm0, (%rcx,%rbp)
	addq	$4, %rbp
	jmp	.L4
.L10:
	incq	%r13
	jmp	.L3
.L9:
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z6init_AP7noyau_tiiPPf, .-_Z6init_AP7noyau_tiiPPf
	.globl	_Z11periodic_bcP7noyau_tiiPPf
	.type	_Z11periodic_bcP7noyau_tiiPPf, @function
_Z11periodic_bcP7noyau_tiiPPf:
.LFB2028:
	.cfi_startproc
	endbr64
	movq	%rdi, %rax
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movslq	%edx, %r9
	movl	%esi, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%r9, %rsi
	salq	$2, %r9
	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
	movslq	(%rax), %r8
	movq	%r8, %rdx
	movq	%r8, %r10
	leal	(%r8,%rdi), %eax
.L12:
	cmpl	%r10d, %eax
	jle	.L29
	movq	%r9, %rbx
	xorl	%r11d, %r11d
.L14:
	cmpl	%r11d, %edx
	jle	.L30
	movq	(%rcx,%r10,8), %rbp
	movss	0(%rbp,%rbx), %xmm0
	addq	$4, %rbx
	movss	%xmm0, 0(%rbp,%r11,4)
	incq	%r11
	jmp	.L14
.L30:
	incq	%r10
	jmp	.L12
.L29:
	leal	(%rdx,%rsi), %r13d
	leal	(%rdx,%rdx), %r10d
	movslq	%r13d, %rbx
	leal	(%r10,%rsi), %ebp
	salq	$2, %rbx
.L15:
	cmpl	%r8d, %eax
	jle	.L31
	movq	%rbx, %r11
	movl	%r13d, %r12d
.L17:
	cmpl	%r12d, %ebp
	jle	.L32
	movq	(%rcx,%r8,8), %r14
	movq	%r11, %r15
	incl	%r12d
	subq	%r9, %r15
	movss	(%r14,%r15), %xmm0
	movss	%xmm0, (%r14,%r11)
	addq	$4, %r11
	jmp	.L17
.L32:
	incq	%r8
	jmp	.L15
.L31:
	xorl	%r9d, %r9d
.L18:
	cmpl	%r9d, %edx
	jle	.L20
	leal	(%rdi,%r9), %r8d
	movslq	%r8d, %r8
	leaq	(%rcx,%r8,8), %r11
	xorl	%r8d, %r8d
.L21:
	cmpl	%r8d, %ebp
	jle	.L33
	movq	(%r11), %rbx
	movss	(%rbx,%r8,4), %xmm0
	movq	(%rcx,%r9,8), %rbx
	movss	%xmm0, (%rbx,%r8,4)
	incq	%r8
	jmp	.L21
.L33:
	incq	%r9
	jmp	.L18
.L20:
	addl	%eax, %edx
	addl	%esi, %r10d
	cltq
.L22:
	cmpl	%eax, %edx
	jle	.L11
	movl	%eax, %esi
	subl	%edi, %esi
	movslq	%esi, %rsi
	leaq	(%rcx,%rsi,8), %r8
	xorl	%esi, %esi
.L25:
	cmpl	%esi, %r10d
	jle	.L34
	movq	(%r8), %r9
	movss	(%r9,%rsi,4), %xmm0
	movq	(%rcx,%rax,8), %r9
	movss	%xmm0, (%r9,%rsi,4)
	incq	%rsi
	jmp	.L25
.L34:
	incq	%rax
	jmp	.L22
.L11:
	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
.LFE2028:
	.size	_Z11periodic_bcP7noyau_tiiPPf, .-_Z11periodic_bcP7noyau_tiiPPf
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC3:
	.string	"w"
.LC4:
	.string	"%.4e %.4e %.4e\n"
.LC5:
	.string	"\n"
.LC6:
	.string	"Ecriture fichier %s terminee.\n"
	.text
	.globl	_Z6outputP7noyau_tiiPPfPKc
	.type	_Z6outputP7noyau_tiiPPfPKc, @function
_Z6outputP7noyau_tiiPPfPKc:
.LFB2029:
	.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
	movq	%rcx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movl	%edx, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%esi, %r12d
	leaq	.LC3(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rdi, %rbp
	movq	%r8, %rdi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%r8, 24(%rsp)
	call	fopen@PLT
	cvtsi2ssl	%r12d, %xmm1
	movslq	0(%rbp), %rbx
	movss	.LC0(%rip), %xmm0
	movq	%rax, 8(%rsp)
	movaps	%xmm0, %xmm4
	divss	%xmm1, %xmm4
	cvtsi2ssl	%r13d, %xmm1
	divss	%xmm1, %xmm0
	movss	%xmm4, 20(%rsp)
	movss	%xmm0, 40(%rsp)
.L36:
	movl	0(%rbp), %r15d
	leal	(%r15,%r12), %eax
	cmpl	%ebx, %eax
	jle	.L41
	movl	%ebx, %eax
	cvtss2sd	20(%rsp), %xmm1
	movslq	%r15d, %r9
	mulsd	.LC1(%rip), %xmm1
	subl	%r15d, %eax
	salq	$2, %r9
	cvtsi2ssl	%eax, %xmm0
	mulss	20(%rsp), %xmm0
	cvtss2sd	%xmm0, %xmm0
	addsd	%xmm1, %xmm0
	mulsd	.LC2(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm3
.L37:
	movl	0(%rbp), %edx
	leal	(%rdx,%r13), %eax
	cmpl	%r15d, %eax
	jle	.L42
	movl	%r15d, %esi
	cvtss2sd	40(%rsp), %xmm0
	movq	(%r14,%rbx,8), %rax
	movq	8(%rsp), %rdi
	subl	%edx, %esi
	leaq	.LC4(%rip), %rdx
	movq	%r9, 32(%rsp)
	incl	%r15d
	cvtsi2ssl	%esi, %xmm1
	mulsd	.LC1(%rip), %xmm0
	movl	$2, %esi
	cvtss2sd	(%rax,%r9), %xmm2
	mulss	40(%rsp), %xmm1
	movb	$3, %al
	movss	%xmm3, 44(%rsp)
	cvtss2sd	%xmm1, %xmm1
	addsd	%xmm0, %xmm1
	mulsd	.LC2(%rip), %xmm1
	cvtss2sd	%xmm3, %xmm0
	cvtsd2ss	%xmm1, %xmm1
	cvtss2sd	%xmm1, %xmm1
	call	__fprintf_chk@PLT
	movq	32(%rsp), %r9
	movss	44(%rsp), %xmm3
	addq	$4, %r9
	jmp	.L37
.L42:
	movq	8(%rsp), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	incq	%rbx
	leaq	.LC5(%rip), %rdx
	call	__fprintf_chk@PLT
	jmp	.L36
.L41:
	movq	8(%rsp), %rdi
	call	fclose@PLT
	movq	24(%rsp), %rdx
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	movl	$2, %edi
	popq	%rbx
	.cfi_def_cfa_offset 48
	leaq	.LC6(%rip), %rsi
	popq	%rbp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	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
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z6outputP7noyau_tiiPPfPKc, .-_Z6outputP7noyau_tiiPPfPKc
	.section	.rodata.str1.1
.LC7:
	.string	"Erreur ligne %d : %s \n"
	.text
	.globl	_Z11check_noerri
	.type	_Z11check_noerri, @function
_Z11check_noerri:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	%edi, %ebx
	call	cudaGetLastError@PLT
	testl	%eax, %eax
	je	.L43
	movl	%eax, %edi
	call	cudaGetErrorString@PLT
	movl	%ebx, %edx
	movl	$2, %edi
	leaq	.LC7(%rip), %rsi
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	call	abort@PLT
.L43:
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z11check_noerri, .-_Z11check_noerri
	.section	.rodata.str1.1
.LC8:
	.string	"Usage: ./convolution Ni Nj\n"
.LC9:
	.string	"nthreads : (%d, %d)\n"
.LC10:
	.string	"nblocks  : (%d, %d)\n"
.LC11:
	.string	"A000"
.LC12:
	.string	"A001"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.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	$136, %rsp
	.cfi_def_cfa_offset 192
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	cmpl	$3, %edi
	je	.L47
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	call	abort@PLT
.L47:
	movq	8(%rsi), %rdi
	movq	%rsi, %rbx
	call	atoi@PLT
	movq	16(%rbx), %rdi
	movl	%eax, 12(%rsp)
	call	atoi@PLT
	movl	$16, %ecx
	movl	$32, %edx
	leaq	.LC9(%rip), %rsi
	movl	%eax, %ebp
	movl	12(%rsp), %eax
	movl	$2, %edi
	movl	$1, 72(%rsp)
	movl	$1, 84(%rsp)
	addl	$31, %eax
	shrl	$5, %eax
	movl	%eax, 32(%rsp)
	leal	15(%rbp), %eax
	shrl	$4, %eax
	movl	%eax, 36(%rsp)
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	36(%rsp), %ecx
	movl	32(%rsp), %edx
	xorl	%eax, %eax
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	$2, %esi
	leaq	88(%rsp), %rdi
	call	_Z10init_noyauP7noyau_ti@PLT
	movq	%rax, 40(%rsp)
	movl	88(%rsp), %eax
	leal	(%rax,%rax), %edx
	movl	12(%rsp), %eax
	leal	(%rdx,%rax), %r15d
	addl	%ebp, %edx
	movl	%r15d, %eax
	movl	%edx, 24(%rsp)
	movslq	%r15d, %r12
	imull	%edx, %eax
	salq	$3, %r12
	cltq
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, 16(%rsp)
	call	malloc@PLT
	movq	16(%rsp), %rdi
	movq	%rax, %r13
	call	malloc@PLT
	movq	%r12, %rdi
	movq	%rax, %r14
	call	malloc@PLT
	movq	%r12, %rdi
	movq	%rax, %rbx
	call	malloc@PLT
	movslq	24(%rsp), %rdx
	movq	%rax, %r12
	leaq	0(,%rdx,4), %rax
	xorl	%edx, %edx
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
.L48:
	cmpl	%eax, %r15d
	jle	.L55
	leaq	0(%r13,%rdx), %rcx
	movq	%rcx, (%rbx,%rax,8)
	leaq	(%r14,%rdx), %rcx
	movq	%rcx, (%r12,%rax,8)
	movq	24(%rsp), %rcx
	incq	%rax
	addq	%rcx, %rdx
	jmp	.L48
.L55:
	movq	16(%rsp), %rsi
	leaq	48(%rsp), %rdi
	leaq	88(%rsp), %r15
	call	cudaMalloc@PLT
	movq	16(%rsp), %rsi
	leaq	56(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	12(%rsp), %esi
	movq	%r15, %rdi
	movq	%rbx, %rcx
	movl	%ebp, %edx
	call	_Z6init_AP7noyau_tiiPPf
	movl	12(%rsp), %esi
	movq	%r15, %rdi
	movq	%rbx, %rcx
	leaq	.LC11(%rip), %r8
	movl	%ebp, %edx
	movl	$5, %r15d
	call	_Z6outputP7noyau_tiiPPfPKc
.L51:
	movl	12(%rsp), %esi
	leaq	88(%rsp), %rdi
	movq	%rbx, %rcx
	movl	%ebp, %edx
	call	_Z11periodic_bcP7noyau_tiiPPf
	movq	16(%rsp), %rdx
	movq	48(%rsp), %rdi
	movq	%r13, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$179, %edi
	call	_Z11check_noerri
	movl	32(%rsp), %eax
	movl	72(%rsp), %ecx
	xorl	%r9d, %r9d
	movl	$2147483649, %edx
	movl	84(%rsp), %esi
	xorl	%r8d, %r8d
	movl	%eax, 76(%rsp)
	movl	36(%rsp), %eax
	salq	$5, %rdx
	movl	%eax, 80(%rsp)
	movq	76(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L50
	pushq	%rax
	.cfi_def_cfa_offset 200
	movl	%ebp, %r8d
	pushq	32(%rsp)
	.cfi_def_cfa_offset 208
	movq	72(%rsp), %r9
	movl	28(%rsp), %ecx
	movq	40(%rsp), %rdx
	movq	64(%rsp), %rsi
	movq	56(%rsp), %rdi
	call	_Z9convol_glP7noyau_tPfmiiS1_m@PLT
	popq	%rdx
	.cfi_def_cfa_offset 200
	popq	%rcx
	.cfi_def_cfa_offset 192
.L50:
	movl	$195, %edi
	call	_Z11check_noerri
	movq	16(%rsp), %rdx
	movq	56(%rsp), %rsi
	movq	%r14, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movl	$207, %edi
	call	_Z11check_noerri
	decl	%r15d
	je	.L56
	xchgq	%r12, %rbx
	xchgq	%r14, %r13
	jmp	.L51
.L56:
	movl	12(%rsp), %esi
	leaq	88(%rsp), %r15
	movq	%r12, %rcx
	movl	%ebp, %edx
	leaq	.LC12(%rip), %r8
	movq	%r15, %rdi
	call	_Z6outputP7noyau_tiiPPfPKc
	movq	%r14, %rdi
	call	free@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	40(%rsp), %rsi
	movq	%r15, %rdi
	call	_Z10free_noyauP7noyau_tS0_@PLT
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L52
	call	__stack_chk_fail@PLT
.L52:
	addq	$136, %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
.LFE2031:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.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
.LFE2057:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC0:
	.long	1065353216
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC1:
	.long	0
	.long	1071644672
	.align 8
.LC2:
	.long	1413754136
	.long	1075388923
	.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 <math.h>
#include <stdlib.h>
#include <stdio.h>

#include "convolution.cuh"

// #define USE_SHARED_MEM // Question 2 et 3
// #define USE_PITCH  // Question 3

void init_A(
	/* IN */
	noyau_t *noyau,
	int Ni,
	int Nj,
	/* OUT */
	float **A
	) {

    const float pasx = 1/float(Ni);
    const float pasy = 1/float(Nj);

    for(int i = noyau->R ; i < Ni+noyau->R ; i++) {
	    const float x = 2*M_PI*(0.5*pasx + (i-noyau->R)*pasx);

	    for(int j = noyau->R ; j < Nj+noyau->R ; j++) {
	        const float y = 2*M_PI*(0.5*pasy + (j-noyau->R)*pasy);
	        A[i][j] = sin(x)*cos(y);
	    }
    }
}

void periodic_bc(
	/* IN */
	noyau_t *noyau,
	int Ni,
	int Nj,
	/* INOUT */
	float **A
	) {

    /* WEST BOUNDARY UPDATE */
    for(int i = noyau->R ; i < Ni+noyau->R ; i++) {
	    for(int j = 0 ; j < noyau->R ; j++) {
	        A[i][j] = A[i][j+Nj];
	    }
    }

    /* EAST BOUNDARY UPDATE */
    for(int i = noyau->R ; i < Ni+noyau->R ; i++) {
	    for(int j = Nj+noyau->R ; j < Nj+2*noyau->R ; j++) {
	        A[i][j] = A[i][j-Nj];
	    }
    }

    /* SOUTH BOUNDARY UPDATE */
    for(int i = 0 ; i < noyau->R ; i++) {
	    for(int j = 0 ; j < Nj+2*noyau->R ; j++) {
	        A[i][j] = A[i+Ni][j];
	    }
    }

    /* NORTH BOUNDARY UPDATE */
    for(int i = Ni+noyau->R ; i < Ni+2*noyau->R ; i++) {
	    for(int j = 0 ; j < Nj+2*noyau->R ; j++) {
	        A[i][j] = A[i-Ni][j];
	    }
    }
}

void output(
	/* IN */
	noyau_t *noyau,
	int Ni,
	int Nj,
	float **A,
	const char *fichier
	) {
    
    FILE *fd = fopen(fichier, "w");

    const float pasx = 1/float(Ni);
    const float pasy = 1/float(Nj);

    for(int i = noyau->R ; i < Ni+noyau->R ; i++) {
	    const float x = 2*M_PI*(0.5*pasx + (i-noyau->R)*pasx);

	    for(int j = noyau->R ; j < Nj+noyau->R ; j++) {
	        const float y = 2*M_PI*(0.5*pasy + (j-noyau->R)*pasy);

	        fprintf(fd, "%.4e %.4e %.4e\n", x, y, A[i][j]);
	    }
	    fprintf(fd, "\n");
    }

    fclose(fd);
    printf("Ecriture fichier %s terminee.\n", fichier);
}

void check_noerr(int linenr) {
    hipError_t err = hipGetLastError();
    if (err != hipSuccess) {
	    printf("Erreur ligne %d : %s \n", linenr, hipGetErrorString(err));
	    abort();
    }
}

int main(int argc, char **argv) {
    int Ni, Nj;

    if (argc !=3) {
        printf("Usage: ./convolution Ni Nj\n");
        abort();
    }
    
    Ni = atoi(argv[1]);
    Nj = atoi(argv[2]);

    float *buf_A, *buf_B;
    float **A, **B;

    float *d_buf_A, *d_buf_B;
    size_t pitchA, pitchB;

    dim3 bThreads(DIM_BX, DIM_BY);
    dim3 gBlocs;

    gBlocs.x = (Ni + bThreads.x-1) / bThreads.x;
    gBlocs.y = (Nj + bThreads.y-1) / bThreads.y;

    printf("nthreads : (%d, %d)\n",  bThreads.x, bThreads.y);
    printf("nblocks  : (%d, %d)\n",  gBlocs.x, gBlocs.y);

    int Ni_2R, Nj_2R;
    noyau_t noyau;
    noyau_t *d_noyau; // pointeur noyau sur device

    d_noyau = init_noyau(&noyau, R_NOYAU);

    Ni_2R = Ni + 2*noyau.R;
    Nj_2R = Nj + 2*noyau.R;

    buf_A = (float*)malloc(Ni_2R*Nj_2R*sizeof(float));
    buf_B = (float*)malloc(Ni_2R*Nj_2R*sizeof(float));

    A = (float**)malloc(Ni_2R*sizeof(float*));
    B = (float**)malloc(Ni_2R*sizeof(float*));

    for(int i = 0 ; i < Ni_2R ; i++) {
	    A[i] = buf_A + i*Nj_2R;
	    B[i] = buf_B + i*Nj_2R;
    }

#ifdef USE_PITCH
    // A COMPLETER    
#else
    hipMalloc(&d_buf_A, Ni_2R*Nj_2R*sizeof(float));
    hipMalloc(&d_buf_B, Ni_2R*Nj_2R*sizeof(float));

    pitchA = Nj_2R*sizeof(float);
    pitchB = Nj_2R*sizeof(float);
#endif

    init_A(&noyau, Ni, Nj, A);
    output(&noyau, Ni, Nj, A, "A000");

    for(int iter = 1 ; iter <= 5 ; iter++) {
	    periodic_bc(&noyau, Ni, Nj, A);

	// Transfert CPU => GPU
#ifdef USE_PITCH
	// A COMPLETER
#else
	    hipMemcpy(
		    d_buf_A, 
		    buf_A, 
		    Ni_2R*Nj_2R*sizeof(float), 
		    hipMemcpyHostToDevice);
#endif
	    check_noerr(__LINE__);

	// Application operateur convolution
#ifdef USE_SHARED_MEM
	    convol_sh<<<gBlocs, bThreads>>>(
		    d_noyau, 
		    d_buf_A, pitchA,
		    Ni, Nj, 
		    d_buf_B, pitchB);
#else
	    convol_gl<<<gBlocs, bThreads>>>(
		    d_noyau, 
		    d_buf_A, pitchA,
		    Ni, Nj, 
		    d_buf_B, pitchB);
#endif
	    check_noerr(__LINE__);

	// Transfert GPU => CPU
#ifdef USE_PITCH
	// A COMPLETER
#else
	    hipMemcpy(
		    buf_B,
		    d_buf_B, 
		    Ni_2R*Nj_2R*sizeof(float), 
		    hipMemcpyDeviceToHost);
#endif
	    check_noerr(__LINE__);

	    float *buf_C;
	    buf_C = buf_A;
	    buf_A = buf_B;
	    buf_B = buf_C;

	    float **C;
	    C = A;
	    A = B;
	    B = C;
    }

    output(&noyau, Ni, Nj, A, "A001");

    free(buf_A);
    free(buf_B);
    free(A);
    free(B);

    free_noyau(&noyau, d_noyau);

    return 0;
}


	.text
	.file	"main.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z6init_AP7noyau_tiiPPf
.LCPI0_0:
	.long	0x3f800000                      # float 1
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI0_1:
	.quad	0x3fe0000000000000              # double 0.5
.LCPI0_2:
	.quad	0x401921fb54442d18              # double 6.2831853071795862
	.text
	.globl	_Z6init_AP7noyau_tiiPPf
	.type	_Z6init_AP7noyau_tiiPPf,@function
_Z6init_AP7noyau_tiiPPf:                # @_Z6init_AP7noyau_tiiPPf
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB0_7
# %bb.1:                                # %.lr.ph28
	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	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rcx, %rbx
	movl	%edx, %r14d
	movl	%esi, %r15d
	movq	%rdi, %r12
	cvtsi2ss	%esi, %xmm0
	movss	.LCPI0_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	movaps	%xmm1, %xmm2
	divss	%xmm0, %xmm2
	xorps	%xmm0, %xmm0
	cvtsi2ss	%edx, %xmm0
	divss	%xmm0, %xmm1
	movl	(%rdi), %eax
	movss	%xmm2, 4(%rsp)                  # 4-byte Spill
	cvtss2sd	%xmm2, %xmm2
	movsd	.LCPI0_1(%rip), %xmm0           # xmm0 = mem[0],zero
	mulsd	%xmm0, %xmm2
	movsd	%xmm2, 24(%rsp)                 # 8-byte Spill
	xorps	%xmm2, %xmm2
	cvtss2sd	%xmm1, %xmm2
	mulsd	%xmm0, %xmm2
	movslq	%eax, %r13
	movsd	.LCPI0_2(%rip), %xmm3           # xmm3 = mem[0],zero
	movss	%xmm1, 8(%rsp)                  # 4-byte Spill
	movsd	%xmm2, 32(%rsp)                 # 8-byte Spill
.LBB0_2:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_4 Depth 2
	movl	%r13d, %ecx
	subl	%eax, %ecx
	xorps	%xmm0, %xmm0
	cvtsi2ss	%ecx, %xmm0
	mulss	4(%rsp), %xmm0                  # 4-byte Folded Reload
	cvtss2sd	%xmm0, %xmm0
	addsd	24(%rsp), %xmm0                 # 8-byte Folded Reload
	mulsd	%xmm3, %xmm0
	testl	%r14d, %r14d
	jle	.LBB0_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB0_2 Depth=1
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 12(%rsp)                 # 4-byte Spill
	movslq	%eax, %rbp
.LBB0_4:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%ebp, %ecx
	subl	%eax, %ecx
	xorps	%xmm0, %xmm0
	cvtsi2ss	%ecx, %xmm0
	mulss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	addsd	%xmm2, %xmm0
	mulsd	%xmm3, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 20(%rsp)                 # 4-byte Spill
	movss	12(%rsp), %xmm0                 # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	callq	sinf
	movss	%xmm0, 16(%rsp)                 # 4-byte Spill
	movss	20(%rsp), %xmm0                 # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	callq	cosf
	movsd	.LCPI0_2(%rip), %xmm3           # xmm3 = mem[0],zero
	movsd	32(%rsp), %xmm2                 # 8-byte Reload
                                        # xmm2 = mem[0],zero
	movss	8(%rsp), %xmm1                  # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	mulss	16(%rsp), %xmm0                 # 4-byte Folded Reload
	movq	(%rbx,%r13,8), %rax
	movss	%xmm0, (%rax,%rbp,4)
	incq	%rbp
	movl	(%r12), %eax
	leal	(%rax,%r14), %ecx
	movslq	%ecx, %rcx
	cmpq	%rcx, %rbp
	jl	.LBB0_4
.LBB0_5:                                # %._crit_edge
                                        #   in Loop: Header=BB0_2 Depth=1
	incq	%r13
	leal	(%rax,%r15), %ecx
	movslq	%ecx, %rcx
	cmpq	%rcx, %r13
	jl	.LBB0_2
# %bb.6:
	addq	$40, %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
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r13
	.cfi_restore %r14
	.cfi_restore %r15
	.cfi_restore %rbp
.LBB0_7:                                # %._crit_edge29
	retq
.Lfunc_end0:
	.size	_Z6init_AP7noyau_tiiPPf, .Lfunc_end0-_Z6init_AP7noyau_tiiPPf
	.cfi_endproc
                                        # -- End function
	.globl	_Z11periodic_bcP7noyau_tiiPPf   # -- Begin function _Z11periodic_bcP7noyau_tiiPPf
	.type	_Z11periodic_bcP7noyau_tiiPPf,@function
_Z11periodic_bcP7noyau_tiiPPf:          # @_Z11periodic_bcP7noyau_tiiPPf
	.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
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
                                        # kill: def $edx killed $edx def $rdx
                                        # kill: def $esi killed $esi def $rsi
	movl	(%rdi), %eax
	movslq	%eax, %rdi
	leal	(%rdi,%rsi), %r8d
	testl	%esi, %esi
	jle	.LBB1_12
# %bb.1:                                # %.preheader75.lr.ph
	movslq	%edx, %r9
	movslq	%r8d, %r10
	movq	%rdi, %r11
.LBB1_2:                                # %.preheader75
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
	testl	%edi, %edi
	jle	.LBB1_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB1_2 Depth=1
	movq	(%rcx,%r11,8), %rbx
	leaq	(%rbx,%r9,4), %r14
	xorl	%r15d, %r15d
.LBB1_4:                                #   Parent Loop BB1_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%r14,%r15,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%rbx,%r15,4)
	incq	%r15
	cmpq	%r15, %rax
	jne	.LBB1_4
.LBB1_5:                                # %._crit_edge
                                        #   in Loop: Header=BB1_2 Depth=1
	incq	%r11
	cmpq	%r10, %r11
	jl	.LBB1_2
# %bb.6:                                # %.preheader74
	testl	%esi, %esi
	jle	.LBB1_12
# %bb.7:                                # %.lr.ph83
	leal	(%rax,%rdx), %ebx
	leal	(%rax,%rax), %r11d
	leal	(%r9,%rax,2), %ebp
	movslq	%ebx, %rbx
	movslq	%ebp, %r14
	shlq	$2, %r9
	negq	%r9
	movq	%rdi, %r15
.LBB1_8:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_10 Depth 2
	cmpl	%r11d, %eax
	jge	.LBB1_11
# %bb.9:                                # %.lr.ph80
                                        #   in Loop: Header=BB1_8 Depth=1
	movq	(%rcx,%r15,8), %r12
	leaq	(%r12,%r9), %r13
	movq	%rbx, %rbp
.LBB1_10:                               #   Parent Loop BB1_8 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%r13,%rbp,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%r12,%rbp,4)
	incq	%rbp
	cmpq	%r14, %rbp
	jl	.LBB1_10
.LBB1_11:                               # %._crit_edge81
                                        #   in Loop: Header=BB1_8 Depth=1
	incq	%r15
	cmpq	%r10, %r15
	jl	.LBB1_8
.LBB1_12:                               # %.preheader73
	leal	(%rax,%rax), %r10d
	movslq	%esi, %r9
	testl	%eax, %eax
	jle	.LBB1_18
# %bb.13:                               # %.preheader72.lr.ph
	leal	(%r10,%rdx), %r11d
	leaq	(%rcx,%r9,8), %rbx
	xorl	%r14d, %r14d
.LBB1_14:                               # %.preheader72
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_16 Depth 2
	testl	%r11d, %r11d
	jle	.LBB1_17
# %bb.15:                               # %.lr.ph85
                                        #   in Loop: Header=BB1_14 Depth=1
	movq	(%rbx,%r14,8), %r15
	movq	(%rcx,%r14,8), %r12
	xorl	%r13d, %r13d
.LBB1_16:                               #   Parent Loop BB1_14 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%r15,%r13,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%r12,%r13,4)
	incq	%r13
	cmpq	%r13, %r11
	jne	.LBB1_16
.LBB1_17:                               # %._crit_edge86
                                        #   in Loop: Header=BB1_14 Depth=1
	incq	%r14
	cmpq	%rax, %r14
	jne	.LBB1_14
.LBB1_18:                               # %.preheader71
	cmpl	%r10d, %edi
	jge	.LBB1_24
# %bb.19:                               # %.preheader.lr.ph
	movslq	%r8d, %rax
	addl	%r10d, %esi
	addl	%r10d, %edx
	movslq	%esi, %rsi
.LBB1_20:                               # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_22 Depth 2
	testl	%edx, %edx
	jle	.LBB1_23
# %bb.21:                               # %.lr.ph89
                                        #   in Loop: Header=BB1_20 Depth=1
	movq	%rax, %rdi
	subq	%r9, %rdi
	movq	(%rcx,%rdi,8), %rdi
	movq	(%rcx,%rax,8), %r8
	xorl	%r10d, %r10d
.LBB1_22:                               #   Parent Loop BB1_20 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rdi,%r10,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%r8,%r10,4)
	incq	%r10
	cmpq	%r10, %rdx
	jne	.LBB1_22
.LBB1_23:                               # %._crit_edge90
                                        #   in Loop: Header=BB1_20 Depth=1
	incq	%rax
	cmpq	%rsi, %rax
	jl	.LBB1_20
.LBB1_24:                               # %._crit_edge92
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z11periodic_bcP7noyau_tiiPPf, .Lfunc_end1-_Z11periodic_bcP7noyau_tiiPPf
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z6outputP7noyau_tiiPPfPKc
.LCPI2_0:
	.long	0x3f800000                      # float 1
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI2_1:
	.quad	0x3fe0000000000000              # double 0.5
.LCPI2_2:
	.quad	0x401921fb54442d18              # double 6.2831853071795862
	.text
	.globl	_Z6outputP7noyau_tiiPPfPKc
	.type	_Z6outputP7noyau_tiiPPfPKc,@function
_Z6outputP7noyau_tiiPPfPKc:             # @_Z6outputP7noyau_tiiPPfPKc
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rcx, %r14
	movl	%edx, %r15d
	movl	%esi, %ebx
	movq	%rdi, %r13
	movl	$.L.str, %esi
	movq	%r8, 32(%rsp)                   # 8-byte Spill
	movq	%r8, %rdi
	callq	fopen
	movq	%rax, %rbp
	movq	%rbx, %rax
	movq	%rbx, 16(%rsp)                  # 8-byte Spill
	testl	%ebx, %ebx
	jle	.LBB2_6
# %bb.1:                                # %.lr.ph33
	cvtsi2ssl	16(%rsp), %xmm0         # 4-byte Folded Reload
	movss	.LCPI2_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	movaps	%xmm1, %xmm2
	divss	%xmm0, %xmm2
	xorps	%xmm0, %xmm0
	cvtsi2ss	%r15d, %xmm0
	divss	%xmm0, %xmm1
	movl	(%r13), %eax
	movss	%xmm2, 12(%rsp)                 # 4-byte Spill
	cvtss2sd	%xmm2, %xmm2
	movsd	.LCPI2_1(%rip), %xmm0           # xmm0 = mem[0],zero
	mulsd	%xmm0, %xmm2
	movsd	%xmm2, 40(%rsp)                 # 8-byte Spill
	movss	%xmm1, 8(%rsp)                  # 4-byte Spill
	cvtss2sd	%xmm1, %xmm1
	mulsd	%xmm0, %xmm1
	movsd	%xmm1, 24(%rsp)                 # 8-byte Spill
	movslq	%eax, %rbx
	movsd	.LCPI2_2(%rip), %xmm1           # xmm1 = mem[0],zero
.LBB2_2:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_4 Depth 2
	testl	%r15d, %r15d
	movss	8(%rsp), %xmm2                  # 4-byte Reload
                                        # xmm2 = mem[0],zero,zero,zero
	movsd	24(%rsp), %xmm3                 # 8-byte Reload
                                        # xmm3 = mem[0],zero
	jle	.LBB2_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB2_2 Depth=1
	movl	%ebx, %ecx
	subl	%eax, %ecx
	xorps	%xmm0, %xmm0
	cvtsi2ss	%ecx, %xmm0
	mulss	12(%rsp), %xmm0                 # 4-byte Folded Reload
	cvtss2sd	%xmm0, %xmm0
	addsd	40(%rsp), %xmm0                 # 8-byte Folded Reload
	mulsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	cvtss2sd	%xmm0, %xmm0
	movsd	%xmm0, 48(%rsp)                 # 8-byte Spill
	movslq	%eax, %r12
.LBB2_4:                                #   Parent Loop BB2_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%r12d, %ecx
	subl	%eax, %ecx
	xorps	%xmm0, %xmm0
	cvtsi2ss	%ecx, %xmm0
	mulss	%xmm2, %xmm0
	cvtss2sd	%xmm0, %xmm0
	addsd	%xmm3, %xmm0
	mulsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	xorps	%xmm1, %xmm1
	cvtss2sd	%xmm0, %xmm1
	movq	(%r14,%rbx,8), %rax
	xorps	%xmm2, %xmm2
	cvtss2sd	(%rax,%r12,4), %xmm2
	movl	$.L.str.1, %esi
	movq	%rbp, %rdi
	movsd	48(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$3, %al
	callq	fprintf
	movsd	24(%rsp), %xmm3                 # 8-byte Reload
                                        # xmm3 = mem[0],zero
	movss	8(%rsp), %xmm2                  # 4-byte Reload
                                        # xmm2 = mem[0],zero,zero,zero
	movsd	.LCPI2_2(%rip), %xmm1           # xmm1 = mem[0],zero
	incq	%r12
	movl	(%r13), %eax
	leal	(%rax,%r15), %ecx
	movslq	%ecx, %rcx
	cmpq	%rcx, %r12
	jl	.LBB2_4
.LBB2_5:                                # %._crit_edge
                                        #   in Loop: Header=BB2_2 Depth=1
	movl	$10, %edi
	movq	%rbp, %rsi
	callq	fputc@PLT
	movsd	.LCPI2_2(%rip), %xmm1           # xmm1 = mem[0],zero
	incq	%rbx
	movl	(%r13), %eax
	movq	16(%rsp), %rcx                  # 8-byte Reload
	addl	%eax, %ecx
	movslq	%ecx, %rcx
	cmpq	%rcx, %rbx
	jl	.LBB2_2
.LBB2_6:                                # %._crit_edge34
	movq	%rbp, %rdi
	callq	fclose
	movl	$.L.str.3, %edi
	movq	32(%rsp), %rsi                  # 8-byte Reload
	xorl	%eax, %eax
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	printf                          # TAILCALL
.Lfunc_end2:
	.size	_Z6outputP7noyau_tiiPPfPKc, .Lfunc_end2-_Z6outputP7noyau_tiiPPfPKc
	.cfi_endproc
                                        # -- End function
	.globl	_Z11check_noerri                # -- Begin function _Z11check_noerri
	.type	_Z11check_noerri,@function
_Z11check_noerri:                       # @_Z11check_noerri
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%edi, %ebx
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB3_2
# %bb.1:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB3_2:
	.cfi_def_cfa_offset 16
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.4, %edi
	movl	%ebx, %esi
	movq	%rax, %rdx
	xorl	%eax, %eax
	callq	printf
	callq	abort
.Lfunc_end3:
	.size	_Z11check_noerri, .Lfunc_end3-_Z11check_noerri
	.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	$136, %rsp
	.cfi_def_cfa_offset 192
	.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	$3, %edi
	jne	.LBB4_9
# %bb.1:
	movq	%rsi, %rbx
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %r14d
	movq	16(%rbx), %rdi
	callq	atoi
	movl	%eax, %r13d
	leal	31(%r14), %r15d
	shrl	$5, %r15d
	leal	15(%r13), %ebx
	shrl	$4, %ebx
	movq	%rbx, %rax
	shlq	$32, %rax
	movq	%rax, 64(%rsp)                  # 8-byte Spill
	movl	$.L.str.6, %edi
	movl	$32, %esi
	movl	$16, %edx
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.7, %edi
	movq	%r15, 88(%rsp)                  # 8-byte Spill
	movl	%r15d, %esi
	movl	%ebx, %edx
	xorl	%eax, %eax
	callq	printf
	leaq	104(%rsp), %rbx
	movq	%rbx, %rdi
	movl	$2, %esi
	callq	_Z10init_noyauP7noyau_ti
	movq	%rax, 56(%rsp)                  # 8-byte Spill
	movslq	(%rbx), %r12
	movq	%r14, 24(%rsp)                  # 8-byte Spill
	movslq	%r14d, %rax
	leaq	(%rax,%r12,2), %r15
	addq	%r12, %r12
	movq	%r13, 16(%rsp)                  # 8-byte Spill
	addl	%r13d, %r12d
	movl	%r15d, %eax
	imull	%r12d, %eax
	movslq	%eax, %rbx
	shlq	$2, %rbx
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r13
	movq	%rbx, 96(%rsp)                  # 8-byte Spill
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movq	%r15, %r14
	shlq	$3, %r14
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, %rbx
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, %r8
	movslq	%r12d, %rax
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	testl	%r15d, %r15d
	jle	.LBB4_4
# %bb.2:                                # %.lr.ph.preheader
	movl	%r15d, %eax
	movq	40(%rsp), %rcx                  # 8-byte Reload
	leaq	(,%rcx,4), %rcx
	xorl	%edx, %edx
	movq	32(%rsp), %rsi                  # 8-byte Reload
	movq	%r13, %rdi
.LBB4_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%rdi, (%rbx,%rdx,8)
	movq	%rsi, (%r8,%rdx,8)
	incq	%rdx
	addq	%rcx, %rdi
	addq	%rcx, %rsi
	cmpq	%rdx, %rax
	jne	.LBB4_3
.LBB4_4:                                # %._crit_edge
	movq	%r8, 48(%rsp)                   # 8-byte Spill
	movq	88(%rsp), %rax                  # 8-byte Reload
	addq	%rax, 64(%rsp)                  # 8-byte Folded Spill
	leaq	80(%rsp), %rdi
	movq	96(%rsp), %r14                  # 8-byte Reload
	movq	%r14, %rsi
	callq	hipMalloc
	leaq	72(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	shlq	$2, 40(%rsp)                    # 8-byte Folded Spill
	leaq	104(%rsp), %rbp
	movq	%rbp, %rdi
	movq	24(%rsp), %r12                  # 8-byte Reload
	movl	%r12d, %esi
	movq	16(%rsp), %r15                  # 8-byte Reload
	movl	%r15d, %edx
	movq	%rbx, %rcx
	callq	_Z6init_AP7noyau_tiiPPf
	movl	$.L.str.8, %r8d
	movq	%rbp, %rdi
	movl	%r12d, %esi
	movl	%r15d, %edx
	movq	%rbx, %rcx
	callq	_Z6outputP7noyau_tiiPPfPKc
	movl	$5, %r15d
.LBB4_5:                                # =>This Inner Loop Header: Depth=1
	movq	48(%rsp), %rbp                  # 8-byte Reload
	movq	32(%rsp), %r12                  # 8-byte Reload
	leaq	104(%rsp), %rdi
	movq	24(%rsp), %rsi                  # 8-byte Reload
                                        # kill: def $esi killed $esi killed $rsi
	movq	16(%rsp), %rdx                  # 8-byte Reload
                                        # kill: def $edx killed $edx killed $rdx
	movq	%rbx, 48(%rsp)                  # 8-byte Spill
	movq	%rbx, %rcx
	callq	_Z11periodic_bcP7noyau_tiiPPf
	movq	80(%rsp), %rdi
	movq	%r13, 32(%rsp)                  # 8-byte Spill
	movq	%r13, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$181, %edi
	callq	_Z11check_noerri
	movq	64(%rsp), %rdi                  # 8-byte Reload
	movl	$1, %esi
	movabsq	$68719476768, %rdx              # imm = 0x1000000020
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_7
# %bb.6:                                #   in Loop: Header=BB4_5 Depth=1
	movq	80(%rsp), %rsi
	movq	72(%rsp), %r9
	movq	40(%rsp), %rdx                  # 8-byte Reload
	movq	%rdx, (%rsp)
	movq	56(%rsp), %rdi                  # 8-byte Reload
	movq	24(%rsp), %rcx                  # 8-byte Reload
                                        # kill: def $ecx killed $ecx killed $rcx
	movq	16(%rsp), %r8                   # 8-byte Reload
                                        # kill: def $r8d killed $r8d killed $r8
	callq	_Z24__device_stub__convol_glP7noyau_tPfmiiS1_m
.LBB4_7:                                #   in Loop: Header=BB4_5 Depth=1
	movl	$197, %edi
	callq	_Z11check_noerri
	movq	72(%rsp), %rsi
	movq	%r12, %rdi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	$209, %edi
	callq	_Z11check_noerri
	movq	%r12, %r13
	movq	%rbp, %rbx
	decl	%r15d
	jne	.LBB4_5
# %bb.8:
	leaq	104(%rsp), %rbx
	movl	$.L.str.9, %r8d
	movq	%rbx, %rdi
	movq	24(%rsp), %rsi                  # 8-byte Reload
                                        # kill: def $esi killed $esi killed $rsi
	movq	16(%rsp), %rdx                  # 8-byte Reload
                                        # kill: def $edx killed $edx killed $rdx
	movq	%rbp, %rcx
	callq	_Z6outputP7noyau_tiiPPfPKc
	movq	%r12, %rdi
	callq	free
	movq	32(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	%rbp, %rdi
	callq	free
	movq	48(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	%rbx, %rdi
	movq	56(%rsp), %rsi                  # 8-byte Reload
	callq	_Z10free_noyauP7noyau_tS0_
	xorl	%eax, %eax
	addq	$136, %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
.LBB4_9:
	.cfi_def_cfa_offset 192
	movl	$.Lstr, %edi
	callq	puts@PLT
	callq	abort
.Lfunc_end4:
	.size	main, .Lfunc_end4-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"w"
	.size	.L.str, 2

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"%.4e %.4e %.4e\n"
	.size	.L.str.1, 16

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Ecriture fichier %s terminee.\n"
	.size	.L.str.3, 31

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Erreur ligne %d : %s \n"
	.size	.L.str.4, 23

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"nthreads : (%d, %d)\n"
	.size	.L.str.6, 21

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"nblocks  : (%d, %d)\n"
	.size	.L.str.7, 21

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

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

	.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	"Usage: ./convolution Ni Nj"
	.size	.Lstr, 27

	.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
