#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <time.h>

#define GIG 1000000000
#define NANO_TO_MILLI 1000000
#define CPG 2.8         // Cycles per GHz -- Adjust to your computer

// Assertion to check for errors
#define CUDA_SAFE_CALL(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
	if (code != cudaSuccess) 
	{
		fprintf(stderr,"CUDA_SAFE_CALL: %s %s %d\n", cudaGetErrorString(code), file, line);
		if (abort) exit(code);
	}
}

const int MATRIX_SIZE = 2000;
const int NUM_BLOCKS = 1;
const int THREADS_PER_BLOCK_X = 16;
const int THREADS_PER_BLOCK_Y = 16;
const int SOR_ITERATIONS = 2000;
const int OMEGA = 1;

#define ALLOCATE_AND_INIT
#define TRANSFER_TO_GPU
#define LAUNCH_KERNEL
#define TRANSFER_RESULTS
#define COMPUTE_CPU_RESULTS
#define COMPARE_RESULTS
#define FREE_MEMORY
#define GPU_TIMING
#define CPU_TIMING
#define DEBUG_PRINT
#define WRITE_2D_ARRAYS

void initialize_array_2D(float **A, int len, int seed);

__global__ void kernel_SOR_internal(float *A, int omega, int N_x, int N_y){
	int i, j = 0;
	//different divisions needed for group of threads etc.
	int xx = blockIdx.x * blockDim.x + threadIdx.x;
	int yy = blockIdx.x * blockDim.y + threadIdx.y;
	float phi;
	for(i = MATRIX_SIZE/THREADS_PER_BLOCK_X*xx; i < MATRIX_SIZE/THREADS_PER_BLOCK_X*(xx+1); i++){
		for(j = MATRIX_SIZE/THREADS_PER_BLOCK_Y*yy; j < MATRIX_SIZE/THREADS_PER_BLOCK_Y*(yy+1); j++){
			if(i > 0 && i < (N_x-1) && j > 0 && j < (N_y-1)){
				phi = A[i*MATRIX_SIZE + j] - .25*((A[(i-1)*MATRIX_SIZE + j] + A[(i+1)*MATRIX_SIZE+j]) + (A[i*MATRIX_SIZE + (j-1)] + A[i*MATRIX_SIZE+(j+1)]));
				A[i*MATRIX_SIZE+j] = abs(A[i*MATRIX_SIZE+j] - (phi*omega));
			}
		}
	}
}

void SOR_internal_sequential(float **A, int omega, int xx, int yy, int N_x, int N_y){
	float phi;
	if(xx > 0 && xx < N_x-1 && yy > 0 && yy < N_y-1){
		phi = A[xx][yy] - .25*((A[xx-1][yy] + A[xx+1][yy]) + (A[xx][yy-1] + A[xx][yy+1]));
		A[xx][yy] = abs(A[xx][yy] - (phi*omega));
	}
}

void write_2d_array_to_file(float **A, int N_x, int N_y, char *filename){
	int i, j;
	FILE *f = fopen(filename, "w");
	for(i = 0; i < N_x; i++){
		for(j = 0; j < N_y-1; j++){
			fprintf(f,"%f, ",A[i][j]);
		}
		fprintf(f, "%f\n", A[i][N_y-1]);
	}
	fclose(f);
}

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

	//loop variables
	int i, j, k, errors = 0;

	//timing variables
	cudaEvent_t start, stop;
	float elapsed_gpu;
	struct timespec diff(struct timespec start, struct timespec end);
	struct timespec time1, time2, elapsed_cpu;

	//array dimensions
	dim3 dimGrid(NUM_BLOCKS,1,1);
	dim3 dimBlock(THREADS_PER_BLOCK_X,THREADS_PER_BLOCK_Y,1);
	
	//Arrays on GPU global memory
	float *g_A;

	//Arrays on host memory	
	float **h_A;
	float **h_A_test;

	//error file init
	FILE *f = fopen("mismatches.txt", "w");
	if(f == NULL){
		printf("Error opening file!\n");
		exit(1);
	}

#ifdef DEBUG_PRINT
	printf("init done\n");
#endif

	//Allocate arrays on GPU memory
#ifdef ALLOCATE_AND_INIT
	CUDA_SAFE_CALL(cudaMalloc((void **) &g_A, MATRIX_SIZE*MATRIX_SIZE*sizeof(float)));

	//Allocate arrays on host memory
	h_A = (float**) malloc(MATRIX_SIZE * sizeof(float*));
	h_A_test = (float**) malloc(MATRIX_SIZE * sizeof(float*));
	for(i = 0; i < MATRIX_SIZE; i++){
		h_A[i] = (float*) malloc(MATRIX_SIZE * sizeof(float));
		h_A_test[i] = (float*) malloc(MATRIX_SIZE * sizeof(float));
	}

	//initialize host arrays
	initialize_array_2D(h_A, MATRIX_SIZE, 2453);
	initialize_array_2D(h_A_test, MATRIX_SIZE, 2453);
#endif 

#ifdef GPU_TIMING
	//create cuda events
	cudaEventCreate(&start);
	cudaEventCreate(&stop);
	//record event on default stream
	cudaEventRecord(start, 0);
#endif

#ifdef DEBUG_PRINT
	printf("all init done\n");
#endif
	//transfer array to GPU memory
#ifdef TRANSFER_TO_GPU
	for(i = 0; i < MATRIX_SIZE; i++){
		CUDA_SAFE_CALL(cudaMemcpy(&g_A[i*MATRIX_SIZE], h_A[i], MATRIX_SIZE, cudaMemcpyHostToDevice));
	}
#endif

	//launch the kernel
#ifdef LAUNCH_KERNEL
	for(i = 0; i < SOR_ITERATIONS; i++){
		kernel_SOR_internal<<<dimGrid, dimBlock>>>(g_A, OMEGA, MATRIX_SIZE, MATRIX_SIZE);
	}
#endif



	//check for errors during launch
	CUDA_SAFE_CALL(cudaPeekAtLastError());

#ifdef DEBUG_PRINT
	printf("kernel run\n");
#endif

	//transfer results back to host
#ifdef TRANSFER_RESULTS
	for(i = 0; i < MATRIX_SIZE; i++){
		CUDA_SAFE_CALL(cudaMemcpy(h_A[i], &g_A[i*MATRIX_SIZE], MATRIX_SIZE, cudaMemcpyDeviceToHost));
	}
#endif
	//stop and destroy the timer
#ifdef GPU_TIMING
	cudaEventRecord(stop, 0);
	cudaEventSynchronize(stop);
	cudaEventElapsedTime(&elapsed_gpu, start, stop);
	printf("\nGPU time: %f (msec)\n", elapsed_gpu);
	cudaEventDestroy(start);
	cudaEventDestroy(stop);
#endif

#ifdef DEBUG_PRINT
	printf("results transfered\n");
#endif
	//compute results on host

#ifdef CPU_TIMING
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
#endif

#ifdef COMPUTE_CPU_RESULTS
	for(i = 0; i < SOR_ITERATIONS; i++){
		for(j = 0; j < MATRIX_SIZE; j++){
			for(k = 0; k < MATRIX_SIZE; k++){
				SOR_internal_sequential(h_A_test, OMEGA, j, k, MATRIX_SIZE, MATRIX_SIZE);
			}
		}
	}
#endif

#ifdef CPU_TIMING
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
	elapsed_cpu = diff(time1,time2);
	printf("\nCPU time: %f(msec)\n", (float)(((double)GIG*elapsed_cpu.tv_sec + elapsed_cpu.tv_nsec)/(double)NANO_TO_MILLI));
#endif


#ifdef DEBUG_PRINT
	printf("results computed on CPU\n");
#endif
	//compare results

