// nvcc jcaobi.cu -o jacobi

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#define NBLOCKS 32

__global__ void compute_xnext(double *r, double *d, double *x, double *x_next,
	double *b, int size) {

	// Size defined as third arg in <<< >>> thing
	extern __shared__ double x_copy[];

	// Copy x to multiply in shared memory for quicker access
	int idx, row_idx = blockDim.x * blockIdx.x + threadIdx.x;
  int upBound = NBLOCKS * (threadIdx.x + 1);
  if (upBound > size)
    upBound = size;
  for (idx=NBLOCKS*threadIdx.x; idx < upBound; idx++)
    x_copy[idx] = x[idx];

	if (row_idx >= size) return;

	__syncthreads();

	double y_k = 0.;
	for (idx=0; idx < size; idx++)
		y_k += x_copy[idx] * r[row_idx * size + idx];

	x_next[row_idx] = (b[row_idx] - y_k) / d[row_idx];
}

int main (int argc, char *argvs[]) {

	if (argc != 4) {
		printf("usage: %s [size] [epsilon] [verbose: 1/2]\n", argvs[0]);
		return 1;
	}

	// init constants & variables
	int nBlocks, nThreadsPerBlock, size, idx1, idx2, niter, epsilon_pow, verbose;
	size = atoi(argvs[1]);
	epsilon_pow = atoi(argvs[2]);
	verbose = atoi(argvs[3]);
	nBlocks = NBLOCKS;
	nThreadsPerBlock = (int) size / nBlocks + 1;
	niter = 0;
	double epsilon = 1.;
	for (idx1=0; idx1 < epsilon_pow; idx1++) epsilon = epsilon / 10.;
	double epsilon2 = epsilon * epsilon;
	double norm2 = epsilon2 + 1.; // to be init over espilon

	struct timeval t0, t1, t2, t3;
	size_t size_mat = size * size * sizeof(double);
	size_t size_vect = size * sizeof(double);

	// init matrices
	double *r_mat, *d_vect, *x, *x_next, *b;
	double *d_r, *d_d, *d_x, *d_xnext, *d_b;
	r_mat = (double *) calloc(size * size, sizeof(double));
	d_vect = (double *) calloc(size, sizeof(double));
	x = (double *) calloc(size, sizeof(double));
	x_next = (double *) calloc(size, sizeof(double));
	b = (double *) calloc(size, sizeof(double));

	for (idx1=0; idx1<size; idx1++) {
		for (idx2=0; idx2<size; idx2++)
			r_mat[idx1*size + idx2] = 1.;
		r_mat[idx1*size + idx1] = 0.;
		d_vect[idx1] = 2. * size + 1.;
		x[idx1] = 1.;
		b[idx1] = 3.;
	}

	// Allocation & copy on device
	gettimeofday(&t0,NULL);
	cudaMalloc(&d_r, size_mat);
	cudaMalloc(&d_d, size_vect);
	cudaMalloc(&d_x, size_vect);
	cudaMalloc(&d_xnext, size_vect);
	cudaMalloc(&d_b, size_vect);

	gettimeofday(&t1,NULL);
	cudaMemcpy(d_r, r_mat, size_mat, cudaMemcpyHostToDevice);
	cudaMemcpy(d_d, d_vect, size_vect, cudaMemcpyHostToDevice);
	cudaMemcpy(d_b, b, size_vect, cudaMemcpyHostToDevice);

	gettimeofday(&t2,NULL);

	// Main Loop
	while (norm2 > epsilon2) {
		niter ++;
		// Send x_k
		cudaMemcpy(d_x, x, size_vect, cudaMemcpyHostToDevice);
		compute_xnext <<<nBlocks, nThreadsPerBlock, size_vect >>> (d_r, d_d,
			d_x, d_xnext, d_b, size);
			// Gather x_k+1
		cudaMemcpy(x_next, d_xnext, size_vect, cudaMemcpyDeviceToHost);

		norm2 = 0.;
		for (idx1=0; idx1 < size; idx1++) {
			norm2 += (x[idx1] - x_next[idx1]) * (x[idx1] - x_next[idx1]);
			x[idx1] = x_next[idx1];
		}
	}

	gettimeofday(&t3,NULL);

	cudaFree(d_r);
	cudaFree(d_d);
	cudaFree(d_x);
	cudaFree(d_xnext);
	cudaFree(d_b);

	// Verbose
	double t_alloc = (double)(t1.tv_sec-t0.tv_sec) + \
		(double)(t1.tv_usec-t0.tv_usec)/1000000;
	double t_trans = (double)(t2.tv_sec-t1.tv_sec) + \
		(double)(t2.tv_usec-t1.tv_usec)/1000000;
	double t_calc  = (double)(t3.tv_sec-t2.tv_sec) + \
		(double)(t3.tv_usec-t2.tv_usec)/1000000;

	if (verbose == 1) {
		printf("N = %d\nEpsilon = %10.9f\n", size, epsilon);
		printf("Nombre d'iterations: %d\n", niter);
		double norm_error = 0.;
		for (int i=0; i < size; i++)
			norm_error += (x[i] - 1./size) * (x[i] - 1./size);
		printf("Error compared to sol.: %25.24f\n\n", norm_error);
		printf("Temps d'alloc. device   : %f s\n", t_alloc);
		printf("Temps de transfert init.: %f s\n", t_trans);
		printf("Temps de calcul:          %f s\n", t_calc);
	} else if (verbose == 2)
		printf("%d, %12.11f, %d, %f\n", size, epsilon, niter, t_calc);

	free(r_mat);
	free(d_vect);
	free(x);
	free(x_next);
	free(b);

	return 0;
}

	.file	"tmpxft_0031a397_00000000-6_jacobi.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z42__device_stub__Z13compute_xnextPdS_S_S_S_iPdS_S_S_S_i
	.type	_Z42__device_stub__Z13compute_xnextPdS_S_S_S_iPdS_S_S_S_i, @function
