#include <stdio.h>
#include <time.h>
#include <math.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_profiler_api.h>
#include <cuda_fp16.h>

#define EPS 0.0000001f
#define SIZE 1024
#define BIG_VALUE 65536
#define BLOCK_SIZE 256

// generate random matrix
void getMatrix(float* matrix, unsigned size)
{
  if (matrix == NULL){
	  fprintf(stderr, "getMatrix doesn't get matrix\n");
	  exit(-1);
  }
  for (unsigned i = 0; i < size; i++) {
    for (unsigned j = 0; j < size; j++) {
      matrix[i + size * j] = (((float)rand() / RAND_MAX) - 0.5f) * 10;
    }
  }

  // add diagonal predominance
  for (unsigned i = 0; i < size; i++) {
    matrix[i + size * i] += 1000;
  }
}

// generate random vector
void get_f(float *f, unsigned size)
{
  if (get_f == NULL){
	  fprintf(stderr, "get_f doen't get vector\n");
	  exit(-1);
  }
  for (unsigned i = 0; i < size; i++) {
    f[i] = (((float)rand() / RAND_MAX) - 0.5f) * 10;
  }
}

// compute Matrix B, and return result in argument
void  computeBMatrix(float *A, unsigned size)
{
  if (A == NULL){
	  fprintf(stderr, "computeBMatrix doesn't get matrix\n");
	  exit(-1);
  }
  float *inverse_D = (float *)calloc(size, sizeof(float));
  if (inverse_D == NULL){
	  fprintf(stderr, "error on allocate memory\n");
	  exit(-1);
  }
  //D = diag(A)
  //compute D^-1
  for (unsigned i = 0; i < size; i++) {
    inverse_D[i] = 1.0f / A[i + size * i];
  }

  //compute D^-1 * A
  for (unsigned i = 0; i < size; i++) { //columns
    for (unsigned j = 0; j < size; j++) { //lines
      A[i + size * j] = inverse_D[j] * A[i + size * j];
    }
  }

  //compute I - D^-1 * A
  for (unsigned i = 0; i < size; i++) {
	for (unsigned j = 0; j < size; j++) {
	  float tmp = (i == j) ? 1 : 0;
	  A[i + size * j] = tmp - A[i + size * j];
	}
  }

  free(inverse_D);
}

// compute vector g and return in argument
void compute_g(float *A, float* b, unsigned size)
{
  if (A == NULL || b == NULL){
	  fprintf(stderr, "compute_g get invalud arguments\n");
	  exit(-1);
  }
  // g = diag(A) ^ -1 * b;
  for (unsigned i = 0; i < size; i++) {
    b[i] = (1.0f / A[i + size * i]) * b[i];
  }
}

// metric is : max(|Ax_i - f_i|)
float precision(float *matrix, float *f, float* x, unsigned size)
{
  if (matrix == NULL || f == NULL || x == NULL){
	  fprintf(stderr, "precision get invalid parametr\n");
	  exit(-1);
  }

  float max = 0;
  for (unsigned i = 0; i < size; i++) {
    float cur = 0;
    for (unsigned j = 0; j < size; j++) {
      cur += matrix[j + size * i] * x[j];
    }
    cur = fabs(f[i] - cur);
    max = max < cur ? cur : max;
  }
  return max;
}

// cuda kernel
//with transpose matrix B
__global__ void jacobi(float* B, float* g, float* x, unsigned size, float* x_next)
{
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  float x_curr = 0;

  __shared__ float shared_x[SIZE];

  for (int i = 0; i < SIZE / blockDim.x; i++){
	  int loc_idx = blockDim.x * i + threadIdx.x;
	  shared_x[loc_idx] = x[loc_idx];
  }
  __syncthreads();

#pragma uroll 16
  for (int i = 0; i < size; i++) {
    // here loc_i is useless, becouse matrix B
    x_curr += B[idx + i * size] * shared_x[i];
  }
  x_next[idx] = x_curr + g[idx];
}

void transpose(float* matrix, unsigned size)
{
	for (unsigned i = 0; i < size; i++){
		for (unsigned j = 0; j < i; j++){
			//swap value
			float tmp = matrix[i + j * size];
			matrix[i + j * size] = matrix[j + i * size];
			matrix[j + i * size] = tmp;
		}
	}
}

void checkGPUOperation()
{
	cudaError_t code = cudaGetLastError();
	if (code != cudaSuccess){
		fprintf(stderr, "Cuda Error : %s\n", cudaGetErrorString(code));
		exit(-1);
	}
}

int main()
{
  float eps = EPS;
  unsigned size = SIZE;

  // alloc memory on CPU
  float *host_A = (float *)malloc(size * size * sizeof(float));
  float *host_B = (float *)malloc(size * size * sizeof(float));
  float *host_f = (float *)malloc(size * sizeof(float));
  float *host_g = (float *)malloc(size * sizeof(float));
  float *host_x = (float *)calloc(size, sizeof(float)); // start with null vector
  if (host_A == NULL || host_B == NULL || host_f == NULL
	  || host_g == NULL || host_x == NULL){
    fprintf(stderr, "error on allocate memory\n");
	return -1;
  }

  getMatrix(host_A, size);
  get_f(host_f, size);

  memcpy(host_B, host_A, size * size * sizeof(float));
  memcpy(host_g, host_f, size * sizeof(float));

  compute_g(host_A, host_g, size);
  computeBMatrix(host_B, size);
  transpose(host_B, size); // transpose for optimization

  // alloc memory on GPU
  float *dev_B, *dev_g, *dev_x_prev, *dev_x_next;
  cudaMalloc((void **)&dev_B, size * size * sizeof(float));
  checkGPUOperation();
  cudaMalloc((void **)&dev_g, size * sizeof(float));
  checkGPUOperation();
  cudaMalloc((void **)&dev_x_prev, size * sizeof(float));
  checkGPUOperation();
  cudaMalloc((void **)&dev_x_next, size * sizeof(float));
  checkGPUOperation();

  //copy memory from CPU to GPU
  cudaMemcpy(dev_x_prev, host_x, size * sizeof(float), cudaMemcpyHostToDevice);
  checkGPUOperation();
  cudaMemcpy(dev_x_next, dev_x_prev, size * sizeof(float), cudaMemcpyDeviceToDevice);
  checkGPUOperation();
  cudaMemcpy(dev_B, host_B, size * size * sizeof(float), cudaMemcpyHostToDevice);
  checkGPUOperation();
  cudaMemcpy(dev_g, host_g, size * sizeof(float), cudaMemcpyHostToDevice);

  float p = 0; //precision
  float prev_p = BIG_VALUE; //previos precision
  // when precision stop decrease stop compute
  do {
    for (int i = 0; i < 512; i++) {
      //swap vectors
      float *tmp = dev_x_next;
      dev_x_next = dev_x_prev;
      dev_x_prev = tmp;

	  jacobi <<<dim3(size / BLOCK_SIZE), dim3(BLOCK_SIZE)>>> (dev_B, dev_g, dev_x_prev, size, dev_x_next);
	  cudaDeviceSynchronize();
    }
	//get result
    cudaMemcpy(host_x, dev_x_next, size * sizeof(float), cudaMemcpyDeviceToHost);
	checkGPUOperation();

	prev_p = p;
	p = precision(host_A, host_f, host_x, size);
	printf("precision : %f \n", p);
  } while (p > eps && p < prev_p);

  printf("success\n");

  //free memory on CPU
  free(host_A);
  free(host_B);
  free(host_f);
  free(host_g);
  free(host_x);

  //free memory on GPU
  cudaFree(dev_B);
  cudaFree(dev_g);
  cudaFree(dev_x_prev);
  cudaFree(dev_x_next);

#ifdef _WIN32
  // stop console
  // only Windows needed
  scanf("\n");
#endif
  return 0;
}


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

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2411:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2411:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"getMatrix doesn't get matrix\n"
	.text
	.globl	_Z9getMatrixPfj
	.type	_Z9getMatrixPfj, @function
