/* ==================================================================
	Programmer: Arunbalaji Prithiviraj (U#80066848) arunbalaji@mail.usf.edu
	The basic SDH algorithm implementation for 3D data
	To compile: nvcc proj2-arunbalaji.cu -o output in the rc machines
   ==================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>

#define BOX_SIZE	23000 /* size of the data box on one dimension            */

/* descriptors for single atom in the tree */
float *h_x;
float *h_y;
float *h_z;

typedef struct hist_entry{
	unsigned int  d_cnt;   /* need a unsigned int data type as the count might be huge */
} bucket;

bucket * histogram;		/* list of all buckets in the histogram   */
unsigned int	PDH_acnt;	/* total number of data points            */
int num_buckets;		/* total number of buckets in the histogram */
float   PDH_res;		/* value of w                             */

/*
	CUDA Kernel. It computes Spatial distance histogram for 3D points. 
	I used concept of shared memory to reduce memory latency in atomic operation. 
	Finally shared memory is loaded to global memory for final result. 
*/
__global__ void SDH (float* x, float*y, float*z ,bucket* hist, const int PDH_acnt, float PDH_res,int num_buckets)
{
	int tx = threadIdx.x;
	int id = blockIdx.x*blockDim.x+tx;
	int j,k;
	int h_pos;
	int half = PDH_acnt/2;
	int odd = PDH_acnt%2;
	extern __shared__ bucket shared_h[];	
	shared_h[tx].d_cnt = 0;
	__syncthreads();
	if(id >= PDH_acnt) return;
	for(k = 1; k <= half; k++)
	{
          	  j=(id+k)%(PDH_acnt);
		h_pos=(int) sqrt((x[id] - x[j])*(x[id]-x[j]) + (y[id] - y[j])*(y[id] - y[j]) + (z[id] - z[j])*(z[id] - z[j]))/PDH_res;
		if(!odd&&k==half&&id>=half) continue;
		atomicAdd(&shared_h[h_pos].d_cnt,1);
	}
	__syncthreads();
	for(k = 0; k<num_buckets&&tx== 0;k++)
		atomicAdd(&hist[k].d_cnt, shared_h[k].d_cnt);
}

/* 
	print the counts in all buckets of the histogram 
*/
void output_histogram(bucket* output){
	int i; 
	unsigned int total_cnt = 0;
	for(i=0; i< num_buckets; i++) {
		if(i%5 == 0) /* we print 5 buckets in a row */
			printf("\n%02d: ", i);
		printf("%15u ", output[i].d_cnt);
		total_cnt += output[i].d_cnt;
	  	/* we also want to make sure the total distance count is correct */
		if(i == num_buckets - 1)	
			printf("\n T:%u \n", total_cnt);
		else printf("| ");
	}
}

int main(int argc, char **argv)
{
	if(argc != 4)
	{
		printf("Missing inputs, Try again!\n");
		exit(0);
	}

	int i;
	bucket* h_histogram;

	/*Device variable declaration*/
	float *d_x, *d_y,*d_z;
	bucket* d_histogram;
	cudaEvent_t start, stop;

	/*command line arguments */
	PDH_acnt = atoi(argv[1]);
	PDH_res	 = atof(argv[2]);
	int threads = atoi(argv[3]);
	
	/*Configuration parameters for the device*/
	dim3 dimGrid(ceil(PDH_acnt/threads)+1,1,1);
	dim3 dimBlock(threads,1,1);

	num_buckets = (int)(BOX_SIZE * 1.732 / PDH_res) + 1;

	/*CPU memory allocation */
	h_histogram = (bucket *)malloc(sizeof(bucket)*num_buckets);
	h_x = (float*)malloc(sizeof(float)*PDH_acnt);
	h_y = (float*)malloc(sizeof(float)*PDH_acnt);
	h_z = (float*)malloc(sizeof(float)*PDH_acnt);

	/*Device memory allocation */
	cudaMalloc(&d_x,sizeof(float)*PDH_acnt);
	cudaMalloc(&d_y,sizeof(float)*PDH_acnt);
	cudaMalloc(&d_z,sizeof(float)*PDH_acnt);
	cudaMalloc(&d_histogram,sizeof(bucket)*num_buckets);
	cudaMemset(d_histogram,0,sizeof(bucket)*num_buckets);
	
	srand(1);
	/* generate data following a uniform distribution */
	for(i = 0;  i < PDH_acnt; i++) {
		h_x[i] = ((float)(rand()) / RAND_MAX) * BOX_SIZE;
		h_y[i]= ((float)(rand()) / RAND_MAX) * BOX_SIZE;
		h_z[i] = ((float)(rand()) / RAND_MAX) * BOX_SIZE;
	}

	/* Start counting time */
	cudaEventCreate(&start);
	cudaEventCreate(&stop);
	cudaEventRecord(start,0);
	
	/* Copy host array to device*/
	cudaMemcpy(d_x,h_x,sizeof(float)*PDH_acnt,cudaMemcpyHostToDevice);
	cudaMemcpy(d_y,h_y,sizeof(float)*PDH_acnt,cudaMemcpyHostToDevice);
	cudaMemcpy(d_z,h_z,sizeof(float)*PDH_acnt,cudaMemcpyHostToDevice);

	/* Excute the kernel */
	SDH<<<dimGrid,dimBlock,sizeof(bucket)*threads>>>(d_x,d_y,d_z,d_histogram,PDH_acnt,PDH_res,num_buckets);

	/*Copy array back to host */
	cudaMemcpy(h_histogram,d_histogram,sizeof(bucket)*num_buckets,cudaMemcpyDeviceToHost);

	/* check the total running time */
	cudaEventRecord(stop,0);
	cudaEventSynchronize(stop);
	float elaspsedTime;
	cudaEventElapsedTime(&elaspsedTime,start,stop);
	cudaEventDestroy(start);
	cudaEventDestroy(stop);

	/* print out the histogram */
	output_histogram(h_histogram);
	printf("\n******** Total Running Time of Kernel = %0.5f ms *******\n",elaspsedTime);

	/*Release Device Memory*/
	cudaFree(d_x);
	cudaFree(d_y);
	cudaFree(d_z);
	cudaFree(d_histogram);

	/*Release CPU  Memory*/
	free(h_x);
	free(h_y);
	free(h_z);
	free(h_histogram);
	
	return 0;
}

	.file	"tmpxft_002379cb_00000000-6_proj2-arunbalaji.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
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"\n%02d: "
.LC1:
	.string	"%15u "