#ifdef COMPARE_RESULTS	
	for(i = 0; i < MATRIX_SIZE; i++){
		for(j = 0; j < MATRIX_SIZE; j++){
			if(h_A[i][j] != h_A_test[i][j]){
				errors++;
				fprintf(f, "Mismatch at [%d,%d] GPU = %f CPU = %f\n", i, j, h_A[i][j], h_A_test[i][j]);
			}
		}
	}
#endif

#ifdef DEBUG_PRINT
	printf("results checked\n");
#endif

#ifdef WRITE_2D_ARRAYS
	write_2d_array_to_file(h_A, MATRIX_SIZE, MATRIX_SIZE, "GPU_output.txt");
	write_2d_array_to_file(h_A_test, MATRIX_SIZE, MATRIX_SIZE, "CPU_output.txt");
#endif

	//errors
	printf("Found %d errors\n", errors);
	fclose(f);

	//free up memory
#ifdef FREE_MEMORY
	CUDA_SAFE_CALL(cudaFree(g_A));
	for(i = 0; i < MATRIX_SIZE; i++){
		free(h_A[i]);
		free(h_A_test[i]);
	}
	free(h_A);
	free(h_A_test);
#ifdef DEBUG_PRINT
	printf("arrays freed\n");
#endif
#endif

	return (float)0;
}

void initialize_array_2D(float **A, int len, int seed){
	int i, j;
	float randNum;
	srand(seed);

	for(i = 0; i < len; i++){
		for(j = 0; j < len; j++){
			randNum = (float) rand();
			A[i][j] = randNum;
		}
	}
}

struct timespec diff(struct timespec start, struct timespec end)
{
  struct timespec temp;
  if ((end.tv_nsec-start.tv_nsec)<0) {
    temp.tv_sec = end.tv_sec-start.tv_sec-1;
    temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
  } else {
    temp.tv_sec = end.tv_sec-start.tv_sec;
    temp.tv_nsec = end.tv_nsec-start.tv_nsec;
  }
  return temp;
}

	.file	"tmpxft_00238cd7_00000000-6_SOR_cuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2035:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2035:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata._Z9gpuAssert9cudaErrorPcib.str1.1,"aMS",@progbits,1
.LC0:
	.string	"CUDA_SAFE_CALL: %s %s %d\n"
	.section	.text._Z9gpuAssert9cudaErrorPcib,"axG",@progbits,_Z9gpuAssert9cudaErrorPcib,comdat
	.weak	_Z9gpuAssert9cudaErrorPcib
	.type	_Z9gpuAssert9cudaErrorPcib, @function
_Z9gpuAssert9cudaErrorPcib:
.LFB2027:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L8
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movl	%edi, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	movl	%edx, 12(%rsp)
	movq	%rsi, (%rsp)
	call	cudaGetErrorString@PLT
	movl	12(%rsp), %r9d
	movq	(%rsp), %r8
	leaq	.LC0(%rip), %rdx
	movq	stderr(%rip), %rdi
	movq	%rax, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	testb	%bpl, %bpl
	je	.L2
	movl	%ebx, %edi
	call	exit@PLT
.L2:
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
.L8:
	.cfi_restore 3
	.cfi_restore 6
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z9gpuAssert9cudaErrorPcib, .-_Z9gpuAssert9cudaErrorPcib
	.text
	.globl	_Z23SOR_internal_sequentialPPfiiiii
	.type	_Z23SOR_internal_sequentialPPfiiiii, @function
_Z23SOR_internal_sequentialPPfiiiii:
.LFB2028:
	.cfi_startproc
	endbr64
	movl	%esi, %eax
	testl	%edx, %edx
	jle	.L12
	decl	%r8d
	cmpl	%edx, %r8d
	jle	.L12
	testl	%ecx, %ecx
	jle	.L12
	decl	%r9d
	cmpl	%ecx, %r9d
	jle	.L12
	movslq	%edx, %rdx
	movslq	%ecx, %rcx
	leaq	0(,%rdx,8), %r9
	movq	(%rdi,%rdx,8), %rsi
	leaq	0(,%rcx,4), %rdx
	movq	-8(%rdi,%r9), %r10
	movq	8(%rdi,%r9), %rdi
	movss	-4(%rsi,%rdx), %xmm3
	addss	4(%rsi,%rdx), %xmm3
	leaq	(%rsi,%rdx), %r8
	movss	(%r10,%rcx,4), %xmm0
	addss	(%rdi,%rcx,4), %xmm0
	movss	(%r8), %xmm2
	addss	%xmm3, %xmm0
	cvtss2sd	%xmm2, %xmm1
	cvtss2sd	%xmm0, %xmm0
	mulsd	.LC1(%rip), %xmm0
	subsd	%xmm0, %xmm1
	cvtsd2ss	%xmm1, %xmm0
	cvtsi2ssl	%eax, %xmm1
	mulss	%xmm1, %xmm0
	subss	%xmm0, %xmm2
	andps	.LC2(%rip), %xmm2
	movss	%xmm2, (%r8)
.L12:
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z23SOR_internal_sequentialPPfiiiii, .-_Z23SOR_internal_sequentialPPfiiiii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC3:
	.string	"w"
.LC4:
	.string	"%f, "
.LC5:
	.string	"%f\n"
	.text
	.globl	_Z22write_2d_array_to_filePPfiiPc
	.type	_Z22write_2d_array_to_filePPfiiPc, @function
