#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<getopt.h>
#include <assert.h>
#include <cuda.h>
#include <chrono>

#define RAND_RANGE_MIN -10.0
#define RAND_RANGE_MAX 10.0
#define SEED 123
#define JACOBI_DEBUG 0

enum ERROR_TYPE { MEMCPY, CMALLOC, ADDK, DEVSYNC };

void init_const(float* v, float x, int dim) {
  for (int i = 0; i < dim; i++)
    v[i] = x;
  return;
}

void init_matrix(float **A, int matrix_order) {

  for (int i = 0; i < matrix_order; i++) {
    A[i] = new float[matrix_order];
    if (A[i] == NULL) {
      std::cerr << "Error while allocating resources." << std::endl;
      exit(-1);
    }
  }
}

/** Generates a random number in a specific range.

    @param fMin The lower bound of the range.
    @param fMax The upper bound of the range.
    @return The generated number.
*/
float generate_random_number(float fMin, float fMax) {
  float f = (float)rand() / RAND_MAX;

  return fMin + f * (fMax - fMin);
}

/** Generates a random square matrix.
    @param A The matrix.
*/
void generate_random_matrix(float **A, int matrix_order) {

  for (int i = 0; i < matrix_order; i++) {
    float sum = 0.0;
    for (int j = 0; j < matrix_order; j++)
      if (j != i) {
        float val = generate_random_number(RAND_RANGE_MIN, RAND_RANGE_MAX);
        sum += abs(val);

        A[i][j] = val;
      }

    /* Change back A[i][i] to be > then sum(A[i][j]) */
    A[i][i] = sum + generate_random_number(1.0, RAND_RANGE_MAX);
  }
}

/** Generates a random vector.
    @param v .
*/
void generate_random_vector(float *v, int matrix_order) {
  /* generate vector v */
  for (int j = 0; j < matrix_order; j++) {
    float val = generate_random_number(RAND_RANGE_MIN, RAND_RANGE_MAX);
    v[j] = val;
  }
}

/** Generate a random number in a specific range.
    @param A The square matrix.
    @param v The vector.
    @param start .
    @param end .
*/
void matrix_vector_multiplication(float *x, float **A, float *v, int matrix_order) {

  for (int i = 0; i < matrix_order; i++) {
    x[i] = 0;
    for (int j = 0; j < matrix_order; j++)
      x[i] += A[i][j] * v[j];
  }
  return;
}

void error_on_computation(float* x, float ** A, float *b, int matrix_order, float *err) {
    float error = 0.0, sum = 0.0;
  
    for (size_t i = 0; i < matrix_order; i++) {
      sum = 0.0;
      for (size_t j = 0; j < matrix_order; j++) {
          
        sum = sum + A[i][j] * x[j]; 
      }
      error = error + abs(sum - b[i]);
    }
    *err = error / matrix_order;
    return;
}


std::chrono::duration<double> delta_time(std::chrono::time_point<std::chrono::system_clock> start, std::chrono::time_point<std::chrono::system_clock> end) {
  return end - start;
}


cudaError_t error_check(cudaError_t cudaStatus, ERROR_TYPE msgtype, float*dev_a, float*dev_x_solution, float*dev_b, float*dev_prec_values){
	
    if (cudaStatus != cudaSuccess) {

        switch(msgtype) {
            case (CMALLOC):{
                std::cerr <<  "cudaMalloc failed!" << std::endl;
            }
            case (MEMCPY):{
                std::cerr <<  "cudaMemcpy failed!" << std::endl;
            }
            case (ADDK):{
                std::cerr << "addKernel launch failed:" << cudaGetErrorString(cudaStatus) << std::endl;
            }
            case(DEVSYNC):{
                std::cerr << "cudaDeviceSynchronize returned error code " << cudaStatus <<  " after launching jacobi!"  << std::endl;
            }
        }
		
    cudaFree(dev_a);
    cudaFree(dev_x_solution);
    cudaFree(dev_prec_values);
    cudaFree(dev_b);
	
    return cudaStatus;
	}
}

__global__ void iteration(float * a, float * x_solution, float * b, float * prec_values, unsigned int matrix_order) { 
    unsigned int j, i;
	float sigma = 0.0, newValue = 0.0;
    int bx = blockIdx.x, tx = threadIdx.x;
    i = tx + bx*blockDim.x;
    
    if (i >= matrix_order) return;

    if (i < matrix_order){
        sigma = b[i];
        int idx_Ai = i*matrix_order;
        
        for (j = 0; j < matrix_order; j++) {
            if (i != j) { sigma = sigma - a[idx_Ai + j] * x_solution[j]; }
        }

        newValue = sigma / a[idx_Ai + i];
        
        prec_values[i] = (x_solution[i] - newValue)*(x_solution[i] - newValue);
        x_solution[i] = newValue;
        __syncthreads();
    }
}

cudaError_t cuda_jacobi_solve(float * a, float * x_solution, float * b, float eps, unsigned int matrix_order, int * max_iter, float *prec) {
	unsigned int i, j;
	
    int k = 0, nTiles;
	float *dev_a = 0, *dev_x_solution = 0, *dev_b = 0, *dev_prec_values = 0;
    float accur = 1.0, sum = 0.0;

    float *prec_values = new float[matrix_order];
    init_const(prec_values, 0.0, matrix_order);

    size_t matrix_size = matrix_order*matrix_order*sizeof(float);
    size_t vector_size = matrix_order*sizeof(float);

	cudaError_t cudaStatus;

    cudaDeviceSetCacheConfig(cudaFuncCachePreferShared);

	cudaStatus = cudaMalloc((void**)&dev_a, matrix_size);
    error_check(cudaStatus, CMALLOC, dev_a, dev_x_solution, dev_b, dev_prec_values);

	cudaStatus = cudaMalloc((void**)&dev_x_solution, vector_size);
    error_check(cudaStatus, CMALLOC, dev_a, dev_x_solution, dev_b, dev_prec_values);

	cudaStatus = cudaMalloc((void**)&dev_b, vector_size);
    error_check(cudaStatus, CMALLOC, dev_a, dev_x_solution, dev_b, dev_prec_values);

    cudaStatus = cudaMalloc((void**)&dev_prec_values, vector_size);
    error_check(cudaStatus, CMALLOC, dev_a, dev_x_solution, dev_b, dev_prec_values); 

	cudaStatus = cudaMemcpy(dev_a, a, matrix_size, cudaMemcpyHostToDevice);
    error_check(cudaStatus, MEMCPY, dev_a, dev_x_solution, dev_b, dev_prec_values);

	cudaStatus = cudaMemcpy(dev_x_solution, x_solution, vector_size, cudaMemcpyHostToDevice);
    error_check(cudaStatus, MEMCPY, dev_a, dev_x_solution, dev_b, dev_prec_values);

	cudaStatus = cudaMemcpy(dev_b, b, vector_size, cudaMemcpyHostToDevice);
    error_check(cudaStatus, MEMCPY, dev_a, dev_x_solution, dev_b, dev_prec_values);

	cudaStatus = cudaMemcpy(dev_prec_values, prec_values, vector_size, cudaMemcpyHostToDevice);    
    error_check(cudaStatus, MEMCPY, dev_a, dev_x_solution, dev_b, dev_prec_values);    

    int tileSize = 16;
    nTiles = matrix_order/tileSize + (matrix_order%tileSize == 0?0:1);

    for (i = 0; i < *max_iter; i++) {

        iteration <<<nTiles,tileSize>>> (dev_a, dev_x_solution, dev_b, dev_prec_values, matrix_order);
        k++;

        // Check for any errors launching the kernel
        cudaStatus = cudaGetLastError();
        error_check(cudaStatus, ADDK, dev_a, dev_x_solution, dev_b, dev_prec_values);

        // cudaDeviceSynchronize waits for the kernel to finish, and returns any errors encountered during the launch.
        cudaStatus = cudaDeviceSynchronize();
        error_check(cudaStatus, DEVSYNC, dev_a, dev_x_solution, dev_b, dev_prec_values);

        // Retreive the dev_prec_values vector with all the precision values
        cudaStatus = cudaMemcpy(prec_values, dev_prec_values, vector_size, cudaMemcpyDeviceToHost);
        error_check(cudaStatus, MEMCPY, dev_a, dev_x_solution, dev_b, dev_prec_values);    

        // Computes the precision 
        sum = 0.0;
        for (j = 0; j < matrix_order; j++) {
            sum = sum + fabs(prec_values[j]);
        }
        accur = sqrt(sum);
        if (accur <= eps) break;
	}

	*max_iter = k;
    *prec = accur;
	cudaStatus = cudaMemcpy(x_solution, dev_x_solution, vector_size, cudaMemcpyDeviceToHost);
    error_check(cudaStatus, MEMCPY, dev_a, dev_x_solution, dev_b, dev_prec_values); 

    cudaFree(dev_a);
    cudaFree(dev_x_solution);
    cudaFree(dev_prec_values);
    cudaFree(dev_b);
}

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

    const int matrix_order = atoi(argv[1]); // order of the matrix
    int max_iter = atoi(argv[2]);     // number of max_iterations
    const float epsilon = atof(argv[3]);    // precision

    int iterations = max_iter;
    std::chrono::time_point<std::chrono::system_clock> start_time, end_time;

    float *x_solution_h, *b_h, **A_h, *rand_x_h;
    float *extended_a = 0;    

    // Allocate memory for CPU.
    A_h = new float *[matrix_order];
    b_h = new float[matrix_order];
    x_solution_h = new float[matrix_order];
    rand_x_h = new float[matrix_order];

    if (A_h == NULL || b_h == NULL || rand_x_h == NULL || x_solution_h == NULL) {
        std::cerr << "Error while allocating resources." << std::endl;
        exit(-1);
    }

    init_matrix(A_h, matrix_order);
    srand(SEED);
    generate_random_matrix(A_h, matrix_order);
    
    extended_a = (float*)malloc(matrix_order*matrix_order*sizeof(float));
	
    for (int i = 0; i < matrix_order; i++) {
		for (int j = 0; j < matrix_order; j++) {
			extended_a[i*matrix_order + j] = A_h[i][j];
		}
	}
    
    generate_random_vector(rand_x_h, matrix_order);

    int repetitions = 20;
    float precision = 1.0, err = 0.0;
    
    matrix_vector_multiplication(b_h, A_h, rand_x_h, matrix_order);
    
    for (int m=0; m<repetitions; m++) {
        
        init_const(x_solution_h, 0.0, matrix_order);
        iterations = max_iter;
        err=0.0;

        start_time = std::chrono::system_clock::now();
        cuda_jacobi_solve(extended_a, x_solution_h, b_h, epsilon, matrix_order, &iterations, &precision);
        end_time = std::chrono::system_clock::now();

        std::cout << delta_time(start_time, end_time).count()  << "\t" ;        
        error_on_computation(x_solution_h, A_h, b_h, matrix_order, &err);
    }

        std::cout << "\t" << iterations << "\t" <<  precision << "\t" <<  err  << std::endl;            

    // Release resources
    for (int i = 0; i < matrix_order; i++)
        delete[] A_h[i];

    delete[] A_h;
    delete[] b_h;
    delete[] rand_x_h;
    delete[] x_solution_h;
    free(extended_a);

    return 0;

}
	.file	"tmpxft_002abee2_00000000-6_jacobi_cuda.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3754:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3754:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z10init_constPffi
	.type	_Z10init_constPffi, @function
