/* ==================================================================
	Programmer: Yicheng Tu (ytu@cse.usf.edu)
	The basic SDH algorithm implementation for 3D data
	To compile: nvcc SDH.c -o SDH in the C4 lab machines
   ==================================================================
*/

/* USF Fall 2019 CIS4930 Programming on Massively Parallel Systems
   Project Description: Write a CUDA program to implement the same
   functionality as the CPU only code

   Student: Alexander Cook
*/

#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 */
typedef struct atomdesc {
	double x_pos;
	double y_pos;
	double z_pos;
} atom;

typedef struct hist_entry{
	//float min;
	//float max;
	unsigned long long d_cnt;   /* need a long long type as the count might be huge */
} bucket;

//Global variables
bucket * h_histogram;	/* list of all buckets in the histogram     */
long long	PDH_acnt;	/* total number of data points              */
int num_buckets;		/* total number of buckets in the histogram */
double   PDH_res;		/* value of w                               */
atom * atom_list;		/* list of all data points					*/

//Device helper function: Distance of two points in the atom_list
//Device helper function which now takes a pointer as an argument instead of using a global pointer
__device__ double d_p2p_distance(atom *atom_list, int ind1, int ind2) {
	
	double x1 = atom_list[ind1].x_pos;
	double x2 = atom_list[ind2].x_pos;
	double y1 = atom_list[ind1].y_pos;
	double y2 = atom_list[ind2].y_pos;
	double z1 = atom_list[ind1].z_pos;
	double z2 = atom_list[ind2].z_pos;
		
	return sqrt((x1 - x2)*(x1-x2) + (y1 - y2)*(y1 - y2) + (z1 - z2)*(z1 - z2));
}


__global__ void PDH_kernel(atom *d_atom_list, bucket *d_histogram, int PDH_acnt, int PDH_res, int num_buckets){
	int T = blockIdx.x * blockDim.x + threadIdx.x;
	int t = threadIdx.x;

	extern __shared__ int s_histogram[];

	for(int i = t; i < num_buckets; i += blockDim.x){ //initialize array to 0 in block sized chunks
		s_histogram[i] = 0;
	}

	__syncthreads();
 
	int h_pos;
	double dist;

	for(int i = T + 1; i < PDH_acnt; i++){
		dist = d_p2p_distance(d_atom_list, T, i);
			h_pos = (int) (dist / PDH_res);
			atomicAdd(&(s_histogram[h_pos]), 1);
			 
	}

	__syncthreads();

	//reduce shared output into global output
	for(int i = t; i < num_buckets; i += blockDim.x){ //output to global memory in block sized chunks
		atomicAdd(&(d_histogram[i].d_cnt), s_histogram[i]);
	}

}


/* 
	Print the counts in all buckets of the histogram 
*/
void output_histogram(bucket *histogram){
	int i; 
	long long 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("%15lld ", histogram[i].d_cnt);
		total_cnt += histogram[i].d_cnt;
	  	/* we also want to make sure the total distance count is correct */
		if(i == num_buckets - 1)	
			printf("\n T:%lld \n", total_cnt);
		else printf("| ");
	}
}


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

	PDH_acnt = atoi(argv[1]);
	PDH_res	 = atof(argv[2]);
    int num_threads = atoi(argv[3]);
    
	//Allocate host memory
	num_buckets = (int)(BOX_SIZE * 1.732 / PDH_res) + 1;
	h_histogram = (bucket *)malloc(sizeof(bucket) * num_buckets);
	atom_list = (atom *)malloc(sizeof(atom)*PDH_acnt);

	//initialize histogram to zero
	memset(h_histogram, 0, sizeof(bucket)*num_buckets);

	//Allocate device memory
	bucket *d_histogram; //pointer to array of buckets
	atom *d_atom_list; //pointer to array of atoms

	cudaMalloc((void**)&d_histogram, sizeof(bucket)*num_buckets);
	cudaMalloc((void**)&d_atom_list, sizeof(atom)*PDH_acnt);
	
	srand(1);
	/* Generate data following a uniform distribution */
	for(i = 0;  i < PDH_acnt; i++) {
		atom_list[i].x_pos = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
		atom_list[i].y_pos = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
		atom_list[i].z_pos = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
	}

	//Copy host data to device memory
	cudaMemcpy(d_histogram, h_histogram, sizeof(bucket)*num_buckets, cudaMemcpyHostToDevice);
	cudaMemcpy(d_atom_list, atom_list, sizeof(atom)*PDH_acnt, cudaMemcpyHostToDevice);

	//Define block and grid size
	//int num_threads = 256; //number of threads in one dimension of a block
	int num_blocks = (PDH_acnt + num_threads - 1)/num_threads; //calculate number of blocks needed
	
	//Start counting time
	cudaEvent_t start, stop;
	cudaEventCreate(&start);
	cudaEventCreate(&stop);
	cudaEventRecord(start, 0);

	//Launch kernel
	PDH_kernel<<<num_blocks,num_threads, sizeof(int)*num_buckets>>>(d_atom_list, d_histogram, PDH_acnt, PDH_res, num_buckets);
	//PDH_kernelST<<<1,1>>>(d_atom_list, d_histogram, PDH_acnt, PDH_res);

	//stop counting time
	cudaEventRecord(stop, 0);
	cudaEventSynchronize(stop);
	float elapsedTime;
	cudaEventElapsedTime(&elapsedTime, start, stop);
	

	//Copy data from gpu memory to host memory
	cudaMemcpy(h_histogram, d_histogram, sizeof(bucket)*num_buckets, cudaMemcpyDeviceToHost);
	
	/* Print out the histogram again for gpu version */
	output_histogram(h_histogram);

	//report running time
	printf("******** Total Running Time of Kernel = %0.5f ms *******\n", elapsedTime);
	cudaEventDestroy(start);
	cudaEventDestroy(stop);

	free(h_histogram);
	cudaFree(d_histogram);
	
	

	return 0;
}



	.file	"tmpxft_00306264_00000000-6_SDH.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z14d_p2p_distanceP8atomdescii
	.type	_Z14d_p2p_distanceP8atomdescii, @function
