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


// Maximum value of the matrix element
#define MAX 100

#define MAX_ITER 100
#define TOL 0.000001




// Generate a random float number with the maximum value of max
float rand_float(int max) {
	return ((float)rand() / (float)(RAND_MAX)) * max;
}




// Calulates the space for the matrices (bytes)
int calc_mem_size(int n, int m) {
	return (n * m * sizeof(float));
}




// Calculates the sum of a given flat matrix (array)
float calc_mat_sum(float **mat, int n, int m) {

	float sum = 0.0f;
	for (long i = 0; i < (n*m); i++) {
		sum += (*mat)[i];
	}

	return sum;
}




// Allocate 2D matrix in the host
void alloc_host_matrix(float **mat, int n, int m, bool must_init) {

	*mat = (float *) malloc(n * m * sizeof(float));

	// In case of initializing the matrix with the initial values
	if (must_init) {
		for (int i = 0; i < (n*m); i++) {
			(*mat)[i] = rand_float(MAX);
		}
	}
}




// Allocate 2D matrix in the device
void alloc_dev_matrix(float **mat, int n, int m) {
	size_t memSize = (n * m * sizeof(float));
	cudaMalloc(&mat, memSize);
}




// Write the time results into a CSV file
void write_to_file(int n, int num_blocks, int num_threads, float total_time, float exec_time) {

	FILE *f;
	char* file_name = "results.csv";

	if (access(file_name, F_OK) == -1) {
 		f = fopen(file_name, "a");
		fprintf(f, "Matrix size;Blocks;Threads per block;Total time;Operations time;\n");
	}
	else {
		f = fopen(file_name, "a");
	}

	fprintf(f, "%d;%d;%d;%f;%f;\n", n, num_blocks, num_threads, total_time, exec_time);
	fclose(f);
}




// Solver (executed by each thread)
__global__ void solver(float **mat, float **mat_diff, int n) {

	// Original position that this thread is assigned
	int i_org = (blockDim.x * blockIdx.x) + threadIdx.x;

	// Real position that this thread is going to compute
	int i = i_org;
	i = i + n;	// VIP: The threads must avoid first row
	i = i + 1;	// VIP: The threads must avoid first column

	// In case the thread is leftover
	if (i >= ((n*n) - n - 1)) {
		return;
	}


	float temp;
	float diff = 0;
	int cnt_iter = 0;

	const int pos_up = i - n;
	const int pos_do = i + n;
	const int pos_le = i - 1;
	const int pos_ri = i + 1;

	while (cnt_iter < MAX_ITER) {

		temp = (*mat)[i];
		(*mat)[i] = 0.2 * ((*mat)[i] + (*mat)[pos_le] + (*mat)[pos_up] + (*mat)[pos_ri] + (*mat)[pos_do]);

		// The LAST difference between the prev value and the new value is stored 
		diff = abs((*mat)[i] - temp);
		cnt_iter ++;
	}

	// Finally the difference is store in its corresponding cell
	// VIP: Use '=' not '+=' to avoid non-zero values on the first func call
	(*mat_diff)[i_org] = diff;
}




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

	if (argc < 3) {
		printf("Call this program with two parameters:\n");
		printf("\t matrix_size: Add 2 to a power of 2 (e.g. : 18, 1026)\n");
		printf("\t threads_per_block: Better a power of 2 (e.g. : 16, 32, 64)\n");
		exit(1);
	}

	int n = atoi(argv[1]);
	int threads_per_block = atoi(argv[2]);
	printf("Matrix size = %d\n", n);
	printf("Threads per block = %d\n", threads_per_block);


	// Start recording the time
	clock_t i_total_t = clock();

	float *host_mat_vals;
	float *host_mat_diff;
	float *dev_mat_vals;
	float *dev_mat_diff;

	// Allocating matrices space both in host and device
	alloc_host_matrix(&host_mat_vals, n, n, true);
	alloc_host_matrix(&host_mat_diff, n-2, n-2, false);
	alloc_dev_matrix(&dev_mat_vals, n, n);
	alloc_dev_matrix(&dev_mat_diff, n-2, n-2);


	// Calculating the memory size for allocating (bytes)
	size_t all_mat_size = calc_mem_size(n, n);
	size_t core_mat_size = calc_mem_size(n-2, n-2);

	// Passing data from host to device
	cudaMemcpy(dev_mat_vals, host_mat_vals, all_mat_size, cudaMemcpyHostToDevice);


	// Calculate the number of threads to launch (1 per core cell)
	int core_dim = (n-2) * (n-2);

	// Given a constant number of threads per block, determine the blocks
	int num_blocks = (int) ceil(core_dim / threads_per_block);
	dim3 dimGrid(num_blocks);
	dim3 dimBlock(threads_per_block);


	// Time before the execution
	clock_t i_exec_t = clock();
 
	while (true) {

		// Make all the threads synchronous
		solver<<< dimGrid, dimBlock >>>(&dev_mat_vals, &dev_mat_diff, n);

		// The ThreadSynchronize would be neccesary in case Memcpy is not done
		// However, as it is called later on, the following line is commented
		// cudaThreadSynchronize();

		// Passing the differential data back from the device to the host
		cudaMemcpy(host_mat_diff, dev_mat_diff, core_mat_size, cudaMemcpyDeviceToHost);

		// Breaks in case of reaching the TOL threshold
		float diffs_sum = calc_mat_sum(&host_mat_diff, n-2, n-2);
		float diffs_mean = diffs_sum / (n-2) / (n-2);
		if (diffs_mean < TOL) {
			break;
		}
	}

	// Time before the execution
	clock_t f_exec_t = clock();


	// Passing data back from the device to the host
	cudaMemcpy(host_mat_vals, dev_mat_vals, all_mat_size, cudaMemcpyDeviceToHost);


	// Finally, the matrices are freed
	cudaFree(dev_mat_vals);
	cudaFree(dev_mat_diff);
	free(host_mat_vals);
	free(host_mat_diff);


	// Finish recording the time
	clock_t f_total_t = clock();


	float total_time = (float)(f_total_t - i_total_t) / CLOCKS_PER_SEC;
	float exec_time = (float)(f_exec_t - i_exec_t) / CLOCKS_PER_SEC;
	printf("Total time: %f\n", total_time);
	printf("Operations time: %f\n", exec_time);


	write_to_file(n, num_blocks, threads_per_block, total_time, exec_time);
	return 0;
}

	.file	"tmpxft_00323531_00000000-6_gs_cuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2049:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2049:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z10rand_floati
	.type	_Z10rand_floati, @function