_Z10init_constPffi:
.LFB3734:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L3:
	cmpl	%eax, %esi
	jle	.L6
	movss	%xmm0, (%rdi,%rax,4)
	incq	%rax
	jmp	.L3
.L6:
	ret
	.cfi_endproc
.LFE3734:
	.size	_Z10init_constPffi, .-_Z10init_constPffi
	.globl	_Z11init_matrixPPfi
	.type	_Z11init_matrixPPfi, @function
_Z11init_matrixPPfi:
.LFB3735:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movslq	%esi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%r12, %rbp
	salq	$2, %r12
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 48
.L8:
	cmpl	%ebx, %ebp
	jle	.L11
	movq	%r12, %rdi
	call	_Znam@PLT
	movq	%rax, 0(%r13,%rbx,8)
	incq	%rbx
	jmp	.L8
.L11:
	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
.LFE3735:
	.size	_Z11init_matrixPPfi, .-_Z11init_matrixPPfi
	.globl	_Z22generate_random_numberff
	.type	_Z22generate_random_numberff, @function
_Z22generate_random_numberff:
.LFB3736:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movss	%xmm0, 12(%rsp)
	movss	%xmm1, 8(%rsp)
	call	rand@PLT
	movss	12(%rsp), %xmm2
	movss	8(%rsp), %xmm1
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	subss	%xmm2, %xmm1
	mulss	%xmm0, %xmm1
	movaps	%xmm1, %xmm0
	addss	%xmm2, %xmm0
	ret
	.cfi_endproc
.LFE3736:
	.size	_Z22generate_random_numberff, .-_Z22generate_random_numberff
	.globl	_Z22generate_random_matrixPPfi
	.type	_Z22generate_random_matrixPPfi, @function
_Z22generate_random_matrixPPfi:
.LFB3737:
	.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
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%esi, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
.L15:
	movl	%ebx, %r14d
	cmpl	%r12d, %ebx
	jge	.L21
	movl	$0x00000000, 12(%rsp)
	movslq	%r12d, %r15
	xorl	%ebp, %ebp
.L17:
	cmpl	%ebp, %r14d
	je	.L16
	movss	.LC2(%rip), %xmm1
	movss	.LC3(%rip), %xmm0
	call	_Z22generate_random_numberff
	movq	0(%r13,%rbx,8), %rax
	movaps	%xmm0, %xmm1
	andps	.LC4(%rip), %xmm1
	addss	12(%rsp), %xmm1
	movss	%xmm0, (%rax,%rbp,4)
	movss	%xmm1, 12(%rsp)
.L16:
	incq	%rbp
	cmpq	%rbp, %r15
	jne	.L17
	movss	.LC2(%rip), %xmm1
	movss	.LC5(%rip), %xmm0
	call	_Z22generate_random_numberff
	addss	12(%rsp), %xmm0
	movq	0(%r13,%rbx,8), %rax
	movss	%xmm0, (%rax,%rbx,4)
	incq	%rbx
	jmp	.L15
.L21:
	addq	$24, %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
.LFE3737:
	.size	_Z22generate_random_matrixPPfi, .-_Z22generate_random_matrixPPfi
	.globl	_Z22generate_random_vectorPfi
	.type	_Z22generate_random_vectorPfi, @function
_Z22generate_random_vectorPfi:
.LFB3738:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	xorl	%ebx, %ebx
.L23:
	cmpl	%ebx, %ebp
	jle	.L26
	movss	.LC2(%rip), %xmm1
	movss	.LC3(%rip), %xmm0
	call	_Z22generate_random_numberff
	movss	%xmm0, (%r12,%rbx,4)
	incq	%rbx
	jmp	.L23
.L26:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3738:
	.size	_Z22generate_random_vectorPfi, .-_Z22generate_random_vectorPfi
	.globl	_Z28matrix_vector_multiplicationPfPS_S_i
	.type	_Z28matrix_vector_multiplicationPfPS_S_i, @function
_Z28matrix_vector_multiplicationPfPS_S_i:
.LFB3739:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L28:
	cmpl	%eax, %ecx
	jle	.L32
	movl	$0x00000000, (%rdi,%rax,4)
	movq	(%rsi,%rax,8), %r9
	xorl	%r8d, %r8d
.L29:
	movss	(%r9,%r8,4), %xmm0
	mulss	(%rdx,%r8,4), %xmm0
	incq	%r8
	addss	(%rdi,%rax,4), %xmm0
	movss	%xmm0, (%rdi,%rax,4)
	cmpl	%r8d, %ecx
	jg	.L29
	incq	%rax
	jmp	.L28
.L32:
	ret
	.cfi_endproc
.LFE3739:
	.size	_Z28matrix_vector_multiplicationPfPS_S_i, .-_Z28matrix_vector_multiplicationPfPS_S_i
	.globl	_Z20error_on_computationPfPS_S_iS_
	.type	_Z20error_on_computationPfPS_S_iS_, @function
_Z20error_on_computationPfPS_S_iS_:
.LFB3740:
	.cfi_startproc
	endbr64
	movq	%rsi, %r9
	xorl	%eax, %eax
	xorps	%xmm1, %xmm1
	movslq	%ecx, %r10
	movss	.LC4(%rip), %xmm3
.L34:
	cmpq	%r10, %rax
	jnb	.L38
	movq	(%r9,%rax,8), %r11
	xorl	%esi, %esi
	xorps	%xmm0, %xmm0
.L35:
	movss	(%r11,%rsi,4), %xmm2
	mulss	(%rdi,%rsi,4), %xmm2
	incq	%rsi
	addss	%xmm2, %xmm0
	cmpq	%r10, %rsi
	jb	.L35
	subss	(%rdx,%rax,4), %xmm0
	incq	%rax
	andps	%xmm3, %xmm0
	addss	%xmm0, %xmm1
	jmp	.L34
.L38:
	cvtsi2ssl	%ecx, %xmm0
	divss	%xmm0, %xmm1
	movss	%xmm1, (%r8)
	ret
	.cfi_endproc
.LFE3740:
	.size	_Z20error_on_computationPfPS_S_iS_, .-_Z20error_on_computationPfPS_S_iS_
	.globl	_Z10delta_timeNSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEES7_
	.type	_Z10delta_timeNSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEES7_, @function
_Z10delta_timeNSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEES7_:
.LFB3741:
	.cfi_startproc
	endbr64
	subq	%rdi, %rsi
	cvtsi2sdq	%rsi, %xmm0
	divsd	.LC6(%rip), %xmm0
	ret
	.cfi_endproc
.LFE3741:
	.size	_Z10delta_timeNSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEES7_, .-_Z10delta_timeNSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEES7_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC7:
	.string	"cudaMalloc failed!"
.LC8:
	.string	"cudaMemcpy failed!"
.LC9:
	.string	"addKernel launch failed:"
.LC10:
	.string	"cudaDeviceSynchronize returned error code "
.LC11:
	.string	" after launching jacobi!"
	.text
	.globl	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	.type	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_, @function
_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_:
.LFB3745:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rdx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rcx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%r9, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%r8, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edi, %ebx
	pushq	%r10
	.cfi_def_cfa_offset 64
	cmpl	$2, %esi
	je	.L41
	ja	.L42
	testl	%esi, %esi
	leaq	_ZSt4cerr(%rip), %r15
	je	.L43
	movq	%r15, %rdi
	leaq	.LC7(%rip), %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	jmp	.L43
.L42:
	cmpl	$3, %esi
	je	.L45
	jmp	.L46
.L43:
	movq	%r15, %rdi
	leaq	.LC8(%rip), %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