_Z14d_p2p_distanceP8atomdescii:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z14d_p2p_distanceP8atomdescii, .-_Z14d_p2p_distanceP8atomdescii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"\n%02d: "
.LC1:
	.string	"%15lld "
.LC2:
	.string	"\n T:%lld \n"
.LC3:
	.string	"| "
	.text
	.globl	_Z16output_histogramP10hist_entry
	.type	_Z16output_histogramP10hist_entry, @function
_Z16output_histogramP10hist_entry:
.LFB2028:
	.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
.L5:
	cmpl	%ebx, num_buckets(%rip)
	movl	%ebx, %ebp
	jle	.L12
	movl	%ebp, %eax
	movl	$5, %ecx
	cltd
	idivl	%ecx
	testl	%edx, %edx
	jne	.L6
	movl	%ebp, %edx
	movq	%r14, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L6:
	movq	(%r12,%rbx,8), %rdx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	num_buckets(%rip), %eax
	addq	(%r12,%rbx,8), %r13
	decl	%eax
	cmpl	%ebp, %eax
	jne	.L7
	movq	%r13, %rdx
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L8
.L7:
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L8:
	incq	%rbx
	jmp	.L5
.L12:
	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
.LFE2028:
	.size	_Z16output_histogramP10hist_entry, .-_Z16output_histogramP10hist_entry
	.globl	_Z54__device_stub__Z10PDH_kernelP8atomdescP10hist_entryiiiP8atomdescP10hist_entryiii
	.type	_Z54__device_stub__Z10PDH_kernelP8atomdescP10hist_entryiiiP8atomdescP10hist_entryiii, @function
