// Program for Matrix Addition in CUDA
// For Hadoop-CUDA Lab

#include <stdio.h>
#include <cuda.h>
#include <stdlib.h>

__global__ void gpu_matrixadd(int *a,int *b, int *c, int N) {

	int col = threadIdx.x + blockDim.x * blockIdx.x; 
	int row = threadIdx.y + blockDim.y * blockIdx.y;

	int index = row * N + col;

      	if(col < N && row < N)
          c[index] = a[index]+b[index];

}

void cpu_matrixadd(int *a,int *b, int *c, int N) {

	int index;
	for(int col=0;col < N; col++) 
		for(int row=0;row < N; row++) {
			index = row * N + col;
           		c[index] = a[index]+b[index];
		}
}

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

	char key;

	int i, j; 					// loop counters

	int Grid_Dim_x=1, Grid_Dim_y=1;			//Grid structure values
	int Block_Dim_x=1, Block_Dim_y=1;		//Block structure values

	int noThreads_x, noThreads_y;		// number of threads available in device, each dimension
	int noThreads_block;				// number of threads in a block

	int N = 10;  					// size of array in each dimension
	int *a,*b,*c,*d;
	int *dev_a, *dev_b, *dev_c;
	int size;					// number of bytes in arrays

	cudaEvent_t start, stop;     		// using cuda events to measure time
	float elapsed_time_ms;       		// which is applicable for asynchronous code also

/* --------------------ENTER INPUT PARAMETERS AND DATA -----------------------*/

do {  // loop to repeat complete program

	printf ("Device characteristics -- some limitations (compute capability 1.0)\n");
	printf ("		Maximum number of threads per block = 512\n");
	printf ("		Maximum sizes of x- and y- dimension of thread block = 512\n");
	printf ("		Maximum size of each dimension of grid of thread blocks = 65535\n");
	
	printf("Enter size of array in one dimension (square array), currently %d\n",N);
	scanf("%d",&N);
		
	do {
		printf("\nEnter nuumber of blocks per grid in x dimension), currently %d  : ",Grid_Dim_x);
		scanf("%d",&Grid_Dim_x);

		printf("\nEnter nuumber of blocks per grid in y dimension), currently %d  : ",Grid_Dim_y);
		scanf("%d",&Grid_Dim_y);

		printf("\nEnter nuumber of threads per block in x dimension), currently %d  : ",Block_Dim_x);
		scanf("%d",&Block_Dim_x);

		printf("\nEnter nuumber of threads per block in y dimension), currently %d  : ",Block_Dim_y);
		scanf("%d",&Block_Dim_y);

		noThreads_x = Grid_Dim_x * Block_Dim_x;		// number of threads in x dimension
		noThreads_y = Grid_Dim_y * Block_Dim_y;		// number of threads in y dimension

		noThreads_block = Block_Dim_x * Block_Dim_y;	// number of threads in a block

		if (noThreads_x < N) printf("Error -- number of threads in x dimension less than number of elements in arrays, try again\n");
		else if (noThreads_y < N) printf("Error -- number of threads in y dimension less than number of elements in arrays, try again\n");
		else if (noThreads_block > 512) printf("Error -- too many threads in block, try again\n");
		else printf("Number of threads not used = %d\n", noThreads_x * noThreads_y - N * N);

	} while (noThreads_x < N || noThreads_y < N || noThreads_block > 512);

	dim3 Grid(Grid_Dim_x, Grid_Dim_x);		//Grid structure
	dim3 Block(Block_Dim_x,Block_Dim_y);	//Block structure, threads/block limited by specific device

	size = N * N * sizeof(int);		// number of bytes in total in arrays

	a = (int*) malloc(size);		//this time use dynamically allocated memory for arrays on host
	b = (int*) malloc(size);
	c = (int*) malloc(size);		// results from GPU
	d = (int*) malloc(size);		// results from CPU

	for(i=0;i < N;i++)			// load arrays with some numbers
	for(j=0;j < N;j++) {
		a[i * N + j] = i;
		b[i * N + j] = i;
	}

/* ------------- COMPUTATION DONE ON GPU ----------------------------*/

	cudaMalloc((void**)&dev_a, size);		// allocate memory on device
	cudaMalloc((void**)&dev_b, size);
	cudaMalloc((void**)&dev_c, size);

	cudaMemcpy(dev_a, a , size ,cudaMemcpyHostToDevice);
	cudaMemcpy(dev_b, b , size ,cudaMemcpyHostToDevice);
	cudaMemcpy(dev_c, c , size ,cudaMemcpyHostToDevice);

	cudaEventCreate(&start);     		// instrument code to measure start time
	cudaEventCreate(&stop);

	cudaEventRecord(start, 0);
//	cudaEventSynchronize(start);  	// Needed?

	gpu_matrixadd<<<Grid,Block>>>(dev_a,dev_b,dev_c,N);

	cudaMemcpy(c,dev_c, size ,cudaMemcpyDeviceToHost);

	cudaEventRecord(stop, 0);     	// instrument code to measue end time
	cudaEventSynchronize(stop);
	cudaEventElapsedTime(&elapsed_time_ms, start, stop );

//	for(i=0;i < N;i++) 
//	for(j=0;j < N;j++)
//	   printf("%d+%d=%d\n",a[i * N + j],b[i * N + j],c[i * N + j]);

	printf("Time to calculate results on GPU: %f ms.\n", elapsed_time_ms);  // print out execution time

/* ------------- COMPUTATION DONE ON HOST CPU ----------------------------*/

	cudaEventRecord(start, 0);		// use same timing
//	cudaEventSynchronize(start);  	// Needed?

	cpu_matrixadd(a,b,d,N);		// do calculation on host

	cudaEventRecord(stop, 0);     	// instrument code to measue end time
	cudaEventSynchronize(stop);
	cudaEventElapsedTime(&elapsed_time_ms, start, stop );

	printf("Time to calculate results on CPU: %f ms.\n", elapsed_time_ms);  // print out execution time

/* ------------------- check device creates correct results -----------------*/

	for(i=0;i < N*N;i++) {
		if (c[i] != d[i]) printf("*********** ERROR in results, CPU and GPU create different answers ********\n");
		break;
	}

	printf("\nEnter c to repeat, return to terminate\n");
	scanf("%c",&key);
	scanf("%c",&key);

} while (key == 'c'); // loop of complete program