.LC2:
	.string	"\n T:%u \n"
.LC3:
	.string	"| "
	.text
	.globl	_Z16output_histogramP10hist_entry
	.type	_Z16output_histogramP10hist_entry, @function
_Z16output_histogramP10hist_entry:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	leaq	.LC0(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	xorl	%r13d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	xorl	%ebx, %ebx
.L3:
	cmpl	%ebx, num_buckets(%rip)
	movl	%ebx, %ebp
	jle	.L10
	movl	%ebp, %eax
	movl	$5, %ecx
	cltd
	idivl	%ecx
	testl	%edx, %edx
	jne	.L4
	movl	%ebp, %edx
	movq	%r14, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L4:
	movl	(%r12,%rbx,4), %edx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	num_buckets(%rip), %eax
	addl	(%r12,%rbx,4), %r13d
	decl	%eax
	cmpl	%ebp, %eax
	jne	.L5
	movl	%r13d, %edx
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L6
.L5:
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L6:
	incq	%rbx
	jmp	.L3
.L10:
	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
.LFE2027:
	.size	_Z16output_histogramP10hist_entry, .-_Z16output_histogramP10hist_entry
	.globl	_Z42__device_stub__Z3SDHPfS_S_P10hist_entryifiPfS_S_P10hist_entryifi
	.type	_Z42__device_stub__Z3SDHPfS_S_P10hist_entryifiPfS_S_P10hist_entryifi, @function
_Z42__device_stub__Z3SDHPfS_S_P10hist_entryifiPfS_S_P10hist_entryifi:
.LFB2053:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movl	%r8d, 12(%rsp)
	movl	%r9d, 4(%rsp)
	movss	%xmm0, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L11
	pushq	56(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z3SDHPfS_S_P10hist_entryifi(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 208
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L11:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2053:
	.size	_Z42__device_stub__Z3SDHPfS_S_P10hist_entryifiPfS_S_P10hist_entryifi, .-_Z42__device_stub__Z3SDHPfS_S_P10hist_entryifiPfS_S_P10hist_entryifi
	.globl	_Z3SDHPfS_S_P10hist_entryifi
	.type	_Z3SDHPfS_S_P10hist_entryifi, @function
_Z3SDHPfS_S_P10hist_entryifi:
.LFB2054:
	.cfi_startproc
	endbr64
	jmp	_Z42__device_stub__Z3SDHPfS_S_P10hist_entryifiPfS_S_P10hist_entryifi
	.cfi_endproc
.LFE2054:
	.size	_Z3SDHPfS_S_P10hist_entryifi, .-_Z3SDHPfS_S_P10hist_entryifi
	.section	.rodata.str1.1
.LC4:
	.string	"Missing inputs, Try again!\n"
.LC9:
	.string	"\n******** Total Running Time of Kernel = %0.5f ms *******\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	subq	$112, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	cmpl	$4, %edi
	je	.L17
	movl	$2, %edi
	leaq	.LC4(%rip), %rsi
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
.L17:
	movq	8(%rsi), %rdi
	movq	%rsi, %rbp
	call	atoi@PLT
	movq	16(%rbp), %rdi
	movl	%eax, %ebx
	movl	%ebx, PDH_acnt(%rip)
	call	atof@PLT
	movq	24(%rbp), %rdi
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, PDH_res(%rip)
	movss	%xmm0, 12(%rsp)
	call	atoi@PLT
	xorl	%edx, %edx
	movss	12(%rsp), %xmm0
	movslq	%eax, %rbp
	movl	%ebx, %eax
	salq	$2, %rbx
	divl	%ebp
	cvtss2sd	%xmm0, %xmm0
	movl	%ebp, 92(%rsp)
	cvtsi2sdq	%rax, %xmm1
	addsd	.LC5(%rip), %xmm1
	cvttsd2siq	%xmm1, %rax
	movsd	.LC6(%rip), %xmm1
	divsd	%xmm0, %xmm1
	movl	%eax, 80(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 84(%rsp)
	movq	%rax, 96(%rsp)
	cvttsd2sil	%xmm1, %edi
	incl	%edi
	movl	%edi, num_buckets(%rip)
	movslq	%edi, %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %r12
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, h_x(%rip)
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, h_y(%rip)
	call	malloc@PLT
	movq	%rbx, %rsi
	leaq	32(%rsp), %rdi
	xorl	%ebx, %ebx
	movq	%rax, h_z(%rip)
	call	cudaMalloc@PLT
	movl	PDH_acnt(%rip), %esi
	leaq	40(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movl	PDH_acnt(%rip), %esi
	leaq	48(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movslq	num_buckets(%rip), %rsi
	leaq	56(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movslq	num_buckets(%rip), %rdx
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	salq	$2, %rdx
	call	cudaMemset@PLT
	movl	$1, %edi
	call	srand@PLT
.L20:
	cmpl	PDH_acnt(%rip), %ebx
	jnb	.L25
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC7(%rip), %xmm0
	movq	h_x(%rip), %rax
	mulss	.LC8(%rip), %xmm0
	movss	%xmm0, (%rax,%rbx,4)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC7(%rip), %xmm0
	movq	h_y(%rip), %rax
	mulss	.LC8(%rip), %xmm0
	movss	%xmm0, (%rax,%rbx,4)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC7(%rip), %xmm0
	movq	h_z(%rip), %rax
	mulss	.LC8(%rip), %xmm0
	movss	%xmm0, (%rax,%rbx,4)
	incq	%rbx
	jmp	.L20
.L25:
	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	PDH_acnt(%rip), %edx
	movq	32(%rsp), %rdi
	movl	$1, %ecx
	movq	h_x(%rip), %rsi
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movl	PDH_acnt(%rip), %edx
	movq	40(%rsp), %rdi
	movl	$1, %ecx
	movq	h_y(%rip), %rsi
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movl	PDH_acnt(%rip), %edx
	movq	48(%rsp), %rdi
	movl	$1, %ecx
	movq	h_z(%rip), %rsi
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movl	100(%rsp), %ecx
	movl	88(%rsp), %esi
	xorl	%r9d, %r9d
	movq	92(%rsp), %rdx
	movq	80(%rsp), %rdi
	leaq	0(,%rbp,4), %r8
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L22
	movl	num_buckets(%rip), %r9d
	movss	PDH_res(%rip), %xmm0
	movl	PDH_acnt(%rip), %r8d
	movq	56(%rsp), %rcx
	movq	48(%rsp), %rdx
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdi
	call	_Z42__device_stub__Z3SDHPfS_S_P10hist_entryifiPfS_S_P10hist_entryifi
.L22:
	movslq	num_buckets(%rip), %rdx
	movq	56(%rsp), %rsi
	movl	$2, %ecx
	movq	%r12, %rdi
	salq	$2, %rdx
	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
	leaq	28(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	64(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	72(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	%r12, %rdi
	call	_Z16output_histogramP10hist_entry
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	28(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	h_x(%rip), %rdi
	call	free@PLT
	movq	h_y(%rip), %rdi
	call	free@PLT
	movq	h_z(%rip), %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L23
	call	__stack_chk_fail@PLT
.L23:
	addq	$112, %rsp
	.cfi_def_cfa_offset 32
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	main, .-main
	.section	.rodata.str1.1
.LC10:
	.string	"_Z3SDHPfS_S_P10hist_entryifi"
	.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	.LC10(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z3SDHPfS_S_P10hist_entryifi(%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
	.globl	PDH_res
	.bss
	.align 4
	.type	PDH_res, @object
	.size	PDH_res, 4
PDH_res:
	.zero	4
	.globl	num_buckets
	.align 4
	.type	num_buckets, @object
	.size	num_buckets, 4
num_buckets:
	.zero	4
	.globl	PDH_acnt
	.align 4
	.type	PDH_acnt, @object
	.size	PDH_acnt, 4
PDH_acnt:
	.zero	4
	.globl	histogram
	.align 8
	.type	histogram, @object
	.size	histogram, 8
histogram:
	.zero	8
	.globl	h_z
	.align 8
	.type	h_z, @object
	.size	h_z, 8
h_z:
	.zero	8
	.globl	h_y
	.align 8
	.type	h_y, @object
	.size	h_y, 8
h_y:
	.zero	8
	.globl	h_x
	.align 8
	.type	h_x, @object
	.size	h_x, 8
h_x:
	.zero	8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC5:
	.long	0
	.long	1072693248
	.align 8
.LC6:
	.long	0
	.long	1088648064
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC7:
	.long	805306368
	.align 4
.LC8:
	.long	1186181120
	.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:

/* ==================================================================
	Programmer: Arunbalaji Prithiviraj (U#80066848) arunbalaji@mail.usf.edu
	The basic SDH algorithm implementation for 3D data
	To compile: nvcc proj2-arunbalaji.cu -o output in the rc machines
   ==================================================================
*/


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

#define BOX_SIZE	23000 /* size of the data box on one dimension            */

/* descriptors for single atom in the tree */
float *h_x;
float *h_y;
float *h_z;

typedef struct hist_entry{
	unsigned int  d_cnt;   /* need a unsigned int data type as the count might be huge */
} bucket;

bucket * histogram;		/* list of all buckets in the histogram   */
unsigned int	PDH_acnt;	/* total number of data points            */
int num_buckets;		/* total number of buckets in the histogram */
float   PDH_res;		/* value of w                             */

/*
	CUDA Kernel. It computes Spatial distance histogram for 3D points. 
	I used concept of shared memory to reduce memory latency in atomic operation. 
	Finally shared memory is loaded to global memory for final result. 
*/
__global__ void SDH (float* x, float*y, float*z ,bucket* hist, const int PDH_acnt, float PDH_res,int num_buckets)
{
	int tx = threadIdx.x;
	int id = blockIdx.x*blockDim.x+tx;
	int j,k;
	int h_pos;
	int half = PDH_acnt/2;
	int odd = PDH_acnt%2;
	extern __shared__ bucket shared_h[];	
	shared_h[tx].d_cnt = 0;
	__syncthreads();
	if(id >= PDH_acnt) return;
	for(k = 1; k <= half; k++)
	{
          	  j=(id+k)%(PDH_acnt);
		h_pos=(int) sqrt((x[id] - x[j])*(x[id]-x[j]) + (y[id] - y[j])*(y[id] - y[j]) + (z[id] - z[j])*(z[id] - z[j]))/PDH_res;
		if(!odd&&k==half&&id>=half) continue;
		atomicAdd(&shared_h[h_pos].d_cnt,1);
	}
	__syncthreads();
	for(k = 0; k<num_buckets&&tx== 0;k++)
		atomicAdd(&hist[k].d_cnt, shared_h[k].d_cnt);
}

/* 
	print the counts in all buckets of the histogram 
*/
void output_histogram(bucket* output){
	int i; 
	unsigned int total_cnt = 0;
	for(i=0; i< num_buckets; i++) {
		if(i%5 == 0) /* we print 5 buckets in a row */
			printf("\n%02d: ", i);
		printf("%15u ", output[i].d_cnt);
		total_cnt += output[i].d_cnt;
	  	/* we also want to make sure the total distance count is correct */
		if(i == num_buckets - 1)	
			printf("\n T:%u \n", total_cnt);
		else printf("| ");
	}
}

int main(int argc, char **argv)
{
	if(argc != 4)
	{
		printf("Missing inputs, Try again!\n");
		exit(0);
	}

	int i;
	bucket* h_histogram;

	/*Device variable declaration*/
	float *d_x, *d_y,*d_z;
	bucket* d_histogram;
	hipEvent_t start, stop;

	/*command line arguments */
	PDH_acnt = atoi(argv[1]);
	PDH_res	 = atof(argv[2]);
	int threads = atoi(argv[3]);
	
	/*Configuration parameters for the device*/
	dim3 dimGrid(ceil(PDH_acnt/threads)+1,1,1);
	dim3 dimBlock(threads,1,1);

	num_buckets = (int)(BOX_SIZE * 1.732 / PDH_res) + 1;

	/*CPU memory allocation */
	h_histogram = (bucket *)malloc(sizeof(bucket)*num_buckets);
	h_x = (float*)malloc(sizeof(float)*PDH_acnt);
	h_y = (float*)malloc(sizeof(float)*PDH_acnt);
	h_z = (float*)malloc(sizeof(float)*PDH_acnt);

	/*Device memory allocation */
	hipMalloc(&d_x,sizeof(float)*PDH_acnt);
	hipMalloc(&d_y,sizeof(float)*PDH_acnt);
	hipMalloc(&d_z,sizeof(float)*PDH_acnt);
	hipMalloc(&d_histogram,sizeof(bucket)*num_buckets);
	hipMemset(d_histogram,0,sizeof(bucket)*num_buckets);
	
	srand(1);
	/* generate data following a uniform distribution */
	for(i = 0;  i < PDH_acnt; i++) {
		h_x[i] = ((float)(rand()) / RAND_MAX) * BOX_SIZE;
		h_y[i]= ((float)(rand()) / RAND_MAX) * BOX_SIZE;
		h_z[i] = ((float)(rand()) / RAND_MAX) * BOX_SIZE;
	}

	/* Start counting time */
	hipEventCreate(&start);
	hipEventCreate(&stop);
	hipEventRecord(start,0);
	
	/* Copy host array to device*/
	hipMemcpy(d_x,h_x,sizeof(float)*PDH_acnt,hipMemcpyHostToDevice);
	hipMemcpy(d_y,h_y,sizeof(float)*PDH_acnt,hipMemcpyHostToDevice);
	hipMemcpy(d_z,h_z,sizeof(float)*PDH_acnt,hipMemcpyHostToDevice);

	/* Excute the kernel */
	SDH<<<dimGrid,dimBlock,sizeof(bucket)*threads>>>(d_x,d_y,d_z,d_histogram,PDH_acnt,PDH_res,num_buckets);

	/*Copy array back to host */
	hipMemcpy(h_histogram,d_histogram,sizeof(bucket)*num_buckets,hipMemcpyDeviceToHost);

	/* check the total running time */
	hipEventRecord(stop,0);
	hipEventSynchronize(stop);
	float elaspsedTime;
	hipEventElapsedTime(&elaspsedTime,start,stop);
	hipEventDestroy(start);
	hipEventDestroy(stop);

	/* print out the histogram */
	output_histogram(h_histogram);
	printf("\n******** Total Running Time of Kernel = %0.5f ms *******\n",elaspsedTime);

	/*Release Device Memory*/
	hipFree(d_x);
	hipFree(d_y);
	hipFree(d_z);
	hipFree(d_histogram);

	/*Release CPU  Memory*/
	free(h_x);
	free(h_y);
	free(h_z);
	free(h_histogram);
	
	return 0;
}

	.text
	.file	"proj2-arunbalaji.hip"
	.globl	_Z18__device_stub__SDHPfS_S_P10hist_entryifi # -- Begin function _Z18__device_stub__SDHPfS_S_P10hist_entryifi
	.type	_Z18__device_stub__SDHPfS_S_P10hist_entryifi,@function
_Z18__device_stub__SDHPfS_S_P10hist_entryifi: # @_Z18__device_stub__SDHPfS_S_P10hist_entryifi
	.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	$160, %rsp
	.cfi_def_cfa_offset 208
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	12(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	8(%rsp), %r8
	movss	%xmm0, (%r8)
	leaq	4(%rsp), %r10
	movl	%r9d, (%r10)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	movq	%r10, 48(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z3SDHPfS_S_P10hist_entryifi, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$176, %rsp
	.cfi_adjust_cfa_offset -176
	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	_Z18__device_stub__SDHPfS_S_P10hist_entryifi, .Lfunc_end0-_Z18__device_stub__SDHPfS_S_P10hist_entryifi
	.cfi_endproc
                                        # -- End function
	.globl	_Z16output_histogramP10hist_entry # -- Begin function _Z16output_histogramP10hist_entry
	.type	_Z16output_histogramP10hist_entry,@function
_Z16output_histogramP10hist_entry:      # @_Z16output_histogramP10hist_entry
	.cfi_startproc
# %bb.0:
	cmpl	$0, num_buckets(%rip)
	jle	.LBB1_9
# %bb.1:                                # %.lr.ph.preheader
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdi, %rbx
	movl	$3435973837, %r15d              # imm = 0xCCCCCCCD
	xorl	%r14d, %r14d
	xorl	%ebp, %ebp
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%r14d, %eax
	imulq	%r15, %rax
	shrq	$34, %rax
	leal	(%rax,%rax,4), %eax
	cmpl	%eax, %r14d
	jne	.LBB1_4
# %bb.3:                                #   in Loop: Header=BB1_2 Depth=1
	movl	$.L.str, %edi
	movl	%r14d, %esi
	xorl	%eax, %eax
	callq	printf
.LBB1_4:                                #   in Loop: Header=BB1_2 Depth=1
	movl	(%rbx,%r14,4), %esi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	addl	(%rbx,%r14,4), %ebp
	movl	num_buckets(%rip), %eax
	decl	%eax
	cmpq	%rax, %r14
	jne	.LBB1_6
# %bb.5:                                #   in Loop: Header=BB1_2 Depth=1
	movl	$.L.str.2, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB1_7
.LBB1_6:                                #   in Loop: Header=BB1_2 Depth=1
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
.LBB1_7:                                #   in Loop: Header=BB1_2 Depth=1
	incq	%r14
	movslq	num_buckets(%rip), %rax
	cmpq	%rax, %r14
	jl	.LBB1_2
# %bb.8:
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
	.cfi_restore %rbp
.LBB1_9:                                # %._crit_edge
	retq
.Lfunc_end1:
	.size	_Z16output_histogramP10hist_entry, .Lfunc_end1-_Z16output_histogramP10hist_entry
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI2_0:
	.quad	0x40e3738000000000              # double 39836
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0
.LCPI2_1:
	.long	0x30000000                      # float 4.65661287E-10
.LCPI2_2:
	.long	0x46b3b000                      # float 23000
	.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	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$64, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$4, %edi
	jne	.LBB2_7
# %bb.1:
	movq	%rsi, %rbx
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %r12d
	movl	%eax, PDH_acnt(%rip)
	movq	16(%rbx), %rdi
	callq	atof
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 44(%rsp)                 # 4-byte Spill
	movss	%xmm0, PDH_res(%rip)
	movq	24(%rbx), %rdi
	callq	atoi
	movl	%eax, %ebp
	movl	%r12d, %eax
	xorl	%edx, %edx
	divl	%ebp
	movl	%eax, %r14d
	incl	%r14d
	btsq	$32, %r14
	movl	%ebp, %r15d
	btsq	$32, %r15
	xorps	%xmm0, %xmm0
	cvtss2sd	44(%rsp), %xmm0                 # 4-byte Folded Reload
	movsd	.LCPI2_0(%rip), %xmm1           # xmm1 = mem[0],zero
	divsd	%xmm0, %xmm1
	cvttsd2si	%xmm1, %eax
	leal	1(%rax), %ecx
	movl	%ecx, num_buckets(%rip)
	cltq
	leaq	4(,%rax,4), %rdi
	callq	malloc
	movq	%rax, %rbx
	movl	%r12d, %r12d
	shlq	$2, %r12
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, h_x(%rip)
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, h_y(%rip)
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, h_z(%rip)
	leaq	32(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	movl	PDH_acnt(%rip), %esi
	shlq	$2, %rsi
	leaq	24(%rsp), %rdi
	callq	hipMalloc
	movl	PDH_acnt(%rip), %esi
	shlq	$2, %rsi
	leaq	16(%rsp), %rdi
	callq	hipMalloc
	movslq	num_buckets(%rip), %rsi
	shlq	$2, %rsi
	leaq	8(%rsp), %r12
	movq	%r12, %rdi
	callq	hipMalloc
	movq	(%r12), %rdi
	movslq	num_buckets(%rip), %rdx
	shlq	$2, %rdx
	xorl	%esi, %esi
	callq	hipMemset
	movl	$1, %edi
	callq	srand
	cmpl	$0, PDH_acnt(%rip)
	je	.LBB2_4
# %bb.2:                                # %.lr.ph.preheader
	xorl	%r12d, %r12d
.LBB2_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	.LCPI2_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	mulss	%xmm1, %xmm0
	movss	.LCPI2_2(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	mulss	%xmm1, %xmm0
	movq	h_x(%rip), %rax
	movss	%xmm0, (%rax,%r12,4)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI2_1(%rip), %xmm0
	mulss	.LCPI2_2(%rip), %xmm0
	movq	h_y(%rip), %rax
	movss	%xmm0, (%rax,%r12,4)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI2_1(%rip), %xmm0
	mulss	.LCPI2_2(%rip), %xmm0
	movq	h_z(%rip), %rax
	movss	%xmm0, (%rax,%r12,4)
	incq	%r12
	movl	PDH_acnt(%rip), %eax
	cmpq	%rax, %r12
	jb	.LBB2_3
.LBB2_4:                                # %._crit_edge
	leaq	48(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventCreate
	movq	%rsp, %rdi
	callq	hipEventCreate
	movq	(%r12), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	32(%rsp), %rdi
	movq	h_x(%rip), %rsi
	movl	PDH_acnt(%rip), %edx
	shlq	$2, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	24(%rsp), %rdi
	movq	h_y(%rip), %rsi
	movl	PDH_acnt(%rip), %edx
	shlq	$2, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	16(%rsp), %rdi
	movq	h_z(%rip), %rsi
	movl	PDH_acnt(%rip), %edx
	shlq	$2, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movslq	%ebp, %r8
	shlq	$2, %r8
	movq	%r14, %rdi
	movl	$1, %esi
	movq	%r15, %rdx
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_6
# %bb.5:
	movq	32(%rsp), %rdi
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rcx
	movl	PDH_acnt(%rip), %r8d
	movss	PDH_res(%rip), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movl	num_buckets(%rip), %r9d
	callq	_Z18__device_stub__SDHPfS_S_P10hist_entryifi
.LBB2_6:
	movq	8(%rsp), %rsi
	movslq	num_buckets(%rip), %rdx
	shlq	$2, %rdx
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rsp), %rdi
	callq	hipEventSynchronize
	movq	48(%rsp), %rsi
	movq	(%rsp), %rdx
	leaq	60(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventElapsedTime
	movq	48(%rsp), %rdi
	callq	hipEventDestroy
	movq	(%rsp), %rdi
	callq	hipEventDestroy
	movq	%rbx, %rdi
	callq	_Z16output_histogramP10hist_entry
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r14), %xmm0
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	h_x(%rip), %rdi
	callq	free
	movq	h_y(%rip), %rdi
	callq	free
	movq	h_z(%rip), %rdi
	callq	free
	movq	%rbx, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$64, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB2_7:
	.cfi_def_cfa_offset 112
	movl	$.Lstr, %edi
	callq	puts@PLT
	xorl	%edi, %edi
	callq	exit
.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	$_Z3SDHPfS_S_P10hist_entryifi, %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	h_x,@object                     # @h_x
	.bss
	.globl	h_x
	.p2align	3, 0x0
h_x:
	.quad	0
	.size	h_x, 8

	.type	h_y,@object                     # @h_y
	.globl	h_y
	.p2align	3, 0x0
h_y:
	.quad	0
	.size	h_y, 8

	.type	h_z,@object                     # @h_z
	.globl	h_z
	.p2align	3, 0x0
h_z:
	.quad	0
	.size	h_z, 8

	.type	histogram,@object               # @histogram
	.globl	histogram
	.p2align	3, 0x0
histogram:
	.quad	0
	.size	histogram, 8

	.type	PDH_acnt,@object                # @PDH_acnt
	.globl	PDH_acnt
	.p2align	2, 0x0
PDH_acnt:
	.long	0                               # 0x0
	.size	PDH_acnt, 4

	.type	num_buckets,@object             # @num_buckets
	.globl	num_buckets
	.p2align	2, 0x0
num_buckets:
	.long	0                               # 0x0
	.size	num_buckets, 4

	.type	PDH_res,@object                 # @PDH_res
	.globl	PDH_res
	.p2align	2, 0x0
PDH_res:
	.long	0x00000000                      # float 0
	.size	PDH_res, 4

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

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"\n%02d: "
	.size	.L.str, 8

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"%15u "
	.size	.L.str.1, 6

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"\n T:%u \n"
	.size	.L.str.2, 9

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"| "
	.size	.L.str.3, 3

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"\n******** Total Running Time of Kernel = %0.5f ms *******\n"
	.size	.L.str.5, 59

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z3SDHPfS_S_P10hist_entryifi"
	.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	"Missing inputs, Try again!"
	.size	.Lstr, 27

	.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 _Z18__device_stub__SDHPfS_S_P10hist_entryifi
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z3SDHPfS_S_P10hist_entryifi
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z3SDHPfS_S_P10hist_entryifi ; -- Begin function _Z3SDHPfS_S_P10hist_entryifi
	.globl	_Z3SDHPfS_S_P10hist_entryifi
	.p2align	8
	.type	_Z3SDHPfS_S_P10hist_entryifi,@function
_Z3SDHPfS_S_P10hist_entryifi:           ; @_Z3SDHPfS_S_P10hist_entryifi
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x3c
	s_load_b128 s[16:19], s[0:1], 0x20
	v_mov_b32_e32 v3, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1)
	v_mad_u64_u32 v[1:2], null, s15, s2, v[0:1]
	v_lshl_add_u32 v2, v0, 2, 0
	s_mov_b32 s2, exec_lo
	ds_store_b32 v2, v3
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_cmpx_gt_i32_e64 s16, v1
	s_cbranch_execz .LBB0_11
; %bb.1:                                ; %.preheader
	s_load_b256 s[4:11], s[0:1], 0x0
	s_cmp_lt_i32 s16, 2
	s_cbranch_scc1 .LBB0_6
; %bb.2:                                ; %.lr.ph
	s_lshr_b32 s0, s16, 31
	v_add_nc_u32_e32 v7, 1, v1
	s_add_i32 s0, s16, s0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_3) | instid1(SALU_CYCLE_1)
	s_ashr_i32 s2, s0, 1
	s_bitcmp1_b32 s16, 0
	s_cselect_b32 s3, -1, 0
	s_ashr_i32 s0, s16, 31
	s_add_i32 s1, s16, s0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_xor_b32 s12, s1, s0
	v_cvt_f32_u32_e32 v2, s12
	s_sub_i32 s0, 0, s12
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_2)
	v_rcp_iflag_f32_e32 v2, v2
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v3, 0x4f7ffffe, v2
	v_ashrrev_i32_e32 v2, 31, v1
	v_cvt_u32_f32_e32 v8, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[5:6], 2, v[1:2]
	v_mul_lo_u32 v3, s0, v8
	v_cmp_gt_i32_e64 s0, s2, v1
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_u32 v1, vcc_lo, s4, v5
	v_add_co_ci_u32_e32 v2, vcc_lo, s5, v6, vcc_lo
	s_delay_alu instid0(VALU_DEP_4)
	v_mul_hi_u32 v9, v8, v3
	v_add_co_u32 v3, vcc_lo, s6, v5
	v_add_co_ci_u32_e32 v4, vcc_lo, s7, v6, vcc_lo
	v_add_co_u32 v5, vcc_lo, s8, v5
	v_add_co_ci_u32_e32 v6, vcc_lo, s9, v6, vcc_lo
	v_dual_mov_b32 v9, 1 :: v_dual_add_nc_u32 v8, v8, v9
.LBB0_3:                                ; =>This Inner Loop Header: Depth=1
	s_cmp_lg_u32 s2, 1
	s_cselect_b32 s1, -1, 0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 s1, s3, s1
	s_or_b32 s1, s0, s1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_saveexec_b32 s13, s1
	s_cbranch_execz .LBB0_5
; %bb.4:                                ;   in Loop: Header=BB0_3 Depth=1
	v_ashrrev_i32_e32 v10, 31, v7
	global_load_b32 v16, v[1:2], off
	global_load_b32 v17, v[3:4], off
	v_add_nc_u32_e32 v11, v7, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_xor_b32_e32 v11, v11, v10
	v_mul_hi_u32 v12, v11, v8
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v12, v12, s12
	v_sub_nc_u32_e32 v11, v11, v12
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_subrev_nc_u32_e32 v12, s12, v11
	v_cmp_le_u32_e32 vcc_lo, s12, v11
	v_cndmask_b32_e32 v11, v11, v12, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_subrev_nc_u32_e32 v12, s12, v11
	v_cmp_le_u32_e32 vcc_lo, s12, v11
	v_cndmask_b32_e32 v11, v11, v12, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_xor_b32_e32 v11, v11, v10
	v_sub_nc_u32_e32 v10, v11, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v11, 31, v10
	v_lshlrev_b64 v[10:11], 2, v[10:11]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v12, vcc_lo, s6, v10
	v_add_co_ci_u32_e32 v13, vcc_lo, s7, v11, vcc_lo
	v_add_co_u32 v14, vcc_lo, s4, v10
	v_add_co_ci_u32_e32 v15, vcc_lo, s5, v11, vcc_lo
	global_load_b32 v12, v[12:13], off
	v_add_co_u32 v10, vcc_lo, s8, v10
	v_add_co_ci_u32_e32 v11, vcc_lo, s9, v11, vcc_lo
	global_load_b32 v13, v[14:15], off
	s_clause 0x1
	global_load_b32 v14, v[5:6], off
	global_load_b32 v10, v[10:11], off
	s_waitcnt vmcnt(2)
	v_dual_sub_f32 v11, v17, v12 :: v_dual_sub_f32 v12, v16, v13
	s_waitcnt vmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_mul_f32 v11, v11, v11 :: v_dual_sub_f32 v10, v14, v10
	v_fmac_f32_e32 v11, v12, v12
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v11, v10, v10
	v_cmp_gt_f32_e32 vcc_lo, 0xf800000, v11
	v_mul_f32_e32 v10, 0x4f800000, v11
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e32 v10, v11, v10, vcc_lo
	v_sqrt_f32_e32 v11, v10
	s_waitcnt_depctr 0xfff
	v_add_nc_u32_e32 v13, 1, v11
	v_add_nc_u32_e32 v12, -1, v11
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v15, -v13, v11, v10
	v_fma_f32 v14, -v12, v11, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ge_f32_e64 s1, 0, v14
	v_cndmask_b32_e64 v11, v11, v12, s1
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_lt_f32_e64 s1, 0, v15
	v_cndmask_b32_e64 v11, v11, v13, s1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v12, 0x37800000, v11
	v_cndmask_b32_e32 v11, v11, v12, vcc_lo
	v_cmp_class_f32_e64 vcc_lo, v10, 0x260
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e32 v10, v11, v10, vcc_lo
	v_cvt_i32_f32_e32 v10, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_f32_i32_e32 v10, v10
	v_div_scale_f32 v11, null, s17, s17, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v12, v11
	s_waitcnt_depctr 0xfff
	v_fma_f32 v13, -v11, v12, 1.0
	v_fmac_f32_e32 v12, v13, v12
	v_div_scale_f32 v13, vcc_lo, v10, s17, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v14, v13, v12
	v_fma_f32 v15, -v11, v14, v13
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v14, v15, v12
	v_fma_f32 v11, -v11, v14, v13
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fmas_f32 v11, v11, v12, v14
	v_div_fixup_f32 v10, v11, s17, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_i32_f32_e32 v10, v10
	v_lshl_add_u32 v10, v10, 2, 0
	ds_add_u32 v10, v9
.LBB0_5:                                ;   in Loop: Header=BB0_3 Depth=1
	s_or_b32 exec_lo, exec_lo, s13
	v_add_nc_u32_e32 v7, 1, v7
	s_add_i32 s2, s2, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lg_u32 s2, 0
	s_cbranch_scc1 .LBB0_3
.LBB0_6:                                ; %._crit_edge
	v_cmp_eq_u32_e32 vcc_lo, 0, v0
	s_cmp_gt_i32 s18, 0
	s_mov_b32 s0, 0
	s_cselect_b32 s1, -1, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s1, vcc_lo, s1
	s_barrier
	buffer_gl0_inv
	s_and_b32 exec_lo, exec_lo, s1
	s_cbranch_execz .LBB0_11
; %bb.7:                                ; %.lr.ph66.preheader
	v_mov_b32_e32 v0, 0
	s_mov_b32 s1, 0
.LBB0_8:                                ; %.lr.ph66
                                        ; =>This Inner Loop Header: Depth=1
	s_mov_b32 s3, exec_lo
	s_mov_b32 s2, exec_lo
	v_mbcnt_lo_u32_b32 v1, s3, 0
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_eq_u32_e32 0, v1
	s_cbranch_execz .LBB0_10
; %bb.9:                                ;   in Loop: Header=BB0_8 Depth=1
	v_mov_b32_e32 v1, s1
	s_bcnt1_i32_b32 s3, s3
	ds_load_b32 v1, v1
	s_waitcnt lgkmcnt(0)
	v_mul_lo_u32 v1, v1, s3
	global_atomic_add_u32 v0, v1, s[10:11]
.LBB0_10:                               ;   in Loop: Header=BB0_8 Depth=1
	s_or_b32 exec_lo, exec_lo, s2
	s_add_i32 s0, s0, 1
	s_add_u32 s10, s10, 4
	s_addc_u32 s11, s11, 0
	s_add_i32 s1, s1, 4
	s_cmp_lt_i32 s0, s18
	s_cbranch_scc1 .LBB0_8
.LBB0_11:                               ; %.loopexit
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z3SDHPfS_S_P10hist_entryifi
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 304
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 18
		.amdhsa_next_free_sgpr 20
		.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	_Z3SDHPfS_S_P10hist_entryifi, .Lfunc_end0-_Z3SDHPfS_S_P10hist_entryifi
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 992
; NumSgprs: 22
; NumVgprs: 18
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 22
; NumVGPRsForWavesPerEU: 18
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym shared_h
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .offset:         32
        .size:           4
        .value_kind:     by_value
      - .offset:         36
        .size:           4
        .value_kind:     by_value
      - .offset:         40
        .size:           4
        .value_kind:     by_value
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         52
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         60
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         62
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         64
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         66
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         68
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         70
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         112
        .size:           2
        .value_kind:     hidden_grid_dims
      - .offset:         168
        .size:           4
        .value_kind:     hidden_dynamic_lds_size
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 304
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z3SDHPfS_S_P10hist_entryifi
    .private_segment_fixed_size: 0
    .sgpr_count:     22
    .sgpr_spill_count: 0
    .symbol:         _Z3SDHPfS_S_P10hist_entryifi.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     18
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