.L41:
	leaq	.LC9(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	%ebx, %edi
	movq	%rax, %r15
	call	cudaGetErrorString@PLT
	movq	%r15, %rdi
	movq	%rax, %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
.L45:
	leaq	.LC10(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	%ebx, %esi
	movq	%rax, %rdi
	call	_ZNSolsEi@PLT
	leaq	.LC11(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
.L46:
	movq	%r14, %rdi
	call	cudaFree@PLT
	movq	%r13, %rdi
	call	cudaFree@PLT
	movq	%r12, %rdi
	call	cudaFree@PLT
	movq	%rbp, %rdi
	call	cudaFree@PLT
	popq	%rdx
	.cfi_def_cfa_offset 56
	movl	%ebx, %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
.LFE3745:
	.size	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_, .-_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	.globl	_Z35__device_stub__Z9iterationPfS_S_S_jPfS_S_S_j
	.type	_Z35__device_stub__Z9iterationPfS_S_S_jPfS_S_S_j, @function
_Z35__device_stub__Z9iterationPfS_S_S_jPfS_S_S_j:
.LFB3776:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movl	%r8d, 12(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L58
	pushq	56(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z9iterationPfS_S_S_j(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 192
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L58:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L60
	call	__stack_chk_fail@PLT
.L60:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3776:
	.size	_Z35__device_stub__Z9iterationPfS_S_S_jPfS_S_S_j, .-_Z35__device_stub__Z9iterationPfS_S_S_jPfS_S_S_j
	.globl	_Z9iterationPfS_S_S_j
	.type	_Z9iterationPfS_S_S_j, @function
_Z9iterationPfS_S_S_j:
.LFB3777:
	.cfi_startproc
	endbr64
	jmp	_Z35__device_stub__Z9iterationPfS_S_S_jPfS_S_S_j
	.cfi_endproc
.LFE3777:
	.size	_Z9iterationPfS_S_S_j, .-_Z9iterationPfS_S_S_j
	.globl	_Z17cuda_jacobi_solvePfS_S_fjPiS_
	.type	_Z17cuda_jacobi_solvePfS_S_fjPiS_, @function
_Z17cuda_jacobi_solvePfS_S_fjPiS_:
.LFB3746:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%r9, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%r8, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%ecx, %ebx
	subq	$104, %rsp
	.cfi_def_cfa_offset 160
	movq	%rsi, 16(%rsp)
	movss	%xmm0, 28(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	xorl	%eax, %eax
	movq	%rax, 32(%rsp)
	movq	%rax, 40(%rsp)
	movq	%rax, 48(%rsp)
	movq	%rax, 56(%rsp)
	movl	%ecx, %eax
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, (%rsp)
	call	_Znam@PLT
	xorps	%xmm0, %xmm0
	movl	%ebx, %esi
	movq	%rax, %rdi
	movq	%rax, %rbp
	call	_Z10init_constPffi
	movl	%ebx, %eax
	movl	$1, %edi
	imull	%ebx, %eax
	salq	$2, %rax
	movq	%rax, 8(%rsp)
	call	cudaDeviceSetCacheConfig@PLT
	movq	8(%rsp), %rsi
	leaq	32(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	56(%rsp), %r9
	movq	48(%rsp), %r8
	movl	$1, %esi
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%eax, %edi
	call	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	movq	(%rsp), %rsi
	leaq	40(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	56(%rsp), %r9
	movq	48(%rsp), %r8
	movl	$1, %esi
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%eax, %edi
	call	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	movq	(%rsp), %rsi
	leaq	48(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	56(%rsp), %r9
	movq	48(%rsp), %r8
	movl	$1, %esi
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%eax, %edi
	call	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	movq	(%rsp), %rsi
	leaq	56(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	56(%rsp), %r9
	movq	48(%rsp), %r8
	movl	$1, %esi
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%eax, %edi
	call	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	movq	8(%rsp), %rdx
	movq	32(%rsp), %rdi
	movq	%r15, %rsi
	movl	$1, %ecx
	xorl	%r15d, %r15d
	call	cudaMemcpy@PLT
	movq	56(%rsp), %r9
	movq	48(%rsp), %r8
	xorl	%esi, %esi
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%eax, %edi
	call	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	movq	(%rsp), %rdx
	movq	16(%rsp), %rsi
	movl	$1, %ecx
	movq	40(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	56(%rsp), %r9
	movq	48(%rsp), %r8
	xorl	%esi, %esi
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%eax, %edi
	call	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	movq	(%rsp), %rdx
	movq	48(%rsp), %rdi
	movq	%r13, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	56(%rsp), %r9
	movq	48(%rsp), %r8
	xorl	%esi, %esi
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%eax, %edi
	call	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	movq	(%rsp), %rdx
	movq	56(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	56(%rsp), %r9
	movq	48(%rsp), %r8
	xorl	%esi, %esi
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%eax, %edi
	call	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	testb	$15, %bl
	movl	%ebx, %eax
	movss	.LC5(%rip), %xmm0
	setne	%r15b
	shrl	$4, %eax
	xorl	%r13d, %r13d
	addl	%eax, %r15d
.L64:
	cmpl	(%r12), %r13d
	jnb	.L68
	movl	%r15d, 64(%rsp)
	movl	$268435457, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	salq	$4, %rdx
	movl	$1, %ecx
	movq	%rax, 68(%rsp)
	movq	64(%rsp), %rdi
	movl	72(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L65
	movq	56(%rsp), %rcx
	movq	48(%rsp), %rdx
	movl	%ebx, %r8d
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdi
	call	_Z35__device_stub__Z9iterationPfS_S_S_jPfS_S_S_j
.L65:
	call	cudaGetLastError@PLT
	movq	56(%rsp), %r9
	movq	48(%rsp), %r8
	movl	$2, %esi
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%eax, %edi
	incl	%r13d
	call	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	call	cudaDeviceSynchronize@PLT
	movq	56(%rsp), %r9
	movq	48(%rsp), %r8
	movl	$3, %esi
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%eax, %edi
	call	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	movq	(%rsp), %rdx
	movq	56(%rsp), %rsi
	movq	%rbp, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	56(%rsp), %r9
	movq	48(%rsp), %r8
	xorl	%esi, %esi
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%eax, %edi
	call	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	xorl	%eax, %eax
	xorps	%xmm0, %xmm0
.L66:
	cmpl	%ebx, %eax
	jnb	.L72
	movss	0(%rbp,%rax,4), %xmm1
	andps	.LC4(%rip), %xmm1
	incq	%rax
	addss	%xmm1, %xmm0
	jmp	.L66
.L72:
	call	sqrtf@PLT
	movss	28(%rsp), %xmm2
	comiss	%xmm0, %xmm2
	jb	.L64
.L68:
	movl	%r13d, (%r12)
	movq	(%rsp), %rdx
	movl	$2, %ecx
	movq	40(%rsp), %rsi
	movq	16(%rsp), %rdi
	movss	%xmm0, (%r14)
	call	cudaMemcpy@PLT
	movq	56(%rsp), %r9
	movq	48(%rsp), %r8
	xorl	%esi, %esi
	movq	40(%rsp), %rcx
	movq	32(%rsp), %rdx
	movl	%eax, %edi
	call	_Z11error_check9cudaError10ERROR_TYPEPfS1_S1_S1_
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	.cfi_endproc
.LFE3746:
	.size	_Z17cuda_jacobi_solvePfS_S_fjPiS_, .-_Z17cuda_jacobi_solvePfS_S_fjPiS_
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3747:
	.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	$40, %rsp
	.cfi_def_cfa_offset 96
	movq	8(%rsi), %rdi
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	call	atoi@PLT
	movq	16(%rbp), %rdi
	movl	%eax, %ebx
	call	atoi@PLT
	movq	24(%rbp), %rdi
	movslq	%ebx, %rbp
	movl	%eax, %r12d
	call	atof@PLT
	movl	%r12d, 16(%rsp)
	movabsq	$1152921504606846975, %rax
	cvtsd2ss	%xmm0, %xmm1
	movss	%xmm1, 12(%rsp)
	cmpq	%rbp, %rax
	jb	.L74
	leaq	0(,%rbp,8), %rdi
	salq	$2, %rbp
	call	_Znam@PLT
	movq	%rbp, %rdi
	movq	%rax, %r12
	call	_Znam@PLT
	movq	%rbp, %rdi
	movq	%rax, %r15
	call	_Znam@PLT
	movq	%rbp, %rdi
	movq	%rax, (%rsp)
	call	_Znam@PLT
	movl	%ebx, %esi
	movq	%r12, %rdi
	movq	%rax, %r14
	call	_Z11init_matrixPPfi
	movl	$123, %edi
	call	srand@PLT
	movl	%ebx, %esi
	movq	%r12, %rdi
	call	_Z22generate_random_matrixPPfi
	movl	%ebx, %edi
	imull	%ebx, %edi
	movslq	%edi, %rdi
	salq	$2, %rdi
	call	malloc@PLT
	xorl	%edx, %edx
	movq	%rax, %r13
	movq	%rax, %rcx
	jmp	.L75
.L74:
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L83
	call	__cxa_throw_bad_array_new_length@PLT
.L77:
	movss	(%rsi,%rax,4), %xmm0
	movss	%xmm0, (%rcx,%rax,4)
	incq	%rax
	cmpl	%eax, %ebx
	jg	.L77
	incq	%rdx
	addq	%rbp, %rcx
.L75:
	cmpl	%edx, %ebx
	jle	.L84
	movq	(%r12,%rdx,8), %rsi
	xorl	%eax, %eax
	jmp	.L77
.L84:
	movl	%ebx, %esi
	movq	%r14, %rdi
	call	_Z22generate_random_vectorPfi
	movl	%ebx, %ecx
	movq	%r14, %rdx
	movq	%r12, %rsi
	movq	%r15, %rdi
	call	_Z28matrix_vector_multiplicationPfPS_S_i
	movq	(%rsp), %rdi
	movl	%ebx, %esi
	xorps	%xmm0, %xmm0
	call	_Z10init_constPffi
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L79
.L83:
	call	__stack_chk_fail@PLT
.L79:
	movss	12(%rsp), %xmm0
	movq	(%rsp), %rsi
	movl	%ebx, %ecx
	movq	%r15, %rdx
	leaq	20(%rsp), %r9
	leaq	16(%rsp), %r8
	movq	%r13, %rdi
	call	_Z17cuda_jacobi_solvePfS_S_fjPiS_
	.cfi_endproc
.LFE3747:
	.size	main, .-main
	.section	.rodata.str1.1
.LC12:
	.string	"_Z9iterationPfS_S_S_j"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3779:
	.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	.LC12(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z9iterationPfS_S_S_j(%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
.LFE3779:
	.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
	.align 4
.LC2:
	.long	1092616192
	.align 4
.LC3:
	.long	-1054867456
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC4:
	.long	2147483647
	.long	0
	.long	0
	.long	0
	.section	.rodata.cst4
	.align 4
.LC5:
	.long	1065353216
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC6:
	.long	0
	.long	1104006501
	.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<iostream>
#include<stdlib.h>
#include<getopt.h>
#include <assert.h>

#include <chrono>

#define RAND_RANGE_MIN -10.0
#define RAND_RANGE_MAX 10.0
#define SEED 123
#define JACOBI_DEBUG 0

enum ERROR_TYPE { MEMCPY, CMALLOC, ADDK, DEVSYNC };

void init_const(float* v, float x, int dim) {
  for (int i = 0; i < dim; i++)
    v[i] = x;
  return;
}

void init_matrix(float **A, int matrix_order) {

  for (int i = 0; i < matrix_order; i++) {
    A[i] = new float[matrix_order];
    if (A[i] == NULL) {
      std::cerr << "Error while allocating resources." << std::endl;
      exit(-1);
    }
  }
}

/** Generates a random number in a specific range.

    @param fMin The lower bound of the range.
    @param fMax The upper bound of the range.
    @return The generated number.
*/
float generate_random_number(float fMin, float fMax) {
  float f = (float)rand() / RAND_MAX;

  return fMin + f * (fMax - fMin);
}

/** Generates a random square matrix.
    @param A The matrix.
*/
void generate_random_matrix(float **A, int matrix_order) {

  for (int i = 0; i < matrix_order; i++) {
    float sum = 0.0;
    for (int j = 0; j < matrix_order; j++)
      if (j != i) {
        float val = generate_random_number(RAND_RANGE_MIN, RAND_RANGE_MAX);
        sum += abs(val);

        A[i][j] = val;
      }

    /* Change back A[i][i] to be > then sum(A[i][j]) */
    A[i][i] = sum + generate_random_number(1.0, RAND_RANGE_MAX);
  }
}

/** Generates a random vector.
    @param v .
*/
void generate_random_vector(float *v, int matrix_order) {
  /* generate vector v */
  for (int j = 0; j < matrix_order; j++) {
    float val = generate_random_number(RAND_RANGE_MIN, RAND_RANGE_MAX);
    v[j] = val;
  }
}

/** Generate a random number in a specific range.
    @param A The square matrix.
    @param v The vector.
    @param start .
    @param end .
*/
void matrix_vector_multiplication(float *x, float **A, float *v, int matrix_order) {

  for (int i = 0; i < matrix_order; i++) {
    x[i] = 0;
    for (int j = 0; j < matrix_order; j++)
      x[i] += A[i][j] * v[j];
  }
  return;
}

void error_on_computation(float* x, float ** A, float *b, int matrix_order, float *err) {
    float error = 0.0, sum = 0.0;
  
    for (size_t i = 0; i < matrix_order; i++) {
      sum = 0.0;
      for (size_t j = 0; j < matrix_order; j++) {
          
        sum = sum + A[i][j] * x[j]; 
      }
      error = error + abs(sum - b[i]);
    }
    *err = error / matrix_order;
    return;
}


std::chrono::duration<double> delta_time(std::chrono::time_point<std::chrono::system_clock> start, std::chrono::time_point<std::chrono::system_clock> end) {
  return end - start;
}


hipError_t error_check(hipError_t cudaStatus, ERROR_TYPE msgtype, float*dev_a, float*dev_x_solution, float*dev_b, float*dev_prec_values){
	
    if (cudaStatus != hipSuccess) {

        switch(msgtype) {
            case (CMALLOC):{
                std::cerr <<  "hipMalloc failed!" << std::endl;
            }
            case (MEMCPY):{
                std::cerr <<  "hipMemcpy failed!" << std::endl;
            }
            case (ADDK):{
                std::cerr << "addKernel launch failed:" << hipGetErrorString(cudaStatus) << std::endl;
            }
            case(DEVSYNC):{
                std::cerr << "hipDeviceSynchronize returned error code " << cudaStatus <<  " after launching jacobi!"  << std::endl;
            }
        }
		
    hipFree(dev_a);
    hipFree(dev_x_solution);
    hipFree(dev_prec_values);
    hipFree(dev_b);
	
    return cudaStatus;
	}
}

__global__ void iteration(float * a, float * x_solution, float * b, float * prec_values, unsigned int matrix_order) { 
    unsigned int j, i;
	float sigma = 0.0, newValue = 0.0;
    int bx = blockIdx.x, tx = threadIdx.x;
    i = tx + bx*blockDim.x;
    
    if (i >= matrix_order) return;

    if (i < matrix_order){
        sigma = b[i];
        int idx_Ai = i*matrix_order;
        
        for (j = 0; j < matrix_order; j++) {
            if (i != j) { sigma = sigma - a[idx_Ai + j] * x_solution[j]; }
        }

        newValue = sigma / a[idx_Ai + i];
        
        prec_values[i] = (x_solution[i] - newValue)*(x_solution[i] - newValue);
        x_solution[i] = newValue;
        __syncthreads();
    }
}

hipError_t cuda_jacobi_solve(float * a, float * x_solution, float * b, float eps, unsigned int matrix_order, int * max_iter, float *prec) {
	unsigned int i, j;
	
    int k = 0, nTiles;
	float *dev_a = 0, *dev_x_solution = 0, *dev_b = 0, *dev_prec_values = 0;
    float accur = 1.0, sum = 0.0;

    float *prec_values = new float[matrix_order];
    init_const(prec_values, 0.0, matrix_order);

    size_t matrix_size = matrix_order*matrix_order*sizeof(float);
    size_t vector_size = matrix_order*sizeof(float);

	hipError_t cudaStatus;

    hipDeviceSetCacheConfig(hipFuncCachePreferShared);

	cudaStatus = hipMalloc((void**)&dev_a, matrix_size);
    error_check(cudaStatus, CMALLOC, dev_a, dev_x_solution, dev_b, dev_prec_values);

	cudaStatus = hipMalloc((void**)&dev_x_solution, vector_size);
    error_check(cudaStatus, CMALLOC, dev_a, dev_x_solution, dev_b, dev_prec_values);

	cudaStatus = hipMalloc((void**)&dev_b, vector_size);
    error_check(cudaStatus, CMALLOC, dev_a, dev_x_solution, dev_b, dev_prec_values);

    cudaStatus = hipMalloc((void**)&dev_prec_values, vector_size);
    error_check(cudaStatus, CMALLOC, dev_a, dev_x_solution, dev_b, dev_prec_values); 

	cudaStatus = hipMemcpy(dev_a, a, matrix_size, hipMemcpyHostToDevice);
    error_check(cudaStatus, MEMCPY, dev_a, dev_x_solution, dev_b, dev_prec_values);

	cudaStatus = hipMemcpy(dev_x_solution, x_solution, vector_size, hipMemcpyHostToDevice);
    error_check(cudaStatus, MEMCPY, dev_a, dev_x_solution, dev_b, dev_prec_values);

	cudaStatus = hipMemcpy(dev_b, b, vector_size, hipMemcpyHostToDevice);
    error_check(cudaStatus, MEMCPY, dev_a, dev_x_solution, dev_b, dev_prec_values);

	cudaStatus = hipMemcpy(dev_prec_values, prec_values, vector_size, hipMemcpyHostToDevice);    
    error_check(cudaStatus, MEMCPY, dev_a, dev_x_solution, dev_b, dev_prec_values);    

    int tileSize = 16;
    nTiles = matrix_order/tileSize + (matrix_order%tileSize == 0?0:1);

    for (i = 0; i < *max_iter; i++) {

        iteration <<<nTiles,tileSize>>> (dev_a, dev_x_solution, dev_b, dev_prec_values, matrix_order);
        k++;

        // Check for any errors launching the kernel
        cudaStatus = hipGetLastError();
        error_check(cudaStatus, ADDK, dev_a, dev_x_solution, dev_b, dev_prec_values);

        // cudaDeviceSynchronize waits for the kernel to finish, and returns any errors encountered during the launch.
        cudaStatus = hipDeviceSynchronize();
        error_check(cudaStatus, DEVSYNC, dev_a, dev_x_solution, dev_b, dev_prec_values);

        // Retreive the dev_prec_values vector with all the precision values
        cudaStatus = hipMemcpy(prec_values, dev_prec_values, vector_size, hipMemcpyDeviceToHost);
        error_check(cudaStatus, MEMCPY, dev_a, dev_x_solution, dev_b, dev_prec_values);    

        // Computes the precision 
        sum = 0.0;
        for (j = 0; j < matrix_order; j++) {
            sum = sum + fabs(prec_values[j]);
        }
        accur = sqrt(sum);
        if (accur <= eps) break;
	}

	*max_iter = k;
    *prec = accur;
	cudaStatus = hipMemcpy(x_solution, dev_x_solution, vector_size, hipMemcpyDeviceToHost);
    error_check(cudaStatus, MEMCPY, dev_a, dev_x_solution, dev_b, dev_prec_values); 

    hipFree(dev_a);
    hipFree(dev_x_solution);
    hipFree(dev_prec_values);
    hipFree(dev_b);
}

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

    const int matrix_order = atoi(argv[1]); // order of the matrix
    int max_iter = atoi(argv[2]);     // number of max_iterations
    const float epsilon = atof(argv[3]);    // precision

    int iterations = max_iter;
    std::chrono::time_point<std::chrono::system_clock> start_time, end_time;

    float *x_solution_h, *b_h, **A_h, *rand_x_h;
    float *extended_a = 0;    

    // Allocate memory for CPU.
    A_h = new float *[matrix_order];
    b_h = new float[matrix_order];
    x_solution_h = new float[matrix_order];
    rand_x_h = new float[matrix_order];

    if (A_h == NULL || b_h == NULL || rand_x_h == NULL || x_solution_h == NULL) {
        std::cerr << "Error while allocating resources." << std::endl;
        exit(-1);
    }

    init_matrix(A_h, matrix_order);
    srand(SEED);
    generate_random_matrix(A_h, matrix_order);
    
    extended_a = (float*)malloc(matrix_order*matrix_order*sizeof(float));
	
    for (int i = 0; i < matrix_order; i++) {
		for (int j = 0; j < matrix_order; j++) {
			extended_a[i*matrix_order + j] = A_h[i][j];
		}
	}
    
    generate_random_vector(rand_x_h, matrix_order);

    int repetitions = 20;
    float precision = 1.0, err = 0.0;
    
    matrix_vector_multiplication(b_h, A_h, rand_x_h, matrix_order);
    
    for (int m=0; m<repetitions; m++) {
        
        init_const(x_solution_h, 0.0, matrix_order);
        iterations = max_iter;
        err=0.0;

        start_time = std::chrono::system_clock::now();
        cuda_jacobi_solve(extended_a, x_solution_h, b_h, epsilon, matrix_order, &iterations, &precision);
        end_time = std::chrono::system_clock::now();

        std::cout << delta_time(start_time, end_time).count()  << "\t" ;        
        error_on_computation(x_solution_h, A_h, b_h, matrix_order, &err);
    }

        std::cout << "\t" << iterations << "\t" <<  precision << "\t" <<  err  << std::endl;            

    // Release resources
    for (int i = 0; i < matrix_order; i++)
        delete[] A_h[i];

    delete[] A_h;
    delete[] b_h;
    delete[] rand_x_h;
    delete[] x_solution_h;
    free(extended_a);

    return 0;

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

                                        # End of file scope inline assembly
	.globl	_Z10init_constPffi              # -- Begin function _Z10init_constPffi
	.type	_Z10init_constPffi,@function
_Z10init_constPffi:                     # @_Z10init_constPffi
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%esi, %eax
	xorl	%ecx, %ecx
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movss	%xmm0, (%rdi,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB0_2
.LBB0_3:                                # %._crit_edge
	retq
.Lfunc_end0:
	.size	_Z10init_constPffi, .Lfunc_end0-_Z10init_constPffi
	.cfi_endproc
                                        # -- End function
	.globl	_Z11init_matrixPPfi             # -- Begin function _Z11init_matrixPPfi
	.type	_Z11init_matrixPPfi,@function
_Z11init_matrixPPfi:                    # @_Z11init_matrixPPfi
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB1_4
# %bb.1:                                # %.lr.ph
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movl	%esi, %r15d
	leaq	(,%r15,4), %r14
	xorl	%r12d, %r12d
.LBB1_2:                                # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	callq	_Znam
	movq	%rax, (%rbx,%r12,8)
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB1_2
# %bb.3:
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r14
	.cfi_restore %r15
.LBB1_4:                                # %._crit_edge
	retq
.Lfunc_end1:
	.size	_Z11init_matrixPPfi, .Lfunc_end1-_Z11init_matrixPPfi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z22generate_random_numberff
.LCPI2_0:
	.long	0x30000000                      # float 4.65661287E-10
	.text
	.globl	_Z22generate_random_numberff
	.type	_Z22generate_random_numberff,@function
_Z22generate_random_numberff:           # @_Z22generate_random_numberff
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movss	%xmm1, 4(%rsp)                  # 4-byte Spill
	movss	%xmm0, (%rsp)                   # 4-byte Spill
	callq	rand
	xorps	%xmm1, %xmm1
	cvtsi2ss	%eax, %xmm1
	mulss	.LCPI2_0(%rip), %xmm1
	movss	4(%rsp), %xmm0                  # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	movss	(%rsp), %xmm2                   # 4-byte Reload
                                        # xmm2 = mem[0],zero,zero,zero
	subss	%xmm2, %xmm0
	mulss	%xmm1, %xmm0
	addss	%xmm2, %xmm0
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z22generate_random_numberff, .Lfunc_end2-_Z22generate_random_numberff
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z22generate_random_matrixPPfi
.LCPI3_0:
	.long	0x30000000                      # float 4.65661287E-10
.LCPI3_1:
	.long	0x41a00000                      # float 20
.LCPI3_2:
	.long	0xc1200000                      # float -10
.LCPI3_4:
	.long	0x41100000                      # float 9
.LCPI3_5:
	.long	0x3f800000                      # float 1
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI3_3:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.text
	.globl	_Z22generate_random_matrixPPfi
	.type	_Z22generate_random_matrixPPfi,@function
_Z22generate_random_matrixPPfi:         # @_Z22generate_random_matrixPPfi
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB3_8
# %bb.1:                                # %.preheader.lr.ph
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movl	%esi, %r14d
	xorl	%r15d, %r15d
.LBB3_2:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_3 Depth 2
	xorps	%xmm0, %xmm0
	movss	%xmm0, 4(%rsp)                  # 4-byte Spill
	xorl	%r12d, %r12d
.LBB3_3:                                #   Parent Loop BB3_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	cmpq	%r12, %r15
	je	.LBB3_5
# %bb.4:                                #   in Loop: Header=BB3_3 Depth=2
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI3_0(%rip), %xmm0
	mulss	.LCPI3_1(%rip), %xmm0
	addss	.LCPI3_2(%rip), %xmm0
	movq	(%rbx,%r15,8), %rax
	movss	%xmm0, (%rax,%r12,4)
	andps	.LCPI3_3(%rip), %xmm0
	movss	4(%rsp), %xmm1                  # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	addss	%xmm0, %xmm1
	movss	%xmm1, 4(%rsp)                  # 4-byte Spill
.LBB3_5:                                #   in Loop: Header=BB3_3 Depth=2
	incq	%r12
	cmpq	%r12, %r14
	jne	.LBB3_3
# %bb.6:                                # %._crit_edge
                                        #   in Loop: Header=BB3_2 Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI3_0(%rip), %xmm0
	mulss	.LCPI3_4(%rip), %xmm0
	addss	.LCPI3_5(%rip), %xmm0
	movss	4(%rsp), %xmm1                  # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	addss	%xmm0, %xmm1
	movq	(%rbx,%r15,8), %rax
	movss	%xmm1, (%rax,%r15,4)
	incq	%r15
	cmpq	%r14, %r15
	jne	.LBB3_2
# %bb.7:
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r14
	.cfi_restore %r15
.LBB3_8:                                # %._crit_edge23
	retq
.Lfunc_end3:
	.size	_Z22generate_random_matrixPPfi, .Lfunc_end3-_Z22generate_random_matrixPPfi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z22generate_random_vectorPfi
.LCPI4_0:
	.long	0x30000000                      # float 4.65661287E-10
.LCPI4_1:
	.long	0x41a00000                      # float 20
.LCPI4_2:
	.long	0xc1200000                      # float -10
	.text
	.globl	_Z22generate_random_vectorPfi
	.type	_Z22generate_random_vectorPfi,@function
_Z22generate_random_vectorPfi:          # @_Z22generate_random_vectorPfi
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB4_4
# %bb.1:                                # %.lr.ph.preheader
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movl	%esi, %r14d
	xorl	%r15d, %r15d
.LBB4_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI4_0(%rip), %xmm0
	mulss	.LCPI4_1(%rip), %xmm0
	addss	.LCPI4_2(%rip), %xmm0
	movss	%xmm0, (%rbx,%r15,4)
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB4_2
# %bb.3:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
.LBB4_4:                                # %._crit_edge
	retq
.Lfunc_end4:
	.size	_Z22generate_random_vectorPfi, .Lfunc_end4-_Z22generate_random_vectorPfi
	.cfi_endproc
                                        # -- End function
	.globl	_Z28matrix_vector_multiplicationPfPS_S_i # -- Begin function _Z28matrix_vector_multiplicationPfPS_S_i
	.type	_Z28matrix_vector_multiplicationPfPS_S_i,@function
_Z28matrix_vector_multiplicationPfPS_S_i: # @_Z28matrix_vector_multiplicationPfPS_S_i
	.cfi_startproc
# %bb.0:
	testl	%ecx, %ecx
	jle	.LBB5_5
# %bb.1:                                # %.lr.ph19
	movl	%ecx, %eax
	xorl	%ecx, %ecx
.LBB5_2:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB5_3 Depth 2
	movl	$0, (%rdi,%rcx,4)
	movq	(%rsi,%rcx,8), %r8
	xorps	%xmm0, %xmm0
	xorl	%r9d, %r9d
.LBB5_3:                                #   Parent Loop BB5_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%r8,%r9,4), %xmm1              # xmm1 = mem[0],zero,zero,zero
	mulss	(%rdx,%r9,4), %xmm1
	addss	%xmm1, %xmm0
	movss	%xmm0, (%rdi,%rcx,4)
	incq	%r9
	cmpq	%r9, %rax
	jne	.LBB5_3
# %bb.4:                                # %._crit_edge
                                        #   in Loop: Header=BB5_2 Depth=1
	incq	%rcx
	cmpq	%rax, %rcx
	jne	.LBB5_2
.LBB5_5:                                # %._crit_edge20
	retq
.Lfunc_end5:
	.size	_Z28matrix_vector_multiplicationPfPS_S_i, .Lfunc_end5-_Z28matrix_vector_multiplicationPfPS_S_i
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function _Z20error_on_computationPfPS_S_iS_
.LCPI6_0:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.text
	.globl	_Z20error_on_computationPfPS_S_iS_
	.type	_Z20error_on_computationPfPS_S_iS_,@function
_Z20error_on_computationPfPS_S_iS_:     # @_Z20error_on_computationPfPS_S_iS_
	.cfi_startproc
# %bb.0:
	testl	%ecx, %ecx
	je	.LBB6_1
# %bb.3:                                # %.preheader.preheader
	movslq	%ecx, %rax
	xorps	%xmm0, %xmm0
	xorl	%r9d, %r9d
	movaps	.LCPI6_0(%rip), %xmm1           # xmm1 = [NaN,NaN,NaN,NaN]
.LBB6_4:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB6_5 Depth 2
	movq	(%rsi,%r9,8), %r10
	xorl	%r11d, %r11d
	xorps	%xmm2, %xmm2
.LBB6_5:                                #   Parent Loop BB6_4 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%r10,%r11,4), %xmm3            # xmm3 = mem[0],zero,zero,zero
	mulss	(%rdi,%r11,4), %xmm3
	addss	%xmm3, %xmm2
	incq	%r11
	cmpq	%r11, %rax
	jne	.LBB6_5
# %bb.6:                                #   in Loop: Header=BB6_4 Depth=1
	subss	(%rdx,%r9,4), %xmm2
	andps	%xmm1, %xmm2
	addss	%xmm2, %xmm0
	incq	%r9
	cmpq	%rax, %r9
	jne	.LBB6_4
	jmp	.LBB6_2
.LBB6_1:
	xorps	%xmm0, %xmm0
.LBB6_2:                                # %._crit_edge
	xorps	%xmm1, %xmm1
	cvtsi2ss	%ecx, %xmm1
	divss	%xmm1, %xmm0
	movss	%xmm0, (%r8)
	retq
.Lfunc_end6:
	.size	_Z20error_on_computationPfPS_S_iS_, .Lfunc_end6-_Z20error_on_computationPfPS_S_iS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z10delta_timeNSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEES7_
.LCPI7_0:
	.quad	0x41cdcd6500000000              # double 1.0E+9
	.text
	.globl	_Z10delta_timeNSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEES7_
	.type	_Z10delta_timeNSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEES7_,@function
_Z10delta_timeNSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEES7_: # @_Z10delta_timeNSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEES7_
	.cfi_startproc
# %bb.0:
	subq	%rdi, %rsi
	cvtsi2sd	%rsi, %xmm0
	divsd	.LCPI7_0(%rip), %xmm0
	retq
.Lfunc_end7:
	.size	_Z10delta_timeNSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEES7_, .Lfunc_end7-_Z10delta_timeNSt6chrono10time_pointINS_3_V212system_clockENS_8durationIlSt5ratioILl1ELl1000000000EEEEEES7_
	.cfi_endproc
                                        # -- End function
	.globl	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_ # -- Begin function _Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	.type	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_,@function
_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_: # @_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	.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
	pushq	%rax
	.cfi_def_cfa_offset 64
	.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	%r9, %r14
	movq	%r8, %rbx
	movq	%rcx, %r15
	movq	%rdx, %r12
	movl	%edi, %ebp
	cmpl	$3, %esi
	ja	.LBB8_9
# %bb.1:
	movl	%esi, %eax
	jmpq	*.LJTI8_0(,%rax,8)
.LBB8_2:
	movl	$_ZSt4cerr, %r13d
	movl	$_ZSt4cerr, %edi
	movl	$.L.str.1, %esi
	movl	$17, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cerr(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%r13, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cerr, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.LBB8_3:
	movl	$_ZSt4cerr, %r13d
	movl	$_ZSt4cerr, %edi
	movl	$.L.str.2, %esi
	movl	$17, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cerr(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%r13, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cerr, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.LBB8_4:
	movl	$_ZSt4cerr, %edi
	movl	$.L.str.3, %esi
	movl	$24, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	%ebp, %edi
	callq	hipGetErrorString
	testq	%rax, %rax
	je	.LBB8_5
# %bb.6:
	movq	%rax, %r13
	movq	%rax, %rdi
	callq	strlen
	movl	$_ZSt4cerr, %edi
	movq	%r13, %rsi
	movq	%rax, %rdx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	jmp	.LBB8_7
.LBB8_5:
	movq	_ZSt4cerr(%rip), %rax
	movq	-24(%rax), %rax
	leaq	_ZSt4cerr(%rax), %rdi
	movl	32(%rdi), %esi
	orl	$1, %esi
	callq	_ZNSt9basic_iosIcSt11char_traitsIcEE5clearESt12_Ios_Iostate
.LBB8_7:                                # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit
	movq	_ZSt4cerr(%rip), %rax
	movl	$_ZSt4cerr, %ecx
	movq	-24(%rax), %rdi
	addq	%rcx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cerr, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.LBB8_8:
	movl	$_ZSt4cerr, %edi
	movl	$.L.str.4, %esi
	movl	$41, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	$_ZSt4cerr, %edi
	movl	%ebp, %esi
	callq	_ZNSolsEi
	movq	%rax, %r13
	movl	$.L.str.5, %esi
	movl	$24, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	(%r13), %rax
	movq	-24(%rax), %rdi
	addq	%r13, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%r13, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.LBB8_9:
	movq	%r12, %rdi
	callq	hipFree
	movq	%r15, %rdi
	callq	hipFree
	movq	%r14, %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	hipFree
	movl	%ebp, %eax
	addq	$8, %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_end8:
	.size	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_, .Lfunc_end8-_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	.cfi_endproc
	.section	.rodata,"a",@progbits
	.p2align	3, 0x0
.LJTI8_0:
	.quad	.LBB8_3
	.quad	.LBB8_2
	.quad	.LBB8_4
	.quad	.LBB8_8
                                        # -- End function
	.text
	.globl	_Z24__device_stub__iterationPfS_S_S_j # -- Begin function _Z24__device_stub__iterationPfS_S_S_j
	.type	_Z24__device_stub__iterationPfS_S_S_j,@function
_Z24__device_stub__iterationPfS_S_S_j:  # @_Z24__device_stub__iterationPfS_S_S_j
	.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	12(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%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	$_Z9iterationPfS_S_S_j, %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_end9:
	.size	_Z24__device_stub__iterationPfS_S_S_j, .Lfunc_end9-_Z24__device_stub__iterationPfS_S_S_j
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z17cuda_jacobi_solvePfS_S_fjPiS_
.LCPI10_0:
	.long	0x3f800000                      # float 1
.LCPI10_2:
	.long	0x00000000                      # float 0
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI10_1:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.text
	.globl	_Z17cuda_jacobi_solvePfS_S_fjPiS_
	.type	_Z17cuda_jacobi_solvePfS_S_fjPiS_,@function
_Z17cuda_jacobi_solvePfS_S_fjPiS_:      # @_Z17cuda_jacobi_solvePfS_S_fjPiS_
	.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
	movq	%r9, 80(%rsp)                   # 8-byte Spill
	movq	%r8, 48(%rsp)                   # 8-byte Spill
	movl	%ecx, %ebp
	movss	%xmm0, 60(%rsp)                 # 4-byte Spill
	movq	%rdx, 40(%rsp)                  # 8-byte Spill
	movq	%rsi, 64(%rsp)                  # 8-byte Spill
	movq	%rdi, 72(%rsp)                  # 8-byte Spill
	xorl	%r15d, %r15d
	movq	%r15, 8(%rsp)
	movq	%r15, 16(%rsp)
	movq	%r15, 32(%rsp)
	movq	%r15, 24(%rsp)
	movl	%ecx, %r12d
	leaq	(,%r12,4), %rbx
	movq	%rbx, %rdi
	callq	_Znam
	movq	%rax, %r13
	testl	%ebp, %ebp
	jle	.LBB10_2
# %bb.1:                                # %.lr.ph.i.preheader
	movq	%r13, %rdi
	xorl	%esi, %esi
	movq	%rbx, %rdx
	callq	memset@PLT
.LBB10_2:                               # %_Z10init_constPffi.exit
	movl	%ebp, 56(%rsp)                  # 4-byte Spill
	movl	%ebp, %r14d
	imull	%r14d, %r14d
	shlq	$2, %r14
	movl	$1, %edi
	callq	hipDeviceSetCacheConfig
	leaq	8(%rsp), %rdi
	movq	%rdi, %rbp
	movq	%r14, %rsi
	callq	hipMalloc
	movq	(%rbp), %rdx
	movl	%eax, %edi
	movl	$1, %esi
	xorl	%ecx, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	leaq	16(%rsp), %rbp
	movq	%rbp, %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	leaq	8(%rsp), %rcx
	movq	(%rcx), %rdx
	movq	(%rbp), %rcx
	movl	%eax, %edi
	movl	$1, %esi
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	leaq	32(%rsp), %rbp
	movq	%rbp, %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	leaq	8(%rsp), %rcx
	movq	(%rcx), %rdx
	leaq	16(%rsp), %rcx
	movq	(%rcx), %rcx
	movq	(%rbp), %r8
	movl	%eax, %edi
	movl	$1, %esi
	xorl	%r9d, %r9d
	callq	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	leaq	24(%rsp), %rbp
	movq	%rbp, %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	leaq	8(%rsp), %rcx
	movq	(%rcx), %rdx
	leaq	16(%rsp), %rcx
	movq	(%rcx), %rcx
	leaq	32(%rsp), %rsi
	movq	(%rsi), %r8
	movq	(%rbp), %r9
	movl	%eax, %edi
	movl	$1, %esi
	callq	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	leaq	8(%rsp), %rbp
	movq	(%rbp), %rdi
	movq	72(%rsp), %rsi                  # 8-byte Reload
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%rbp), %rdx
	leaq	16(%rsp), %rbp
	movq	(%rbp), %rcx
	leaq	32(%rsp), %r14
	movq	(%r14), %r8
	leaq	24(%rsp), %rsi
	movq	(%rsi), %r9
	movl	%eax, %edi
	xorl	%esi, %esi
	callq	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	movq	(%rbp), %rdi
	movq	64(%rsp), %rsi                  # 8-byte Reload
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	8(%rsp), %rcx
	movq	(%rcx), %rdx
	movq	(%rbp), %rcx
	movq	(%r14), %r8
	leaq	24(%rsp), %rbp
	movq	(%rbp), %r9
	movl	%eax, %edi
	xorl	%esi, %esi
	callq	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	movq	(%r14), %rdi
	movq	40(%rsp), %rsi                  # 8-byte Reload
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	8(%rsp), %rcx
	movq	(%rcx), %rdx
	leaq	16(%rsp), %rcx
	movq	(%rcx), %rcx
	movq	(%r14), %r8
	movq	(%rbp), %r9
	movl	%eax, %edi
	xorl	%esi, %esi
	callq	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	movq	(%rbp), %rdi
	movq	%r13, %rsi
	movl	56(%rsp), %r14d                 # 4-byte Reload
	movq	%rbx, 40(%rsp)                  # 8-byte Spill
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	8(%rsp), %rcx
	movq	(%rcx), %rdx
	leaq	16(%rsp), %rcx
	movq	(%rcx), %rcx
	leaq	32(%rsp), %rsi
	movq	(%rsi), %r8
	movq	(%rbp), %r9
	movl	%eax, %edi
	xorl	%esi, %esi
	callq	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	movq	48(%rsp), %rax                  # 8-byte Reload
	cmpl	$0, (%rax)
	je	.LBB10_3
# %bb.4:                                # %.lr.ph73
	movabsq	$4294967296, %rbx               # imm = 0x100000000
	movl	%r14d, %eax
	andl	$15, %eax
	movl	%r14d, %ebp
                                        # kill: def $r14d killed $r14d def $r14
	shrl	$4, %r14d
	cmpl	$1, %eax
	sbbl	$-1, %r14d
	btsq	$32, %r14
	xorl	%r15d, %r15d
	addq	$16, %rbx
.LBB10_6:                               # =>This Loop Header: Depth=1
                                        #     Child Loop BB10_10 Depth 2
	movq	%r14, %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB10_8
# %bb.7:                                #   in Loop: Header=BB10_6 Depth=1
	movq	8(%rsp), %rdi
	movq	16(%rsp), %rsi
	movq	32(%rsp), %rdx
	movq	24(%rsp), %rcx
	movl	%ebp, %r8d
	callq	_Z24__device_stub__iterationPfS_S_S_j
.LBB10_8:                               #   in Loop: Header=BB10_6 Depth=1
	callq	hipGetLastError
	movq	8(%rsp), %rdx
	movq	16(%rsp), %rcx
	movq	32(%rsp), %r8
	movq	24(%rsp), %r9
	movl	%eax, %edi
	movl	$2, %esi
	callq	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	callq	hipDeviceSynchronize
	movq	8(%rsp), %rdx
	movq	16(%rsp), %rcx
	movq	32(%rsp), %r8
	movq	24(%rsp), %r9
	movl	%eax, %edi
	movl	$3, %esi
	callq	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	movq	24(%rsp), %rsi
	movq	%r13, %rdi
	movq	40(%rsp), %rdx                  # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rdx
	movq	16(%rsp), %rcx
	movq	32(%rsp), %r8
	movq	24(%rsp), %r9
	movl	%eax, %edi
	xorl	%esi, %esi
	callq	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	xorps	%xmm0, %xmm0
	testl	%ebp, %ebp
	je	.LBB10_11
# %bb.9:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB10_6 Depth=1
	xorl	%eax, %eax
	movaps	.LCPI10_1(%rip), %xmm2          # xmm2 = [NaN,NaN,NaN,NaN]
.LBB10_10:                              # %.lr.ph
                                        #   Parent Loop BB10_6 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%r13,%rax,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	andps	%xmm2, %xmm1
	addss	%xmm1, %xmm0
	incq	%rax
	cmpq	%rax, %r12
	jne	.LBB10_10
.LBB10_11:                              # %._crit_edge
                                        #   in Loop: Header=BB10_6 Depth=1
	ucomiss	.LCPI10_2(%rip), %xmm0
	jb	.LBB10_13
# %bb.12:                               #   in Loop: Header=BB10_6 Depth=1
	sqrtss	%xmm0, %xmm0
	jmp	.LBB10_14
.LBB10_13:                              # %call.sqrt
                                        #   in Loop: Header=BB10_6 Depth=1
	callq	sqrtf@PLT
.LBB10_14:                              # %._crit_edge.split
                                        #   in Loop: Header=BB10_6 Depth=1
	incl	%r15d
	movss	60(%rsp), %xmm1                 # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm0, %xmm1
	jae	.LBB10_15
# %bb.5:                                #   in Loop: Header=BB10_6 Depth=1
	movq	48(%rsp), %rax                  # 8-byte Reload
	cmpl	(%rax), %r15d
	jb	.LBB10_6
	jmp	.LBB10_15
.LBB10_3:
	movss	.LCPI10_0(%rip), %xmm0          # xmm0 = mem[0],zero,zero,zero
.LBB10_15:                              # %._crit_edge74
	movq	48(%rsp), %rax                  # 8-byte Reload
	movl	%r15d, (%rax)
	movq	80(%rsp), %rax                  # 8-byte Reload
	movss	%xmm0, (%rax)
	movq	16(%rsp), %rsi
	movq	64(%rsp), %rdi                  # 8-byte Reload
	movq	40(%rsp), %rdx                  # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rdx
	movq	16(%rsp), %rcx
	movq	32(%rsp), %r8
	movq	24(%rsp), %r9
	movl	%eax, %edi
	xorl	%esi, %esi
	callq	_Z11error_check10hipError_t10ERROR_TYPEPfS1_S1_S1_
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
.Lfunc_end10:
	.size	_Z17cuda_jacobi_solvePfS_S_fjPiS_, .Lfunc_end10-_Z17cuda_jacobi_solvePfS_S_fjPiS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI11_0:
	.long	0x30000000                      # float 4.65661287E-10
.LCPI11_1:
	.long	0x41a00000                      # float 20
.LCPI11_2:
	.long	0xc1200000                      # float -10
	.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	$56, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %r14d
	movq	16(%rbx), %rdi
	callq	atoi
	movl	%eax, %ebp
	movq	24(%rbx), %rdi
	callq	atof
	movsd	%xmm0, 40(%rsp)                 # 8-byte Spill
	movl	%ebp, 16(%rsp)                  # 4-byte Spill
	movl	%ebp, 20(%rsp)
	movl	%r14d, 12(%rsp)                 # 4-byte Spill
	movslq	%r14d, %r14
	leaq	(,%r14,8), %rdi
	testl	%r14d, %r14d
	movq	$-1, %rax
	cmovsq	%rax, %rdi
	leaq	(,%r14,4), %rbx
	cmovsq	%rax, %rbx
	callq	_Znam
	movq	%rax, %r13
	movq	%rbx, %rdi
	callq	_Znam
	movq	%rax, %r15
	movq	%rbx, %rdi
	callq	_Znam
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	movq	%rbx, %rdi
	callq	_Znam
	movq	%rax, %rbp
	movl	%r14d, %r12d
	movq	%r14, 32(%rsp)                  # 8-byte Spill
	testl	%r14d, %r14d
	jle	.LBB11_3
# %bb.1:                                # %.lr.ph.i
	leaq	(,%r12,4), %rbx
	xorl	%r14d, %r14d
.LBB11_2:                               # =>This Inner Loop Header: Depth=1
	movq	%rbx, %rdi
	callq	_Znam
	movq	%rax, (%r13,%r14,8)
	incq	%r14
	cmpq	%r14, %r12
	jne	.LBB11_2
.LBB11_3:                               # %_Z11init_matrixPPfi.exit
	movl	$123, %edi
	callq	srand
	movq	%r13, %rdi
	movq	32(%rsp), %rsi                  # 8-byte Reload
                                        # kill: def $esi killed $esi killed $rsi
	callq	_Z22generate_random_matrixPPfi
	movl	12(%rsp), %r14d                 # 4-byte Reload
	movl	%r14d, %edi
	imull	%edi, %edi
	shlq	$2, %rdi
	callq	malloc
	movq	%rax, %rbx
	testl	%r14d, %r14d
	jle	.LBB11_9
# %bb.4:                                # %.preheader.lr.ph
	xorl	%eax, %eax
	xorl	%ecx, %ecx
.LBB11_5:                               # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB11_6 Depth 2
	movl	%eax, %edx
	leaq	(%rbx,%rdx,4), %rdx
	movq	(%r13,%rcx,8), %rsi
	xorl	%edi, %edi
.LBB11_6:                               #   Parent Loop BB11_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rsi,%rdi,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%rdx,%rdi,4)
	incq	%rdi
	cmpq	%rdi, %r12
	jne	.LBB11_6
# %bb.7:                                # %._crit_edge
                                        #   in Loop: Header=BB11_5 Depth=1
	incq	%rcx
	addl	%r14d, %eax
	cmpq	%r12, %rcx
	jne	.LBB11_5
# %bb.8:                                # %._crit_edge97
	testl	%r14d, %r14d
	jle	.LBB11_9
# %bb.10:                               # %.lr.ph.preheader.i
	xorl	%r14d, %r14d
.LBB11_11:                              # %.lr.ph.i75
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI11_0(%rip), %xmm0
	mulss	.LCPI11_1(%rip), %xmm0
	addss	.LCPI11_2(%rip), %xmm0
	movss	%xmm0, (%rbp,%r14,4)
	incq	%r14
	cmpq	%r14, %r12
	jne	.LBB11_11
# %bb.12:                               # %_Z22generate_random_vectorPfi.exit
	xorl	%eax, %eax
	movl	12(%rsp), %esi                  # 4-byte Reload
.LBB11_13:                              # %.lr.ph.i79
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB11_14 Depth 2
	movl	$0, (%r15,%rax,4)
	movq	(%r13,%rax,8), %rcx
	xorps	%xmm0, %xmm0
	xorl	%edx, %edx
.LBB11_14:                              #   Parent Loop BB11_13 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rcx,%rdx,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	mulss	(%rbp,%rdx,4), %xmm1
	addss	%xmm1, %xmm0
	movss	%xmm0, (%r15,%rax,4)
	incq	%rdx
	cmpq	%rdx, %r12
	jne	.LBB11_14
# %bb.15:                               # %._crit_edge.i
                                        #   in Loop: Header=BB11_13 Depth=1
	incq	%rax
	cmpq	%r12, %rax
	jne	.LBB11_13
# %bb.16:                               # %_Z28matrix_vector_multiplicationPfPS_S_i.exit
	testl	%esi, %esi
	movq	24(%rsp), %r13                  # 8-byte Reload
	jle	.LBB11_18
# %bb.17:                               # %.lr.ph.preheader.i84
	shlq	$2, %r12
	movq	%r13, %rdi
	xorl	%esi, %esi
	movq	%r12, %rdx
	callq	memset@PLT
	jmp	.LBB11_18
.LBB11_9:                               # %_Z28matrix_vector_multiplicationPfPS_S_i.exit.thread
	movq	24(%rsp), %r13                  # 8-byte Reload
.LBB11_18:                              # %_Z10init_constPffi.exit
	xorps	%xmm0, %xmm0
	cvtsd2ss	40(%rsp), %xmm0                 # 8-byte Folded Reload
	movss	%xmm0, 12(%rsp)                 # 4-byte Spill
	leaq	20(%rsp), %r14
	movl	16(%rsp), %eax                  # 4-byte Reload
	movl	%eax, (%r14)
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	leaq	52(%rsp), %r9
	movq	%rbx, %rdi
	movq	%r13, %rsi
	movq	%r15, %rdx
	movss	12(%rsp), %xmm0                 # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	movq	32(%rsp), %rcx                  # 8-byte Reload
                                        # kill: def $ecx killed $ecx killed $rcx
	movq	%r14, %r8
	callq	_Z17cuda_jacobi_solvePfS_S_fjPiS_
.Lfunc_end11:
	.size	main, .Lfunc_end11-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	.LBB12_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB12_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z9iterationPfS_S_S_j, %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_end12:
	.size	__hip_module_ctor, .Lfunc_end12-__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	.LBB13_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
.LBB13_2:
	retq
.Lfunc_end13:
	.size	__hip_module_dtor, .Lfunc_end13-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"hipMalloc failed!"
	.size	.L.str.1, 18

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"hipMemcpy failed!"
	.size	.L.str.2, 18

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"addKernel launch failed:"
	.size	.L.str.3, 25

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"hipDeviceSynchronize returned error code "
	.size	.L.str.4, 42

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	" after launching jacobi!"
	.size	.L.str.5, 25

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

	.type	.L__unnamed_1,@object           # @0
	.section	.rodata.str1.1,"aMS",@progbits,1
.L__unnamed_1:
	.asciz	"_Z9iterationPfS_S_S_j"
	.size	.L__unnamed_1, 22

	.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 _Z24__device_stub__iterationPfS_S_S_j
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _ZSt4cerr
	.addrsig_sym _Z9iterationPfS_S_S_j
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z9iterationPfS_S_S_j   ; -- Begin function _Z9iterationPfS_S_S_j
	.globl	_Z9iterationPfS_S_S_j
	.p2align	8
	.type	_Z9iterationPfS_S_S_j,@function
_Z9iterationPfS_S_S_j:                  ; @_Z9iterationPfS_S_S_j
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x34
	s_load_b32 s8, s[0:1], 0x20
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[2:3], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_u32_e64 s8, v2
	s_cbranch_execz .LBB0_6
; %bb.1:
	s_load_b256 s[0:7], s[0:1], 0x0
	v_dual_mov_b32 v4, 0 :: v_dual_mov_b32 v7, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mov_b32_e32 v3, v4
	v_lshlrev_b64 v[0:1], 2, v[2:3]
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v5, vcc_lo, s4, v0
	v_add_co_ci_u32_e32 v6, vcc_lo, s5, v1, vcc_lo
	s_mov_b64 s[4:5], s[2:3]
	global_load_b32 v5, v[5:6], off
	v_mul_lo_u32 v6, v2, s8
	s_delay_alu instid0(VALU_DEP_1)
	v_mov_b32_e32 v3, v6
.LBB0_2:                                ; =>This Inner Loop Header: Depth=1
	s_mov_b32 s9, exec_lo
	v_cmpx_ne_u32_e32 0, v7
	s_cbranch_execz .LBB0_4
; %bb.3:                                ;   in Loop: Header=BB0_2 Depth=1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[8:9], 2, v[3:4]
	s_load_b32 s10, s[4:5], 0x0
	v_add_co_u32 v8, vcc_lo, s0, v8
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v9, vcc_lo, s1, v9, vcc_lo
	global_load_b32 v8, v[8:9], off
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_fma_f32 v5, -v8, s10, v5
.LBB0_4:                                ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s9
	s_add_i32 s8, s8, -1
	v_add_nc_u32_e32 v7, -1, v7
	v_add_nc_u32_e32 v3, 1, v3
	s_add_u32 s4, s4, 4
	s_addc_u32 s5, s5, 0
	s_cmp_lg_u32 s8, 0
	s_cbranch_scc1 .LBB0_2
; %bb.5:
	v_dual_mov_b32 v3, 0 :: v_dual_add_nc_u32 v2, v6, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[2:3]
	v_add_co_u32 v2, vcc_lo, s0, v2
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v3, vcc_lo
	global_load_b32 v4, v[2:3], off
	v_add_co_u32 v2, vcc_lo, s2, v0
	v_add_co_ci_u32_e32 v3, vcc_lo, s3, v1, vcc_lo
	global_load_b32 v6, v[2:3], off
	s_waitcnt vmcnt(1)
	v_div_scale_f32 v7, null, v4, v4, v5
	v_div_scale_f32 v10, vcc_lo, v5, v4, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v8, v7
	s_waitcnt_depctr 0xfff
	v_fma_f32 v9, -v7, v8, 1.0
	v_fmac_f32_e32 v8, v9, v8
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v9, v10, v8
	v_fma_f32 v11, -v7, v9, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v9, v11, v8
	v_fma_f32 v7, -v7, v9, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_div_fmas_f32 v7, v7, v8, v9
	v_add_co_u32 v0, vcc_lo, s6, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s7, v1, vcc_lo
	v_div_fixup_f32 v4, v7, v4, v5
	s_waitcnt vmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sub_f32_e32 v5, v6, v4
	v_mul_f32_e32 v5, v5, v5
	global_store_b32 v[0:1], v5, off
	global_store_b32 v[2:3], v4, off
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
.LBB0_6:
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z9iterationPfS_S_S_j
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 296
		.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 12
		.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	_Z9iterationPfS_S_S_j, .Lfunc_end0-_Z9iterationPfS_S_S_j
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 464
; NumSgprs: 18
; NumVgprs: 12
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 12
; 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
      - .offset:         32
        .size:           4
        .value_kind:     by_value
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         44
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         52
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         54
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         56
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         58
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         60
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         62
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         104
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 296
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z9iterationPfS_S_S_j
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z9iterationPfS_S_S_j.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     12
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