/* --------------  clean up  ---------------------------------------*/
	free(a);
	free(b);
	free(c);
	cudaFree(dev_a);
	cudaFree(dev_b);
	cudaFree(dev_c);

	cudaEventDestroy(start);
	cudaEventDestroy(stop);

	return 0;
}




	.file	"tmpxft_00239434_00000000-6_2_Matrix_addition.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2031:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2031:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z13cpu_matrixaddPiS_S_i
	.type	_Z13cpu_matrixaddPiS_S_i, @function
_Z13cpu_matrixaddPiS_S_i:
.LFB2027:
	.cfi_startproc
	endbr64
	movslq	%ecx, %r8
	movq	%rdi, %r9
	xorl	%edi, %edi
	salq	$2, %r8
.L3:
	cmpl	%edi, %ecx
	jle	.L8
	leaq	0(,%rdi,4), %rax
	xorl	%r10d, %r10d
.L4:
	movl	(%rsi,%rax), %r11d
	incl	%r10d
	addl	(%r9,%rax), %r11d
	movl	%r11d, (%rdx,%rax)
	addq	%r8, %rax
	cmpl	%r10d, %ecx
	jne	.L4
	incq	%rdi
	jmp	.L3
.L8:
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z13cpu_matrixaddPiS_S_i, .-_Z13cpu_matrixaddPiS_S_i
	.globl	_Z38__device_stub__Z13gpu_matrixaddPiS_S_iPiS_S_i
	.type	_Z38__device_stub__Z13gpu_matrixaddPiS_S_iPiS_S_i, @function