_Z22write_2d_array_to_filePPfiiPc:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdi, %r13
	movq	%rcx, %rdi
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%esi, %r12d
	leaq	.LC3(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%edx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	call	fopen@PLT
	movq	%rax, 8(%rsp)
	movslq	%ebp, %rax
	leaq	-4(,%rax,4), %r14
.L21:
	cmpl	%ebx, %r12d
	jle	.L26
	xorl	%r15d, %r15d
.L23:
	leal	-1(%rbp), %edx
	movq	0(%r13,%rbx,8), %rax
	cmpl	%r15d, %edx
	jle	.L27
	movq	8(%rsp), %rdi
	cvtss2sd	(%rax,%r15,4), %xmm0
	movb	$1, %al
	incq	%r15
	leaq	.LC4(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	jmp	.L23
.L27:
	movq	8(%rsp), %rdi
	cvtss2sd	(%rax,%r14), %xmm0
	movl	$2, %esi
	movb	$1, %al
	leaq	.LC5(%rip), %rdx
	incq	%rbx
	call	__fprintf_chk@PLT
	jmp	.L21
.L26:
	movq	8(%rsp), %rdi
	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
	jmp	fclose@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z22write_2d_array_to_filePPfiiPc, .-_Z22write_2d_array_to_filePPfiiPc
	.globl	_Z19initialize_array_2DPPfii
	.type	_Z19initialize_array_2DPPfii, @function
_Z19initialize_array_2DPPfii:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movslq	%esi, %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rdi, %r13
	movl	%edx, %edi
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movq	%r14, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	xorl	%ebx, %ebx
	call	srand@PLT
.L29:
	cmpl	%ebx, %ebp
	jle	.L34
	xorl	%r12d, %r12d
.L30:
	call	rand@PLT
	movq	0(%r13,%rbx,8), %rdx
	cvtsi2ssl	%eax, %xmm0
	movss	%xmm0, (%rdx,%r12,4)
	incq	%r12
	cmpq	%r14, %r12
	jne	.L30
	incq	%rbx
	jmp	.L29
.L34:
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z19initialize_array_2DPPfii, .-_Z19initialize_array_2DPPfii
	.globl	_Z4diff8timespecS_
	.type	_Z4diff8timespecS_, @function
_Z4diff8timespecS_:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	%rdx, %rax
	movq	%rcx, %rdx
	subq	%rdi, %rax
	subq	%rsi, %rdx
	jns	.L36
	leaq	1000000000(%rcx), %rdx
	decq	%rax
	subq	%rsi, %rdx
.L36:
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z4diff8timespecS_, .-_Z4diff8timespecS_
	.globl	_Z42__device_stub__Z19kernel_SOR_internalPfiiiPfiii
	.type	_Z42__device_stub__Z19kernel_SOR_internalPfiiiPfiii, @function
_Z42__device_stub__Z19kernel_SOR_internalPfiiiPfiii:
.LFB2057:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movl	%esi, 20(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 16(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 12(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L37
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z19kernel_SOR_internalPfiii(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L37:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L39
	call	__stack_chk_fail@PLT
.L39:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2057:
	.size	_Z42__device_stub__Z19kernel_SOR_internalPfiiiPfiii, .-_Z42__device_stub__Z19kernel_SOR_internalPfiiiPfiii
	.globl	_Z19kernel_SOR_internalPfiii
	.type	_Z19kernel_SOR_internalPfiii, @function
_Z19kernel_SOR_internalPfiii:
.LFB2058:
	.cfi_startproc
	endbr64
	jmp	_Z42__device_stub__Z19kernel_SOR_internalPfiiiPfiii
	.cfi_endproc
.LFE2058:
	.size	_Z19kernel_SOR_internalPfiii, .-_Z19kernel_SOR_internalPfiii
	.section	.rodata.str1.1
.LC6:
	.string	"mismatches.txt"
.LC7:
	.string	"Error opening file!\n"
.LC8:
	.string	"init done\n"
.LC9:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/Chen-Yu-Chang/High-Performance-Programming-with-Multicore-and-GPUs/master/Assignment7/SOR_cuda.cu"
.LC10:
	.string	"all init done\n"
.LC11:
	.string	"kernel run\n"
.LC12:
	.string	"\nGPU time: %f (msec)\n"
.LC13:
	.string	"results transfered\n"
.LC16:
	.string	"\nCPU time: %f(msec)\n"
.LC17:
	.string	"results computed on CPU\n"
.LC18:
	.string	"Mismatch at [%d,%d] GPU = %f CPU = %f\n"
.LC19:
	.string	"results checked\n"
.LC20:
	.string	"GPU_output.txt"
.LC21:
	.string	"CPU_output.txt"
.LC22:
	.string	"Found %d errors\n"
.LC23:
	.string	"arrays freed\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC3(%rip), %rsi
	leaq	.LC6(%rip), %rdi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	movabsq	$4294967297, %rax
	movl	$1, 56(%rsp)
	movl	$1, 68(%rsp)
	movq	%rax, 48(%rsp)
	movabsq	$68719476752, %rax
	movq	%rax, 60(%rsp)
	call	fopen@PLT
	testq	%rax, %rax
	jne	.L43
	movl	$2, %edi
	leaq	.LC7(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L43:
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %r12
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	40(%rsp), %rdi
	movl	$16000000, %esi
	xorl	%r13d, %r13d
	call	cudaMalloc@PLT
	movl	$1, %ecx
	movl	$113, %edx
	leaq	.LC9(%rip), %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPcib
	movl	$16000, %edi
	call	malloc@PLT
	movl	$16000, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	movq	%rax, %rbp
.L44:
	movl	$8000, %edi
	call	malloc@PLT
	movl	$8000, %edi
	movq	%rax, (%rbx,%r13)
	call	malloc@PLT
	movq	%rax, 0(%rbp,%r13)
	addq	$8, %r13
	cmpq	$16000, %r13
	jne	.L44
	movl	$2453, %edx
	movl	$2000, %esi
	movq	%rbx, %rdi
	xorl	%r13d, %r13d
	call	_Z19initialize_array_2DPPfii
	movl	$2453, %edx
	movl	$2000, %esi
	movq	%rbp, %rdi
	call	_Z19initialize_array_2DPPfii
	leaq	24(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	32(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L45:
	movq	(%rbx,%r13,8), %rsi
	movl	$1, %ecx
	movl	$2000, %edx
	imulq	$8000, %r13, %rdi
	addq	40(%rsp), %rdi
	incq	%r13
	call	cudaMemcpy@PLT
	movl	$1, %ecx
	movl	$142, %edx
	leaq	.LC9(%rip), %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPcib
	cmpq	$2000, %r13
	jne	.L45
.L47:
	movl	68(%rsp), %ecx
	movq	60(%rsp), %rdx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	48(%rsp), %rdi
	movl	56(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L46
	movq	40(%rsp), %rdi
	movl	$2000, %ecx
	movl	$2000, %edx
	movl	$1, %esi
	call	_Z42__device_stub__Z19kernel_SOR_internalPfiiiPfiii
.L46:
	decl	%r13d
	jne	.L47
	call	cudaPeekAtLastError@PLT
	movl	$1, %ecx
	movl	$156, %edx
	xorl	%r13d, %r13d
	movl	%eax, %edi
	leaq	.LC9(%rip), %rsi
	call	_Z9gpuAssert9cudaErrorPcib
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L48:
	movq	(%rbx,%r13,8), %rdi
	movl	$2, %ecx
	movl	$2000, %edx
	imulq	$8000, %r13, %rsi
	addq	40(%rsp), %rsi
	incq	%r13
	call	cudaMemcpy@PLT
	movl	$1, %ecx
	movl	$165, %edx
	leaq	.LC9(%rip), %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPcib
	cmpq	$2000, %r13
	jne	.L48
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	movl	$2000, %r14d
	call	cudaEventRecord@PLT
	movq	32(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	32(%rsp), %rdx
	movq	24(%rsp), %rsi
	leaq	20(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	20(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	24(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	32(%rsp), %rdi
	call	cudaEventDestroy@PLT
	leaq	.LC13(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	72(%rsp), %rsi
	movl	$2, %edi
	call	clock_gettime@PLT
.L49:
	xorl	%r11d, %r11d
.L53:
	xorl	%r13d, %r13d
.L50:
	movl	%r13d, %ecx
	movl	$2000, %r9d
	movl	%r11d, %edx
	movq	%rbp, %rdi
	movl	$2000, %r8d
	movl	$1, %esi
	incl	%r13d
	call	_Z23SOR_internal_sequentialPPfiiiii
	cmpl	$2000, %r13d
	jne	.L50
	incl	%r11d
	cmpl	$2000, %r11d
	jne	.L53
	decl	%r14d
	jne	.L49
	leaq	88(%rsp), %rsi
	movl	$2, %edi
	xorl	%r15d, %r15d
	xorl	%r13d, %r13d
	call	clock_gettime@PLT
	movq	96(%rsp), %rcx
	movq	72(%rsp), %rdi
	movq	80(%rsp), %rsi
	movq	88(%rsp), %rdx
	call	_Z4diff8timespecS_
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	cvtsi2sdq	%rax, %xmm0
	mulsd	.LC14(%rip), %xmm0
	movb	$1, %al
	cvtsi2sdq	%rdx, %xmm1
	addsd	%xmm1, %xmm0
	divsd	.LC15(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L54:
	movl	%r15d, 12(%rsp)
	xorl	%r14d, %r14d
.L57:
	movq	(%rbx,%r15,8), %rax
	movss	(%rax,%r14,4), %xmm0
	movq	0(%rbp,%r15,8), %rax
	movss	(%rax,%r14,4), %xmm1
	ucomiss	%xmm1, %xmm0
	jp	.L61
	je	.L55
.L61:
	movl	12(%rsp), %ecx
	movl	%r14d, %r8d
	leaq	.LC18(%rip), %rdx
	movb	$2, %al
	movl	$2, %esi
	movq	%r12, %rdi
	incl	%r13d
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	call	__fprintf_chk@PLT
.L55:
	incq	%r14
	cmpq	$2000, %r14
	jne	.L57
	incq	%r15
	cmpq	$2000, %r15
	jne	.L54
	leaq	.LC19(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC20(%rip), %rcx
	movl	$2000, %edx
	movq	%rbx, %rdi
	movl	$2000, %esi
	call	_Z22write_2d_array_to_filePPfiiPc
	leaq	.LC21(%rip), %rcx
	movl	$2000, %edx
	movq	%rbp, %rdi
	movl	$2000, %esi
	call	_Z22write_2d_array_to_filePPfiiPc
	movl	%r13d, %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC22(%rip), %rsi
	call	__printf_chk@PLT
	movq	%r12, %rdi
	xorl	%r12d, %r12d
	call	fclose@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movl	$1, %ecx
	movl	$235, %edx
	leaq	.LC9(%rip), %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPcib
.L59:
	movq	(%rbx,%r12), %rdi
	call	free@PLT
	movq	0(%rbp,%r12), %rdi
	addq	$8, %r12
	call	free@PLT
	cmpq	$16000, %r12
	jne	.L59
	movq	%rbx, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	xorl	%eax, %eax
	movl	$2, %edi
	leaq	.LC23(%rip), %rsi
	call	__printf_chk@PLT
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L60
	call	__stack_chk_fail@PLT
.L60:
	addq	$120, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	main, .-main
	.section	.rodata.str1.1
.LC24:
	.string	"_Z19kernel_SOR_internalPfiii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2060:
	.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	.LC24(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z19kernel_SOR_internalPfiii(%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
.LFE2060:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC1:
	.long	0
	.long	1070596096
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC2:
	.long	2147483647
	.long	0
	.long	0
	.long	0
	.section	.rodata.cst8
	.align 8
.LC14:
	.long	0
	.long	1104006501
	.align 8
.LC15:
	.long	0
	.long	1093567616
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <time.h>

#define GIG 1000000000
#define NANO_TO_MILLI 1000000
#define CPG 2.8         // Cycles per GHz -- Adjust to your computer

// Assertion to check for errors
#define CUDA_SAFE_CALL(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(hipError_t code, char *file, int line, bool abort=true)
{
	if (code != hipSuccess) 
	{
		fprintf(stderr,"CUDA_SAFE_CALL: %s %s %d\n", hipGetErrorString(code), file, line);
		if (abort) exit(code);
	}
}

const int MATRIX_SIZE = 2000;
const int NUM_BLOCKS = 1;
const int THREADS_PER_BLOCK_X = 16;
const int THREADS_PER_BLOCK_Y = 16;
const int SOR_ITERATIONS = 2000;
const int OMEGA = 1;

#define ALLOCATE_AND_INIT
#define TRANSFER_TO_GPU
#define LAUNCH_KERNEL
#define TRANSFER_RESULTS
#define COMPUTE_CPU_RESULTS
#define COMPARE_RESULTS
#define FREE_MEMORY
#define GPU_TIMING
#define CPU_TIMING
#define DEBUG_PRINT
#define WRITE_2D_ARRAYS

void initialize_array_2D(float **A, int len, int seed);

__global__ void kernel_SOR_internal(float *A, int omega, int N_x, int N_y){
	int i, j = 0;
	//different divisions needed for group of threads etc.
	int xx = blockIdx.x * blockDim.x + threadIdx.x;
	int yy = blockIdx.x * blockDim.y + threadIdx.y;
	float phi;
	for(i = MATRIX_SIZE/THREADS_PER_BLOCK_X*xx; i < MATRIX_SIZE/THREADS_PER_BLOCK_X*(xx+1); i++){
		for(j = MATRIX_SIZE/THREADS_PER_BLOCK_Y*yy; j < MATRIX_SIZE/THREADS_PER_BLOCK_Y*(yy+1); j++){
			if(i > 0 && i < (N_x-1) && j > 0 && j < (N_y-1)){
				phi = A[i*MATRIX_SIZE + j] - .25*((A[(i-1)*MATRIX_SIZE + j] + A[(i+1)*MATRIX_SIZE+j]) + (A[i*MATRIX_SIZE + (j-1)] + A[i*MATRIX_SIZE+(j+1)]));
				A[i*MATRIX_SIZE+j] = abs(A[i*MATRIX_SIZE+j] - (phi*omega));
			}
		}
	}
}

void SOR_internal_sequential(float **A, int omega, int xx, int yy, int N_x, int N_y){
	float phi;
	if(xx > 0 && xx < N_x-1 && yy > 0 && yy < N_y-1){
		phi = A[xx][yy] - .25*((A[xx-1][yy] + A[xx+1][yy]) + (A[xx][yy-1] + A[xx][yy+1]));
		A[xx][yy] = abs(A[xx][yy] - (phi*omega));
	}
}

void write_2d_array_to_file(float **A, int N_x, int N_y, char *filename){
	int i, j;
	FILE *f = fopen(filename, "w");
	for(i = 0; i < N_x; i++){
		for(j = 0; j < N_y-1; j++){
			fprintf(f,"%f, ",A[i][j]);
		}
		fprintf(f, "%f\n", A[i][N_y-1]);
	}
	fclose(f);
}

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

	//loop variables
	int i, j, k, errors = 0;

	//timing variables
	hipEvent_t start, stop;
	float elapsed_gpu;
	struct timespec diff(struct timespec start, struct timespec end);
	struct timespec time1, time2, elapsed_cpu;

	//array dimensions
	dim3 dimGrid(NUM_BLOCKS,1,1);
	dim3 dimBlock(THREADS_PER_BLOCK_X,THREADS_PER_BLOCK_Y,1);
	
	//Arrays on GPU global memory
	float *g_A;

	//Arrays on host memory	
	float **h_A;
	float **h_A_test;

	//error file init
	FILE *f = fopen("mismatches.txt", "w");
	if(f == NULL){
		printf("Error opening file!\n");
		exit(1);
	}

#ifdef DEBUG_PRINT
	printf("init done\n");
#endif

	//Allocate arrays on GPU memory
#ifdef ALLOCATE_AND_INIT
	CUDA_SAFE_CALL(hipMalloc((void **) &g_A, MATRIX_SIZE*MATRIX_SIZE*sizeof(float)));

	//Allocate arrays on host memory
	h_A = (float**) malloc(MATRIX_SIZE * sizeof(float*));
	h_A_test = (float**) malloc(MATRIX_SIZE * sizeof(float*));
	for(i = 0; i < MATRIX_SIZE; i++){
		h_A[i] = (float*) malloc(MATRIX_SIZE * sizeof(float));
		h_A_test[i] = (float*) malloc(MATRIX_SIZE * sizeof(float));
	}

	//initialize host arrays
	initialize_array_2D(h_A, MATRIX_SIZE, 2453);
	initialize_array_2D(h_A_test, MATRIX_SIZE, 2453);
#endif 

#ifdef GPU_TIMING
	//create cuda events
	hipEventCreate(&start);
	hipEventCreate(&stop);
	//record event on default stream
	hipEventRecord(start, 0);
#endif

#ifdef DEBUG_PRINT
	printf("all init done\n");
#endif
	//transfer array to GPU memory
#ifdef TRANSFER_TO_GPU
	for(i = 0; i < MATRIX_SIZE; i++){
		CUDA_SAFE_CALL(hipMemcpy(&g_A[i*MATRIX_SIZE], h_A[i], MATRIX_SIZE, hipMemcpyHostToDevice));
	}
#endif

	//launch the kernel
#ifdef LAUNCH_KERNEL
	for(i = 0; i < SOR_ITERATIONS; i++){
		kernel_SOR_internal<<<dimGrid, dimBlock>>>(g_A, OMEGA, MATRIX_SIZE, MATRIX_SIZE);
	}
#endif



	//check for errors during launch
	CUDA_SAFE_CALL(hipPeekAtLastError());

#ifdef DEBUG_PRINT
	printf("kernel run\n");
#endif

	//transfer results back to host
#ifdef TRANSFER_RESULTS
	for(i = 0; i < MATRIX_SIZE; i++){
		CUDA_SAFE_CALL(hipMemcpy(h_A[i], &g_A[i*MATRIX_SIZE], MATRIX_SIZE, hipMemcpyDeviceToHost));
	}
#endif
	//stop and destroy the timer
#ifdef GPU_TIMING
	hipEventRecord(stop, 0);
	hipEventSynchronize(stop);
	hipEventElapsedTime(&elapsed_gpu, start, stop);
	printf("\nGPU time: %f (msec)\n", elapsed_gpu);
	hipEventDestroy(start);
	hipEventDestroy(stop);
#endif

#ifdef DEBUG_PRINT
	printf("results transfered\n");
#endif
	//compute results on host

#ifdef CPU_TIMING
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
#endif

#ifdef COMPUTE_CPU_RESULTS
	for(i = 0; i < SOR_ITERATIONS; i++){
		for(j = 0; j < MATRIX_SIZE; j++){
			for(k = 0; k < MATRIX_SIZE; k++){
				SOR_internal_sequential(h_A_test, OMEGA, j, k, MATRIX_SIZE, MATRIX_SIZE);
			}
		}
	}
#endif

#ifdef CPU_TIMING
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
	elapsed_cpu = diff(time1,time2);
	printf("\nCPU time: %f(msec)\n", (float)(((double)GIG*elapsed_cpu.tv_sec + elapsed_cpu.tv_nsec)/(double)NANO_TO_MILLI));
#endif


#ifdef DEBUG_PRINT
	printf("results computed on CPU\n");
#endif
	//compare results

#ifdef COMPARE_RESULTS	
	for(i = 0; i < MATRIX_SIZE; i++){
		for(j = 0; j < MATRIX_SIZE; j++){
			if(h_A[i][j] != h_A_test[i][j]){
				errors++;
				fprintf(f, "Mismatch at [%d,%d] GPU = %f CPU = %f\n", i, j, h_A[i][j], h_A_test[i][j]);
			}
		}
	}
#endif

#ifdef DEBUG_PRINT
	printf("results checked\n");
#endif

#ifdef WRITE_2D_ARRAYS
	write_2d_array_to_file(h_A, MATRIX_SIZE, MATRIX_SIZE, "GPU_output.txt");
	write_2d_array_to_file(h_A_test, MATRIX_SIZE, MATRIX_SIZE, "CPU_output.txt");
#endif

	//errors
	printf("Found %d errors\n", errors);
	fclose(f);

	//free up memory
#ifdef FREE_MEMORY
	CUDA_SAFE_CALL(hipFree(g_A));
	for(i = 0; i < MATRIX_SIZE; i++){
		free(h_A[i]);
		free(h_A_test[i]);
	}
	free(h_A);
	free(h_A_test);
#ifdef DEBUG_PRINT
	printf("arrays freed\n");
#endif
#endif

	return (float)0;
}

void initialize_array_2D(float **A, int len, int seed){
	int i, j;
	float randNum;
	srand(seed);

	for(i = 0; i < len; i++){
		for(j = 0; j < len; j++){
			randNum = (float) rand();
			A[i][j] = randNum;
		}
	}
}

struct timespec diff(struct timespec start, struct timespec end)
{
  struct timespec temp;
  if ((end.tv_nsec-start.tv_nsec)<0) {
    temp.tv_sec = end.tv_sec-start.tv_sec-1;
    temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
  } else {
    temp.tv_sec = end.tv_sec-start.tv_sec;
    temp.tv_nsec = end.tv_nsec-start.tv_nsec;
  }
  return temp;
}

	.text
	.file	"SOR_cuda.hip"
	.globl	_Z34__device_stub__kernel_SOR_internalPfiii # -- Begin function _Z34__device_stub__kernel_SOR_internalPfiii
	.type	_Z34__device_stub__kernel_SOR_internalPfiii,@function
_Z34__device_stub__kernel_SOR_internalPfiii: # @_Z34__device_stub__kernel_SOR_internalPfiii
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	20(%rsp), %rdi
	movl	%esi, (%rdi)
	leaq	16(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	12(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	32(%rsp), %r12
	leaq	24(%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	$_Z19kernel_SOR_internalPfiii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z34__device_stub__kernel_SOR_internalPfiii, .Lfunc_end0-_Z34__device_stub__kernel_SOR_internalPfiii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z23SOR_internal_sequentialPPfiiiii
.LCPI1_0:
	.quad	0xbfd0000000000000              # double -0.25
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI1_1:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.text
	.globl	_Z23SOR_internal_sequentialPPfiiiii
	.type	_Z23SOR_internal_sequentialPPfiiiii,@function
_Z23SOR_internal_sequentialPPfiiiii:    # @_Z23SOR_internal_sequentialPPfiiiii
	.cfi_startproc
# %bb.0:
	testl	%edx, %edx
	jle	.LBB1_5
# %bb.1:
	testl	%ecx, %ecx
	jle	.LBB1_5
# %bb.2:
	decl	%r8d
	cmpl	%edx, %r8d
	jle	.LBB1_5
# %bb.3:
	decl	%r9d
	cmpl	%ecx, %r9d
	jle	.LBB1_5
# %bb.4:
	movl	%edx, %eax
	movq	-8(%rdi,%rax,8), %rdx
	movq	(%rdi,%rax,8), %r8
	movl	%ecx, %ecx
	movss	-4(%r8,%rcx,4), %xmm0           # xmm0 = mem[0],zero,zero,zero
	movss	(%r8,%rcx,4), %xmm1             # xmm1 = mem[0],zero,zero,zero
	movss	(%rdx,%rcx,4), %xmm2            # xmm2 = mem[0],zero,zero,zero
	movq	8(%rdi,%rax,8), %rax
	addss	(%rax,%rcx,4), %xmm2
	addss	4(%r8,%rcx,4), %xmm0
	addss	%xmm2, %xmm0
	cvtss2sd	%xmm0, %xmm0
	mulsd	.LCPI1_0(%rip), %xmm0
	xorps	%xmm2, %xmm2
	cvtss2sd	%xmm1, %xmm2
	addsd	%xmm2, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	xorps	%xmm2, %xmm2
	cvtsi2ss	%esi, %xmm2
	mulss	%xmm0, %xmm2
	subss	%xmm2, %xmm1
	andps	.LCPI1_1(%rip), %xmm1
	movss	%xmm1, (%r8,%rcx,4)
.LBB1_5:
	retq
.Lfunc_end1:
	.size	_Z23SOR_internal_sequentialPPfiiiii, .Lfunc_end1-_Z23SOR_internal_sequentialPPfiiiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z22write_2d_array_to_filePPfiiPc # -- Begin function _Z22write_2d_array_to_filePPfiiPc
	.type	_Z22write_2d_array_to_filePPfiiPc,@function
_Z22write_2d_array_to_filePPfiiPc:      # @_Z22write_2d_array_to_filePPfiiPc
	.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	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
                                        # kill: def $edx killed $edx def $rdx
	movq	%rdx, 8(%rsp)                   # 8-byte Spill
	movl	%esi, %ebp
	movq	%rdi, %r14
	movl	$.L.str, %esi
	movq	%rcx, %rdi
	callq	fopen
	movq	%rax, %r15
	testl	%ebp, %ebp
	jle	.LBB2_6
# %bb.1:                                # %.preheader.lr.ph
	movq	8(%rsp), %rax                   # 8-byte Reload
	leal	-1(%rax), %r12d
	movslq	%r12d, %rax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	%ebp, %ebp
	xorl	%ebx, %ebx
.LBB2_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_4 Depth 2
	cmpl	$2, 8(%rsp)                     # 4-byte Folded Reload
	jl	.LBB2_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB2_2 Depth=1
	xorl	%r13d, %r13d
.LBB2_4:                                #   Parent Loop BB2_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	(%r14,%rbx,8), %rax
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rax,%r13,4), %xmm0
	movl	$.L.str.1, %esi
	movq	%r15, %rdi
	movb	$1, %al
	callq	fprintf
	incq	%r13
	cmpq	%r13, %r12
	jne	.LBB2_4
.LBB2_5:                                # %._crit_edge
                                        #   in Loop: Header=BB2_2 Depth=1
	movq	(%r14,%rbx,8), %rax
	movq	16(%rsp), %rcx                  # 8-byte Reload
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rax,%rcx,4), %xmm0
	movl	$.L.str.2, %esi
	movq	%r15, %rdi
	movb	$1, %al
	callq	fprintf
	incq	%rbx
	cmpq	%rbp, %rbx
	jne	.LBB2_2
.LBB2_6:                                # %._crit_edge19
	movq	%r15, %rdi
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	fclose                          # TAILCALL
.Lfunc_end2:
	.size	_Z22write_2d_array_to_filePPfiiPc, .Lfunc_end2-_Z22write_2d_array_to_filePPfiiPc
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI3_0:
	.quad	0x41cdcd6500000000              # double 1.0E+9
.LCPI3_1:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$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
	movl	$.L.str.3, %edi
	movl	$.L.str, %esi
	callq	fopen
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	testq	%rax, %rax
	je	.LBB3_35
# %bb.1:
	movl	$.Lstr, %edi
	callq	puts@PLT
	leaq	8(%rsp), %rdi
	movl	$16000000, %esi                 # imm = 0xF42400
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB3_36
# %bb.2:                                # %_Z9gpuAssert10hipError_tPcib.exit
	movl	$16000, %edi                    # imm = 0x3E80
	callq	malloc
	movq	%rax, %r15
	movl	$16000, %edi                    # imm = 0x3E80
	callq	malloc
	movq	%rax, %r14
	xorl	%ebx, %ebx
.LBB3_3:                                # =>This Inner Loop Header: Depth=1
	movl	$8000, %edi                     # imm = 0x1F40
	callq	malloc
	movq	%rax, (%r15,%rbx,8)
	movl	$8000, %edi                     # imm = 0x1F40
	callq	malloc
	movq	%rax, (%r14,%rbx,8)
	incq	%rbx
	cmpq	$2000, %rbx                     # imm = 0x7D0
	jne	.LBB3_3
# %bb.4:
	movq	%r15, %rdi
	movl	$2000, %esi                     # imm = 0x7D0
	movl	$2453, %edx                     # imm = 0x995
	callq	_Z19initialize_array_2DPPfii
	movq	%r14, %rdi
	movl	$2000, %esi                     # imm = 0x7D0
	movl	$2453, %edx                     # imm = 0x995
	callq	_Z19initialize_array_2DPPfii
	leaq	40(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventCreate
	leaq	16(%rsp), %rdi
	callq	hipEventCreate
	movq	(%r12), %rdi
	xorl	%r12d, %r12d
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movq	%r15, %rbx
.LBB3_5:                                # %_Z9gpuAssert10hipError_tPcib.exit91
                                        # =>This Inner Loop Header: Depth=1
	movq	8(%rsp), %rdi
	addq	%r12, %rdi
	movq	(%rbx), %rsi
	movl	$2000, %edx                     # imm = 0x7D0
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_13
# %bb.6:                                #   in Loop: Header=BB3_5 Depth=1
	addq	$8000, %r12                     # imm = 0x1F40
	addq	$8, %rbx
	cmpq	$16000000, %r12                 # imm = 0xF42400
	jne	.LBB3_5
# %bb.7:                                # %.preheader102.preheader
	movl	$2000, %ebx                     # imm = 0x7D0
	movabsq	$4294967297, %r12               # imm = 0x100000001
	movabsq	$68719476752, %r13              # imm = 0x1000000010
.LBB3_8:                                # %.preheader102
                                        # =>This Inner Loop Header: Depth=1
	movq	%r12, %rdi
	movl	$1, %esi
	movq	%r13, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_10
# %bb.9:                                #   in Loop: Header=BB3_8 Depth=1
	movq	8(%rsp), %rdi
	movl	$1, %esi
	movl	$2000, %edx                     # imm = 0x7D0
	movl	$2000, %ecx                     # imm = 0x7D0
	callq	_Z34__device_stub__kernel_SOR_internalPfiii
.LBB3_10:                               #   in Loop: Header=BB3_8 Depth=1
	decl	%ebx
	jne	.LBB3_8
# %bb.11:
	callq	hipPeekAtLastError
	testl	%eax, %eax
	jne	.LBB3_12
# %bb.15:                               # %_Z9gpuAssert10hipError_tPcib.exit93
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	xorl	%ebx, %ebx
	movq	%r15, 24(%rsp)                  # 8-byte Spill
.LBB3_17:                               # %_Z9gpuAssert10hipError_tPcib.exit95
                                        # =>This Inner Loop Header: Depth=1
	movq	(%r15), %rdi
	movq	8(%rsp), %rsi
	addq	%rbx, %rsi
	movl	$2000, %edx                     # imm = 0x7D0
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_18
# %bb.16:                               #   in Loop: Header=BB3_17 Depth=1
	addq	$8000, %rbx                     # imm = 0x1F40
	addq	$8, %r15
	cmpq	$16000000, %rbx                 # imm = 0xF42400
	jne	.LBB3_17
# %bb.19:
	movq	16(%rsp), %rdi
	xorl	%r13d, %r13d
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	16(%rsp), %rdi
	callq	hipEventSynchronize
	movq	40(%rsp), %rsi
	movq	16(%rsp), %rdx
	leaq	52(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventElapsedTime
	cvtss2sd	(%r12), %xmm0
	movl	$.L.str.9, %edi
	movb	$1, %al
	callq	printf
	movq	40(%rsp), %rdi
	callq	hipEventDestroy
	movq	16(%rsp), %rdi
	callq	hipEventDestroy
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	leaq	56(%rsp), %rsi
	movl	$2, %edi
	callq	clock_gettime
	movq	24(%rsp), %r15                  # 8-byte Reload
.LBB3_20:                               # %.preheader101
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_21 Depth 2
                                        #       Child Loop BB3_22 Depth 3
	xorl	%ebp, %ebp
.LBB3_21:                               # %.preheader100
                                        #   Parent Loop BB3_20 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB3_22 Depth 3
	xorl	%r12d, %r12d
.LBB3_22:                               #   Parent Loop BB3_20 Depth=1
                                        #     Parent Loop BB3_21 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movq	%r14, %rdi
	movl	$1, %esi
	movl	%ebp, %edx
	movl	%r12d, %ecx
	movl	$2000, %r8d                     # imm = 0x7D0
	movl	$2000, %r9d                     # imm = 0x7D0
	callq	_Z23SOR_internal_sequentialPPfiiiii
	incl	%r12d
	cmpl	$2000, %r12d                    # imm = 0x7D0
	jne	.LBB3_22
# %bb.23:                               #   in Loop: Header=BB3_21 Depth=2
	incl	%ebp
	cmpl	$2000, %ebp                     # imm = 0x7D0
	jne	.LBB3_21
# %bb.24:                               #   in Loop: Header=BB3_20 Depth=1
	incl	%r13d
	cmpl	$2000, %r13d                    # imm = 0x7D0
	jne	.LBB3_20
# %bb.25:
	leaq	72(%rsp), %r12
	movl	$2, %edi
	movq	%r12, %rsi
	callq	clock_gettime
	movq	56(%rsp), %rax
	movq	8(%r12), %rcx
	subq	64(%rsp), %rcx
	leaq	1000000000(%rcx), %rdx
	movq	%rax, %rsi
	negq	%rsi
	testq	%rcx, %rcx
	notq	%rax
	cmovnsq	%rsi, %rax
	cmovnsq	%rcx, %rdx
	addq	(%r12), %rax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	cvtsi2sd	%rdx, %xmm1
	addsd	%xmm0, %xmm1
	divsd	.LCPI3_1(%rip), %xmm1
	xorps	%xmm0, %xmm0
	cvtsd2ss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.11, %edi
	movb	$1, %al
	callq	printf
	movl	$.Lstr.4, %edi
	callq	puts@PLT
	xorl	%r12d, %r12d
	xorl	%ebp, %ebp
.LBB3_26:                               # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_27 Depth 2
	movq	(%r15,%r12,8), %rbx
	movq	(%r14,%r12,8), %r15
	xorl	%r13d, %r13d
.LBB3_27:                               #   Parent Loop BB3_26 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rbx,%r13,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	(%r15,%r13,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm1, %xmm0
	jne	.LBB3_28
	jnp	.LBB3_29
.LBB3_28:                               #   in Loop: Header=BB3_27 Depth=2
	incl	%ebp
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	movl	$.L.str.13, %esi
	movq	32(%rsp), %rdi                  # 8-byte Reload
	movl	%r12d, %edx
	movl	%r13d, %ecx
	movb	$2, %al
	callq	fprintf
.LBB3_29:                               #   in Loop: Header=BB3_27 Depth=2
	incq	%r13
	cmpq	$2000, %r13                     # imm = 0x7D0
	jne	.LBB3_27
# %bb.30:                               #   in Loop: Header=BB3_26 Depth=1
	incq	%r12
	cmpq	$2000, %r12                     # imm = 0x7D0
	movq	24(%rsp), %r15                  # 8-byte Reload
	jne	.LBB3_26
# %bb.31:
	movl	$.Lstr.5, %edi
	callq	puts@PLT
	movl	$.L.str.15, %ecx
	movq	%r15, %rdi
	movl	$2000, %esi                     # imm = 0x7D0
	movl	$2000, %edx                     # imm = 0x7D0
	callq	_Z22write_2d_array_to_filePPfiiPc
	movl	$.L.str.16, %ecx
	movq	%r14, %rdi
	movl	$2000, %esi                     # imm = 0x7D0
	movl	$2000, %edx                     # imm = 0x7D0
	callq	_Z22write_2d_array_to_filePPfiiPc
	movl	$.L.str.17, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
	movq	32(%rsp), %rdi                  # 8-byte Reload
	callq	fclose
	movq	8(%rsp), %rdi
	callq	hipFree
	testl	%eax, %eax
	jne	.LBB3_37
# %bb.32:                               # %_Z9gpuAssert10hipError_tPcib.exit97.preheader
	xorl	%ebx, %ebx
.LBB3_33:                               # %_Z9gpuAssert10hipError_tPcib.exit97
                                        # =>This Inner Loop Header: Depth=1
	movq	(%r15,%rbx,8), %rdi
	callq	free
	movq	(%r14,%rbx,8), %rdi
	callq	free
	incq	%rbx
	cmpq	$2000, %rbx                     # imm = 0x7D0
	jne	.LBB3_33
# %bb.34:
	movq	%r15, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movl	$.Lstr.6, %edi
	callq	puts@PLT
	xorl	%eax, %eax
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB3_13:
	.cfi_def_cfa_offset 144
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.19, %esi
	movl	$.L.str.6, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$144, %r8d
	jmp	.LBB3_14
.LBB3_18:
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.19, %esi
	movl	$.L.str.6, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$167, %r8d
.LBB3_14:
	xorl	%eax, %eax
	callq	fprintf
	movl	%ebp, %edi
	callq	exit
.LBB3_35:
	movl	$.Lstr.7, %edi
	callq	puts@PLT
	movl	$1, %edi
	callq	exit
.LBB3_36:
	movl	%eax, %ebx
	movq	stderr(%rip), %r14
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.19, %esi
	movl	$.L.str.6, %ecx
	movq	%r14, %rdi
	movq	%rax, %rdx
	movl	$115, %r8d
	xorl	%eax, %eax
	callq	fprintf
	movl	%ebx, %edi
	callq	exit
.LBB3_12:
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.19, %esi
	movl	$.L.str.6, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$158, %r8d
	jmp	.LBB3_14
.LBB3_37:
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.19, %esi
	movl	$.L.str.6, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$237, %r8d
	jmp	.LBB3_14
.Lfunc_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z19initialize_array_2DPPfii    # -- Begin function _Z19initialize_array_2DPPfii
	.type	_Z19initialize_array_2DPPfii,@function
_Z19initialize_array_2DPPfii:           # @_Z19initialize_array_2DPPfii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebp
	movq	%rdi, %rbx
	movl	%edx, %edi
	callq	srand
	testl	%ebp, %ebp
	jle	.LBB4_5
# %bb.1:                                # %.preheader.lr.ph
	movl	%ebp, %r14d
	xorl	%r15d, %r15d
.LBB4_2:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_3 Depth 2
	xorl	%r12d, %r12d
.LBB4_3:                                #   Parent Loop BB4_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movq	(%rbx,%r15,8), %rax
	movss	%xmm0, (%rax,%r12,4)
	incq	%r12
	cmpq	%r12, %r14
	jne	.LBB4_3
# %bb.4:                                # %._crit_edge
                                        #   in Loop: Header=BB4_2 Depth=1
	incq	%r15
	cmpq	%r14, %r15
	jne	.LBB4_2
.LBB4_5:                                # %._crit_edge14
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z19initialize_array_2DPPfii, .Lfunc_end4-_Z19initialize_array_2DPPfii
	.cfi_endproc
                                        # -- End function
	.globl	_Z4diff8timespecS_              # -- Begin function _Z4diff8timespecS_
	.type	_Z4diff8timespecS_,@function
_Z4diff8timespecS_:                     # @_Z4diff8timespecS_
	.cfi_startproc
# %bb.0:
	subq	%rsi, %rcx
	leaq	1000000000(%rcx), %rsi
	movq	%rdi, %rax
	negq	%rax
	testq	%rcx, %rcx
	notq	%rdi
	cmovsq	%rdi, %rax
	cmovnsq	%rcx, %rsi
	addq	%rdx, %rax
	movq	%rsi, %rdx
	retq
.Lfunc_end5:
	.size	_Z4diff8timespecS_, .Lfunc_end5-_Z4diff8timespecS_
	.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	.LBB6_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB6_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z19kernel_SOR_internalPfiii, %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_end6:
	.size	__hip_module_ctor, .Lfunc_end6-__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	.LBB7_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
.LBB7_2:
	retq
.Lfunc_end7:
	.size	__hip_module_dtor, .Lfunc_end7-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z19kernel_SOR_internalPfiii,@object # @_Z19kernel_SOR_internalPfiii
	.section	.rodata,"a",@progbits
	.globl	_Z19kernel_SOR_internalPfiii
	.p2align	3, 0x0
_Z19kernel_SOR_internalPfiii:
	.quad	_Z34__device_stub__kernel_SOR_internalPfiii
	.size	_Z19kernel_SOR_internalPfiii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"w"
	.size	.L.str, 2

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"%f, "
	.size	.L.str.1, 5

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%f\n"
	.size	.L.str.2, 4

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"mismatches.txt"
	.size	.L.str.3, 15

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/Chen-Yu-Chang/High-Performance-Programming-with-Multicore-and-GPUs/master/Assignment7/SOR_cuda.hip"
	.size	.L.str.6, 161

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"\nGPU time: %f (msec)\n"
	.size	.L.str.9, 22

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"\nCPU time: %f(msec)\n"
	.size	.L.str.11, 21

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Mismatch at [%d,%d] GPU = %f CPU = %f\n"
	.size	.L.str.13, 39

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"GPU_output.txt"
	.size	.L.str.15, 15

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"CPU_output.txt"
	.size	.L.str.16, 15

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"Found %d errors\n"
	.size	.L.str.17, 17

	.type	.L.str.19,@object               # @.str.19
.L.str.19:
	.asciz	"CUDA_SAFE_CALL: %s %s %d\n"
	.size	.L.str.19, 26

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

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

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

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"init done"
	.size	.Lstr, 10

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"all init done"
	.size	.Lstr.1, 14

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"kernel run"
	.size	.Lstr.2, 11

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"results transfered"
	.size	.Lstr.3, 19

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"results computed on CPU"
	.size	.Lstr.4, 24

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"results checked"
	.size	.Lstr.5, 16

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"arrays freed"
	.size	.Lstr.6, 13

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"Error opening file!"
	.size	.Lstr.7, 20

	.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 _Z34__device_stub__kernel_SOR_internalPfiii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z19kernel_SOR_internalPfiii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z19kernel_SOR_internalPfiii ; -- Begin function _Z19kernel_SOR_internalPfiii
	.globl	_Z19kernel_SOR_internalPfiii
	.p2align	8
	.type	_Z19kernel_SOR_internalPfiii,@function
_Z19kernel_SOR_internalPfiii:           ; @_Z19kernel_SOR_internalPfiii
; %bb.0:
	s_load_b32 s4, s[0:1], 0x24
	s_add_u32 s2, s0, 24
	v_and_b32_e32 v6, 0x3ff, v0
	s_addc_u32 s3, s1, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	s_mul_i32 s6, s15, s4
	s_mov_b32 s4, exec_lo
	v_add_nc_u32_e32 v3, s6, v6
	v_mul_lo_u32 v1, 0x7d, v3
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e32 0x7fffff83, v1
	s_cbranch_execz .LBB0_8
; %bb.1:                                ; %.lr.ph45
	s_load_b32 s2, s[2:3], 0xc
	s_load_b128 s[8:11], s[0:1], 0x8
	v_bfe_u32 v0, v0, 10, 10
	s_load_b64 s[4:5], s[0:1], 0x0
	v_mul_lo_u32 v3, 0x3d090, v3
	s_mul_i32 s6, s6, 0x3d090
	v_add_nc_u32_e32 v2, 0x7d, v1
	v_mad_u32_u24 v6, 0x3d090, v6, s6
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s2, s2, 16
	s_add_i32 s6, s9, -1
	v_mad_u64_u32 v[4:5], null, s15, s2, v[0:1]
	s_add_i32 s7, s10, -1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_mul_lo_u32 v0, 0x7d, v4
	v_cvt_f32_i32_e32 v4, s8
	s_mov_b32 s8, 0
	v_add_nc_u32_e32 v5, 0x7d, v0
	v_cmp_gt_i32_e32 vcc_lo, 0x7fffff83, v0
.LBB0_2:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_4 Depth 2
	s_and_saveexec_b32 s9, vcc_lo
	s_cbranch_execz .LBB0_7
; %bb.3:                                ; %.lr.ph
                                        ;   in Loop: Header=BB0_2 Depth=1
	v_cmp_lt_i32_e64 s0, 0, v1
	v_cmp_gt_i32_e64 s1, s6, v1
	v_mov_b32_e32 v7, v0
	s_mov_b32 s10, 0
.LBB0_4:                                ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cmp_lt_i32_e64 s2, 0, v7
	v_cmp_gt_i32_e64 s3, s7, v7
	s_and_b32 s2, s1, s2
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, s2, s3
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, s0, s2
	s_and_saveexec_b32 s3, s2
	s_cbranch_execz .LBB0_6
; %bb.5:                                ;   in Loop: Header=BB0_4 Depth=2
	v_add_nc_u32_e32 v8, v3, v7
	v_add3_u32 v11, v6, v7, -1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_4)
	v_add_nc_u32_e32 v9, 0xfffff830, v8
	v_add_nc_u32_e32 v13, 0x7d0, v8
	v_add_nc_u32_e32 v15, 1, v8
	v_ashrrev_i32_e32 v12, 31, v11
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_ashrrev_i32_e32 v10, 31, v9
	v_ashrrev_i32_e32 v14, 31, v13
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_ashrrev_i32_e32 v16, 31, v15
	v_lshlrev_b64 v[17:18], 2, v[9:10]
	v_lshlrev_b64 v[10:11], 2, v[11:12]
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_lshlrev_b64 v[12:13], 2, v[13:14]
	v_lshlrev_b64 v[14:15], 2, v[15:16]
	v_ashrrev_i32_e32 v9, 31, v8
	v_add_co_u32 v16, s2, s4, v17
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_add_co_ci_u32_e64 v17, s2, s5, v18, s2
	v_add_co_u32 v12, s2, s4, v12
	v_add_co_ci_u32_e64 v13, s2, s5, v13, s2
	v_add_co_u32 v10, s2, s4, v10
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v11, s2, s5, v11, s2
	v_add_co_u32 v14, s2, s4, v14
	v_lshlrev_b64 v[8:9], 2, v[8:9]
	v_add_co_ci_u32_e64 v15, s2, s5, v15, s2
	s_clause 0x3
	global_load_b32 v16, v[16:17], off
	global_load_b32 v12, v[12:13], off
	global_load_b32 v10, v[10:11], off
	global_load_b32 v11, v[14:15], off
	v_add_co_u32 v8, s2, s4, v8
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v9, s2, s5, v9, s2
	global_load_b32 v14, v[8:9], off
	s_waitcnt vmcnt(3)
	v_add_f32_e32 v12, v16, v12
	s_waitcnt vmcnt(1)
	v_add_f32_e32 v10, v10, v11
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_add_f32_e32 v12, v12, v10
	s_waitcnt vmcnt(0)
	v_cvt_f64_f32_e32 v[10:11], v14
	v_cvt_f64_f32_e32 v[12:13], v12
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[10:11], 0xbfd00000, v[12:13], v[10:11]
	v_cvt_f32_f64_e32 v10, v[10:11]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f32 v10, -v4, v10, v14
	v_and_b32_e32 v10, 0x7fffffff, v10
	global_store_b32 v[8:9], v10, off
.LBB0_6:                                ;   in Loop: Header=BB0_4 Depth=2
	s_or_b32 exec_lo, exec_lo, s3
	v_add_nc_u32_e32 v7, 1, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ge_i32_e64 s2, v7, v5
	s_or_b32 s10, s2, s10
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s10
	s_cbranch_execnz .LBB0_4
.LBB0_7:                                ; %Flow59
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s9
	v_add_nc_u32_e32 v1, 1, v1
	v_add_nc_u32_e32 v6, 0x7d0, v6
	v_add_nc_u32_e32 v3, 0x7d0, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ge_i32_e64 s0, v1, v2
	s_or_b32 s8, s0, s8
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s8
	s_cbranch_execnz .LBB0_2
.LBB0_8:                                ; %._crit_edge46
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z19kernel_SOR_internalPfiii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 19
		.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	_Z19kernel_SOR_internalPfiii, .Lfunc_end0-_Z19kernel_SOR_internalPfiii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 724
; NumSgprs: 18
; NumVgprs: 19
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 19
; 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: 1
	.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
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .offset:         12
        .size:           4
        .value_kind:     by_value
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z19kernel_SOR_internalPfiii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z19kernel_SOR_internalPfiii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     19
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