_Z42__device_stub__Z13compute_xnextPdS_S_S_S_iPdS_S_S_S_i:
.LFB2052:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	84(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	56(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	64(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movl	%r9d, 4(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	64(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z13compute_xnextPdS_S_S_S_i(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 208
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L2:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2052:
	.size	_Z42__device_stub__Z13compute_xnextPdS_S_S_S_iPdS_S_S_S_i, .-_Z42__device_stub__Z13compute_xnextPdS_S_S_S_iPdS_S_S_S_i
	.globl	_Z13compute_xnextPdS_S_S_S_i
	.type	_Z13compute_xnextPdS_S_S_S_i, @function
_Z13compute_xnextPdS_S_S_S_i:
.LFB2053:
	.cfi_startproc
	endbr64
	jmp	_Z42__device_stub__Z13compute_xnextPdS_S_S_S_iPdS_S_S_S_i
	.cfi_endproc
.LFE2053:
	.size	_Z13compute_xnextPdS_S_S_S_i, .-_Z13compute_xnextPdS_S_S_S_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC2:
	.string	"usage: %s [size] [epsilon] [verbose: 1/2]\n"
.LC6:
	.string	"N = %d\nEpsilon = %10.9f\n"
.LC7:
	.string	"Nombre d'iterations: %d\n"
.LC8:
	.string	"Error compared to sol.: %25.24f\n\n"
.LC9:
	.string	"Temps d'alloc. device   : %f s\n"
.LC10:
	.string	"Temps de transfert init.: %f s\n"
.LC11:
	.string	"Temps de calcul:          %f s\n"
.LC12:
	.string	"%d, %12.11f, %d, %f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.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
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$216, %rsp
	.cfi_def_cfa_offset 272
	movq	%fs:40, %rax
	movq	%rax, 200(%rsp)
	xorl	%eax, %eax
	cmpl	$4, %edi
	je	.L9
	movq	(%rsi), %rdx
	movl	$2, %edi
	leaq	.LC2(%rip), %rsi
	call	__printf_chk@PLT
	movl	$1, %eax
	jmp	.L8
.L9:
	movq	8(%rsi), %rdi
	call	atoi@PLT
	movq	16(%rbp), %rdi
	movl	%eax, %ebx
	call	atoi@PLT
	movq	24(%rbp), %rdi
	movl	%eax, %r12d
	call	atoi@PLT
	movl	$32, %ecx
	movsd	.LC0(%rip), %xmm7
	movsd	.LC3(%rip), %xmm0
	movl	%eax, 56(%rsp)
	movl	%ebx, %eax
	cltd
	movsd	%xmm7, 8(%rsp)
	movaps	%xmm7, %xmm2
	idivl	%ecx
	leal	1(%rax), %r15d
	xorl	%eax, %eax
.L11:
	cmpl	%r12d, %eax
	jge	.L28
	movsd	8(%rsp), %xmm5
	incl	%eax
	divsd	%xmm0, %xmm5
	movsd	%xmm5, 8(%rsp)
	jmp	.L11
.L28:
	movsd	8(%rsp), %xmm6
	movl	%ebx, %edi
	movslq	%ebx, %r13
	movl	$8, %esi
	imull	%ebx, %edi
	leaq	0(,%r13,8), %r12
	mulsd	%xmm6, %xmm6
	movslq	%edi, %rdi
	leaq	0(,%rdi,8), %r14
	movaps	%xmm6, %xmm1
	movsd	%xmm6, 64(%rsp)
	addsd	%xmm2, %xmm1
	movsd	%xmm1, 24(%rsp)
	call	calloc@PLT
	movl	$8, %esi
	movq	%r13, %rdi
	movq	%rax, 16(%rsp)
	call	calloc@PLT
	movl	$8, %esi
	movq	%r13, %rdi
	movq	%rax, 32(%rsp)
	call	calloc@PLT
	movl	$8, %esi
	movq	%r13, %rdi
	movq	%rax, %rbp
	call	calloc@PLT
	movl	$8, %esi
	movq	%r13, %rdi
	movq	%rax, 40(%rsp)
	call	calloc@PLT
	movq	16(%rsp), %rcx
	movsd	.LC4(%rip), %xmm3
	leaq	8(%r12), %rdi
	movsd	24(%rsp), %xmm1
	movsd	.LC0(%rip), %xmm2
	movq	%rax, 48(%rsp)
	xorl	%eax, %eax
	movq	%rcx, %rsi
.L13:
	cmpl	%eax, %ebx
	jle	.L29
	xorl	%edx, %edx
.L14:
	movsd	%xmm2, (%rsi,%rdx,8)
	incq	%rdx
	cmpl	%edx, %ebx
	jg	.L14
	cvtsi2sdl	%ebx, %xmm0
	movq	32(%rsp), %rdx
	movq	$0x000000000, (%rcx)
	addq	%r12, %rsi
	movsd	%xmm2, 0(%rbp,%rax,8)
	addq	%rdi, %rcx
	addsd	%xmm0, %xmm0
	addsd	%xmm2, %xmm0
	movsd	%xmm0, (%rdx,%rax,8)
	movq	48(%rsp), %rdx
	movsd	%xmm3, (%rdx,%rax,8)
	incq	%rax
	jmp	.L13
.L29:
	leaq	136(%rsp), %rdi
	xorl	%esi, %esi
	movsd	%xmm1, 72(%rsp)
	movabsq	$4294967297, %r13
	call	gettimeofday@PLT
	leaq	80(%rsp), %rdi
	movq	%r14, %rsi
	call	cudaMalloc@PLT
	leaq	88(%rsp), %rdi
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	leaq	96(%rsp), %rdi
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	leaq	104(%rsp), %rdi
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	leaq	112(%rsp), %rdi
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	leaq	152(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movq	16(%rsp), %rsi
	movq	80(%rsp), %rdi
	movq	%r14, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	32(%rsp), %rsi
	movq	88(%rsp), %rdi
	movq	%r12, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	48(%rsp), %rsi
	movq	112(%rsp), %rdi
	movq	%r12, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	168(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	xorl	%eax, %eax
	movsd	72(%rsp), %xmm1
	movl	%eax, 24(%rsp)
.L15:
	comisd	64(%rsp), %xmm1
	jbe	.L30
	movq	96(%rsp), %rdi
	movl	$1, %ecx
	movq	%r12, %rdx
	movq	%rbp, %rsi
	incl	24(%rsp)
	call	cudaMemcpy@PLT
	movl	$134217729, %edi
	xorl	%r9d, %r9d
	movq	%r12, %r8
	movq	%r13, 188(%rsp)
	movl	192(%rsp), %ecx
	salq	$5, %rdi
	movl	$1, %esi
	movl	%r15d, 184(%rsp)
	movq	184(%rsp), %rdx
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L16
	movq	112(%rsp), %r8
	movq	104(%rsp), %rcx
	movl	%ebx, %r9d
	movq	96(%rsp), %rdx
	movq	88(%rsp), %rsi
	movq	80(%rsp), %rdi
	call	_Z42__device_stub__Z13compute_xnextPdS_S_S_S_iPdS_S_S_S_i
.L16:
	movq	104(%rsp), %rsi
	movq	40(%rsp), %rdi
	movl	$2, %ecx
	movq	%r12, %rdx
	call	cudaMemcpy@PLT
	xorl	%eax, %eax
	xorps	%xmm1, %xmm1
.L17:
	cmpl	%eax, %ebx
	jle	.L15
	movq	40(%rsp), %rcx
	movsd	0(%rbp,%rax,8), %xmm0
	movsd	(%rcx,%rax,8), %xmm2
	subsd	%xmm2, %xmm0
	movsd	%xmm2, 0(%rbp,%rax,8)
	incq	%rax
	mulsd	%xmm0, %xmm0
	addsd	%xmm0, %xmm1
	jmp	.L17
.L30:
	xorl	%esi, %esi
	leaq	184(%rsp), %rdi
	call	gettimeofday@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	88(%rsp), %rdi
	call	cudaFree@PLT
	movq	96(%rsp), %rdi
	call	cudaFree@PLT
	movq	104(%rsp), %rdi
	call	cudaFree@PLT
	movq	112(%rsp), %rdi
	call	cudaFree@PLT
	movq	136(%rsp), %rax
	movq	176(%rsp), %r13
	movq	168(%rsp), %r12
	movq	152(%rsp), %r14
	movq	%rax, 64(%rsp)
	movq	144(%rsp), %rax
	movq	160(%rsp), %r15
	movq	%rax, 72(%rsp)
	movq	192(%rsp), %rax
	subq	%r13, %rax
	cvtsi2sdq	%rax, %xmm1
	movq	184(%rsp), %rax
	divsd	.LC5(%rip), %xmm1
	subq	%r12, %rax
	cmpl	$1, 56(%rsp)
	cvtsi2sdq	%rax, %xmm0
	addsd	%xmm0, %xmm1
	jne	.L20
	movsd	8(%rsp), %xmm0
	movl	%ebx, %edx
	movl	$2, %edi
	movb	$1, %al
	leaq	.LC6(%rip), %rsi
	movsd	%xmm1, 56(%rsp)
	call	__printf_chk@PLT
	movl	24(%rsp), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC7(%rip), %rsi
	call	__printf_chk@PLT
	movsd	56(%rsp), %xmm1
	xorl	%eax, %eax
	xorps	%xmm2, %xmm2
	movsd	.LC0(%rip), %xmm3
.L21:
	cmpl	%eax, %ebx
	jle	.L31
	cvtsi2sdl	%ebx, %xmm0
	movaps	%xmm3, %xmm4
	divsd	%xmm0, %xmm4
	movsd	0(%rbp,%rax,8), %xmm0
	incq	%rax
	subsd	%xmm4, %xmm0
	mulsd	%xmm0, %xmm0
	addsd	%xmm0, %xmm2
	jmp	.L21
.L31:
	movaps	%xmm2, %xmm0
	leaq	.LC8(%rip), %rsi
	movb	$1, %al
	subq	%r14, %r12
	movl	$2, %edi
	movsd	%xmm1, 8(%rsp)
	subq	%r15, %r13
	call	__printf_chk@PLT
	movq	72(%rsp), %rbx
	movq	%r15, %rax
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	subq	%rbx, %rax
	movq	64(%rsp), %rbx
	cvtsi2sdq	%rax, %xmm0
	movq	%r14, %rax
	divsd	.LC5(%rip), %xmm0
	subq	%rbx, %rax
	cvtsi2sdq	%rax, %xmm2
	movb	$1, %al
	addsd	%xmm2, %xmm0
	call	__printf_chk@PLT
	cvtsi2sdq	%r12, %xmm2
	movl	$2, %edi
	movb	$1, %al
	cvtsi2sdq	%r13, %xmm0
	divsd	.LC5(%rip), %xmm0
	leaq	.LC10(%rip), %rsi
	addsd	%xmm2, %xmm0
	call	__printf_chk@PLT
	movsd	8(%rsp), %xmm1
	leaq	.LC11(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	movaps	%xmm1, %xmm0
	call	__printf_chk@PLT
	jmp	.L23
.L20:
	cmpl	$2, 56(%rsp)
	jne	.L23
	movl	24(%rsp), %ecx
	movsd	8(%rsp), %xmm0
	movl	%ebx, %edx
	movb	$2, %al
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
.L23:
	movq	16(%rsp), %rdi
	call	free@PLT
	movq	32(%rsp), %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	40(%rsp), %rdi
	call	free@PLT
	movq	48(%rsp), %rdi
	call	free@PLT
	xorl	%eax, %eax
.L8:
	movq	200(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L24
	call	__stack_chk_fail@PLT
.L24:
	addq	$216, %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	main, .-main
	.section	.rodata.str1.1
.LC13:
	.string	"_Z13compute_xnextPdS_S_S_S_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC13(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z13compute_xnextPdS_S_S_S_i(%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
.LFE2055:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	0
	.long	1072693248
	.align 8
.LC3:
	.long	0
	.long	1076101120
	.align 8
.LC4:
	.long	0
	.long	1074266112
	.align 8
.LC5:
	.long	0
	.long	1093567616
	.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:

// nvcc jcaobi.cu -o jacobi


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#define NBLOCKS 32

__global__ void compute_xnext(double *r, double *d, double *x, double *x_next,
	double *b, int size) {

	// Size defined as third arg in <<< >>> thing
	extern __shared__ double x_copy[];

	// Copy x to multiply in shared memory for quicker access
	int idx, row_idx = blockDim.x * blockIdx.x + threadIdx.x;
  int upBound = NBLOCKS * (threadIdx.x + 1);
  if (upBound > size)
    upBound = size;
  for (idx=NBLOCKS*threadIdx.x; idx < upBound; idx++)
    x_copy[idx] = x[idx];

	if (row_idx >= size) return;

	__syncthreads();

	double y_k = 0.;
	for (idx=0; idx < size; idx++)
		y_k += x_copy[idx] * r[row_idx * size + idx];

	x_next[row_idx] = (b[row_idx] - y_k) / d[row_idx];
}

int main (int argc, char *argvs[]) {

	if (argc != 4) {
		printf("usage: %s [size] [epsilon] [verbose: 1/2]\n", argvs[0]);
		return 1;
	}

	// init constants & variables
	int nBlocks, nThreadsPerBlock, size, idx1, idx2, niter, epsilon_pow, verbose;
	size = atoi(argvs[1]);
	epsilon_pow = atoi(argvs[2]);
	verbose = atoi(argvs[3]);
	nBlocks = NBLOCKS;
	nThreadsPerBlock = (int) size / nBlocks + 1;
	niter = 0;
	double epsilon = 1.;
	for (idx1=0; idx1 < epsilon_pow; idx1++) epsilon = epsilon / 10.;
	double epsilon2 = epsilon * epsilon;
	double norm2 = epsilon2 + 1.; // to be init over espilon

	struct timeval t0, t1, t2, t3;
	size_t size_mat = size * size * sizeof(double);
	size_t size_vect = size * sizeof(double);

	// init matrices
	double *r_mat, *d_vect, *x, *x_next, *b;
	double *d_r, *d_d, *d_x, *d_xnext, *d_b;
	r_mat = (double *) calloc(size * size, sizeof(double));
	d_vect = (double *) calloc(size, sizeof(double));
	x = (double *) calloc(size, sizeof(double));
	x_next = (double *) calloc(size, sizeof(double));
	b = (double *) calloc(size, sizeof(double));

	for (idx1=0; idx1<size; idx1++) {
		for (idx2=0; idx2<size; idx2++)
			r_mat[idx1*size + idx2] = 1.;
		r_mat[idx1*size + idx1] = 0.;
		d_vect[idx1] = 2. * size + 1.;
		x[idx1] = 1.;
		b[idx1] = 3.;
	}

	// Allocation & copy on device
	gettimeofday(&t0,NULL);
	hipMalloc(&d_r, size_mat);
	hipMalloc(&d_d, size_vect);
	hipMalloc(&d_x, size_vect);
	hipMalloc(&d_xnext, size_vect);
	hipMalloc(&d_b, size_vect);

	gettimeofday(&t1,NULL);
	hipMemcpy(d_r, r_mat, size_mat, hipMemcpyHostToDevice);
	hipMemcpy(d_d, d_vect, size_vect, hipMemcpyHostToDevice);
	hipMemcpy(d_b, b, size_vect, hipMemcpyHostToDevice);

	gettimeofday(&t2,NULL);

	// Main Loop
	while (norm2 > epsilon2) {
		niter ++;
		// Send x_k
		hipMemcpy(d_x, x, size_vect, hipMemcpyHostToDevice);
		compute_xnext <<<nBlocks, nThreadsPerBlock, size_vect >>> (d_r, d_d,
			d_x, d_xnext, d_b, size);
			// Gather x_k+1
		hipMemcpy(x_next, d_xnext, size_vect, hipMemcpyDeviceToHost);

		norm2 = 0.;
		for (idx1=0; idx1 < size; idx1++) {
			norm2 += (x[idx1] - x_next[idx1]) * (x[idx1] - x_next[idx1]);
			x[idx1] = x_next[idx1];
		}
	}

	gettimeofday(&t3,NULL);

	hipFree(d_r);
	hipFree(d_d);
	hipFree(d_x);
	hipFree(d_xnext);
	hipFree(d_b);

	// Verbose
	double t_alloc = (double)(t1.tv_sec-t0.tv_sec) + \
		(double)(t1.tv_usec-t0.tv_usec)/1000000;
	double t_trans = (double)(t2.tv_sec-t1.tv_sec) + \
		(double)(t2.tv_usec-t1.tv_usec)/1000000;
	double t_calc  = (double)(t3.tv_sec-t2.tv_sec) + \
		(double)(t3.tv_usec-t2.tv_usec)/1000000;

	if (verbose == 1) {
		printf("N = %d\nEpsilon = %10.9f\n", size, epsilon);
		printf("Nombre d'iterations: %d\n", niter);
		double norm_error = 0.;
		for (int i=0; i < size; i++)
			norm_error += (x[i] - 1./size) * (x[i] - 1./size);
		printf("Error compared to sol.: %25.24f\n\n", norm_error);
		printf("Temps d'alloc. device   : %f s\n", t_alloc);
		printf("Temps de transfert init.: %f s\n", t_trans);
		printf("Temps de calcul:          %f s\n", t_calc);
	} else if (verbose == 2)
		printf("%d, %12.11f, %d, %f\n", size, epsilon, niter, t_calc);

	free(r_mat);
	free(d_vect);
	free(x);
	free(x_next);
	free(b);

	return 0;
}

	.text
	.file	"jacobi.hip"
	.globl	_Z28__device_stub__compute_xnextPdS_S_S_S_i # -- Begin function _Z28__device_stub__compute_xnextPdS_S_S_S_i
	.type	_Z28__device_stub__compute_xnextPdS_S_S_S_i,@function
_Z28__device_stub__compute_xnextPdS_S_S_S_i: # @_Z28__device_stub__compute_xnextPdS_S_S_S_i
	.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	$144, %rsp
	.cfi_def_cfa_offset 192
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	24(%rsp), %rcx
	movq	%r8, (%rcx)
	leaq	4(%rsp), %r8
	movl	%r9d, (%r8)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%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	$_Z13compute_xnextPdS_S_S_S_i, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$160, %rsp
	.cfi_adjust_cfa_offset -160
	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	_Z28__device_stub__compute_xnextPdS_S_S_S_i, .Lfunc_end0-_Z28__device_stub__compute_xnextPdS_S_S_S_i
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0x3ff0000000000000              # double 1
.LCPI1_1:
	.quad	0x4024000000000000              # double 10
.LCPI1_2:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	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	$168, %rsp
	.cfi_def_cfa_offset 224
	.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	%rsi, %r14
	cmpl	$4, %edi
	jne	.LBB1_1
# %bb.2:
	movq	8(%r14), %rdi
	callq	atoi
	movl	%eax, %r15d
	movq	16(%r14), %rdi
	callq	atoi
	movl	%eax, %ebx
	movq	24(%r14), %rdi
	callq	atoi
	leal	31(%r15), %ecx
	testl	%r15d, %r15d
	cmovnsl	%r15d, %ecx
	movl	%ecx, 72(%rsp)                  # 4-byte Spill
	testl	%ebx, %ebx
	movl	%eax, 76(%rsp)                  # 4-byte Spill
	movsd	.LCPI1_0(%rip), %xmm1           # xmm1 = mem[0],zero
	jle	.LBB1_5
# %bb.3:                                # %.lr.ph.preheader
	movsd	.LCPI1_1(%rip), %xmm0           # xmm0 = mem[0],zero
.LBB1_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	divsd	%xmm0, %xmm1
	decl	%ebx
	jne	.LBB1_4
.LBB1_5:                                # %._crit_edge
	movsd	%xmm1, 80(%rsp)                 # 8-byte Spill
	mulsd	%xmm1, %xmm1
	movsd	.LCPI1_0(%rip), %xmm0           # xmm0 = mem[0],zero
	movsd	%xmm1, 24(%rsp)                 # 8-byte Spill
	addsd	%xmm1, %xmm0
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	movl	%r15d, %edi
	imull	%edi, %edi
	leaq	(,%rdi,8), %rax
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	movq	%r15, (%rsp)                    # 8-byte Spill
	movslq	(%rsp), %rbp                    # 4-byte Folded Reload
	leaq	(,%rbp,8), %r14
	movl	$8, %esi
	callq	calloc
	movq	%rax, %rbx
	movl	$8, %esi
	movq	%rbp, %rdi
	callq	calloc
	movq	%rax, %r12
	movl	$8, %esi
	movq	%rbp, %rdi
	callq	calloc
	movq	%rax, %r15
	movl	$8, %esi
	movq	%rbp, %rdi
	callq	calloc
	movq	%rax, %r13
	movl	$8, %esi
	movq	%rbp, %rdi
	movq	(%rsp), %rbp                    # 8-byte Reload
	callq	calloc
	testl	%ebp, %ebp
	jle	.LBB1_10
# %bb.6:                                # %.preheader.lr.ph
	leal	1(%rbp), %r11d
	xorps	%xmm0, %xmm0
	cvtsi2sd	%ebp, %xmm0
	addsd	%xmm0, %xmm0
	addsd	.LCPI1_0(%rip), %xmm0
	movl	%ebp, %ecx
	xorl	%edx, %edx
	movabsq	$4607182418800017408, %rsi      # imm = 0x3FF0000000000000
	movabsq	$4613937818241073152, %rdi      # imm = 0x4008000000000000
	xorl	%r8d, %r8d
.LBB1_7:                                # %.lr.ph130
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_8 Depth 2
	movl	%edx, %r9d
	leaq	(%rbx,%r9,8), %r9
	xorl	%r10d, %r10d
.LBB1_8:                                #   Parent Loop BB1_7 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	%rsi, (%r9,%r10,8)
	incq	%r10
	cmpq	%r10, %rcx
	jne	.LBB1_8
# %bb.9:                                # %._crit_edge131
                                        #   in Loop: Header=BB1_7 Depth=1
	movl	%r11d, %r9d
	imull	%r8d, %r9d
	movslq	%r9d, %r9
	movq	$0, (%rbx,%r9,8)
	movsd	%xmm0, (%r12,%r8,8)
	movq	%rsi, (%r15,%r8,8)
	movq	%rdi, (%rax,%r8,8)
	incq	%r8
	addl	%ebp, %edx
	cmpq	%rcx, %r8
	jne	.LBB1_7
.LBB1_10:                               # %._crit_edge133
	xorl	%ebp, %ebp
	leaq	136(%rsp), %rdi
	xorl	%esi, %esi
	movq	%rax, 96(%rsp)                  # 8-byte Spill
	callq	gettimeofday
	leaq	64(%rsp), %rdi
	movq	8(%rsp), %rsi                   # 8-byte Reload
	callq	hipMalloc
	leaq	56(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	leaq	48(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	leaq	40(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	leaq	32(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	leaq	120(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	leaq	64(%rsp), %rax
	movq	(%rax), %rdi
	movq	%rbx, %rsi
	movq	8(%rsp), %rdx                   # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	56(%rsp), %rax
	movq	(%rax), %rdi
	movq	%r12, 88(%rsp)                  # 8-byte Spill
	movq	%r12, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	32(%rsp), %rax
	movq	(%rax), %rdi
	movq	96(%rsp), %rsi                  # 8-byte Reload
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	104(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movsd	16(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	ucomisd	24(%rsp), %xmm0                 # 8-byte Folded Reload
	jbe	.LBB1_18
# %bb.11:                               # %.lr.ph141
	movl	72(%rsp), %eax                  # 4-byte Reload
	sarl	$5, %eax
	incl	%eax
	movabsq	$4294967296, %rcx               # imm = 0x100000000
	movl	%eax, %eax
	btsq	$32, %rax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	(%rsp), %r12d                   # 4-byte Reload
	xorl	%ebp, %ebp
	addq	$32, %rcx
	movq	%rcx, 8(%rsp)                   # 8-byte Spill
.LBB1_13:                               # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_17 Depth 2
	movq	48(%rsp), %rdi
	movq	%r15, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rdi                   # 8-byte Reload
	movl	$1, %esi
	movq	16(%rsp), %rdx                  # 8-byte Reload
	movl	$1, %ecx
	movq	%r14, %r8
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_15
# %bb.14:                               #   in Loop: Header=BB1_13 Depth=1
	movq	64(%rsp), %rdi
	movq	56(%rsp), %rsi
	movq	48(%rsp), %rdx
	movq	40(%rsp), %rcx
	movq	32(%rsp), %r8
	movq	(%rsp), %r9                     # 8-byte Reload
                                        # kill: def $r9d killed $r9d killed $r9
	callq	_Z28__device_stub__compute_xnextPdS_S_S_S_i
.LBB1_15:                               #   in Loop: Header=BB1_13 Depth=1
	incl	%ebp
	movq	40(%rsp), %rsi
	movq	%r13, %rdi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	cmpl	$0, (%rsp)                      # 4-byte Folded Reload
	jle	.LBB1_18
# %bb.16:                               # %.lr.ph137.preheader
                                        #   in Loop: Header=BB1_13 Depth=1
	xorpd	%xmm0, %xmm0
	xorl	%eax, %eax
.LBB1_17:                               # %.lr.ph137
                                        #   Parent Loop BB1_13 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movsd	(%r15,%rax,8), %xmm1            # xmm1 = mem[0],zero
	movsd	(%r13,%rax,8), %xmm2            # xmm2 = mem[0],zero
	subsd	%xmm2, %xmm1
	mulsd	%xmm1, %xmm1
	addsd	%xmm1, %xmm0
	movsd	%xmm2, (%r15,%rax,8)
	incq	%rax
	cmpq	%rax, %r12
	jne	.LBB1_17
# %bb.12:                               # %.loopexit
                                        #   in Loop: Header=BB1_13 Depth=1
	ucomisd	24(%rsp), %xmm0                 # 8-byte Folded Reload
	ja	.LBB1_13
.LBB1_18:                               # %._crit_edge142
	leaq	152(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	64(%rsp), %rdi
	callq	hipFree
	movq	56(%rsp), %rdi
	callq	hipFree
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	104(%rsp), %rax
	movq	112(%rsp), %rcx
	movq	(%r14), %rdx
	subq	%rax, %rdx
	xorps	%xmm2, %xmm2
	cvtsi2sd	%rdx, %xmm2
	movq	8(%r14), %rdx
	subq	%rcx, %rdx
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rdx, %xmm1
	movsd	.LCPI1_2(%rip), %xmm0           # xmm0 = mem[0],zero
	divsd	%xmm0, %xmm1
	addsd	%xmm2, %xmm1
	movl	76(%rsp), %edx                  # 4-byte Reload
	cmpl	$2, %edx
	je	.LBB1_25
# %bb.19:                               # %._crit_edge142
	cmpl	$1, %edx
	movq	88(%rsp), %r14                  # 8-byte Reload
	jne	.LBB1_26
# %bb.20:
	movsd	%xmm1, 24(%rsp)                 # 8-byte Spill
	movq	120(%rsp), %rdx
	subq	%rdx, %rax
	subq	136(%rsp), %rdx
	movq	128(%rsp), %rsi
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rdx, %xmm1
	subq	%rsi, %rcx
	subq	144(%rsp), %rsi
	xorps	%xmm2, %xmm2
	cvtsi2sd	%rsi, %xmm2
	divsd	%xmm0, %xmm2
	addsd	%xmm1, %xmm2
	movsd	%xmm2, 16(%rsp)                 # 8-byte Spill
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rax, %xmm1
	xorps	%xmm2, %xmm2
	cvtsi2sd	%rcx, %xmm2
	divsd	%xmm0, %xmm2
	addsd	%xmm1, %xmm2
	movsd	%xmm2, 8(%rsp)                  # 8-byte Spill
	movl	$.L.str.1, %edi
	movq	(%rsp), %r12                    # 8-byte Reload
	movl	%r12d, %esi
	movsd	80(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	movl	$.L.str.2, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
	testl	%r12d, %r12d
	jle	.LBB1_21
# %bb.23:                               # %.lr.ph147
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r12d, %xmm0
	movsd	.LCPI1_0(%rip), %xmm1           # xmm1 = mem[0],zero
	divsd	%xmm0, %xmm1
	movl	%r12d, %eax
	xorpd	%xmm0, %xmm0
	xorl	%ecx, %ecx
.LBB1_24:                               # =>This Inner Loop Header: Depth=1
	movsd	(%r15,%rcx,8), %xmm2            # xmm2 = mem[0],zero
	subsd	%xmm1, %xmm2
	mulsd	%xmm2, %xmm2
	addsd	%xmm2, %xmm0
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB1_24
	jmp	.LBB1_22
.LBB1_1:
	movq	(%r14), %rsi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %eax
	jmp	.LBB1_27
.LBB1_25:
	movl	$.L.str.7, %edi
	movq	(%rsp), %rsi                    # 8-byte Reload
                                        # kill: def $esi killed $esi killed $rsi
	movsd	80(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movl	%ebp, %edx
	movb	$2, %al
	callq	printf
	movq	88(%rsp), %r14                  # 8-byte Reload
	jmp	.LBB1_26
.LBB1_21:
	xorpd	%xmm0, %xmm0
.LBB1_22:                               # %._crit_edge148
	movl	$.L.str.3, %edi
	movb	$1, %al
	callq	printf
	movl	$.L.str.4, %edi
	movsd	16(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	movl	$.L.str.5, %edi
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	movl	$.L.str.6, %edi
	movsd	24(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
.LBB1_26:
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	%r13, %rdi
	callq	free
	movq	96(%rsp), %rdi                  # 8-byte Reload
	callq	free
	xorl	%eax, %eax
.LBB1_27:
	addq	$168, %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
.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	$_Z13compute_xnextPdS_S_S_S_i, %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	_Z13compute_xnextPdS_S_S_S_i,@object # @_Z13compute_xnextPdS_S_S_S_i
	.section	.rodata,"a",@progbits
	.globl	_Z13compute_xnextPdS_S_S_S_i
	.p2align	3, 0x0
_Z13compute_xnextPdS_S_S_S_i:
	.quad	_Z28__device_stub__compute_xnextPdS_S_S_S_i
	.size	_Z13compute_xnextPdS_S_S_S_i, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"usage: %s [size] [epsilon] [verbose: 1/2]\n"
	.size	.L.str, 43

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"N = %d\nEpsilon = %10.9f\n"
	.size	.L.str.1, 25

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Nombre d'iterations: %d\n"
	.size	.L.str.2, 25

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Error compared to sol.: %25.24f\n\n"
	.size	.L.str.3, 34

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Temps d'alloc. device   : %f s\n"
	.size	.L.str.4, 32

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Temps de transfert init.: %f s\n"
	.size	.L.str.5, 32

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Temps de calcul:          %f s\n"
	.size	.L.str.6, 32

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"%d, %12.11f, %d, %f\n"
	.size	.L.str.7, 21

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z13compute_xnextPdS_S_S_S_i"
	.size	.L__unnamed_1, 29

	.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 _Z28__device_stub__compute_xnextPdS_S_S_S_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z13compute_xnextPdS_S_S_S_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z13compute_xnextPdS_S_S_S_i ; -- Begin function _Z13compute_xnextPdS_S_S_S_i
	.globl	_Z13compute_xnextPdS_S_S_S_i
	.p2align	8
	.type	_Z13compute_xnextPdS_S_S_S_i,@function
_Z13compute_xnextPdS_S_S_S_i:           ; @_Z13compute_xnextPdS_S_S_S_i
; %bb.0:
	s_clause 0x3
	s_load_b32 s12, s[0:1], 0x28
	s_load_b64 s[2:3], s[0:1], 0x20
	s_load_b256 s[4:11], s[0:1], 0x0
	s_load_b32 s1, s[0:1], 0x3c
	v_lshlrev_b32_e32 v3, 5, v0
	s_mov_b32 s13, exec_lo
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e64 s12, v3
	s_cbranch_execz .LBB0_3
; %bb.1:                                ; %.lr.ph.preheader
	v_add_nc_u32_e32 v1, 32, v3
	v_lshlrev_b32_e32 v5, 8, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_min_i32_e32 v4, s12, v1
	v_add_co_u32 v1, s0, s8, v5
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v2, null, s9, 0, s0
	v_add_nc_u32_e32 v5, 0, v5
	s_mov_b32 s8, 0
.LBB0_2:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	global_load_b64 v[6:7], v[1:2], off
	v_add_nc_u32_e32 v3, 1, v3
	v_add_co_u32 v1, vcc_lo, v1, 8
	v_add_co_ci_u32_e32 v2, vcc_lo, 0, v2, vcc_lo
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ge_i32_e64 s0, v3, v4
	s_or_b32 s8, s0, s8
	s_waitcnt vmcnt(0)
	ds_store_b64 v5, v[6:7]
	v_add_nc_u32_e32 v5, 8, v5
	s_and_not1_b32 exec_lo, exec_lo, s8
	s_cbranch_execnz .LBB0_2
.LBB0_3:                                ; %Flow72
	s_or_b32 exec_lo, exec_lo, s13
	s_and_b32 s0, 0xffff, s1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s0, v[0:1]
	s_mov_b32 s0, exec_lo
	v_cmpx_gt_i32_e64 s12, v1
	s_cbranch_execz .LBB0_9
; %bb.4:
	s_cmp_lt_i32 s12, 1
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_7
; %bb.5:                                ; %.lr.ph41
	v_mul_lo_u32 v2, v1, s12
	s_mov_b32 s0, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v3, 31, v2
	v_lshlrev_b64 v[5:6], 3, v[2:3]
	v_mov_b32_e32 v3, 0
	v_mov_b32_e32 v4, 0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_u32 v5, vcc_lo, s4, v5
	v_add_co_ci_u32_e32 v6, vcc_lo, s5, v6, vcc_lo
.LBB0_6:                                ; =>This Inner Loop Header: Depth=1
	global_load_b64 v[7:8], v[5:6], off
	v_mov_b32_e32 v0, s0
	v_add_co_u32 v5, vcc_lo, v5, 8
	v_add_co_ci_u32_e32 v6, vcc_lo, 0, v6, vcc_lo
	ds_load_b64 v[9:10], v0
	s_add_i32 s12, s12, -1
	s_add_i32 s0, s0, 8
	s_cmp_lg_u32 s12, 0
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_fma_f64 v[3:4], v[9:10], v[7:8], v[3:4]
	s_cbranch_scc1 .LBB0_6
	s_branch .LBB0_8
.LBB0_7:
	v_mov_b32_e32 v3, 0
	v_mov_b32_e32 v4, 0
.LBB0_8:                                ; %._crit_edge42
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 3, v[1:2]
	v_add_co_u32 v5, vcc_lo, s2, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v6, vcc_lo, s3, v1, vcc_lo
	v_add_co_u32 v7, vcc_lo, s6, v0
	v_add_co_ci_u32_e32 v8, vcc_lo, s7, v1, vcc_lo
	global_load_b64 v[5:6], v[5:6], off
	global_load_b64 v[7:8], v[7:8], off
	s_waitcnt vmcnt(1)
	v_add_f64 v[2:3], v[5:6], -v[3:4]
	s_waitcnt vmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_scale_f64 v[4:5], null, v[7:8], v[7:8], v[2:3]
	v_rcp_f64_e32 v[9:10], v[4:5]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[11:12], -v[4:5], v[9:10], 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[9:10], v[9:10], v[11:12], v[9:10]
	v_fma_f64 v[11:12], -v[4:5], v[9:10], 1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_fma_f64 v[9:10], v[9:10], v[11:12], v[9:10]
	v_div_scale_f64 v[11:12], vcc_lo, v[2:3], v[7:8], v[2:3]
	v_mul_f64 v[13:14], v[11:12], v[9:10]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[4:5], -v[4:5], v[13:14], v[11:12]
	v_div_fmas_f64 v[4:5], v[4:5], v[9:10], v[13:14]
	v_add_co_u32 v0, vcc_lo, s10, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s11, v1, vcc_lo
	s_delay_alu instid0(VALU_DEP_3)
	v_div_fixup_f64 v[2:3], v[4:5], v[7:8], v[2:3]
	global_store_b64 v[0:1], v[2:3], off
.LBB0_9:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z13compute_xnextPdS_S_S_S_i
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 304
		.amdhsa_user_sgpr_count 15
		.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 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 15
		.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	_Z13compute_xnextPdS_S_S_S_i, .Lfunc_end0-_Z13compute_xnextPdS_S_S_S_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 576
; NumSgprs: 18
; NumVgprs: 15
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 15
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.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
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         32
        .size:           8
        .value_kind:     global_buffer
      - .offset:         40
        .size:           4
        .value_kind:     by_value
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         52
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         60
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         62
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         64
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         66
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         68
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         70
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         112
        .size:           2
        .value_kind:     hidden_grid_dims
      - .offset:         168
        .size:           4
        .value_kind:     hidden_dynamic_lds_size
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 304
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z13compute_xnextPdS_S_S_S_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z13compute_xnextPdS_S_S_S_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     15
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