_Z54__device_stub__Z10PDH_kernelP8atomdescP10hist_entryiiiP8atomdescP10hist_entryiii:
.LFB2054:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	40(%rsp), %rcx
	movl	%r8d, 4(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	12(%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, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L13
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z10PDH_kernelP8atomdescP10hist_entryiii(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 176
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L13:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2054:
	.size	_Z54__device_stub__Z10PDH_kernelP8atomdescP10hist_entryiiiP8atomdescP10hist_entryiii, .-_Z54__device_stub__Z10PDH_kernelP8atomdescP10hist_entryiiiP8atomdescP10hist_entryiii
	.globl	_Z10PDH_kernelP8atomdescP10hist_entryiii
	.type	_Z10PDH_kernelP8atomdescP10hist_entryiii, @function
_Z10PDH_kernelP8atomdescP10hist_entryiii:
.LFB2055:
	.cfi_startproc
	endbr64
	jmp	_Z54__device_stub__Z10PDH_kernelP8atomdescP10hist_entryiiiP8atomdescP10hist_entryiii
	.cfi_endproc
.LFE2055:
	.size	_Z10PDH_kernelP8atomdescP10hist_entryiii, .-_Z10PDH_kernelP8atomdescP10hist_entryiii
	.section	.rodata.str1.1
.LC7:
	.string	"******** Total Running Time of Kernel = %0.5f ms *******\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rsi, %rbx
	subq	$88, %rsp
	.cfi_def_cfa_offset 128
	movq	8(%rsi), %rdi
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	call	atoi@PLT
	movq	16(%rbx), %rdi
	movslq	%eax, %rbp
	movq	%rbp, PDH_acnt(%rip)
	call	atof@PLT
	movq	24(%rbx), %rdi
	movsd	%xmm0, PDH_res(%rip)
	movsd	%xmm0, 8(%rsp)
	call	atoi@PLT
	movsd	8(%rsp), %xmm0
	movsd	.LC4(%rip), %xmm1
	movl	%eax, %r12d
	divsd	%xmm0, %xmm1
	cvttsd2sil	%xmm1, %ebx
	incl	%ebx
	movl	%ebx, num_buckets(%rip)
	movslq	%ebx, %rbx
	salq	$3, %rbx
	movq	%rbx, %rdi
	call	malloc@PLT
	imulq	$24, %rbp, %rdi
	movq	%rax, %r13
	movq	%rax, h_histogram(%rip)
	call	malloc@PLT
	movq	%rbx, %rcx
	movq	%rbx, %rdx
	xorl	%esi, %esi
	movq	%r13, %rdi
	movq	%rax, atom_list(%rip)
	call	__memset_chk@PLT
	movq	%rbx, %rsi
	leaq	16(%rsp), %rdi
	xorl	%ebx, %ebx
	call	cudaMalloc@PLT
	imulq	$24, PDH_acnt(%rip), %rsi
	leaq	24(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	$1, %edi
	call	srand@PLT
.L19:
	cmpq	%rbx, PDH_acnt(%rip)
	jle	.L24
	call	rand@PLT
	imulq	$24, %rbx, %rbp
	incq	%rbx
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC5(%rip), %xmm0
	mulsd	.LC6(%rip), %xmm0
	movq	atom_list(%rip), %rax
	movsd	%xmm0, (%rax,%rbp)
	call	rand@PLT
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC5(%rip), %xmm0
	mulsd	.LC6(%rip), %xmm0
	movq	atom_list(%rip), %rax
	movsd	%xmm0, 8(%rax,%rbp)
	call	rand@PLT
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC5(%rip), %xmm0
	mulsd	.LC6(%rip), %xmm0
	movq	atom_list(%rip), %rax
	movsd	%xmm0, 16(%rax,%rbp)
	jmp	.L19
.L24:
	movslq	num_buckets(%rip), %rdx
	movq	16(%rsp), %rdi
	movl	$1, %ecx
	movq	h_histogram(%rip), %rsi
	salq	$3, %rdx
	call	cudaMemcpy@PLT
	movq	24(%rsp), %rdi
	movl	$1, %ecx
	imulq	$24, PDH_acnt(%rip), %rdx
	movq	atom_list(%rip), %rsi
	call	cudaMemcpy@PLT
	movq	PDH_acnt(%rip), %rax
	movslq	%r12d, %rcx
	leaq	32(%rsp), %rdi
	addq	%rcx, %rax
	decq	%rax
	cqto
	idivq	%rcx
	movq	%rax, %rbx
	call	cudaEventCreate@PLT
	leaq	40(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movslq	num_buckets(%rip), %rdx
	movl	%r12d, 60(%rsp)
	xorl	%r9d, %r9d
	movabsq	$4294967297, %rax
	movl	%ebx, 48(%rsp)
	movq	%rax, 64(%rsp)
	leaq	0(,%rdx,4), %r8
	movl	68(%rsp), %ecx
	movq	%rax, 52(%rsp)
	movq	60(%rsp), %rdx
	movq	48(%rsp), %rdi
	movl	56(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L21
	cvttsd2sil	PDH_res(%rip), %ecx
	movl	num_buckets(%rip), %r8d
	movl	PDH_acnt(%rip), %edx
	movq	16(%rsp), %rsi
	movq	24(%rsp), %rdi
	call	_Z54__device_stub__Z10PDH_kernelP8atomdescP10hist_entryiiiP8atomdescP10hist_entryiii
.L21:
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	40(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	leaq	60(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	16(%rsp), %rsi
	movl	$2, %ecx
	movslq	num_buckets(%rip), %rdx
	movq	h_histogram(%rip), %rdi
	salq	$3, %rdx
	call	cudaMemcpy@PLT
	movq	h_histogram(%rip), %rdi
	call	_Z16output_histogramP10hist_entry
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	60(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	32(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	40(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	h_histogram(%rip), %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L22
	call	__stack_chk_fail@PLT
.L22:
	addq	$88, %rsp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	main, .-main
	.section	.rodata.str1.1
.LC8:
	.string	"_Z10PDH_kernelP8atomdescP10hist_entryiii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.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	.LC8(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z10PDH_kernelP8atomdescP10hist_entryiii(%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
.LFE2057:
	.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	atom_list
	.bss
	.align 8
	.type	atom_list, @object
	.size	atom_list, 8
atom_list:
	.zero	8
	.globl	PDH_res
	.align 8
	.type	PDH_res, @object
	.size	PDH_res, 8
PDH_res:
	.zero	8
	.globl	num_buckets
	.align 4
	.type	num_buckets, @object
	.size	num_buckets, 4
num_buckets:
	.zero	4
	.globl	PDH_acnt
	.align 8
	.type	PDH_acnt, @object
	.size	PDH_acnt, 8
PDH_acnt:
	.zero	8
	.globl	h_histogram
	.align 8
	.type	h_histogram, @object
	.size	h_histogram, 8
h_histogram:
	.zero	8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC4:
	.long	0
	.long	1088648064
	.align 8
.LC5:
	.long	-4194304
	.long	1105199103
	.align 8
.LC6:
	.long	0
	.long	1087796736
	.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: Yicheng Tu (ytu@cse.usf.edu)
	The basic SDH algorithm implementation for 3D data
	To compile: nvcc SDH.c -o SDH in the C4 lab machines
   ==================================================================
*/

/* USF Fall 2019 CIS4930 Programming on Massively Parallel Systems
   Project Description: Write a CUDA program to implement the same
   functionality as the CPU only code

   Student: Alexander Cook
*/


#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 */
typedef struct atomdesc {
	double x_pos;
	double y_pos;
	double z_pos;
} atom;

typedef struct hist_entry{
	//float min;
	//float max;
	unsigned long long d_cnt;   /* need a long long type as the count might be huge */
} bucket;

//Global variables
bucket * h_histogram;	/* list of all buckets in the histogram     */
long long	PDH_acnt;	/* total number of data points              */
int num_buckets;		/* total number of buckets in the histogram */
double   PDH_res;		/* value of w                               */
atom * atom_list;		/* list of all data points					*/

//Device helper function: Distance of two points in the atom_list
//Device helper function which now takes a pointer as an argument instead of using a global pointer
__device__ double d_p2p_distance(atom *atom_list, int ind1, int ind2) {
	
	double x1 = atom_list[ind1].x_pos;
	double x2 = atom_list[ind2].x_pos;
	double y1 = atom_list[ind1].y_pos;
	double y2 = atom_list[ind2].y_pos;
	double z1 = atom_list[ind1].z_pos;
	double z2 = atom_list[ind2].z_pos;
		
	return sqrt((x1 - x2)*(x1-x2) + (y1 - y2)*(y1 - y2) + (z1 - z2)*(z1 - z2));
}


__global__ void PDH_kernel(atom *d_atom_list, bucket *d_histogram, int PDH_acnt, int PDH_res, int num_buckets){
	int T = blockIdx.x * blockDim.x + threadIdx.x;
	int t = threadIdx.x;

	extern __shared__ int s_histogram[];

	for(int i = t; i < num_buckets; i += blockDim.x){ //initialize array to 0 in block sized chunks
		s_histogram[i] = 0;
	}

	__syncthreads();
 
	int h_pos;
	double dist;

	for(int i = T + 1; i < PDH_acnt; i++){
		dist = d_p2p_distance(d_atom_list, T, i);
			h_pos = (int) (dist / PDH_res);
			atomicAdd(&(s_histogram[h_pos]), 1);
			 
	}

	__syncthreads();

	//reduce shared output into global output
	for(int i = t; i < num_buckets; i += blockDim.x){ //output to global memory in block sized chunks
		atomicAdd(&(d_histogram[i].d_cnt), s_histogram[i]);
	}

}


/* 
	Print the counts in all buckets of the histogram 
*/
void output_histogram(bucket *histogram){
	int i; 
	long long 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("%15lld ", histogram[i].d_cnt);
		total_cnt += histogram[i].d_cnt;
	  	/* we also want to make sure the total distance count is correct */
		if(i == num_buckets - 1)	
			printf("\n T:%lld \n", total_cnt);
		else printf("| ");
	}
}


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

	PDH_acnt = atoi(argv[1]);
	PDH_res	 = atof(argv[2]);
    int num_threads = atoi(argv[3]);
    
	//Allocate host memory
	num_buckets = (int)(BOX_SIZE * 1.732 / PDH_res) + 1;
	h_histogram = (bucket *)malloc(sizeof(bucket) * num_buckets);
	atom_list = (atom *)malloc(sizeof(atom)*PDH_acnt);

	//initialize histogram to zero
	memset(h_histogram, 0, sizeof(bucket)*num_buckets);

	//Allocate device memory
	bucket *d_histogram; //pointer to array of buckets
	atom *d_atom_list; //pointer to array of atoms

	hipMalloc((void**)&d_histogram, sizeof(bucket)*num_buckets);
	hipMalloc((void**)&d_atom_list, sizeof(atom)*PDH_acnt);
	
	srand(1);
	/* Generate data following a uniform distribution */
	for(i = 0;  i < PDH_acnt; i++) {
		atom_list[i].x_pos = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
		atom_list[i].y_pos = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
		atom_list[i].z_pos = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
	}

	//Copy host data to device memory
	hipMemcpy(d_histogram, h_histogram, sizeof(bucket)*num_buckets, hipMemcpyHostToDevice);
	hipMemcpy(d_atom_list, atom_list, sizeof(atom)*PDH_acnt, hipMemcpyHostToDevice);

	//Define block and grid size
	//int num_threads = 256; //number of threads in one dimension of a block
	int num_blocks = (PDH_acnt + num_threads - 1)/num_threads; //calculate number of blocks needed
	
	//Start counting time
	hipEvent_t start, stop;
	hipEventCreate(&start);
	hipEventCreate(&stop);
	hipEventRecord(start, 0);

	//Launch kernel
	PDH_kernel<<<num_blocks,num_threads, sizeof(int)*num_buckets>>>(d_atom_list, d_histogram, PDH_acnt, PDH_res, num_buckets);
	//PDH_kernelST<<<1,1>>>(d_atom_list, d_histogram, PDH_acnt, PDH_res);

	//stop counting time
	hipEventRecord(stop, 0);
	hipEventSynchronize(stop);
	float elapsedTime;
	hipEventElapsedTime(&elapsedTime, start, stop);
	

	//Copy data from gpu memory to host memory
	hipMemcpy(h_histogram, d_histogram, sizeof(bucket)*num_buckets, hipMemcpyDeviceToHost);
	
	/* Print out the histogram again for gpu version */
	output_histogram(h_histogram);

	//report running time
	printf("******** Total Running Time of Kernel = %0.5f ms *******\n", elapsedTime);
	hipEventDestroy(start);
	hipEventDestroy(stop);

	free(h_histogram);
	hipFree(d_histogram);
	
	

	return 0;
}



	.text
	.file	"SDH.hip"
	.globl	_Z25__device_stub__PDH_kernelP8atomdescP10hist_entryiii # -- Begin function _Z25__device_stub__PDH_kernelP8atomdescP10hist_entryiii
	.type	_Z25__device_stub__PDH_kernelP8atomdescP10hist_entryiii,@function
_Z25__device_stub__PDH_kernelP8atomdescP10hist_entryiii: # @_Z25__device_stub__PDH_kernelP8atomdescP10hist_entryiii
	.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	$128, %rsp
	.cfi_def_cfa_offset 176
	.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	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	4(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%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	$_Z10PDH_kernelP8atomdescP10hist_entryiii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	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	_Z25__device_stub__PDH_kernelP8atomdescP10hist_entryiii, .Lfunc_end0-_Z25__device_stub__PDH_kernelP8atomdescP10hist_entryiii
	.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	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movl	$3435973837, %r12d              # imm = 0xCCCCCCCD
	xorl	%r14d, %r14d
	xorl	%r15d, %r15d
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%r14d, %eax
	imulq	%r12, %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
	movq	(%rbx,%r14,8), %rsi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	addq	(%rbx,%r14,8), %r15
	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
	movq	%r15, %rsi
	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	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r14
	.cfi_restore %r15
.LBB1_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
.LCPI2_1:
	.quad	0x41dfffffffc00000              # double 2147483647
.LCPI2_2:
	.quad	0x40d6760000000000              # double 23000
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$48, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %rbx
	movq	8(%rsi), %rdi
	callq	atoi
	movslq	%eax, %r15
	movq	%r15, PDH_acnt(%rip)
	movq	16(%rbx), %rdi
	callq	atof
	movsd	%xmm0, 32(%rsp)                 # 8-byte Spill
	movsd	%xmm0, PDH_res(%rip)
	movq	24(%rbx), %rdi
	callq	atoi
	movl	%eax, %ebx
	movsd	.LCPI2_0(%rip), %xmm0           # xmm0 = mem[0],zero
	divsd	32(%rsp), %xmm0                 # 8-byte Folded Reload
	cvttsd2si	%xmm0, %eax
	leal	1(%rax), %ecx
	movl	%ecx, num_buckets(%rip)
	cltq
	leaq	8(,%rax,8), %r14
	movl	$1, %edi
	movq	%r14, %rsi
	callq	calloc@PLT
	movq	%rax, h_histogram(%rip)
	shlq	$3, %r15
	leaq	(%r15,%r15,2), %rdi
	callq	malloc
	movq	%rax, atom_list(%rip)
	leaq	8(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	PDH_acnt(%rip), %rax
	shlq	$3, %rax
	leaq	(%rax,%rax,2), %rsi
	leaq	24(%rsp), %rdi
	callq	hipMalloc
	movl	$1, %edi
	callq	srand
	cmpq	$0, PDH_acnt(%rip)
	jle	.LBB2_3
# %bb.1:                                # %.lr.ph.preheader
	xorl	%r14d, %r14d
	xorl	%r15d, %r15d
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	movsd	.LCPI2_1(%rip), %xmm1           # xmm1 = mem[0],zero
	divsd	%xmm1, %xmm0
	movsd	.LCPI2_2(%rip), %xmm1           # xmm1 = mem[0],zero
	mulsd	%xmm1, %xmm0
	movq	atom_list(%rip), %rax
	movsd	%xmm0, (%rax,%r14)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI2_1(%rip), %xmm0
	mulsd	.LCPI2_2(%rip), %xmm0
	movq	atom_list(%rip), %rax
	movsd	%xmm0, 8(%rax,%r14)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI2_1(%rip), %xmm0
	mulsd	.LCPI2_2(%rip), %xmm0
	movq	atom_list(%rip), %rax
	movsd	%xmm0, 16(%rax,%r14)
	incq	%r15
	addq	$24, %r14
	cmpq	%r15, PDH_acnt(%rip)
	jg	.LBB2_2
.LBB2_3:                                # %._crit_edge
	movq	8(%rsp), %rdi
	movq	h_histogram(%rip), %rsi
	movslq	num_buckets(%rip), %rdx
	shlq	$3, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	24(%rsp), %rdi
	movq	atom_list(%rip), %rsi
	movq	PDH_acnt(%rip), %rax
	shlq	$3, %rax
	leaq	(%rax,%rax,2), %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	PDH_acnt(%rip), %rax
	movslq	%ebx, %r15
	addq	%r15, %rax
	decq	%rax
	cqto
	idivq	%r15
	movq	%rax, %rbx
	leaq	16(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventCreate
	movq	%rsp, %rdi
	callq	hipEventCreate
	movq	(%r14), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movslq	num_buckets(%rip), %r8
	shlq	$2, %r8
	movl	%ebx, %edi
	btsq	$32, %rdi
	movl	%r15d, %edx
	btsq	$32, %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_5
# %bb.4:
	movq	24(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	PDH_acnt(%rip), %edx
	cvttsd2si	PDH_res(%rip), %ecx
	movl	num_buckets(%rip), %r8d
	callq	_Z25__device_stub__PDH_kernelP8atomdescP10hist_entryiii
.LBB2_5:
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rsp), %rdi
	callq	hipEventSynchronize
	movq	16(%rsp), %rsi
	movq	(%rsp), %rdx
	leaq	44(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	movq	h_histogram(%rip), %rdi
	movq	8(%rsp), %rsi
	movslq	num_buckets(%rip), %rdx
	shlq	$3, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	h_histogram(%rip), %rdi
	callq	_Z16output_histogramP10hist_entry
	cvtss2sd	(%rbx), %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	movq	16(%rsp), %rdi
	callq	hipEventDestroy
	movq	(%rsp), %rdi
	callq	hipEventDestroy
	movq	h_histogram(%rip), %rdi
	callq	free
	movq	8(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
	addq	$48, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.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	$_Z10PDH_kernelP8atomdescP10hist_entryiii, %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_histogram,@object             # @h_histogram
	.bss
	.globl	h_histogram
	.p2align	3, 0x0
h_histogram:
	.quad	0
	.size	h_histogram, 8

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

	.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	3, 0x0
PDH_res:
	.quad	0x0000000000000000              # double 0
	.size	PDH_res, 8

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

	.type	_Z10PDH_kernelP8atomdescP10hist_entryiii,@object # @_Z10PDH_kernelP8atomdescP10hist_entryiii
	.section	.rodata,"a",@progbits
	.globl	_Z10PDH_kernelP8atomdescP10hist_entryiii
	.p2align	3, 0x0
_Z10PDH_kernelP8atomdescP10hist_entryiii:
	.quad	_Z25__device_stub__PDH_kernelP8atomdescP10hist_entryiii
	.size	_Z10PDH_kernelP8atomdescP10hist_entryiii, 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	"%15lld "
	.size	.L.str.1, 8

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

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

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

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z10PDH_kernelP8atomdescP10hist_entryiii"
	.size	.L__unnamed_1, 41

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

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

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

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z10PDH_kernelP8atomdescP10hist_entryiii ; -- Begin function _Z10PDH_kernelP8atomdescP10hist_entryiii
	.globl	_Z10PDH_kernelP8atomdescP10hist_entryiii
	.p2align	8
	.type	_Z10PDH_kernelP8atomdescP10hist_entryiii,@function
_Z10PDH_kernelP8atomdescP10hist_entryiii: ; @_Z10PDH_kernelP8atomdescP10hist_entryiii
; %bb.0:
	s_clause 0x1
	s_load_b128 s[4:7], s[0:1], 0x10
	s_load_b32 s3, s[0:1], 0x2c
	v_lshl_add_u32 v11, v0, 2, 0
	s_waitcnt lgkmcnt(0)
	v_cmp_gt_i32_e64 s2, s6, v0
	s_and_b32 s3, s3, 0xffff
	s_delay_alu instid0(VALU_DEP_1)
	s_and_saveexec_b32 s7, s2
	s_cbranch_execz .LBB0_3
; %bb.1:                                ; %.lr.ph.preheader
	v_lshl_add_u32 v1, v0, 2, 0
	v_dual_mov_b32 v2, 0 :: v_dual_mov_b32 v3, v0
	s_mov_b32 s8, 0
	s_lshl_b32 s9, s3, 2
.LBB0_2:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_4) | instid1(SALU_CYCLE_1)
	v_add_nc_u32_e32 v3, s3, v3
	ds_store_b32 v1, v2
	v_add_nc_u32_e32 v1, s9, v1
	v_cmp_le_i32_e32 vcc_lo, s6, v3
	s_or_b32 s8, vcc_lo, s8
	s_and_not1_b32 exec_lo, exec_lo, s8
	s_cbranch_execnz .LBB0_2
.LBB0_3:                                ; %Flow63
	s_or_b32 exec_lo, exec_lo, s7
	s_load_b128 s[8:11], s[0:1], 0x0
	v_mad_u64_u32 v[1:2], null, s15, s3, v[0:1]
	s_mov_b32 s1, exec_lo
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_add_nc_u32_e32 v12, 1, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e64 s4, v12
	s_cbranch_execz .LBB0_6
; %bb.4:                                ; %.lr.ph30
	v_mad_i64_i32 v[2:3], null, v1, 24, s[8:9]
	v_cvt_f64_i32_e32 v[7:8], s5
	v_mad_i64_i32 v[9:10], null, v12, 24, s[8:9]
	v_mov_b32_e32 v13, 1
	s_mov_b32 s5, 0
	s_clause 0x1
	global_load_b64 v[5:6], v[2:3], off offset:16
	global_load_b128 v[1:4], v[2:3], off
	v_add_co_u32 v9, vcc_lo, v9, 8
	v_add_co_ci_u32_e32 v10, vcc_lo, 0, v10, vcc_lo
.LBB0_5:                                ; =>This Inner Loop Header: Depth=1
	s_clause 0x1
	global_load_b128 v[14:17], v[9:10], off offset:-8
	global_load_b64 v[18:19], v[9:10], off offset:8
	v_add_nc_u32_e32 v12, 1, v12
	v_add_co_u32 v9, s0, v9, 24
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v10, s0, 0, v10, s0
	s_waitcnt vmcnt(1)
	v_add_f64 v[16:17], v[3:4], -v[16:17]
	v_add_f64 v[14:15], v[1:2], -v[14:15]
	s_waitcnt vmcnt(0)
	v_add_f64 v[18:19], v[5:6], -v[18:19]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[16:17], v[16:17], v[16:17]
	v_fma_f64 v[14:15], v[14:15], v[14:15], v[16:17]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[14:15], v[18:19], v[18:19], v[14:15]
	v_cmp_gt_f64_e32 vcc_lo, 0x10000000, v[14:15]
	v_cndmask_b32_e64 v16, 0, 1, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b32_e32 v16, 8, v16
	v_ldexp_f64 v[14:15], v[14:15], v16
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_rsq_f64_e32 v[16:17], v[14:15]
	s_waitcnt_depctr 0xfff
	v_mul_f64 v[18:19], v[14:15], v[16:17]
	v_mul_f64 v[16:17], v[16:17], 0.5
	v_fma_f64 v[20:21], -v[16:17], v[18:19], 0.5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fma_f64 v[18:19], v[18:19], v[20:21], v[18:19]
	v_fma_f64 v[16:17], v[16:17], v[20:21], v[16:17]
	v_fma_f64 v[20:21], -v[18:19], v[18:19], v[14:15]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[18:19], v[20:21], v[16:17], v[18:19]
	v_fma_f64 v[20:21], -v[18:19], v[18:19], v[14:15]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_fma_f64 v[16:17], v[20:21], v[16:17], v[18:19]
	v_cndmask_b32_e64 v18, 0, 0xffffff80, vcc_lo
	v_cmp_class_f64_e64 vcc_lo, v[14:15], 0x260
	v_ldexp_f64 v[16:17], v[16:17], v18
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_cndmask_b32 v15, v17, v15 :: v_dual_cndmask_b32 v14, v16, v14
	v_div_scale_f64 v[16:17], null, v[7:8], v[7:8], v[14:15]
	v_div_scale_f64 v[22:23], vcc_lo, v[14:15], v[7:8], v[14:15]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f64_e32 v[18:19], v[16:17]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[20:21], -v[16:17], v[18:19], 1.0
	v_fma_f64 v[18:19], v[18:19], v[20:21], v[18:19]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[20:21], -v[16:17], v[18:19], 1.0
	v_fma_f64 v[18:19], v[18:19], v[20:21], v[18:19]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[20:21], v[22:23], v[18:19]
	v_fma_f64 v[16:17], -v[16:17], v[20:21], v[22:23]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_div_fmas_f64 v[16:17], v[16:17], v[18:19], v[20:21]
	v_cmp_le_i32_e32 vcc_lo, s4, v12
	s_or_b32 s5, vcc_lo, s5
	v_div_fixup_f64 v[14:15], v[16:17], v[7:8], v[14:15]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_i32_f64_e32 v14, v[14:15]
	v_lshl_add_u32 v14, v14, 2, 0
	ds_add_u32 v14, v13
	s_and_not1_b32 exec_lo, exec_lo, s5
	s_cbranch_execnz .LBB0_5
.LBB0_6:                                ; %Flow61
	s_or_b32 exec_lo, exec_lo, s1
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_and_saveexec_b32 s0, s2
	s_cbranch_execz .LBB0_9
; %bb.7:                                ; %.lr.ph34.preheader
	s_mov_b32 s0, 0
	s_lshl_b32 s1, s3, 2
.LBB0_8:                                ; %.lr.ph34
                                        ; =>This Inner Loop Header: Depth=1
	ds_load_b32 v2, v11
	v_ashrrev_i32_e32 v1, 31, v0
	v_add_nc_u32_e32 v11, s1, v11
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[3:4], 3, v[0:1]
	v_add_nc_u32_e32 v0, s3, v0
	v_add_co_u32 v5, vcc_lo, s10, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_ci_u32_e32 v6, vcc_lo, s11, v4, vcc_lo
	v_cmp_le_i32_e32 vcc_lo, s6, v0
	s_waitcnt lgkmcnt(0)
	v_ashrrev_i32_e32 v3, 31, v2
	s_or_b32 s0, vcc_lo, s0
	global_atomic_add_u64 v[5:6], v[2:3], off
	s_and_not1_b32 exec_lo, exec_lo, s0
	s_cbranch_execnz .LBB0_8
.LBB0_9:                                ; %Flow59
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z10PDH_kernelP8atomdescP10hist_entryiii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.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 24
		.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	_Z10PDH_kernelP8atomdescP10hist_entryiii, .Lfunc_end0-_Z10PDH_kernelP8atomdescP10hist_entryiii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 792
; NumSgprs: 18
; NumVgprs: 24
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 24
; 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 s_histogram
	.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
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
      - .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
      - .offset:         152
        .size:           4
        .value_kind:     hidden_dynamic_lds_size
    .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:           _Z10PDH_kernelP8atomdescP10hist_entryiii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z10PDH_kernelP8atomdescP10hist_entryiii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     24
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