_Z38__device_stub__Z13gpu_matrixaddPiS_S_iPiS_S_i:
.LFB2053:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 4(%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	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%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	.L9
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z13gpu_matrixaddPiS_S_i(%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
.L9:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L11
	call	__stack_chk_fail@PLT
.L11:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2053:
	.size	_Z38__device_stub__Z13gpu_matrixaddPiS_S_iPiS_S_i, .-_Z38__device_stub__Z13gpu_matrixaddPiS_S_iPiS_S_i
	.globl	_Z13gpu_matrixaddPiS_S_i
	.type	_Z13gpu_matrixaddPiS_S_i, @function
_Z13gpu_matrixaddPiS_S_i:
.LFB2054:
	.cfi_startproc
	endbr64
	jmp	_Z38__device_stub__Z13gpu_matrixaddPiS_S_iPiS_S_i
	.cfi_endproc
.LFE2054:
	.size	_Z13gpu_matrixaddPiS_S_i, .-_Z13gpu_matrixaddPiS_S_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Device characteristics -- some limitations (compute capability 1.0)\n"
.LC1:
	.string	"\t\tMaximum number of threads per block = 512\n"
.LC2:
	.string	"\t\tMaximum sizes of x- and y- dimension of thread block = 512\n"
.LC3:
	.string	"\t\tMaximum size of each dimension of grid of thread blocks = 65535\n"
.LC4:
	.string	"Enter size of array in one dimension (square array), currently %d\n"
.LC5:
	.string	"%d"
.LC6:
	.string	"\nEnter nuumber of blocks per grid in x dimension), currently %d  : "
.LC7:
	.string	"\nEnter nuumber of blocks per grid in y dimension), currently %d  : "
.LC8:
	.string	"\nEnter nuumber of threads per block in x dimension), currently %d  : "
.LC9:
	.string	"\nEnter nuumber of threads per block in y dimension), currently %d  : "
.LC10:
	.string	"Error -- number of threads in x dimension less than number of elements in arrays, try again\n"
.LC11:
	.string	"Error -- number of threads in y dimension less than number of elements in arrays, try again\n"
.LC12:
	.string	"Error -- too many threads in block, try again\n"
.LC13:
	.string	"Number of threads not used = %d\n"
.LC14:
	.string	"Time to calculate results on GPU: %f ms.\n"
.LC15:
	.string	"Time to calculate results on CPU: %f ms.\n"
.LC16:
	.string	"*********** ERROR in results, CPU and GPU create different answers ********\n"
.LC17:
	.string	"\nEnter c to repeat, return to terminate\n"
.LC18:
	.string	"%c"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	movl	$1, 16(%rsp)
	movl	$1, 20(%rsp)
	movl	$1, 24(%rsp)
	movl	$1, 28(%rsp)
	movl	$10, 32(%rsp)
.L24:
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	.LC2(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	.LC3(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	32(%rsp), %edx
	xorl	%eax, %eax
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	leaq	.LC5(%rip), %r12
	leaq	.LC6(%rip), %r15
	call	__printf_chk@PLT
	leaq	32(%rsp), %rsi
	movq	%r12, %rdi
	xorl	%eax, %eax
	call	__isoc23_scanf@PLT
.L31:
	movl	16(%rsp), %edx
	movq	%r15, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	16(%rsp), %rsi
	movq	%r12, %rdi
	xorl	%eax, %eax
	call	__isoc23_scanf@PLT
	movl	20(%rsp), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC7(%rip), %rsi
	call	__printf_chk@PLT
	leaq	20(%rsp), %rsi
	movq	%r12, %rdi
	xorl	%eax, %eax
	call	__isoc23_scanf@PLT
	movl	24(%rsp), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC8(%rip), %rsi
	call	__printf_chk@PLT
	leaq	24(%rsp), %rsi
	movq	%r12, %rdi
	xorl	%eax, %eax
	call	__isoc23_scanf@PLT
	movl	28(%rsp), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC9(%rip), %rsi
	call	__printf_chk@PLT
	leaq	28(%rsp), %rsi
	movq	%r12, %rdi
	xorl	%eax, %eax
	call	__isoc23_scanf@PLT
	movl	24(%rsp), %ebp
	movl	16(%rsp), %ebx
	leaq	.LC10(%rip), %rsi
	movl	28(%rsp), %eax
	movl	20(%rsp), %r13d
	imull	%ebp, %ebx
	imull	%eax, %r13d
	imull	%eax, %ebp
	movl	32(%rsp), %eax
	cmpl	%ebx, %eax
	jg	.L34
	cmpl	%r13d, %eax
	jle	.L17
	leaq	.LC11(%rip), %rsi
.L34:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L16
.L17:
	leaq	.LC12(%rip), %rsi
	cmpl	$512, %ebp
	jg	.L34
	movl	%ebx, %edx
	imull	%eax, %eax
	movl	$2, %edi
	imull	%r13d, %edx
	leaq	.LC13(%rip), %rsi
	subl	%eax, %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L16:
	cmpl	%r13d, %ebx
	movl	32(%rsp), %r14d
	cmovg	%r13d, %ebx
	cmpl	%ebx, %r14d
	jg	.L31
	cmpl	$512, %ebp
	jg	.L31
	movl	%r14d, %ebx
	movl	16(%rsp), %eax
	movl	$1, 88(%rsp)
	imull	%r14d, %ebx
	movl	$1, 100(%rsp)
	movl	%eax, 80(%rsp)
	movl	%eax, 84(%rsp)
	movl	24(%rsp), %eax
	sall	$2, %ebx
	movslq	%ebx, %rbx
	movl	%eax, 92(%rsp)
	movl	28(%rsp), %eax
	movq	%rbx, %rdi
	movl	%eax, 96(%rsp)
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %r13
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %r12
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %rbp
	call	malloc@PLT
	movslq	%r14d, %rsi
	xorl	%edi, %edi
	movq	%rax, %r15
	salq	$2, %rsi
	xorl	%eax, %eax
.L20:
	cmpl	%eax, %r14d
	jle	.L35
	movq	%rdi, %rcx
	xorl	%r8d, %r8d
.L21:
	incl	%r8d
	movl	%eax, 0(%r13,%rcx)
	movl	%eax, (%r12,%rcx)
	addq	$4, %rcx
	cmpl	%r8d, %r14d
	jne	.L21
	incl	%eax
	addq	%rsi, %rdi
	jmp	.L20
.L35:
	leaq	40(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	48(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	56(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	40(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r13, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	48(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r12, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	56(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	64(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	72(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	100(%rsp), %ecx
	movl	88(%rsp), %esi
	xorl	%r9d, %r9d
	movq	92(%rsp), %rdx
	movq	80(%rsp), %rdi
	xorl	%r8d, %r8d
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L22
	movl	32(%rsp), %ecx
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	movq	40(%rsp), %rdi
	call	_Z38__device_stub__Z13gpu_matrixaddPiS_S_iPiS_S_i
.L22:
	movq	56(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdx
	movq	%rbp, %rdi
	leaq	36(%rsp), %rbx
	call	cudaMemcpy@PLT
	movq	72(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	72(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	72(%rsp), %rdx
	movq	64(%rsp), %rsi
	movq	%rbx, %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC14(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	36(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	32(%rsp), %ecx
	movq	%r15, %rdx
	movq	%r12, %rsi
	movq	%r13, %rdi
	call	_Z13cpu_matrixaddPiS_S_i
	movq	72(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	72(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	72(%rsp), %rdx
	movq	64(%rsp), %rsi
	movq	%rbx, %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC15(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	36(%rsp), %xmm0
	call	__printf_chk@PLT
	movl	32(%rsp), %eax
	imull	%eax, %eax
	testl	%eax, %eax
	je	.L23
	movl	(%r15), %eax
	cmpl	%eax, 0(%rbp)
	je	.L23
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L23:
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	15(%rsp), %r15
	leaq	.LC18(%rip), %rbx
	call	__printf_chk@PLT
	movq	%r15, %rsi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_scanf@PLT
	xorl	%eax, %eax
	movq	%r15, %rsi
	movq	%rbx, %rdi
	call	__isoc23_scanf@PLT
	cmpb	$99, 15(%rsp)
	je	.L24
	movq	%r13, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	64(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	72(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L25
	call	__stack_chk_fail@PLT
.L25:
	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
.LFE2028:
	.size	main, .-main
	.section	.rodata.str1.1
.LC19:
	.string	"_Z13gpu_matrixaddPiS_S_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2056:
	.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	.LC19(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z13gpu_matrixaddPiS_S_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2056:
	.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
	.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:

// Program for Matrix Addition in CUDA
// For Hadoop-CUDA Lab

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

__global__ void gpu_matrixadd(int *a,int *b, int *c, int N) {

	int col = threadIdx.x + blockDim.x * blockIdx.x; 
	int row = threadIdx.y + blockDim.y * blockIdx.y;

	int index = row * N + col;

      	if(col < N && row < N)
          c[index] = a[index]+b[index];

}

void cpu_matrixadd(int *a,int *b, int *c, int N) {

	int index;
	for(int col=0;col < N; col++) 
		for(int row=0;row < N; row++) {
			index = row * N + col;
           		c[index] = a[index]+b[index];
		}
}

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

	char key;

	int i, j; 					// loop counters

	int Grid_Dim_x=1, Grid_Dim_y=1;			//Grid structure values
	int Block_Dim_x=1, Block_Dim_y=1;		//Block structure values

	int noThreads_x, noThreads_y;		// number of threads available in device, each dimension
	int noThreads_block;				// number of threads in a block

	int N = 10;  					// size of array in each dimension
	int *a,*b,*c,*d;
	int *dev_a, *dev_b, *dev_c;
	int size;					// number of bytes in arrays

	hipEvent_t start, stop;     		// using cuda events to measure time
	float elapsed_time_ms;       		// which is applicable for asynchronous code also

/* --------------------ENTER INPUT PARAMETERS AND DATA -----------------------*/

do {  // loop to repeat complete program

	printf ("Device characteristics -- some limitations (compute capability 1.0)\n");
	printf ("		Maximum number of threads per block = 512\n");
	printf ("		Maximum sizes of x- and y- dimension of thread block = 512\n");
	printf ("		Maximum size of each dimension of grid of thread blocks = 65535\n");
	
	printf("Enter size of array in one dimension (square array), currently %d\n",N);
	scanf("%d",&N);
		
	do {
		printf("\nEnter nuumber of blocks per grid in x dimension), currently %d  : ",Grid_Dim_x);
		scanf("%d",&Grid_Dim_x);

		printf("\nEnter nuumber of blocks per grid in y dimension), currently %d  : ",Grid_Dim_y);
		scanf("%d",&Grid_Dim_y);

		printf("\nEnter nuumber of threads per block in x dimension), currently %d  : ",Block_Dim_x);
		scanf("%d",&Block_Dim_x);

		printf("\nEnter nuumber of threads per block in y dimension), currently %d  : ",Block_Dim_y);
		scanf("%d",&Block_Dim_y);

		noThreads_x = Grid_Dim_x * Block_Dim_x;		// number of threads in x dimension
		noThreads_y = Grid_Dim_y * Block_Dim_y;		// number of threads in y dimension

		noThreads_block = Block_Dim_x * Block_Dim_y;	// number of threads in a block

		if (noThreads_x < N) printf("Error -- number of threads in x dimension less than number of elements in arrays, try again\n");
		else if (noThreads_y < N) printf("Error -- number of threads in y dimension less than number of elements in arrays, try again\n");
		else if (noThreads_block > 512) printf("Error -- too many threads in block, try again\n");
		else printf("Number of threads not used = %d\n", noThreads_x * noThreads_y - N * N);

	} while (noThreads_x < N || noThreads_y < N || noThreads_block > 512);

	dim3 Grid(Grid_Dim_x, Grid_Dim_x);		//Grid structure
	dim3 Block(Block_Dim_x,Block_Dim_y);	//Block structure, threads/block limited by specific device

	size = N * N * sizeof(int);		// number of bytes in total in arrays

	a = (int*) malloc(size);		//this time use dynamically allocated memory for arrays on host
	b = (int*) malloc(size);
	c = (int*) malloc(size);		// results from GPU
	d = (int*) malloc(size);		// results from CPU

	for(i=0;i < N;i++)			// load arrays with some numbers
	for(j=0;j < N;j++) {
		a[i * N + j] = i;
		b[i * N + j] = i;
	}

/* ------------- COMPUTATION DONE ON GPU ----------------------------*/

	hipMalloc((void**)&dev_a, size);		// allocate memory on device
	hipMalloc((void**)&dev_b, size);
	hipMalloc((void**)&dev_c, size);

	hipMemcpy(dev_a, a , size ,hipMemcpyHostToDevice);
	hipMemcpy(dev_b, b , size ,hipMemcpyHostToDevice);
	hipMemcpy(dev_c, c , size ,hipMemcpyHostToDevice);

	hipEventCreate(&start);     		// instrument code to measure start time
	hipEventCreate(&stop);

	hipEventRecord(start, 0);
//	cudaEventSynchronize(start);  	// Needed?

	gpu_matrixadd<<<Grid,Block>>>(dev_a,dev_b,dev_c,N);

	hipMemcpy(c,dev_c, size ,hipMemcpyDeviceToHost);

	hipEventRecord(stop, 0);     	// instrument code to measue end time
	hipEventSynchronize(stop);
	hipEventElapsedTime(&elapsed_time_ms, start, stop );

//	for(i=0;i < N;i++) 
//	for(j=0;j < N;j++)
//	   printf("%d+%d=%d\n",a[i * N + j],b[i * N + j],c[i * N + j]);

	printf("Time to calculate results on GPU: %f ms.\n", elapsed_time_ms);  // print out execution time

/* ------------- COMPUTATION DONE ON HOST CPU ----------------------------*/

	hipEventRecord(start, 0);		// use same timing
//	cudaEventSynchronize(start);  	// Needed?

	cpu_matrixadd(a,b,d,N);		// do calculation on host

	hipEventRecord(stop, 0);     	// instrument code to measue end time
	hipEventSynchronize(stop);
	hipEventElapsedTime(&elapsed_time_ms, start, stop );

	printf("Time to calculate results on CPU: %f ms.\n", elapsed_time_ms);  // print out execution time

/* ------------------- check device creates correct results -----------------*/

	for(i=0;i < N*N;i++) {
		if (c[i] != d[i]) printf("*********** ERROR in results, CPU and GPU create different answers ********\n");
		break;
	}

	printf("\nEnter c to repeat, return to terminate\n");
	scanf("%c",&key);
	scanf("%c",&key);

} while (key == 'c'); // loop of complete program

/* --------------  clean up  ---------------------------------------*/
	free(a);
	free(b);
	free(c);
	hipFree(dev_a);
	hipFree(dev_b);
	hipFree(dev_c);

	hipEventDestroy(start);
	hipEventDestroy(stop);

	return 0;
}




	.text
	.file	"2_Matrix_addition.hip"
	.globl	_Z28__device_stub__gpu_matrixaddPiS_S_i # -- Begin function _Z28__device_stub__gpu_matrixaddPiS_S_i
	.type	_Z28__device_stub__gpu_matrixaddPiS_S_i,@function
_Z28__device_stub__gpu_matrixaddPiS_S_i: # @_Z28__device_stub__gpu_matrixaddPiS_S_i
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%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	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z13gpu_matrixaddPiS_S_i, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z28__device_stub__gpu_matrixaddPiS_S_i, .Lfunc_end0-_Z28__device_stub__gpu_matrixaddPiS_S_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z13cpu_matrixaddPiS_S_i        # -- Begin function _Z13cpu_matrixaddPiS_S_i
	.type	_Z13cpu_matrixaddPiS_S_i,@function
_Z13cpu_matrixaddPiS_S_i:               # @_Z13cpu_matrixaddPiS_S_i
	.cfi_startproc
# %bb.0:
	testl	%ecx, %ecx
	jle	.LBB1_5
# %bb.1:                                # %.preheader.lr.ph
	movl	%ecx, %eax
	leaq	(,%rax,4), %rcx
	xorl	%r8d, %r8d
.LBB1_2:                                # %.lr.ph.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_3 Depth 2
	movq	%rax, %r9
	xorl	%r10d, %r10d
.LBB1_3:                                # %.lr.ph
                                        #   Parent Loop BB1_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%rsi,%r10), %r11d
	addl	(%rdi,%r10), %r11d
	movl	%r11d, (%rdx,%r10)
	addq	%rcx, %r10
	decq	%r9
	jne	.LBB1_3
# %bb.4:                                # %._crit_edge
                                        #   in Loop: Header=BB1_2 Depth=1
	incq	%r8
	addq	$4, %rdx
	addq	$4, %rsi
	addq	$4, %rdi
	cmpq	%rax, %r8
	jne	.LBB1_2
.LBB1_5:                                # %._crit_edge18
	retq
.Lfunc_end1:
	.size	_Z13cpu_matrixaddPiS_S_i, .Lfunc_end1-_Z13cpu_matrixaddPiS_S_i
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function 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	$104, %rsp
	.cfi_def_cfa_offset 160
	.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	$1, %eax
	leaq	20(%rsp), %r12
	movl	%eax, (%r12)
	leaq	36(%rsp), %rcx
	movl	%eax, (%rcx)
	leaq	16(%rsp), %rcx
	movl	%eax, (%rcx)
	leaq	12(%rsp), %rcx
	movl	%eax, (%rcx)
	leaq	4(%rsp), %rax
	movl	$10, (%rax)
.LBB2_1:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_2 Depth 2
                                        #     Child Loop BB2_15 Depth 2
                                        #       Child Loop BB2_16 Depth 3
                                        #     Child Loop BB2_22 Depth 2
                                        #       Child Loop BB2_23 Depth 3
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	4(%rsp), %esi
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.5, %edi
	leaq	4(%rsp), %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	leaq	36(%rsp), %r13
.LBB2_2:                                # %.critedge
                                        #   Parent Loop BB2_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	20(%rsp), %esi
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.5, %edi
	movq	%r12, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	36(%rsp), %esi
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.5, %edi
	movq	%r13, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	16(%rsp), %esi
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.5, %edi
	leaq	16(%rsp), %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	12(%rsp), %esi
	movl	$.L.str.9, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.5, %edi
	leaq	12(%rsp), %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	16(%rsp), %eax
	movl	20(%rsp), %ebp
	imull	%eax, %ebp
	movl	12(%rsp), %ebx
	movl	36(%rsp), %r14d
	imull	%ebx, %r14d
	imull	%eax, %ebx
	movl	4(%rsp), %eax
	cmpl	%eax, %ebp
	jge	.LBB2_5
# %bb.3:                                #   in Loop: Header=BB2_2 Depth=2
	movl	$.Lstr.6, %edi
	jmp	.LBB2_4
.LBB2_5:                                #   in Loop: Header=BB2_2 Depth=2
	cmpl	%eax, %r14d
	jge	.LBB2_7
# %bb.6:                                #   in Loop: Header=BB2_2 Depth=2
	movl	$.Lstr.5, %edi
	jmp	.LBB2_4
.LBB2_7:                                #   in Loop: Header=BB2_2 Depth=2
	cmpl	$513, %ebx                      # imm = 0x201
	jl	.LBB2_9
# %bb.8:                                #   in Loop: Header=BB2_2 Depth=2
	movl	$.Lstr.4, %edi
.LBB2_4:                                #   in Loop: Header=BB2_2 Depth=2
	callq	puts@PLT
.LBB2_10:                               #   in Loop: Header=BB2_2 Depth=2
	movl	4(%rsp), %r15d
	cmpl	%r15d, %ebp
	jl	.LBB2_2
# %bb.11:                               #   in Loop: Header=BB2_2 Depth=2
	cmpl	%r15d, %r14d
	jl	.LBB2_2
# %bb.12:                               #   in Loop: Header=BB2_2 Depth=2
	cmpl	$512, %ebx                      # imm = 0x200
	jg	.LBB2_2
	jmp	.LBB2_13
.LBB2_9:                                #   in Loop: Header=BB2_2 Depth=2
	movl	%r14d, %esi
	imull	%ebp, %esi
	imull	%eax, %eax
	subl	%eax, %esi
	movl	$.L.str.13, %edi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB2_10
.LBB2_13:                               #   in Loop: Header=BB2_1 Depth=1
	movl	20(%rsp), %r12d
	movl	16(%rsp), %eax
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	movl	12(%rsp), %r14d
	movl	%r15d, %eax
	imull	%r15d, %eax
	shll	$2, %eax
	movslq	%eax, %rbx
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %rbp
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r13
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, 96(%rsp)                  # 8-byte Spill
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, 88(%rsp)                  # 8-byte Spill
	testl	%r15d, %r15d
	jle	.LBB2_18
# %bb.14:                               # %.preheader.preheader
                                        #   in Loop: Header=BB2_1 Depth=1
	xorl	%eax, %eax
	xorl	%ecx, %ecx
.LBB2_15:                               # %.preheader
                                        #   Parent Loop BB2_1 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB2_16 Depth 3
	movl	%eax, %esi
	leaq	(,%rsi,4), %rdx
	addq	%r13, %rdx
	leaq	(,%rsi,4), %rsi
	addq	%rbp, %rsi
	xorl	%edi, %edi
.LBB2_16:                               #   Parent Loop BB2_1 Depth=1
                                        #     Parent Loop BB2_15 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movl	%ecx, (%rsi,%rdi,4)
	movl	%ecx, (%rdx,%rdi,4)
	incq	%rdi
	cmpq	%rdi, %r15
	jne	.LBB2_16
# %bb.17:                               # %._crit_edge
                                        #   in Loop: Header=BB2_15 Depth=2
	incq	%rcx
	addl	%r15d, %eax
	cmpq	%r15, %rcx
	jne	.LBB2_15
.LBB2_18:                               # %._crit_edge75
                                        #   in Loop: Header=BB2_1 Depth=1
	movq	%r12, %r15
	shlq	$32, %r15
	orq	%r12, %r15
	shlq	$32, %r14
	addq	80(%rsp), %r14                  # 8-byte Folded Reload
	leaq	72(%rsp), %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	leaq	64(%rsp), %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	leaq	56(%rsp), %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	movq	72(%rsp), %rdi
	movq	%rbp, %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	64(%rsp), %rdi
	movq	%r13, %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	56(%rsp), %rdi
	movq	96(%rsp), %r12                  # 8-byte Reload
	movq	%r12, %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	40(%rsp), %rdi
	callq	hipEventCreate
	leaq	24(%rsp), %rdi
	callq	hipEventCreate
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	%r15, %rdi
	movl	$1, %esi
	movq	%r14, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_20
# %bb.19:                               #   in Loop: Header=BB2_1 Depth=1
	movq	72(%rsp), %rdi
	movq	64(%rsp), %rsi
	movq	56(%rsp), %rdx
	movl	4(%rsp), %ecx
	callq	_Z28__device_stub__gpu_matrixaddPiS_S_i
.LBB2_20:                               #   in Loop: Header=BB2_1 Depth=1
	movq	56(%rsp), %rsi
	movq	%r12, %rdi
	movq	%rbx, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	24(%rsp), %rdi
	callq	hipEventSynchronize
	movq	40(%rsp), %rsi
	movq	24(%rsp), %rdx
	leaq	52(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	cvtss2sd	52(%rsp), %xmm0
	movl	$.L.str.14, %edi
	movb	$1, %al
	callq	printf
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	4(%rsp), %eax
	testl	%eax, %eax
	movq	%r12, %r14
	leaq	20(%rsp), %r12
	movq	88(%rsp), %r15                  # 8-byte Reload
	jle	.LBB2_25
# %bb.21:                               # %.preheader.lr.ph.i
                                        #   in Loop: Header=BB2_1 Depth=1
	leaq	(,%rax,4), %rcx
	movq	%rbp, %rdx
	movq	%r13, %rsi
	movq	%r15, %rdi
	xorl	%r8d, %r8d
.LBB2_22:                               # %.lr.ph.preheader.i
                                        #   Parent Loop BB2_1 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB2_23 Depth 3
	xorl	%r9d, %r9d
	movq	%rax, %r10
.LBB2_23:                               # %.lr.ph.i
                                        #   Parent Loop BB2_1 Depth=1
                                        #     Parent Loop BB2_22 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movl	(%rsi,%r9), %r11d
	addl	(%rdx,%r9), %r11d
	movl	%r11d, (%rdi,%r9)
	addq	%rcx, %r9
	decq	%r10
	jne	.LBB2_23
# %bb.24:                               # %._crit_edge.i
                                        #   in Loop: Header=BB2_22 Depth=2
	incq	%r8
	addq	$4, %rdi
	addq	$4, %rsi
	addq	$4, %rdx
	cmpq	%rax, %r8
	jne	.LBB2_22
.LBB2_25:                               # %_Z13cpu_matrixaddPiS_S_i.exit
                                        #   in Loop: Header=BB2_1 Depth=1
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	24(%rsp), %rdi
	callq	hipEventSynchronize
	movq	40(%rsp), %rsi
	movq	24(%rsp), %rdx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	xorps	%xmm0, %xmm0
	cvtss2sd	52(%rsp), %xmm0
	movl	$.L.str.15, %edi
	movb	$1, %al
	callq	printf
	cmpl	$0, 4(%rsp)
	je	.LBB2_28
# %bb.26:                               #   in Loop: Header=BB2_1 Depth=1
	movl	(%r14), %eax
	cmpl	(%r15), %eax
	je	.LBB2_28
# %bb.27:                               #   in Loop: Header=BB2_1 Depth=1
	movl	$.Lstr.7, %edi
	callq	puts@PLT
.LBB2_28:                               #   in Loop: Header=BB2_1 Depth=1
	movl	$.Lstr.8, %edi
	callq	puts@PLT
	movl	$.L.str.18, %edi
	leaq	11(%rsp), %rbx
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	$.L.str.18, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	cmpb	$99, 11(%rsp)
	je	.LBB2_1
# %bb.29:
	movq	%rbp, %rdi
	callq	free
	movq	%r13, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	72(%rsp), %rdi
	callq	hipFree
	movq	64(%rsp), %rdi
	callq	hipFree
	movq	56(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipEventDestroy
	movq	24(%rsp), %rdi
	callq	hipEventDestroy
	xorl	%eax, %eax
	addq	$104, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	main, .Lfunc_end2-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	.LBB3_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB3_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z13gpu_matrixaddPiS_S_i, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end3:
	.size	__hip_module_ctor, .Lfunc_end3-__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	.LBB4_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
.LBB4_2:
	retq
.Lfunc_end4:
	.size	__hip_module_dtor, .Lfunc_end4-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z13gpu_matrixaddPiS_S_i,@object # @_Z13gpu_matrixaddPiS_S_i
	.section	.rodata,"a",@progbits
	.globl	_Z13gpu_matrixaddPiS_S_i
	.p2align	3, 0x0
_Z13gpu_matrixaddPiS_S_i:
	.quad	_Z28__device_stub__gpu_matrixaddPiS_S_i
	.size	_Z13gpu_matrixaddPiS_S_i, 8

	.type	.L.str.4,@object                # @.str.4
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.4:
	.asciz	"Enter size of array in one dimension (square array), currently %d\n"
	.size	.L.str.4, 67

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"%d"
	.size	.L.str.5, 3

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"\nEnter nuumber of blocks per grid in x dimension), currently %d  : "
	.size	.L.str.6, 68

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"\nEnter nuumber of blocks per grid in y dimension), currently %d  : "
	.size	.L.str.7, 68

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"\nEnter nuumber of threads per block in x dimension), currently %d  : "
	.size	.L.str.8, 70

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"\nEnter nuumber of threads per block in y dimension), currently %d  : "
	.size	.L.str.9, 70

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Number of threads not used = %d\n"
	.size	.L.str.13, 33

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"Time to calculate results on GPU: %f ms.\n"
	.size	.L.str.14, 42

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"Time to calculate results on CPU: %f ms.\n"
	.size	.L.str.15, 42

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"%c"
	.size	.L.str.18, 3

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z13gpu_matrixaddPiS_S_i"
	.size	.L__unnamed_1, 25

	.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	"Device characteristics -- some limitations (compute capability 1.0)"
	.size	.Lstr, 68

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"\t\tMaximum number of threads per block = 512"
	.size	.Lstr.1, 44

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"\t\tMaximum sizes of x- and y- dimension of thread block = 512"
	.size	.Lstr.2, 61

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"\t\tMaximum size of each dimension of grid of thread blocks = 65535"
	.size	.Lstr.3, 66

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"Error -- too many threads in block, try again"
	.size	.Lstr.4, 46

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"Error -- number of threads in y dimension less than number of elements in arrays, try again"
	.size	.Lstr.5, 92

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"Error -- number of threads in x dimension less than number of elements in arrays, try again"
	.size	.Lstr.6, 92

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"*********** ERROR in results, CPU and GPU create different answers ********"
	.size	.Lstr.7, 76

	.type	.Lstr.8,@object                 # @str.8
.Lstr.8:
	.asciz	"\nEnter c to repeat, return to terminate"
	.size	.Lstr.8, 40

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z28__device_stub__gpu_matrixaddPiS_S_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z13gpu_matrixaddPiS_S_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z13gpu_matrixaddPiS_S_i ; -- Begin function _Z13gpu_matrixaddPiS_S_i
	.globl	_Z13gpu_matrixaddPiS_S_i
	.p2align	8
	.type	_Z13gpu_matrixaddPiS_S_i,@function
_Z13gpu_matrixaddPiS_S_i:               ; @_Z13gpu_matrixaddPiS_S_i
; %bb.0:
	s_clause 0x1
	s_load_b32 s3, s[0:1], 0x2c
	s_load_b32 s2, s[0:1], 0x18
	v_and_b32_e32 v2, 0x3ff, v0
	v_bfe_u32 v3, v0, 10, 10
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s4, s3, 16
	s_and_b32 s3, s3, 0xffff
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[0:1], null, s14, s3, v[2:3]
	v_mad_u64_u32 v[1:2], null, s15, s4, v[3:4]
	s_mov_b32 s3, exec_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_max_i32_e32 v2, v0, v1
	v_cmpx_gt_i32_e64 s2, v2
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b128 s[4:7], s[0:1], 0x0
	v_mad_u64_u32 v[2:3], null, v1, s2, v[0:1]
	s_load_b64 s[0:1], s[0:1], 0x10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v3, 31, v2
	v_lshlrev_b64 v[0:1], 2, v[2:3]
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v2, vcc_lo, s4, v0
	v_add_co_ci_u32_e32 v3, vcc_lo, s5, v1, vcc_lo
	v_add_co_u32 v4, vcc_lo, s6, v0
	v_add_co_ci_u32_e32 v5, vcc_lo, s7, v1, vcc_lo
	v_add_co_u32 v0, vcc_lo, s0, v0
	global_load_b32 v2, v[2:3], off
	global_load_b32 v3, v[4:5], off
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v2, v3, v2
	global_store_b32 v[0:1], v2, off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z13gpu_matrixaddPiS_S_i
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.amdhsa_user_sgpr_count 14
		.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 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.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	_Z13gpu_matrixaddPiS_S_i, .Lfunc_end0-_Z13gpu_matrixaddPiS_S_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 224
; NumSgprs: 18
; NumVgprs: 6
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 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: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; 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
      - .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
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z13gpu_matrixaddPiS_S_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z13gpu_matrixaddPiS_S_i.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