_Z10rand_floati:
.LFB2040:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	%edi, %ebx
	call	rand@PLT
	cvtsi2ssl	%ebx, %xmm1
	popq	%rbx
	.cfi_def_cfa_offset 8
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	mulss	%xmm1, %xmm0
	ret
	.cfi_endproc
.LFE2040:
	.size	_Z10rand_floati, .-_Z10rand_floati
	.globl	_Z13calc_mem_sizeii
	.type	_Z13calc_mem_sizeii, @function
_Z13calc_mem_sizeii:
.LFB2041:
	.cfi_startproc
	endbr64
	imull	%esi, %edi
	leal	0(,%rdi,4), %eax
	ret
	.cfi_endproc
.LFE2041:
	.size	_Z13calc_mem_sizeii, .-_Z13calc_mem_sizeii
	.globl	_Z12calc_mat_sumPPfii
	.type	_Z12calc_mat_sumPPfii, @function
_Z12calc_mat_sumPPfii:
.LFB2042:
	.cfi_startproc
	endbr64
	imull	%edx, %esi
	xorl	%eax, %eax
	xorps	%xmm0, %xmm0
	movslq	%esi, %rsi
.L6:
	cmpq	%rax, %rsi
	jle	.L9
	movq	(%rdi), %rdx
	addss	(%rdx,%rax,4), %xmm0
	incq	%rax
	jmp	.L6
.L9:
	ret
	.cfi_endproc
.LFE2042:
	.size	_Z12calc_mat_sumPPfii, .-_Z12calc_mat_sumPPfii
	.globl	_Z17alloc_host_matrixPPfiib
	.type	_Z17alloc_host_matrixPPfiib, @function
_Z17alloc_host_matrixPPfiib:
.LFB2043:
	.cfi_startproc
	endbr64
	imull	%edx, %esi
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movslq	%esi, %rdi
	movl	%ecx, %ebx
	pushq	%r8
	.cfi_def_cfa_offset 48
	movq	%rdi, %rbp
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rax, (%r12)
	testb	%bl, %bl
	je	.L10
	xorl	%ebx, %ebx
.L11:
	cmpl	%ebx, %ebp
	jle	.L10
	movq	(%r12), %rax
	movl	$100, %edi
	leaq	(%rax,%rbx,4), %r13
	incq	%rbx
	call	_Z10rand_floati
	movss	%xmm0, 0(%r13)
	jmp	.L11
.L10:
	popq	%rax
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2043:
	.size	_Z17alloc_host_matrixPPfiib, .-_Z17alloc_host_matrixPPfiib
	.globl	_Z16alloc_dev_matrixPPfii
	.type	_Z16alloc_dev_matrixPPfii, @function
_Z16alloc_dev_matrixPPfii:
.LFB2044:
	.cfi_startproc
	endbr64
	imull	%edx, %esi
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movq	%rdi, 8(%rsp)
	leaq	8(%rsp), %rdi
	movslq	%esi, %rsi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2044:
	.size	_Z16alloc_dev_matrixPPfii, .-_Z16alloc_dev_matrixPPfii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC2:
	.string	"results.csv"
.LC3:
	.string	"a"
.LC4:
	.string	"Matrix size;Blocks;Threads per block;Total time;Operations time;\n"
.LC5:
	.string	"%d;%d;%d;%f;%f;\n"
	.text
	.globl	_Z13write_to_fileiiiff
	.type	_Z13write_to_fileiiiff, @function
_Z13write_to_fileiiiff:
.LFB2045:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movd	%xmm1, %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movd	%xmm0, %r14d
	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
	xorl	%esi, %esi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%edi, %ebp
	leaq	.LC2(%rip), %rdi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	pushq	%rcx
	.cfi_def_cfa_offset 64
	call	access@PLT
	leaq	.LC3(%rip), %rsi
	leaq	.LC2(%rip), %rdi
	incl	%eax
	jne	.L19
	call	fopen@PLT
	leaq	.LC4(%rip), %rdx
	movl	$2, %esi
	movq	%rax, %rbx
	movq	%rax, %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L20