_Z9getMatrixPfj:
.LFB2401:
	.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
	pushq	%rdx
	.cfi_def_cfa_offset 64
	testq	%rdi, %rdi
	je	.L3
	movq	%rdi, %rbp
	movl	%esi, %ebx
	movl	%esi, %r15d
	xorl	%r12d, %r12d
	jmp	.L4
.L3:
	movq	stderr(%rip), %rdi
	leaq	.LC0(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L5:
	call	rand@PLT
	incq	%r14
	movl	%eax, %edx
	movl	%r13d, %eax
	addl	%ebx, %r13d
	cvtsi2ssl	%edx, %xmm0
	mulss	.LC1(%rip), %xmm0
	subss	.LC2(%rip), %xmm0
	mulss	.LC3(%rip), %xmm0
	movss	%xmm0, 0(%rbp,%rax,4)
	cmpq	%r15, %r14
	jne	.L5
	incq	%r12
.L4:
	movl	%r12d, %r13d
	cmpq	%r15, %r12
	je	.L12
	xorl	%r14d, %r14d
	jmp	.L5
.L12:
	movss	.LC4(%rip), %xmm0
	leal	1(%rbx), %esi
	xorl	%edx, %edx
	xorl	%eax, %eax
.L6:
	cmpl	%eax, %ebx
	je	.L13
	movl	%edx, %ecx
	incl	%eax
	addl	%esi, %edx
	leaq	0(%rbp,%rcx,4), %rcx
	movss	(%rcx), %xmm1
	addss	%xmm0, %xmm1
	movss	%xmm1, (%rcx)
	jmp	.L6
.L13:
	popq	%rax
	.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
.LFE2401:
	.size	_Z9getMatrixPfj, .-_Z9getMatrixPfj
	.globl	_Z5get_fPfj
	.type	_Z5get_fPfj, @function
_Z5get_fPfj:
.LFB2402:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	%esi, %esi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	leaq	(%rdi,%rsi,4), %rbp
	movq	%rdi, %rbx
	pushq	%rdx
	.cfi_def_cfa_offset 32
.L15:
	cmpq	%rbp, %rbx
	je	.L18
	call	rand@PLT
	addq	$4, %rbx
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC1(%rip), %xmm0
	subss	.LC2(%rip), %xmm0
	mulss	.LC3(%rip), %xmm0
	movss	%xmm0, -4(%rbx)
	jmp	.L15
.L18:
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2402:
	.size	_Z5get_fPfj, .-_Z5get_fPfj
	.section	.rodata.str1.1
.LC7:
	.string	"computeBMatrix doesn't get matrix\n"
.LC8:
	.string	"error on allocate memory\n"
	.text
	.globl	_Z14computeBMatrixPfj
	.type	_Z14computeBMatrixPfj, @function
_Z14computeBMatrixPfj:
.LFB2403:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	pushq	%rdx
	.cfi_def_cfa_offset 32
	leaq	.LC7(%rip), %rdx
	testq	%rdi, %rdi
	je	.L35
	movq	%rdi, %rbp
	movl	%esi, %ebx
	movl	%esi, %edi
	movl	$4, %esi
	call	calloc@PLT
	movq	%rax, %rdi
	testq	%rax, %rax
	je	.L21
	movss	.LC5(%rip), %xmm0
	leal	1(%rbx), %ecx
	xorl	%edx, %edx
	xorl	%eax, %eax
	jmp	.L22
.L21:
	leaq	.LC8(%rip), %rdx
.L35:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L22:
	cmpl	%ebx, %eax
	jnb	.L36
	movl	%edx, %esi
	movaps	%xmm0, %xmm1
	addl	%ecx, %edx
	divss	0(%rbp,%rsi,4), %xmm1
	movss	%xmm1, (%rdi,%rax,4)
	incq	%rax
	jmp	.L22
.L36:
	xorl	%eax, %eax
.L24:
	cmpl	%ebx, %eax
	je	.L37
	movl	%eax, %ecx
	xorl	%edx, %edx
.L25:
	movl	%ecx, %esi
	addl	%ebx, %ecx
	leaq	0(%rbp,%rsi,4), %rsi
	movss	(%rsi), %xmm0
	mulss	(%rdi,%rdx,4), %xmm0
	incq	%rdx
	movss	%xmm0, (%rsi)
	cmpl	%ebx, %edx
	jb	.L25
	incl	%eax
	jmp	.L24
.L37:
	xorl	%eax, %eax
.L26:
	cmpl	%ebx, %eax
	je	.L38
	movl	%eax, %ecx
	xorl	%edx, %edx
.L28:
	xorps	%xmm0, %xmm0
	cmpl	%edx, %eax
	jne	.L27
	movss	.LC5(%rip), %xmm0
.L27:
	movl	%ecx, %esi
	incl	%edx
	addl	%ebx, %ecx
	leaq	0(%rbp,%rsi,4), %rsi
	subss	(%rsi), %xmm0
	movss	%xmm0, (%rsi)
	cmpl	%edx, %ebx
	jne	.L28
	incl	%eax
	jmp	.L26
.L38:
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	free@PLT
	.cfi_endproc
.LFE2403:
	.size	_Z14computeBMatrixPfj, .-_Z14computeBMatrixPfj
	.section	.rodata.str1.1
.LC9:
	.string	"compute_g get invalud arguments\n"
	.text
	.globl	_Z9compute_gPfS_j
	.type	_Z9compute_gPfS_j, @function
_Z9compute_gPfS_j:
.LFB2404:
	.cfi_startproc
	endbr64
	testq	%rdi, %rdi
	je	.L40
	testq	%rsi, %rsi
	je	.L40
	movss	.LC5(%rip), %xmm1
	leal	1(%rdx), %r8d
	xorl	%ecx, %ecx
	xorl	%eax, %eax
	jmp	.L41
.L40:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC9(%rip), %rdx
	call	__fprintf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L41:
	.cfi_def_cfa_offset 8
	cmpl	%edx, %eax
	jnb	.L45
	movl	%ecx, %r9d
	movaps	%xmm1, %xmm0
	addl	%r8d, %ecx
	divss	(%rdi,%r9,4), %xmm0
	mulss	(%rsi,%rax,4), %xmm0
	movss	%xmm0, (%rsi,%rax,4)
	incq	%rax
	jmp	.L41
.L45:
	ret
	.cfi_endproc
.LFE2404:
	.size	_Z9compute_gPfS_j, .-_Z9compute_gPfS_j
	.section	.rodata.str1.1
.LC10:
	.string	"precision get invalid parametr\n"
	.text
	.globl	_Z9precisionPfS_S_j
	.type	_Z9precisionPfS_S_j, @function
_Z9precisionPfS_S_j:
.LFB2405:
	.cfi_startproc
	endbr64
	testq	%rsi, %rsi
	movq	%rdx, %r8
	sete	%al
	testq	%rdx, %rdx
	sete	%dl
	orb	%dl, %al
	jne	.L54
	testq	%rdi, %rdi
	jne	.L52
.L54:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC10(%rip), %rdx
	call	__fprintf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L52:
	.cfi_def_cfa_offset 8
	xorl	%r9d, %r9d
	xorps	%xmm1, %xmm1
	movss	.LC11(%rip), %xmm3
	xorl	%edx, %edx
.L47:
	cmpl	%ecx, %edx
	jnb	.L59
	xorl	%eax, %eax
	xorps	%xmm2, %xmm2
.L49:
	leal	(%r9,%rax), %r10d
	movss	(%rdi,%r10,4), %xmm0
	mulss	(%r8,%rax,4), %xmm0
	incq	%rax
	addss	%xmm0, %xmm2
	cmpl	%ecx, %eax
	jb	.L49
	movss	(%rsi,%rdx,4), %xmm0
	addl	%ecx, %r9d
	incq	%rdx
	subss	%xmm2, %xmm0
	andps	%xmm3, %xmm0
	maxss	%xmm1, %xmm0
	movaps	%xmm0, %xmm1
	jmp	.L47
.L59:
	movaps	%xmm1, %xmm0
	ret
	.cfi_endproc
.LFE2405:
	.size	_Z9precisionPfS_S_j, .-_Z9precisionPfS_S_j
	.globl	_Z9transposePfj
	.type	_Z9transposePfj, @function
_Z9transposePfj:
.LFB2406:
	.cfi_startproc
	endbr64
	xorl	%ecx, %ecx
	xorl	%eax, %eax
.L61:
	cmpl	%esi, %eax
	je	.L65
	movl	%eax, %r8d
	xorl	%edx, %edx
.L63:
	cmpl	%edx, %eax
	je	.L66
	movl	%r8d, %r9d
	addl	%esi, %r8d
	leaq	(%rdi,%r9,4), %r10
	leal	(%rdx,%rcx), %r9d
	incl	%edx
	leaq	(%rdi,%r9,4), %r9
	movss	(%r10), %xmm0
	movss	(%r9), %xmm1
	movss	%xmm1, (%r10)
	movss	%xmm0, (%r9)
	jmp	.L63
.L66:
	incl	%eax
	addl	%esi, %ecx
	jmp	.L61
.L65:
	ret
	.cfi_endproc
.LFE2406:
	.size	_Z9transposePfj, .-_Z9transposePfj
	.section	.rodata.str1.1
.LC12:
	.string	"Cuda Error : %s\n"
	.text
	.globl	_Z17checkGPUOperationv
	.type	_Z17checkGPUOperationv, @function
_Z17checkGPUOperationv:
.LFB2407:
	.cfi_startproc
	endbr64
	pushq	%rdx
	.cfi_def_cfa_offset 16
	call	cudaGetLastError@PLT
	testl	%eax, %eax
	je	.L67
	movl	%eax, %edi
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC12(%rip), %rdx
	movq	%rax, %rcx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L67:
	popq	%rax
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2407:
	.size	_Z17checkGPUOperationv, .-_Z17checkGPUOperationv
	.globl	_Z32__device_stub__Z6jacobiPfS_S_jS_PfS_S_jS_
	.type	_Z32__device_stub__Z6jacobiPfS_S_jS_PfS_S_jS_, @function
_Z32__device_stub__Z6jacobiPfS_S_jS_PfS_S_jS_:
.LFB2433:
	.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
	movl	%ecx, 20(%rsp)
	leaq	56(%rsp), %rcx
	movq	%r8, 8(%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	20(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	8(%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	.L70
	pushq	56(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z6jacobiPfS_S_jS_(%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
.L70:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L72
	call	__stack_chk_fail@PLT
.L72:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2433:
	.size	_Z32__device_stub__Z6jacobiPfS_S_jS_PfS_S_jS_, .-_Z32__device_stub__Z6jacobiPfS_S_jS_PfS_S_jS_
	.globl	_Z6jacobiPfS_S_jS_
	.type	_Z6jacobiPfS_S_jS_, @function
_Z6jacobiPfS_S_jS_:
.LFB2434:
	.cfi_startproc
	endbr64
	jmp	_Z32__device_stub__Z6jacobiPfS_S_jS_PfS_S_jS_
	.cfi_endproc
.LFE2434:
	.size	_Z6jacobiPfS_S_jS_, .-_Z6jacobiPfS_S_jS_
	.section	.rodata.str1.1
.LC13:
	.string	"precision : %f \n"
.LC15:
	.string	"success\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2408:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$4194304, %edi
	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	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$4194304, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movl	$4096, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	movl	$4096, %edi
	movq	%rax, %r13
	call	malloc@PLT
	movl	$4, %esi
	movl	$1024, %edi
	movq	%rax, %r12
	call	calloc@PLT
	testq	%rbp, %rbp
	je	.L76
	testq	%rbx, %rbx
	je	.L76
	testq	%r13, %r13
	movq	%rax, %r14
	sete	%al
	testq	%r12, %r12
	sete	%dl
	orb	%dl, %al
	jne	.L76
	testq	%r14, %r14
	jne	.L77
.L76:
	movq	stderr(%rip), %rdi
	leaq	.LC8(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	orl	$-1, %eax
	jmp	.L75
.L77:
	movl	$1024, %esi
	movq	%rbp, %rdi
	call	_Z9getMatrixPfj
	movl	$1024, %esi
	movq	%r13, %rdi
	call	_Z5get_fPfj
	movl	$1048576, %ecx
	movq	%rbx, %rdi
	movq	%rbp, %rsi
	rep movsl
	movl	$1024, %ecx
	movq	%r12, %rdi
	movq	%r13, %rsi
	rep movsl
	movl	$1024, %edx
	movq	%r12, %rsi
	movq	%rbp, %rdi
	call	_Z9compute_gPfS_j
	movl	$1024, %esi
	movq	%rbx, %rdi
	call	_Z14computeBMatrixPfj
	movl	$1024, %esi
	movq	%rbx, %rdi
	call	_Z9transposePfj
	leaq	16(%rsp), %rdi
	movl	$4194304, %esi
	call	cudaMalloc@PLT
	call	_Z17checkGPUOperationv
	leaq	24(%rsp), %rdi
	movl	$4096, %esi
	call	cudaMalloc@PLT
	call	_Z17checkGPUOperationv
	leaq	32(%rsp), %rdi
	movl	$4096, %esi
	call	cudaMalloc@PLT
	call	_Z17checkGPUOperationv
	leaq	40(%rsp), %rdi
	movl	$4096, %esi
	call	cudaMalloc@PLT
	call	_Z17checkGPUOperationv
	movq	32(%rsp), %rdi
	movl	$1, %ecx
	movq	%r14, %rsi
	movl	$4096, %edx
	call	cudaMemcpy@PLT
	call	_Z17checkGPUOperationv
	movq	32(%rsp), %rsi
	movq	40(%rsp), %rdi
	movl	$3, %ecx
	movl	$4096, %edx
	call	cudaMemcpy@PLT
	call	_Z17checkGPUOperationv
	movq	16(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$4194304, %edx
	call	cudaMemcpy@PLT
	call	_Z17checkGPUOperationv
	movq	24(%rsp), %rdi
	movl	$1, %ecx
	movq	%r12, %rsi
	movl	$4096, %edx
	call	cudaMemcpy@PLT
	movl	$0x00000000, 8(%rsp)
.L84:
	movl	$512, %r15d
.L81:
	movq	32(%rsp), %rdx
	movq	40(%rsp), %rax
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1073741825, %edi
	movl	$1, %ecx
	movl	$1, %esi
	movq	%rdx, 40(%rsp)
	movl	$16777217, %edx
	salq	$2, %rdi
	salq	$8, %rdx
	movq	%rax, 32(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L80
	movq	40(%rsp), %r8
	movq	32(%rsp), %rdx
	movl	$1024, %ecx
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdi
	call	_Z32__device_stub__Z6jacobiPfS_S_jS_PfS_S_jS_
.L80:
	call	cudaDeviceSynchronize@PLT
	decl	%r15d
	jne	.L81
	movq	40(%rsp), %rsi
	movl	$2, %ecx
	movl	$4096, %edx
	movq	%r14, %rdi
	call	cudaMemcpy@PLT
	call	_Z17checkGPUOperationv
	movl	$1024, %ecx
	movq	%r14, %rdx
	movq	%r13, %rsi
	movq	%rbp, %rdi
	call	_Z9precisionPfS_S_j
	leaq	.LC13(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movaps	%xmm0, %xmm1
	cvtss2sd	%xmm0, %xmm0
	movss	%xmm1, 12(%rsp)
	call	__printf_chk@PLT
	movss	12(%rsp), %xmm1
	comiss	.LC14(%rip), %xmm1
	jbe	.L82
	movss	8(%rsp), %xmm2
	comiss	%xmm1, %xmm2
	jbe	.L82
	movss	%xmm1, 8(%rsp)
	jmp	.L84
.L82:
	leaq	.LC15(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	%r14, %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	xorl	%eax, %eax
.L75:
	movq	72(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L85
	call	__stack_chk_fail@PLT
.L85:
	addq	$88, %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
.LFE2408:
	.size	main, .-main
	.section	.rodata.str1.1
.LC16:
	.string	"_Z6jacobiPfS_S_jS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2436:
	.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	.LC16(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z6jacobiPfS_S_jS_(%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
.LFE2436:
	.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
.LC1:
	.long	805306368
	.align 4
.LC2:
	.long	1056964608
	.align 4
.LC3:
	.long	1092616192
	.align 4
.LC4:
	.long	1148846080
	.align 4
.LC5:
	.long	1065353216
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC11:
	.long	2147483647
	.long	0
	.long	0
	.long	0
	.section	.rodata.cst4
	.align 4
.LC14:
	.long	869711765
	.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 <stdio.h>
#include <time.h>
#include <math.h>
#include <hip/hip_runtime.h>

#include <hip/hip_runtime_api.h>
#include <hip/hip_fp16.h>

#define EPS 0.0000001f
#define SIZE 1024
#define BIG_VALUE 65536
#define BLOCK_SIZE 256

// generate random matrix
void getMatrix(float* matrix, unsigned size)
{
  if (matrix == NULL){
	  fprintf(stderr, "getMatrix doesn't get matrix\n");
	  exit(-1);
  }
  for (unsigned i = 0; i < size; i++) {
    for (unsigned j = 0; j < size; j++) {
      matrix[i + size * j] = (((float)rand() / RAND_MAX) - 0.5f) * 10;
    }
  }

  // add diagonal predominance
  for (unsigned i = 0; i < size; i++) {
    matrix[i + size * i] += 1000;
  }
}

// generate random vector
void get_f(float *f, unsigned size)
{
  if (get_f == NULL){
	  fprintf(stderr, "get_f doen't get vector\n");
	  exit(-1);
  }
  for (unsigned i = 0; i < size; i++) {
    f[i] = (((float)rand() / RAND_MAX) - 0.5f) * 10;
  }
}

// compute Matrix B, and return result in argument
void  computeBMatrix(float *A, unsigned size)
{
  if (A == NULL){
	  fprintf(stderr, "computeBMatrix doesn't get matrix\n");
	  exit(-1);
  }
  float *inverse_D = (float *)calloc(size, sizeof(float));
  if (inverse_D == NULL){
	  fprintf(stderr, "error on allocate memory\n");
	  exit(-1);
  }
  //D = diag(A)
  //compute D^-1
  for (unsigned i = 0; i < size; i++) {
    inverse_D[i] = 1.0f / A[i + size * i];
  }

  //compute D^-1 * A
  for (unsigned i = 0; i < size; i++) { //columns
    for (unsigned j = 0; j < size; j++) { //lines
      A[i + size * j] = inverse_D[j] * A[i + size * j];
    }
  }

  //compute I - D^-1 * A
  for (unsigned i = 0; i < size; i++) {
	for (unsigned j = 0; j < size; j++) {
	  float tmp = (i == j) ? 1 : 0;
	  A[i + size * j] = tmp - A[i + size * j];
	}
  }

  free(inverse_D);
}

// compute vector g and return in argument
void compute_g(float *A, float* b, unsigned size)
{
  if (A == NULL || b == NULL){
	  fprintf(stderr, "compute_g get invalud arguments\n");
	  exit(-1);
  }
  // g = diag(A) ^ -1 * b;
  for (unsigned i = 0; i < size; i++) {
    b[i] = (1.0f / A[i + size * i]) * b[i];
  }
}

// metric is : max(|Ax_i - f_i|)
float precision(float *matrix, float *f, float* x, unsigned size)
{
  if (matrix == NULL || f == NULL || x == NULL){
	  fprintf(stderr, "precision get invalid parametr\n");
	  exit(-1);
  }

  float max = 0;
  for (unsigned i = 0; i < size; i++) {
    float cur = 0;
    for (unsigned j = 0; j < size; j++) {
      cur += matrix[j + size * i] * x[j];
    }
    cur = fabs(f[i] - cur);
    max = max < cur ? cur : max;
  }
  return max;
}

// cuda kernel
//with transpose matrix B
__global__ void jacobi(float* B, float* g, float* x, unsigned size, float* x_next)
{
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  float x_curr = 0;

  __shared__ float shared_x[SIZE];

  for (int i = 0; i < SIZE / blockDim.x; i++){
	  int loc_idx = blockDim.x * i + threadIdx.x;
	  shared_x[loc_idx] = x[loc_idx];
  }
  __syncthreads();

#pragma uroll 16
  for (int i = 0; i < size; i++) {
    // here loc_i is useless, becouse matrix B
    x_curr += B[idx + i * size] * shared_x[i];
  }
  x_next[idx] = x_curr + g[idx];
}

void transpose(float* matrix, unsigned size)
{
	for (unsigned i = 0; i < size; i++){
		for (unsigned j = 0; j < i; j++){
			//swap value
			float tmp = matrix[i + j * size];
			matrix[i + j * size] = matrix[j + i * size];
			matrix[j + i * size] = tmp;
		}
	}
}

void checkGPUOperation()
{
	hipError_t code = hipGetLastError();
	if (code != hipSuccess){
		fprintf(stderr, "Cuda Error : %s\n", hipGetErrorString(code));
		exit(-1);
	}
}

int main()
{
  float eps = EPS;
  unsigned size = SIZE;

  // alloc memory on CPU
  float *host_A = (float *)malloc(size * size * sizeof(float));
  float *host_B = (float *)malloc(size * size * sizeof(float));
  float *host_f = (float *)malloc(size * sizeof(float));
  float *host_g = (float *)malloc(size * sizeof(float));
  float *host_x = (float *)calloc(size, sizeof(float)); // start with null vector
  if (host_A == NULL || host_B == NULL || host_f == NULL
	  || host_g == NULL || host_x == NULL){
    fprintf(stderr, "error on allocate memory\n");
	return -1;
  }

  getMatrix(host_A, size);
  get_f(host_f, size);

  memcpy(host_B, host_A, size * size * sizeof(float));
  memcpy(host_g, host_f, size * sizeof(float));

  compute_g(host_A, host_g, size);
  computeBMatrix(host_B, size);
  transpose(host_B, size); // transpose for optimization

  // alloc memory on GPU
  float *dev_B, *dev_g, *dev_x_prev, *dev_x_next;
  hipMalloc((void **)&dev_B, size * size * sizeof(float));
  checkGPUOperation();
  hipMalloc((void **)&dev_g, size * sizeof(float));
  checkGPUOperation();
  hipMalloc((void **)&dev_x_prev, size * sizeof(float));
  checkGPUOperation();
  hipMalloc((void **)&dev_x_next, size * sizeof(float));
  checkGPUOperation();

  //copy memory from CPU to GPU
  hipMemcpy(dev_x_prev, host_x, size * sizeof(float), hipMemcpyHostToDevice);
  checkGPUOperation();
  hipMemcpy(dev_x_next, dev_x_prev, size * sizeof(float), hipMemcpyDeviceToDevice);
  checkGPUOperation();
  hipMemcpy(dev_B, host_B, size * size * sizeof(float), hipMemcpyHostToDevice);
  checkGPUOperation();
  hipMemcpy(dev_g, host_g, size * sizeof(float), hipMemcpyHostToDevice);

  float p = 0; //precision
  float prev_p = BIG_VALUE; //previos precision
  // when precision stop decrease stop compute
  do {
    for (int i = 0; i < 512; i++) {
      //swap vectors
      float *tmp = dev_x_next;
      dev_x_next = dev_x_prev;
      dev_x_prev = tmp;

	  jacobi <<<dim3(size / BLOCK_SIZE), dim3(BLOCK_SIZE)>>> (dev_B, dev_g, dev_x_prev, size, dev_x_next);
	  hipDeviceSynchronize();
    }
	//get result
    hipMemcpy(host_x, dev_x_next, size * sizeof(float), hipMemcpyDeviceToHost);
	checkGPUOperation();

	prev_p = p;
	p = precision(host_A, host_f, host_x, size);
	printf("precision : %f \n", p);
  } while (p > eps && p < prev_p);

  printf("success\n");

  //free memory on CPU
  free(host_A);
  free(host_B);
  free(host_f);
  free(host_g);
  free(host_x);

  //free memory on GPU
  hipFree(dev_B);
  hipFree(dev_g);
  hipFree(dev_x_prev);
  hipFree(dev_x_next);

#ifdef _WIN32
  // stop console
  // only Windows needed
  scanf("\n");
#endif
  return 0;
}


	.text
	.file	"jacobi.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z9getMatrixPfj
.LCPI0_0:
	.long	0x30000000                      # float 4.65661287E-10
.LCPI0_1:
	.long	0xbf000000                      # float -0.5
.LCPI0_2:
	.long	0x41200000                      # float 10
.LCPI0_3:
	.long	0x447a0000                      # float 1000
	.text
	.globl	_Z9getMatrixPfj
	.type	_Z9getMatrixPfj,@function
_Z9getMatrixPfj:                        # @_Z9getMatrixPfj
	.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
	testq	%rdi, %rdi
	je	.LBB0_9
# %bb.1:                                # %.preheader22
	movl	%esi, %ebx
	testl	%esi, %esi
	je	.LBB0_8
# %bb.2:                                # %.preheader21.preheader
	movq	%rdi, %r14
	movl	%ebx, %r15d
	xorl	%ebp, %ebp
.LBB0_3:                                # %.preheader21
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_4 Depth 2
	movq	%r15, %r12
	movl	%ebp, %r13d
.LBB0_4:                                #   Parent Loop BB0_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	movss	.LCPI0_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	%xmm1, %xmm0
	movss	.LCPI0_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	addss	%xmm1, %xmm0
	movss	.LCPI0_2(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	mulss	%xmm1, %xmm0
	movl	%r13d, %eax
	movss	%xmm0, (%r14,%rax,4)
	addl	%ebx, %r13d
	decq	%r12
	jne	.LBB0_4
# %bb.5:                                #   in Loop: Header=BB0_3 Depth=1
	incl	%ebp
	cmpl	%ebx, %ebp
	jne	.LBB0_3
# %bb.6:                                # %.lr.ph
	incl	%ebx
	xorl	%eax, %eax
	movss	.LCPI0_3(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
.LBB0_7:                                # =>This Inner Loop Header: Depth=1
	movl	%eax, %ecx
	movss	(%r14,%rcx,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	addss	%xmm0, %xmm1
	movss	%xmm1, (%r14,%rcx,4)
	addl	%ebx, %eax
	decq	%r15
	jne	.LBB0_7
.LBB0_8:                                # %._crit_edge
	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
.LBB0_9:
	.cfi_def_cfa_offset 64
	movq	stderr(%rip), %rcx
	movl	$.L.str, %edi
	movl	$29, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$-1, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z9getMatrixPfj, .Lfunc_end0-_Z9getMatrixPfj
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z5get_fPfj
.LCPI1_0:
	.long	0x30000000                      # float 4.65661287E-10
.LCPI1_1:
	.long	0xbf000000                      # float -0.5
.LCPI1_2:
	.long	0x41200000                      # float 10
	.text
	.globl	_Z5get_fPfj
	.type	_Z5get_fPfj,@function
_Z5get_fPfj:                            # @_Z5get_fPfj
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	je	.LBB1_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
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	addss	.LCPI1_1(%rip), %xmm0
	mulss	.LCPI1_2(%rip), %xmm0
	movss	%xmm0, (%rbx,%r15,4)
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB1_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
.LBB1_4:                                # %._crit_edge
	retq
.Lfunc_end1:
	.size	_Z5get_fPfj, .Lfunc_end1-_Z5get_fPfj
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z14computeBMatrixPfj
.LCPI2_0:
	.long	0x3f800000                      # float 1
	.text
	.globl	_Z14computeBMatrixPfj
	.type	_Z14computeBMatrixPfj,@function
_Z14computeBMatrixPfj:                  # @_Z14computeBMatrixPfj
	.cfi_startproc
# %bb.0:
	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
	testq	%rdi, %rdi
	je	.LBB2_1
# %bb.3:
	movl	%esi, %ebx
	movq	%rdi, %r14
	movl	%esi, %r15d
	movl	$4, %esi
	movq	%r15, %rdi
	callq	calloc
	testq	%rax, %rax
	je	.LBB2_19
# %bb.4:                                # %.preheader55
	testl	%ebx, %ebx
	je	.LBB2_18
# %bb.5:                                # %.lr.ph
	leal	1(%rbx), %ecx
	xorl	%edx, %edx
	movss	.LCPI2_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	xorl	%esi, %esi
.LBB2_6:                                # =>This Inner Loop Header: Depth=1
	movl	%edx, %edi
	movaps	%xmm0, %xmm1
	divss	(%r14,%rdi,4), %xmm1
	movss	%xmm1, (%rax,%rsi,4)
	incq	%rsi
	addl	%ecx, %edx
	cmpq	%rsi, %r15
	jne	.LBB2_6
# %bb.7:                                # %.preheader54
	testl	%ebx, %ebx
	je	.LBB2_18
# %bb.8:                                # %.preheader53.preheader
	xorl	%ecx, %ecx
.LBB2_9:                                # %.preheader53
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_10 Depth 2
	movl	%ecx, %edx
	xorl	%esi, %esi
.LBB2_10:                               #   Parent Loop BB2_9 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rax,%rsi,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	movl	%edx, %edi
	mulss	(%r14,%rdi,4), %xmm1
	movss	%xmm1, (%r14,%rdi,4)
	incq	%rsi
	addl	%ebx, %edx
	cmpq	%rsi, %r15
	jne	.LBB2_10
# %bb.11:                               #   in Loop: Header=BB2_9 Depth=1
	incl	%ecx
	cmpl	%r15d, %ecx
	jne	.LBB2_9
# %bb.12:                               # %.preheader.preheader
	xorl	%ecx, %ecx
.LBB2_13:                               # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_14 Depth 2
	movl	%ecx, %edx
	xorl	%esi, %esi
.LBB2_14:                               #   Parent Loop BB2_13 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movaps	%xmm0, %xmm1
	cmpq	%rsi, %rcx
	je	.LBB2_16
# %bb.15:                               #   in Loop: Header=BB2_14 Depth=2
	xorps	%xmm1, %xmm1
.LBB2_16:                               #   in Loop: Header=BB2_14 Depth=2
	movl	%edx, %edi
	subss	(%r14,%rdi,4), %xmm1
	movss	%xmm1, (%r14,%rdi,4)
	incq	%rsi
	addl	%ebx, %edx
	cmpq	%rsi, %r15
	jne	.LBB2_14
# %bb.17:                               #   in Loop: Header=BB2_13 Depth=1
	incq	%rcx
	cmpq	%r15, %rcx
	jne	.LBB2_13
.LBB2_18:                               # %._crit_edge
	movq	%rax, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	free                            # TAILCALL
.LBB2_1:
	.cfi_def_cfa_offset 32
	movq	stderr(%rip), %rcx
	movl	$.L.str.1, %edi
	movl	$34, %esi
	jmp	.LBB2_2
.LBB2_19:
	movq	stderr(%rip), %rcx
	movl	$.L.str.2, %edi
	movl	$25, %esi
.LBB2_2:
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$-1, %edi
	callq	exit
.Lfunc_end2:
	.size	_Z14computeBMatrixPfj, .Lfunc_end2-_Z14computeBMatrixPfj
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z9compute_gPfS_j
.LCPI3_0:
	.long	0x3f800000                      # float 1
	.text
	.globl	_Z9compute_gPfS_j
	.type	_Z9compute_gPfS_j,@function
_Z9compute_gPfS_j:                      # @_Z9compute_gPfS_j
	.cfi_startproc
# %bb.0:
	testq	%rdi, %rdi
	je	.LBB3_6
# %bb.1:
	testq	%rsi, %rsi
	je	.LBB3_6
# %bb.2:                                # %.preheader
	testl	%edx, %edx
	je	.LBB3_5
# %bb.3:                                # %.lr.ph
	movl	%edx, %eax
	incl	%edx
	xorl	%ecx, %ecx
	movss	.LCPI3_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	xorl	%r8d, %r8d
.LBB3_4:                                # =>This Inner Loop Header: Depth=1
	movl	%ecx, %r9d
	movaps	%xmm0, %xmm1
	divss	(%rdi,%r9,4), %xmm1
	mulss	(%rsi,%r8,4), %xmm1
	movss	%xmm1, (%rsi,%r8,4)
	incq	%r8
	addl	%edx, %ecx
	cmpq	%r8, %rax
	jne	.LBB3_4
.LBB3_5:                                # %._crit_edge
	retq
.LBB3_6:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rcx
	movl	$.L.str.3, %edi
	movl	$32, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$-1, %edi
	callq	exit
.Lfunc_end3:
	.size	_Z9compute_gPfS_j, .Lfunc_end3-_Z9compute_gPfS_j
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function _Z9precisionPfS_S_j
.LCPI4_0:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.text
	.globl	_Z9precisionPfS_S_j
	.type	_Z9precisionPfS_S_j,@function
_Z9precisionPfS_S_j:                    # @_Z9precisionPfS_S_j
	.cfi_startproc
# %bb.0:
	testq	%rdi, %rdi
	je	.LBB4_5
# %bb.1:
	testq	%rsi, %rsi
	je	.LBB4_5
# %bb.2:
	testq	%rdx, %rdx
	je	.LBB4_5
# %bb.3:                                # %.preheader33
	testl	%ecx, %ecx
	je	.LBB4_4
# %bb.6:                                # %.preheader.preheader
	movl	%ecx, %eax
	xorps	%xmm2, %xmm2
	xorl	%ecx, %ecx
	movaps	.LCPI4_0(%rip), %xmm1           # xmm1 = [NaN,NaN,NaN,NaN]
	xorl	%r8d, %r8d
.LBB4_7:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_8 Depth 2
	xorl	%r9d, %r9d
	xorps	%xmm3, %xmm3
.LBB4_8:                                #   Parent Loop BB4_7 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leal	(%rcx,%r9), %r10d
	movss	(%rdi,%r10,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	mulss	(%rdx,%r9,4), %xmm0
	addss	%xmm0, %xmm3
	incq	%r9
	cmpq	%r9, %rax
	jne	.LBB4_8
# %bb.9:                                #   in Loop: Header=BB4_7 Depth=1
	movss	(%rsi,%r8,4), %xmm0             # xmm0 = mem[0],zero,zero,zero
	subss	%xmm3, %xmm0
	andps	%xmm1, %xmm0
	maxss	%xmm2, %xmm0
	incq	%r8
	addq	%rax, %rcx
	movaps	%xmm0, %xmm2
	cmpq	%rax, %r8
	jne	.LBB4_7
# %bb.10:                               # %._crit_edge
	retq
.LBB4_4:
	xorps	%xmm0, %xmm0
	retq
.LBB4_5:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rcx
	movl	$.L.str.4, %edi
	movl	$31, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$-1, %edi
	callq	exit
.Lfunc_end4:
	.size	_Z9precisionPfS_S_j, .Lfunc_end4-_Z9precisionPfS_S_j
	.cfi_endproc
                                        # -- End function
	.globl	_Z21__device_stub__jacobiPfS_S_jS_ # -- Begin function _Z21__device_stub__jacobiPfS_S_jS_
	.type	_Z21__device_stub__jacobiPfS_S_jS_,@function
_Z21__device_stub__jacobiPfS_S_jS_:     # @_Z21__device_stub__jacobiPfS_S_jS_
	.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	12(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	32(%rsp), %rcx
	movq	%r8, (%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	$_Z6jacobiPfS_S_jS_, %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_end5:
	.size	_Z21__device_stub__jacobiPfS_S_jS_, .Lfunc_end5-_Z21__device_stub__jacobiPfS_S_jS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z9transposePfj                 # -- Begin function _Z9transposePfj
	.type	_Z9transposePfj,@function
_Z9transposePfj:                        # @_Z9transposePfj
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	je	.LBB6_6
# %bb.1:                                # %.preheader.preheader
	movl	%esi, %eax
	xorl	%ecx, %ecx
	xorl	%edx, %edx
.LBB6_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB6_4 Depth 2
	testq	%rdx, %rdx
	je	.LBB6_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB6_2 Depth=1
	movl	%edx, %r8d
	xorl	%r9d, %r9d
.LBB6_4:                                #   Parent Loop BB6_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%r8d, %r10d
	movss	(%rdi,%r10,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	leal	(%rcx,%r9), %r11d
	movss	(%rdi,%r11,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	movss	%xmm1, (%rdi,%r10,4)
	movss	%xmm0, (%rdi,%r11,4)
	incq	%r9
	addl	%esi, %r8d
	cmpq	%r9, %rdx
	jne	.LBB6_4
.LBB6_5:                                # %._crit_edge
                                        #   in Loop: Header=BB6_2 Depth=1
	incq	%rdx
	addq	%rax, %rcx
	cmpq	%rax, %rdx
	jne	.LBB6_2
.LBB6_6:                                # %._crit_edge26
	retq
.Lfunc_end6:
	.size	_Z9transposePfj, .Lfunc_end6-_Z9transposePfj
	.cfi_endproc
                                        # -- End function
	.globl	_Z17checkGPUOperationv          # -- Begin function _Z17checkGPUOperationv
	.type	_Z17checkGPUOperationv,@function
_Z17checkGPUOperationv:                 # @_Z17checkGPUOperationv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB7_2
# %bb.1:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB7_2:
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.5, %esi
	movq	%rbx, %rdi
	movq	%rax, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movl	$-1, %edi
	callq	exit
.Lfunc_end7:
	.size	_Z17checkGPUOperationv, .Lfunc_end7-_Z17checkGPUOperationv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI8_0:
	.long	0x30000000                      # float 4.65661287E-10
.LCPI8_1:
	.long	0xbf000000                      # float -0.5
.LCPI8_2:
	.long	0x41200000                      # float 10
.LCPI8_3:
	.long	0x33d6bf95                      # float 1.00000001E-7
	.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	$72, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$4194304, %edi                  # imm = 0x400000
	callq	malloc
	movq	%rax, %rbx
	movl	$4194304, %edi                  # imm = 0x400000
	callq	malloc
	movq	%rax, %r12
	movl	$4096, %edi                     # imm = 0x1000
	callq	malloc
	movq	%rax, %r15
	movl	$4096, %edi                     # imm = 0x1000
	callq	malloc
	movq	%rax, %rbp
	movl	$1024, %edi                     # imm = 0x400
	movl	$4, %esi
	callq	calloc
	testq	%rbx, %rbx
	je	.LBB8_5
# %bb.1:
	testq	%r12, %r12
	je	.LBB8_5
# %bb.2:
	testq	%r15, %r15
	je	.LBB8_5
# %bb.3:
	testq	%rbp, %rbp
	je	.LBB8_5
# %bb.4:
	movq	%rax, %r13
	testq	%rax, %rax
	je	.LBB8_5
# %bb.6:
	movq	%rbx, %rdi
	movl	$1024, %esi                     # imm = 0x400
	callq	_Z9getMatrixPfj
	xorl	%r14d, %r14d
.LBB8_7:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI8_0(%rip), %xmm0
	addss	.LCPI8_1(%rip), %xmm0
	mulss	.LCPI8_2(%rip), %xmm0
	movss	%xmm0, (%r15,%r14,4)
	incq	%r14
	cmpq	$1024, %r14                     # imm = 0x400
	jne	.LBB8_7
# %bb.8:                                # %_Z5get_fPfj.exit
	movl	$4194304, %edx                  # imm = 0x400000
	movq	%r12, %rdi
	movq	%rbx, %rsi
	callq	memcpy@PLT
	movl	$4096, %edx                     # imm = 0x1000
	movq	%rbp, %rdi
	movq	%r15, %rsi
	callq	memcpy@PLT
	movq	%rbx, %rdi
	movq	%rbp, 56(%rsp)                  # 8-byte Spill
	movq	%rbp, %rsi
	movl	$1024, %edx                     # imm = 0x400
	callq	_Z9compute_gPfS_j
	movq	%r12, %rdi
	movl	$1024, %esi                     # imm = 0x400
	callq	_Z14computeBMatrixPfj
	xorl	%eax, %eax
	movq	%r12, %rcx
	movq	%r12, 64(%rsp)                  # 8-byte Spill
.LBB8_9:                                # %.preheader.i
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB8_11 Depth 2
	testq	%rax, %rax
	je	.LBB8_12
# %bb.10:                               # %.lr.ph.i81
                                        #   in Loop: Header=BB8_9 Depth=1
	movq	%rcx, %rsi
	xorl	%edi, %edi
.LBB8_11:                               #   Parent Loop BB8_9 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rsi), %xmm0                   # xmm0 = mem[0],zero,zero,zero
	movss	(%r12,%rdi,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	movss	%xmm1, (%rsi)
	movss	%xmm0, (%r12,%rdi,4)
	incq	%rdi
	addq	$4096, %rsi                     # imm = 0x1000
	cmpq	%rdi, %rax
	jne	.LBB8_11
.LBB8_12:                               # %._crit_edge.i
                                        #   in Loop: Header=BB8_9 Depth=1
	incq	%rax
	addq	$4096, %r12                     # imm = 0x1000
	addq	$4, %rcx
	cmpq	$1024, %rax                     # imm = 0x400
	jne	.LBB8_9
# %bb.13:                               # %_Z9transposePfj.exit
	movabsq	$4294967300, %rbp               # imm = 0x100000004
	leaq	40(%rsp), %rdi
	movl	$4194304, %esi                  # imm = 0x400000
	callq	hipMalloc
	callq	_Z17checkGPUOperationv
	leaq	32(%rsp), %rdi
	movl	$4096, %esi                     # imm = 0x1000
	callq	hipMalloc
	callq	_Z17checkGPUOperationv
	leaq	24(%rsp), %r12
	movl	$4096, %esi                     # imm = 0x1000
	movq	%r12, %rdi
	callq	hipMalloc
	callq	_Z17checkGPUOperationv
	leaq	8(%rsp), %r14
	movl	$4096, %esi                     # imm = 0x1000
	movq	%r14, %rdi
	callq	hipMalloc
	callq	_Z17checkGPUOperationv
	movq	(%r12), %rdi
	movl	$4096, %edx                     # imm = 0x1000
	movq	%r13, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	callq	_Z17checkGPUOperationv
	movq	(%r14), %rdi
	movq	(%r12), %rsi
	movl	$4096, %edx                     # imm = 0x1000
	movl	$3, %ecx
	callq	hipMemcpy
	callq	_Z17checkGPUOperationv
	leaq	40(%rsp), %rax
	movq	(%rax), %rdi
	movl	$4194304, %edx                  # imm = 0x400000
	movq	64(%rsp), %rsi                  # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	callq	_Z17checkGPUOperationv
	leaq	32(%rsp), %rax
	movq	(%rax), %rdi
	movl	$4096, %edx                     # imm = 0x1000
	movq	56(%rsp), %rsi                  # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	xorps	%xmm0, %xmm0
	movss	%xmm0, 20(%rsp)                 # 4-byte Spill
	leaq	252(%rbp), %r12
.LBB8_14:                               # =>This Loop Header: Depth=1
                                        #     Child Loop BB8_15 Depth 2
	movl	$512, %r14d                     # imm = 0x200
.LBB8_15:                               #   Parent Loop BB8_14 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	8(%rsp), %rax
	movq	24(%rsp), %rcx
	movq	%rcx, 8(%rsp)
	movq	%rax, 24(%rsp)
	movq	%rbp, %rdi
	movl	$1, %esi
	movq	%r12, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB8_17
# %bb.16:                               #   in Loop: Header=BB8_15 Depth=2
	movq	40(%rsp), %rdi
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdx
	movq	8(%rsp), %r8
	movl	$1024, %ecx                     # imm = 0x400
	callq	_Z21__device_stub__jacobiPfS_S_jS_
.LBB8_17:                               #   in Loop: Header=BB8_15 Depth=2
	callq	hipDeviceSynchronize
	decl	%r14d
	jne	.LBB8_15
# %bb.18:                               #   in Loop: Header=BB8_14 Depth=1
	movq	8(%rsp), %rsi
	movl	$4096, %edx                     # imm = 0x1000
	movq	%r13, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	callq	_Z17checkGPUOperationv
	movq	%rbx, %rdi
	movq	%r15, %rsi
	movq	%r13, %rdx
	movl	$1024, %ecx                     # imm = 0x400
	callq	_Z9precisionPfS_S_j
	movss	%xmm0, 52(%rsp)                 # 4-byte Spill
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	movss	52(%rsp), %xmm1                 # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	ucomiss	.LCPI8_3(%rip), %xmm1
	jbe	.LBB8_20
# %bb.19:                               #   in Loop: Header=BB8_14 Depth=1
	movss	20(%rsp), %xmm0                 # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	ucomiss	%xmm1, %xmm0
	movss	%xmm1, 20(%rsp)                 # 4-byte Spill
	ja	.LBB8_14
.LBB8_20:
	movl	$.Lstr, %edi
	callq	puts@PLT
	movq	%rbx, %rdi
	callq	free
	movq	64(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	56(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	%r13, %rdi
	callq	free
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
.LBB8_21:
	addq	$72, %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
.LBB8_5:
	.cfi_def_cfa_offset 128
	movq	stderr(%rip), %rcx
	movl	$.L.str.2, %edi
	movl	$25, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$-1, %eax
	jmp	.LBB8_21
.Lfunc_end8:
	.size	main, .Lfunc_end8-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	.LBB9_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB9_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z6jacobiPfS_S_jS_, %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_end9:
	.size	__hip_module_ctor, .Lfunc_end9-__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	.LBB10_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
.LBB10_2:
	retq
.Lfunc_end10:
	.size	__hip_module_dtor, .Lfunc_end10-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"getMatrix doesn't get matrix\n"
	.size	.L.str, 30

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"computeBMatrix doesn't get matrix\n"
	.size	.L.str.1, 35

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"error on allocate memory\n"
	.size	.L.str.2, 26

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"compute_g get invalud arguments\n"
	.size	.L.str.3, 33

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"precision get invalid parametr\n"
	.size	.L.str.4, 32

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

	.type	.L.str.5,@object                # @.str.5
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.5:
	.asciz	"Cuda Error : %s\n"
	.size	.L.str.5, 17

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"precision : %f \n"
	.size	.L.str.6, 17

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z6jacobiPfS_S_jS_"
	.size	.L__unnamed_1, 19

	.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	"success"
	.size	.Lstr, 8

	.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__jacobiPfS_S_jS_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z6jacobiPfS_S_jS_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z6jacobiPfS_S_jS_      ; -- Begin function _Z6jacobiPfS_S_jS_
	.globl	_Z6jacobiPfS_S_jS_
	.p2align	8
	.type	_Z6jacobiPfS_S_jS_,@function
_Z6jacobiPfS_S_jS_:                     ; @_Z6jacobiPfS_S_jS_
; %bb.0:
	s_clause 0x2
	s_load_b32 s8, s[0:1], 0x34
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[2:3], s[0:1], 0x10
	s_waitcnt lgkmcnt(0)
	s_and_b32 s8, s8, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmpk_gt_u32 s8, 0x400
	s_cbranch_scc1 .LBB0_3
; %bb.1:                                ; %.lr.ph.preheader
	v_cvt_f32_u32_e32 v1, s8
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_iflag_f32_e32 v2, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v2, 0x44800000, v2
	v_trunc_f32_e32 v2, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fma_f32 v3, -v2, v1, 0x44800000
	v_cvt_u32_f32_e32 v2, v2
	v_cmp_ge_f32_e64 s9, |v3|, v1
	v_lshlrev_b32_e32 v3, 2, v0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_readfirstlane_b32 s10, v2
	s_cmp_lg_u32 s9, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v1, s2, s2, v3
	v_add_co_ci_u32_e64 v2, null, s3, 0, s2
	s_delay_alu instid0(VALU_DEP_3)
	s_addc_u32 s3, s10, 0
	s_mov_b32 s2, 0
	s_and_b32 s3, s3, 0xffff
	s_lshl_b32 s9, s8, 2
.LBB0_2:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	global_load_b32 v4, v[1:2], off
	v_add_co_u32 v1, vcc_lo, v1, s9
	v_add_co_ci_u32_e32 v2, vcc_lo, 0, v2, vcc_lo
	s_add_i32 s2, s2, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_ge_u32 s2, s3
	s_waitcnt vmcnt(0)
	ds_store_b32 v3, v4
	v_add_nc_u32_e32 v3, s9, v3
	s_cbranch_scc0 .LBB0_2
.LBB0_3:                                ; %._crit_edge
	s_load_b32 s2, s[0:1], 0x18
	v_mad_u64_u32 v[1:2], null, s15, s8, v[0:1]
	s_mov_b32 s3, 0
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cmp_eq_u32 s2, 0
	s_cbranch_scc1 .LBB0_6
; %bb.4:                                ; %.lr.ph30.preheader
	v_dual_mov_b32 v3, 0 :: v_dual_mov_b32 v2, v1
	s_mov_b32 s8, s2
	s_delay_alu instid0(VALU_DEP_1)
	v_mov_b32_e32 v0, v3
.LBB0_5:                                ; %.lr.ph30
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[4:5], 2, v[2:3]
	s_add_i32 s8, s8, -1
	v_add_nc_u32_e32 v2, s2, v2
	v_add_co_u32 v4, vcc_lo, s4, v4
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v5, vcc_lo, s5, v5, vcc_lo
	global_load_b32 v4, v[4:5], off
	v_mov_b32_e32 v5, s3
	s_add_i32 s3, s3, 4
	s_cmp_eq_u32 s8, 0
	ds_load_b32 v5, v5
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_fmac_f32_e32 v0, v4, v5
	s_cbranch_scc0 .LBB0_5
	s_branch .LBB0_7
.LBB0_6:
	v_mov_b32_e32 v0, 0
.LBB0_7:                                ; %Flow54
	v_ashrrev_i32_e32 v2, 31, v1
	s_load_b64 s[0:1], s[0:1], 0x20
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[1:2], 2, v[1:2]
	v_add_co_u32 v3, vcc_lo, s6, v1
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v4, vcc_lo, s7, v2, vcc_lo
	global_load_b32 v3, v[3:4], off
	s_waitcnt vmcnt(0)
	v_add_f32_e32 v3, v0, v3
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v0, vcc_lo, s0, v1
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v2, vcc_lo
	global_store_b32 v[0:1], v3, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z6jacobiPfS_S_jS_
		.amdhsa_group_segment_fixed_size 4096
		.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 6
		.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	_Z6jacobiPfS_S_jS_, .Lfunc_end0-_Z6jacobiPfS_S_jS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 460
; NumSgprs: 18
; NumVgprs: 6
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 4096 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 6
; 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
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         32
        .size:           8
        .value_kind:     global_buffer
      - .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: 4096
    .kernarg_segment_align: 8
    .kernarg_segment_size: 296
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z6jacobiPfS_S_jS_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z6jacobiPfS_S_jS_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     6
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