.L19:
	call	fopen@PLT
	movq	%rax, %rbx
.L20:
	movd	%r14d, %xmm3
	movd	%r15d, %xmm2
	movq	%rbx, %rdi
	movl	%r13d, %r9d
	movl	%r12d, %r8d
	movl	%ebp, %ecx
	leaq	.LC5(%rip), %rdx
	movb	$2, %al
	movl	$2, %esi
	cvtss2sd	%xmm3, %xmm0
	cvtss2sd	%xmm2, %xmm1
	call	__fprintf_chk@PLT
	popq	%rax
	.cfi_def_cfa_offset 56
	movq	%rbx, %rdi
	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
	jmp	fclose@PLT
	.cfi_endproc
.LFE2045:
	.size	_Z13write_to_fileiiiff, .-_Z13write_to_fileiiiff
	.globl	_Z30__device_stub__Z6solverPPfS0_iPPfS0_i
	.type	_Z30__device_stub__Z6solverPPfS0_iPPfS0_i, @function
_Z30__device_stub__Z6solverPPfS0_iPPfS0_i:
.LFB2071:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L22
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z6solverPPfS0_i(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L22:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L24
	call	__stack_chk_fail@PLT
.L24:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2071:
	.size	_Z30__device_stub__Z6solverPPfS0_iPPfS0_i, .-_Z30__device_stub__Z6solverPPfS0_iPPfS0_i
	.globl	_Z6solverPPfS0_i
	.type	_Z6solverPPfS0_i, @function
_Z6solverPPfS0_i:
.LFB2072:
	.cfi_startproc
	endbr64
	jmp	_Z30__device_stub__Z6solverPPfS0_iPPfS0_i
	.cfi_endproc
.LFE2072:
	.size	_Z6solverPPfS0_i, .-_Z6solverPPfS0_i
	.section	.rodata.str1.1
.LC6:
	.string	"Call this program with two parameters:\n"
.LC7:
	.string	"\t matrix_size: Add 2 to a power of 2 (e.g. : 18, 1026)\n"
.LC8:
	.string	"\t threads_per_block: Better a power of 2 (e.g. : 16, 32, 64)\n"
.LC9:
	.string	"Matrix size = %d\n"
.LC10:
	.string	"Threads per block = %d\n"
.LC13:
	.string	"Total time: %f\n"
.LC14:
	.string	"Operations time: %f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2046:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	cmpl	$2, %edi
	jg	.L28
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$2, %edi
	leaq	.LC8(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L28:
	movq	8(%rsi), %rdi
	movq	%rsi, %rbp
	call	atoi@PLT
	movq	16(%rbp), %rdi
	movl	%eax, %ebx
	call	atoi@PLT
	movl	%ebx, %edx
	movl	$2, %edi
	leal	-2(%rbx), %ebp
	movl	%eax, %r12d
	leaq	.LC9(%rip), %rsi
	xorl	%eax, %eax
	movl	%ebp, %r13d
	call	__printf_chk@PLT
	movl	%r12d, %edx
	movl	$2, %edi
	xorl	%eax, %eax
	imull	%ebp, %r13d
	leaq	.LC10(%rip), %rsi
	call	__printf_chk@PLT
	call	clock@PLT
	leaq	48(%rsp), %rdi
	movl	%ebx, %edx
	movl	%ebx, %esi
	movl	$1, %ecx
	movq	%rax, %r14
	call	_Z17alloc_host_matrixPPfiib
	leaq	56(%rsp), %rax
	xorl	%ecx, %ecx
	movl	%ebp, %edx
	movq	%rax, %rdi
	movl	%ebp, %esi
	movq	%rax, 24(%rsp)
	call	_Z17alloc_host_matrixPPfiib
	leaq	64(%rsp), %rdi
	movl	%ebx, %edx
	movl	%ebx, %esi
	call	_Z16alloc_dev_matrixPPfii
	leaq	72(%rsp), %rdi
	movl	%ebp, %edx
	movl	%ebp, %esi
	call	_Z16alloc_dev_matrixPPfii
	movl	%ebx, %eax
	movq	64(%rsp), %rdi
	movl	$1, %ecx
	imull	%ebx, %eax
	sall	$2, %eax
	cltq
	movq	%rax, 16(%rsp)
	leal	0(,%r13,4), %eax
	movq	16(%rsp), %rdx
	cltq
	movq	%rax, 40(%rsp)
	movq	48(%rsp), %rax
	movq	%rax, %rsi
	movq	%rax, 8(%rsp)
	call	cudaMemcpy@PLT
	movl	%r13d, %eax
	movl	%r12d, 92(%rsp)
	cltd
	idivl	%r12d
	movl	%eax, 36(%rsp)
	movl	%eax, 80(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 84(%rsp)
	movq	%rax, 96(%rsp)
	call	clock@PLT
	movq	%rax, %r13
.L30:
	movl	100(%rsp), %ecx
	movq	92(%rsp), %rdx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	80(%rsp), %rdi
	movl	88(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L29
	leaq	72(%rsp), %rsi
	leaq	64(%rsp), %rdi
	movl	%ebx, %edx
	call	_Z30__device_stub__Z6solverPPfS0_iPPfS0_i
.L29:
	movq	56(%rsp), %r15
	movq	40(%rsp), %rdx
	movl	$2, %ecx
	movq	72(%rsp), %rsi
	movq	%r15, %rdi
	call	cudaMemcpy@PLT
	cvtsi2ssl	%ebp, %xmm1
	movq	24(%rsp), %rdi
	movl	%ebp, %edx
	movl	%ebp, %esi
	call	_Z12calc_mat_sumPPfii
	movsd	.LC11(%rip), %xmm3
	divss	%xmm1, %xmm0
	divss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	comisd	%xmm0, %xmm3
	jbe	.L30
	call	clock@PLT
	movq	16(%rsp), %rdx
	movq	64(%rsp), %rsi
	movl	$2, %ecx
	movq	8(%rsp), %rdi
	movq	%rax, %rbp
	subq	%r13, %rbp
	call	cudaMemcpy@PLT
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rdi
	call	cudaFree@PLT
	movq	8(%rsp), %rdi
	call	free@PLT
	movq	%r15, %rdi
	call	free@PLT
	call	clock@PLT
	cvtsi2ssq	%rbp, %xmm1
	movl	$2, %edi
	movss	.LC12(%rip), %xmm2
	subq	%r14, %rax
	leaq	.LC13(%rip), %rsi
	cvtsi2ssq	%rax, %xmm0
	movb	$1, %al
	divss	%xmm2, %xmm1
	divss	%xmm2, %xmm0
	movss	%xmm1, 8(%rsp)
	movd	%xmm0, %r14d
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movss	8(%rsp), %xmm1
	leaq	.LC14(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	cvtss2sd	%xmm1, %xmm0
	call	__printf_chk@PLT
	movl	36(%rsp), %esi
	movl	%r12d, %edx
	movss	8(%rsp), %xmm1
	movd	%r14d, %xmm0
	movl	%ebx, %edi
	call	_Z13write_to_fileiiiff
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L32
	call	__stack_chk_fail@PLT
.L32:
	addq	$120, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2046:
	.size	main, .-main
	.section	.rodata.str1.1
.LC15:
	.string	"_Z6solverPPfS0_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2074:
	.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	.LC15(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z6solverPPfS0_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
.LFE2074:
	.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	805306368
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC11:
	.long	-1598689907
	.long	1051772663
	.section	.rodata.cst4
	.align 4
.LC12:
	.long	1232348160
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <math.h>
#include <time.h>


// Maximum value of the matrix element
#define MAX 100

#define MAX_ITER 100
#define TOL 0.000001




// Generate a random float number with the maximum value of max
float rand_float(int max) {
	return ((float)rand() / (float)(RAND_MAX)) * max;
}




// Calulates the space for the matrices (bytes)
int calc_mem_size(int n, int m) {
	return (n * m * sizeof(float));
}




// Calculates the sum of a given flat matrix (array)
float calc_mat_sum(float **mat, int n, int m) {

	float sum = 0.0f;
	for (long i = 0; i < (n*m); i++) {
		sum += (*mat)[i];
	}

	return sum;
}




// Allocate 2D matrix in the host
void alloc_host_matrix(float **mat, int n, int m, bool must_init) {

	*mat = (float *) malloc(n * m * sizeof(float));

	// In case of initializing the matrix with the initial values
	if (must_init) {
		for (int i = 0; i < (n*m); i++) {
			(*mat)[i] = rand_float(MAX);
		}
	}
}




// Allocate 2D matrix in the device
void alloc_dev_matrix(float **mat, int n, int m) {
	size_t memSize = (n * m * sizeof(float));
	hipMalloc(&mat, memSize);
}




// Write the time results into a CSV file
void write_to_file(int n, int num_blocks, int num_threads, float total_time, float exec_time) {

	FILE *f;
	char* file_name = "results.csv";

	if (access(file_name, F_OK) == -1) {
 		f = fopen(file_name, "a");
		fprintf(f, "Matrix size;Blocks;Threads per block;Total time;Operations time;\n");
	}
	else {
		f = fopen(file_name, "a");
	}

	fprintf(f, "%d;%d;%d;%f;%f;\n", n, num_blocks, num_threads, total_time, exec_time);
	fclose(f);
}




// Solver (executed by each thread)
__global__ void solver(float **mat, float **mat_diff, int n) {

	// Original position that this thread is assigned
	int i_org = (blockDim.x * blockIdx.x) + threadIdx.x;

	// Real position that this thread is going to compute
	int i = i_org;
	i = i + n;	// VIP: The threads must avoid first row
	i = i + 1;	// VIP: The threads must avoid first column

	// In case the thread is leftover
	if (i >= ((n*n) - n - 1)) {
		return;
	}


	float temp;
	float diff = 0;
	int cnt_iter = 0;

	const int pos_up = i - n;
	const int pos_do = i + n;
	const int pos_le = i - 1;
	const int pos_ri = i + 1;

	while (cnt_iter < MAX_ITER) {

		temp = (*mat)[i];
		(*mat)[i] = 0.2 * ((*mat)[i] + (*mat)[pos_le] + (*mat)[pos_up] + (*mat)[pos_ri] + (*mat)[pos_do]);

		// The LAST difference between the prev value and the new value is stored 
		diff = abs((*mat)[i] - temp);
		cnt_iter ++;
	}

	// Finally the difference is store in its corresponding cell
	// VIP: Use '=' not '+=' to avoid non-zero values on the first func call
	(*mat_diff)[i_org] = diff;
}




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

	if (argc < 3) {
		printf("Call this program with two parameters:\n");
		printf("\t matrix_size: Add 2 to a power of 2 (e.g. : 18, 1026)\n");
		printf("\t threads_per_block: Better a power of 2 (e.g. : 16, 32, 64)\n");
		exit(1);
	}

	int n = atoi(argv[1]);
	int threads_per_block = atoi(argv[2]);
	printf("Matrix size = %d\n", n);
	printf("Threads per block = %d\n", threads_per_block);


	// Start recording the time
	clock_t i_total_t = clock();

	float *host_mat_vals;
	float *host_mat_diff;
	float *dev_mat_vals;
	float *dev_mat_diff;

	// Allocating matrices space both in host and device
	alloc_host_matrix(&host_mat_vals, n, n, true);
	alloc_host_matrix(&host_mat_diff, n-2, n-2, false);
	alloc_dev_matrix(&dev_mat_vals, n, n);
	alloc_dev_matrix(&dev_mat_diff, n-2, n-2);


	// Calculating the memory size for allocating (bytes)
	size_t all_mat_size = calc_mem_size(n, n);
	size_t core_mat_size = calc_mem_size(n-2, n-2);

	// Passing data from host to device
	hipMemcpy(dev_mat_vals, host_mat_vals, all_mat_size, hipMemcpyHostToDevice);


	// Calculate the number of threads to launch (1 per core cell)
	int core_dim = (n-2) * (n-2);

	// Given a constant number of threads per block, determine the blocks
	int num_blocks = (int) ceil(core_dim / threads_per_block);
	dim3 dimGrid(num_blocks);
	dim3 dimBlock(threads_per_block);


	// Time before the execution
	clock_t i_exec_t = clock();
 
	while (true) {

		// Make all the threads synchronous
		solver<<< dimGrid, dimBlock >>>(&dev_mat_vals, &dev_mat_diff, n);

		// The ThreadSynchronize would be neccesary in case Memcpy is not done
		// However, as it is called later on, the following line is commented
		// cudaThreadSynchronize();

		// Passing the differential data back from the device to the host
		hipMemcpy(host_mat_diff, dev_mat_diff, core_mat_size, hipMemcpyDeviceToHost);

		// Breaks in case of reaching the TOL threshold
		float diffs_sum = calc_mat_sum(&host_mat_diff, n-2, n-2);
		float diffs_mean = diffs_sum / (n-2) / (n-2);
		if (diffs_mean < TOL) {
			break;
		}
	}

	// Time before the execution
	clock_t f_exec_t = clock();


	// Passing data back from the device to the host
	hipMemcpy(host_mat_vals, dev_mat_vals, all_mat_size, hipMemcpyDeviceToHost);


	// Finally, the matrices are freed
	hipFree(dev_mat_vals);
	hipFree(dev_mat_diff);
	free(host_mat_vals);
	free(host_mat_diff);


	// Finish recording the time
	clock_t f_total_t = clock();


	float total_time = (float)(f_total_t - i_total_t) / CLOCKS_PER_SEC;
	float exec_time = (float)(f_exec_t - i_exec_t) / CLOCKS_PER_SEC;
	printf("Total time: %f\n", total_time);
	printf("Operations time: %f\n", exec_time);


	write_to_file(n, num_blocks, threads_per_block, total_time, exec_time);
	return 0;
}

	.text
	.file	"gs_cuda.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z10rand_floati
.LCPI0_0:
	.long	0x30000000                      # float 4.65661287E-10
	.text
	.globl	_Z10rand_floati
	.type	_Z10rand_floati,@function
_Z10rand_floati:                        # @_Z10rand_floati
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%edi, %ebx
	callq	rand
	cvtsi2ss	%eax, %xmm1
	cvtsi2ss	%ebx, %xmm0
	mulss	.LCPI0_0(%rip), %xmm1
	mulss	%xmm1, %xmm0
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z10rand_floati, .Lfunc_end0-_Z10rand_floati
	.cfi_endproc
                                        # -- End function
	.globl	_Z13calc_mem_sizeii             # -- Begin function _Z13calc_mem_sizeii
	.type	_Z13calc_mem_sizeii,@function
_Z13calc_mem_sizeii:                    # @_Z13calc_mem_sizeii
	.cfi_startproc
# %bb.0:
                                        # kill: def $edi killed $edi def $rdi
	imull	%esi, %edi
	leal	(,%rdi,4), %eax
	retq
.Lfunc_end1:
	.size	_Z13calc_mem_sizeii, .Lfunc_end1-_Z13calc_mem_sizeii
	.cfi_endproc
                                        # -- End function
	.globl	_Z12calc_mat_sumPPfii           # -- Begin function _Z12calc_mat_sumPPfii
	.type	_Z12calc_mat_sumPPfii,@function
_Z12calc_mat_sumPPfii:                  # @_Z12calc_mat_sumPPfii
	.cfi_startproc
# %bb.0:
	imull	%edx, %esi
	testl	%esi, %esi
	jle	.LBB2_1
# %bb.3:                                # %.lr.ph
	movslq	%esi, %rax
	movq	(%rdi), %rcx
	xorps	%xmm0, %xmm0
	xorl	%edx, %edx
.LBB2_4:                                # =>This Inner Loop Header: Depth=1
	addss	(%rcx,%rdx,4), %xmm0
	incq	%rdx
	cmpq	%rdx, %rax
	jne	.LBB2_4
# %bb.2:                                # %._crit_edge
	retq
.LBB2_1:
	xorps	%xmm0, %xmm0
	retq
.Lfunc_end2:
	.size	_Z12calc_mat_sumPPfii, .Lfunc_end2-_Z12calc_mat_sumPPfii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z17alloc_host_matrixPPfiib
.LCPI3_0:
	.long	0x30000000                      # float 4.65661287E-10
.LCPI3_1:
	.long	0x42c80000                      # float 100
	.text
	.globl	_Z17alloc_host_matrixPPfiib
	.type	_Z17alloc_host_matrixPPfiib,@function
_Z17alloc_host_matrixPPfiib:            # @_Z17alloc_host_matrixPPfiib
	.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	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%ecx, %r14d
	movl	%esi, %ebp
	movq	%rdi, %rbx
	imull	%edx, %ebp
	movslq	%ebp, %r15
	leaq	(,%r15,4), %rdi
	callq	malloc
	movq	%rax, (%rbx)
	testl	%r15d, %r15d
	jle	.LBB3_4
# %bb.1:
	testb	%r14b, %r14b
	je	.LBB3_4
# %bb.2:                                # %.lr.ph.preheader
	movl	%ebp, %r14d
	xorl	%r15d, %r15d
.LBB3_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI3_0(%rip), %xmm0
	mulss	.LCPI3_1(%rip), %xmm0
	movq	(%rbx), %rax
	movss	%xmm0, (%rax,%r15,4)
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB3_3
.LBB3_4:                                # %.loopexit
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.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_end3:
	.size	_Z17alloc_host_matrixPPfiib, .Lfunc_end3-_Z17alloc_host_matrixPPfiib
	.cfi_endproc
                                        # -- End function
	.globl	_Z16alloc_dev_matrixPPfii       # -- Begin function _Z16alloc_dev_matrixPPfii
	.type	_Z16alloc_dev_matrixPPfii,@function
_Z16alloc_dev_matrixPPfii:              # @_Z16alloc_dev_matrixPPfii
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rsp, %rax
	movq	%rdi, (%rax)
	imull	%edx, %esi
	movslq	%esi, %rsi
	shlq	$2, %rsi
	movq	%rax, %rdi
	callq	hipMalloc
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z16alloc_dev_matrixPPfii, .Lfunc_end4-_Z16alloc_dev_matrixPPfii
	.cfi_endproc
                                        # -- End function
	.globl	_Z13write_to_fileiiiff          # -- Begin function _Z13write_to_fileiiiff
	.type	_Z13write_to_fileiiiff,@function
_Z13write_to_fileiiiff:                 # @_Z13write_to_fileiiiff
	.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	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movss	%xmm1, 12(%rsp)                 # 4-byte Spill
	movss	%xmm0, 8(%rsp)                  # 4-byte Spill
	movl	%edx, %ebx
	movl	%esi, %ebp
	movl	%edi, %r14d
	movl	$.L.str, %edi
	xorl	%esi, %esi
	callq	access
	movl	%eax, %r12d
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %r15
	cmpl	$-1, %r12d
	jne	.LBB5_2
# %bb.1:
	movl	$.L.str.2, %edi
	movl	$65, %esi
	movl	$1, %edx
	movq	%r15, %rcx
	callq	fwrite@PLT
.LBB5_2:
	xorps	%xmm0, %xmm0
	cvtss2sd	8(%rsp), %xmm0                  # 4-byte Folded Reload
	xorps	%xmm1, %xmm1
	cvtss2sd	12(%rsp), %xmm1                 # 4-byte Folded Reload
	movl	$.L.str.3, %esi
	movq	%r15, %rdi
	movl	%r14d, %edx
	movl	%ebp, %ecx
	movl	%ebx, %r8d
	movb	$2, %al
	callq	fprintf
	movq	%r15, %rdi
	addq	$16, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.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	fclose                          # TAILCALL
.Lfunc_end5:
	.size	_Z13write_to_fileiiiff, .Lfunc_end5-_Z13write_to_fileiiiff
	.cfi_endproc
                                        # -- End function
	.globl	_Z21__device_stub__solverPPfS0_i # -- Begin function _Z21__device_stub__solverPPfS0_i
	.type	_Z21__device_stub__solverPPfS0_i,@function
_Z21__device_stub__solverPPfS0_i:       # @_Z21__device_stub__solverPPfS0_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	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%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	$_Z6solverPPfS0_i, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end6:
	.size	_Z21__device_stub__solverPPfS0_i, .Lfunc_end6-_Z21__device_stub__solverPPfS0_i
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI7_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0
.LCPI7_1:
	.long	0x49742400                      # float 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	$88, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$2, %edi
	jle	.LBB7_9
# %bb.1:
	movq	%rsi, %rbx
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %r14d
	movq	16(%rbx), %rdi
	callq	atoi
	movl	%eax, %ebx
	movl	%eax, 4(%rsp)                   # 4-byte Spill
	movl	$.L.str.7, %edi
	movl	%r14d, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.8, %edi
	movl	%ebx, %esi
	xorl	%eax, %eax
	callq	printf
	callq	clock
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	leaq	72(%rsp), %rdi
	movl	%r14d, %esi
	movl	%r14d, %edx
	movl	$1, %ecx
	callq	_Z17alloc_host_matrixPPfiib
	leal	-2(%r14), %ebx
	movl	%ebx, %r13d
	imull	%r13d, %r13d
	leaq	(,%r13,4), %r12
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %r15
	leaq	8(%rsp), %rax
	leaq	80(%rsp), %rbp
	movq	%rax, (%rbp)
	movq	%r14, 32(%rsp)                  # 8-byte Spill
                                        # kill: def $r14d killed $r14d killed $r14 def $r14
	imull	%r14d, %r14d
	leaq	(,%r14,4), %rsi
	movq	%rbp, %rdi
	callq	hipMalloc
	leaq	24(%rsp), %rax
	movq	%rax, (%rbp)
	leaq	80(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	shll	$2, %r14d
	movslq	%r14d, %rdx
	leal	(,%r13,4), %eax
	movslq	%eax, %r12
	leaq	8(%rsp), %rax
	movq	(%rax), %rdi
	leaq	72(%rsp), %rax
	movq	(%rax), %rsi
	movq	%rsi, 56(%rsp)                  # 8-byte Spill
	movq	%rdx, 40(%rsp)                  # 8-byte Spill
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%r13d, %eax
	cltd
	movl	4(%rsp), %ecx                   # 4-byte Reload
	idivl	%ecx
	movl	%eax, %r14d
	movq	%r14, 64(%rsp)                  # 8-byte Spill
	btsq	$32, %r14
	movl	%ecx, %ebp
	btsq	$32, %rbp
	callq	clock
	movq	%rax, 48(%rsp)                  # 8-byte Spill
	cvtsi2ss	%ebx, %xmm0
	movss	%xmm0, (%rsp)                   # 4-byte Spill
.LBB7_2:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB7_6 Depth 2
	movq	%r14, %rdi
	movl	$1, %esi
	movq	%rbp, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB7_4
# %bb.3:                                #   in Loop: Header=BB7_2 Depth=1
	leaq	8(%rsp), %rdi
	leaq	24(%rsp), %rsi
	movq	32(%rsp), %rdx                  # 8-byte Reload
                                        # kill: def $edx killed $edx killed $rdx
	callq	_Z21__device_stub__solverPPfS0_i
.LBB7_4:                                #   in Loop: Header=BB7_2 Depth=1
	movq	24(%rsp), %rsi
	movq	%r15, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	xorps	%xmm0, %xmm0
	testl	%ebx, %ebx
	je	.LBB7_7
# %bb.5:                                # %.lr.ph.i.preheader
                                        #   in Loop: Header=BB7_2 Depth=1
	xorl	%eax, %eax
.LBB7_6:                                # %.lr.ph.i
                                        #   Parent Loop BB7_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	addss	(%r15,%rax,4), %xmm0
	incq	%rax
	cmpq	%rax, %r13
	jne	.LBB7_6
.LBB7_7:                                # %_Z12calc_mat_sumPPfii.exit
                                        #   in Loop: Header=BB7_2 Depth=1
	movss	(%rsp), %xmm1                   # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	divss	%xmm1, %xmm0
	divss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	movsd	.LCPI7_0(%rip), %xmm1           # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	jbe	.LBB7_2
# %bb.8:
	callq	clock
	movq	%rax, %r14
	movq	8(%rsp), %rsi
	movq	56(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rdi
	movq	40(%rsp), %rdx                  # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	callq	clock
	subq	16(%rsp), %rax                  # 8-byte Folded Reload
	xorps	%xmm1, %xmm1
	cvtsi2ss	%rax, %xmm1
	subq	48(%rsp), %r14                  # 8-byte Folded Reload
	cvtsi2ss	%r14, %xmm2
	movss	.LCPI7_1(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	divss	%xmm0, %xmm1
	movss	%xmm1, 16(%rsp)                 # 4-byte Spill
	divss	%xmm0, %xmm2
	movss	%xmm2, (%rsp)                   # 4-byte Spill
	xorps	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm0
	movl	$.L.str.9, %edi
	movb	$1, %al
	callq	printf
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rsp), %xmm0                   # 4-byte Folded Reload
	movl	$.L.str.10, %edi
	movb	$1, %al
	callq	printf
	movq	32(%rsp), %rdi                  # 8-byte Reload
                                        # kill: def $edi killed $edi killed $rdi
	movq	64(%rsp), %rsi                  # 8-byte Reload
                                        # kill: def $esi killed $esi killed $rsi
	movl	4(%rsp), %edx                   # 4-byte Reload
	movss	16(%rsp), %xmm0                 # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	movss	(%rsp), %xmm1                   # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	callq	_Z13write_to_fileiiiff
	xorl	%eax, %eax
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB7_9:
	.cfi_def_cfa_offset 144
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end7:
	.size	main, .Lfunc_end7-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	.LBB8_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB8_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z6solverPPfS0_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_end8:
	.size	__hip_module_ctor, .Lfunc_end8-__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	.LBB9_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
.LBB9_2:
	retq
.Lfunc_end9:
	.size	__hip_module_dtor, .Lfunc_end9-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"results.csv"
	.size	.L.str, 12

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

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Matrix size;Blocks;Threads per block;Total time;Operations time;\n"
	.size	.L.str.2, 66

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"%d;%d;%d;%f;%f;\n"
	.size	.L.str.3, 17

	.type	_Z6solverPPfS0_i,@object        # @_Z6solverPPfS0_i
	.section	.rodata,"a",@progbits
	.globl	_Z6solverPPfS0_i
	.p2align	3, 0x0
_Z6solverPPfS0_i:
	.quad	_Z21__device_stub__solverPPfS0_i
	.size	_Z6solverPPfS0_i, 8

	.type	.L.str.7,@object                # @.str.7
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.7:
	.asciz	"Matrix size = %d\n"
	.size	.L.str.7, 18

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Threads per block = %d\n"
	.size	.L.str.8, 24

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Total time: %f\n"
	.size	.L.str.9, 16

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Operations time: %f\n"
	.size	.L.str.10, 21

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z6solverPPfS0_i"
	.size	.L__unnamed_1, 17

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

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Call this program with two parameters:"
	.size	.Lstr, 39

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"\t matrix_size: Add 2 to a power of 2 (e.g. : 18, 1026)"
	.size	.Lstr.1, 55

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"\t threads_per_block: Better a power of 2 (e.g. : 16, 32, 64)"
	.size	.Lstr.2, 61

	.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 _Z21__device_stub__solverPPfS0_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z6solverPPfS0_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z6solverPPfS0_i        ; -- Begin function _Z6solverPPfS0_i
	.globl	_Z6solverPPfS0_i
	.p2align	8
	.type	_Z6solverPPfS0_i,@function
_Z6solverPPfS0_i:                       ; @_Z6solverPPfS0_i
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x24
	s_load_b32 s4, s[0:1], 0x10
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[3:4], null, s15, s2, v[0:1]
	s_add_i32 s2, s4, -1
	s_mul_i32 s2, s2, s4
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	s_add_i32 s2, s2, -1
	v_add_nc_u32_e32 v6, s4, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v0, 1, v6
	v_cmp_gt_i32_e32 vcc_lo, s2, v0
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB0_4
; %bb.1:
	s_load_b128 s[0:3], s[0:1], 0x0
	v_ashrrev_i32_e32 v7, 31, v6
	s_ashr_i32 s5, s4, 31
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[6:7]
	s_waitcnt lgkmcnt(0)
	s_load_b64 s[0:1], s[0:1], 0x0
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v4, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v5, vcc_lo, s1, v1, vcc_lo
	v_sub_co_u32 v6, vcc_lo, v6, s4
	v_subrev_co_ci_u32_e32 v7, vcc_lo, s5, v7, vcc_lo
	global_load_b96 v[0:2], v[4:5], off
	s_lshl_b64 s[4:5], s[4:5], 2
	v_lshlrev_b64 v[6:7], 2, v[6:7]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v6, vcc_lo, s0, v6
	v_add_co_ci_u32_e32 v7, vcc_lo, s1, v7, vcc_lo
	v_add_co_u32 v8, vcc_lo, v4, s4
	v_add_co_ci_u32_e32 v9, vcc_lo, s5, v5, vcc_lo
	s_mov_b32 s0, 0x9999999a
	s_mov_b32 s1, 0x3fc99999
	s_movk_i32 s4, 0x64
.LBB0_2:                                ; =>This Inner Loop Header: Depth=1
	s_clause 0x1
	global_load_b32 v11, v[6:7], off offset:4
	global_load_b32 v12, v[8:9], off offset:4
	s_waitcnt vmcnt(2)
	v_mov_b32_e32 v10, v1
	s_add_i32 s4, s4, -1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	s_cmp_lg_u32 s4, 0
	v_add_f32_e32 v1, v10, v0
	s_waitcnt vmcnt(1)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f32_e32 v1, v1, v11
	v_add_f32_e32 v1, v1, v2
	s_waitcnt vmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f32_e32 v1, v1, v12
	v_cvt_f64_f32_e32 v[11:12], v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[11:12], v[11:12], s[0:1]
	v_cvt_f32_f64_e32 v1, v[11:12]
	global_store_b32 v[4:5], v1, off offset:4
	s_cbranch_scc1 .LBB0_2
; %bb.3:
	s_load_b64 s[0:1], s[2:3], 0x0
	v_ashrrev_i32_e32 v4, 31, v3
	v_sub_f32_e32 v2, v1, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[0:1], 2, v[3:4]
	v_and_b32_e32 v2, 0x7fffffff, v2
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_u32 v0, vcc_lo, s0, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	global_store_b32 v[0:1], v2, off
.LBB0_4:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z6solverPPfS0_i
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.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 13
		.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	_Z6solverPPfS0_i, .Lfunc_end0-_Z6solverPPfS0_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 404
; NumSgprs: 18
; NumVgprs: 13
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 13
; Occupancy: 16
; WaveLimiterHint : 1
; 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
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z6solverPPfS0_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z6solverPPfS0_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     13
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
