//
// CUDA code to compute minimum distance between n points
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>

#define MAX_POINTS 1048576
#define BLOCK_SIZE 1024
#define M_LOG2E 1.44269504088896340736 //log2(e)

inline long double my_log(const long double x){
    return  log(x) * M_LOG2E;
}

int blocks;     //log2(num_points);
int threads;    //num_points/blocks;
int num_points;

// ---------------------------------------------------------------------------- 
// Kernel Function to compute distance between all pairs of points
// Input: 
//  X: X[i] = x-coordinate of the ith point
//  Y: Y[i] = y-coordinate of the ith point
//  n: number of points
//
// Output: 
//  D: D[0] = minimum distance
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// Compute minimum distance on host to check device computation
// Need to:  modify the kernel functino to compute the minimum distance.
// You are allowed to make other changes to the code that facilitate
// parallelization on the GPU
//
// 1. You need to develop CUDA-based parallel code to compute the
// distance between the closest pair of points on a GPU
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// References
// ----------------------------------------------------------------------------
/* 
__global__ void VecAdd(float* A, float* B, float* C, int N) 
{ 
 int i = blockDim.x * blockIdx.x + threadIdx.x; 
 if (i < N) 
 C[i] = A[i] + B[i]; 
} 
*/

/*
__global__ void MatMult(float* A, float* B, float* C, int N) 
{ 
int i = blockIdx.x * blockDim.x + threadIdx.x; 
 int j = blockIdx.y * blockDim.y + threadIdx.y; 
 if (i < N && j < N) 
 C[i][j] = A[i][j] + B[i][j];
 }
 */
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// Kernel function code goes here
// ---------------------------------------------------------------------------- 
__global__ void minimum_distance(float * X, float * Y, float * D, int n) 
{
    //CUDA version of the code
    int i = blockIdx.y * blockDim.y + threadIdx.y;
    //int j = blockIdx.x * blockDim.x + threadIdx.x + 1;

    if(threadIdx.y == 0)
    {
        D[0] = 10;
    }

    extern __shared__ float mins[]; 
    __threadfence();

    //if (i < n && j < n) 
    if(i<n)
    {
        for (int j = i+1; j < n; j++) 
        {
            float dx = X[j]-X[i];
            float dy = Y[j]-Y[i];
            float Dij = sqrtf(dx*dx+dy*dy);
            
            //__threadfence();

            //mins local to the block
            mins[threadIdx.x] = Dij;

            __syncthreads();
        }
    }

    //let the last block handle finding the actual minimum
    if(i >= n)
    {
        //sort mins to 
        for(unsigned int s = blockDim.y/2; s > 0; s >>= 1) 
        {

            if(threadIdx.y < s)
            {
                if (mins[threadIdx.y] > mins[threadIdx.y + s]) 
                {
                    mins[threadIdx.y] = mins[threadIdx.y + s];
                }
            }
            __syncthreads();
        }

        //now update global minimum
        if(threadIdx.y == 0)
        {
            if(D[0] > mins[0])
            {
                D[0] = mins[0];
            }
        }
    }
    //D[0] = -15;
} 
// ---------------------------------------------------------------------------- 

// ---------------------------------------------------------------------------- 
// Main program - initializes points and computes minimum distance 
// between the points
// ---------------------------------------------------------------------------- 
int main(int argc, char* argv[]) {

    // Host Data
    float * hVx;            // host x-coordinate array
    float * hVy;            // host y-coordinate array
    float * hmin_dist;      // minimum value on host

    // Device Data
    float * dVx;            // device x-coordinate array
    float * dVy;            // device x-coordinate array
    float * dmin_dist;      // minimum value on device

    int i, j, size;// num_points, blocks, threads; 
    float dx, dy, Dij, distance;
    unsigned int seed = 0;

    cudaEvent_t start, stop;        // GPU timing variables

    struct timeval cpu_start, cpu_stop; // CPU timing variables

    float time_array[10]; 

    // Timing initializations
    cudaEventCreate(&start);
    cudaEventCreate(&stop);

    // Check input
    if (argc != 2) {
    printf("Use: %s <number of points>\n", argv[0]);  
    exit(0);
    }
    if ((num_points = atoi(argv[argc-1])) > MAX_POINTS) {
    printf("Maximum number of points allowed: %d\n", MAX_POINTS);
    exit(0);
    } 

    // Allocate host coordinate arrays 
    size = num_points * sizeof(float); 
    hVx = (float *) malloc(size); 
    hVy = (float *) malloc(size);
    hmin_dist = (float *) malloc(sizeof(float));

    // Initialize points
    for (i = 0; i < num_points; i++) {
        hVx[i] = (float)(rand_r(&seed))/(float)(RAND_MAX);
        hVy[i] = (float)(rand_r(&seed))/(float)(RAND_MAX);
    }

    // Allocate device coordinate arrays
    cudaMalloc(&dVx, size);
    cudaMalloc(&dVy, size);
    cudaMalloc(&dmin_dist, sizeof(float));

    // Copy coordinate arrays from host memory to device memory 
    cudaEventRecord( start, 0 ); 

    cudaMemcpy(dVx, hVx, size, cudaMemcpyHostToDevice);
    cudaMemcpy(dVy, hVy, size, cudaMemcpyHostToDevice);

    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);
    cudaEventElapsedTime(&(time_array[0]), start, stop);

    // Invoke kernel
    cudaEventRecord(start, 0); 

    // ------------------------------------------------------------
    //
    // Invoke kernel function here
    //
    // ------------------------------------------------------------
    //choose the number of blocks and points
    threads = 16;                      //num_points/blocks;
    blocks = num_points/threads;       //my_log(num_points);
    minimum_distance<<<(blocks), threads>>>(dVx, dVy, dmin_dist, num_points);

    //alternative declaration
    //dim3 block(16,16);
    //dim3 grid((num_points + 15)/16, (num_points + 15)/16);
    //minimum_distance<<<grid, block>>>(dVx, dVy, dmin_dist, num_points);

    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);
    cudaEventElapsedTime(&(time_array[1]), start, stop);

    // Copy result from device memory to host memory 
    cudaEventRecord(start, 0); 

    //this was wrong! converting size to sizeof(float)
    cudaMemcpy(hmin_dist, dmin_dist, sizeof(float), cudaMemcpyDeviceToHost);

    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);
    cudaEventElapsedTime(&(time_array[2]), start, stop);

    printf("Number of Points = %d\n", num_points); 
    printf("GPU Host-to-device = %f ms \n", time_array[0]);
    printf("GPU execution time = %f ms \n", time_array[1]);
    printf("GPU Device-to-host = %f ms \n", time_array[2]);
    printf("Minimum distance (GPU) = %e\n", hmin_dist[0]);

    gettimeofday(&cpu_start, NULL);

    dx = hVx[1]-hVx[0];
    dy = hVy[1]-hVy[0];
    distance = sqrtf(dx*dx+dy*dy);
    
    for (i = 0; i < num_points; i++) {
       for (j = i+1; j < num_points; j++) {
           dx = hVx[j]-hVx[i];
           dy = hVy[j]-hVy[i];
           Dij = sqrtf(dx*dx+dy*dy);
           if (distance > Dij) distance = Dij;
       }
    }

    gettimeofday(&cpu_stop, NULL); 
    time_array[3] = 1000*(cpu_stop.tv_sec-cpu_start.tv_sec)
    +0.000001*(cpu_stop.tv_usec-cpu_start.tv_usec);
    printf("CPU execution time = %f ms\n", time_array[3]);
    printf("Minimum distance (CPU) = %e\n", distance);

    // Free device memory 
    cudaFree(dVx);
    cudaFree(dVy);
    cudaFree(dmin_dist);

    // Free host memory 
    free(hVx);
    free(hVy);
    free(hmin_dist);

    return 0;
}  
// ---------------------------------------------------------------------------- 

	.file	"tmpxft_002baee0_00000000-6_sidney.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2031:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2031:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z41__device_stub__Z16minimum_distancePfS_S_iPfS_S_i
	.type	_Z41__device_stub__Z16minimum_distancePfS_S_iPfS_S_i, @function
_Z41__device_stub__Z16minimum_distancePfS_S_iPfS_S_i:
.LFB2053:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 4(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z16minimum_distancePfS_S_i(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L2:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2053:
	.size	_Z41__device_stub__Z16minimum_distancePfS_S_iPfS_S_i, .-_Z41__device_stub__Z16minimum_distancePfS_S_iPfS_S_i
	.globl	_Z16minimum_distancePfS_S_i
	.type	_Z16minimum_distancePfS_S_i, @function
_Z16minimum_distancePfS_S_i:
.LFB2054:
	.cfi_startproc
	endbr64
	jmp	_Z41__device_stub__Z16minimum_distancePfS_S_iPfS_S_i
	.cfi_endproc
.LFE2054:
	.size	_Z16minimum_distancePfS_S_i, .-_Z16minimum_distancePfS_S_i
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Use: %s <number of points>\n"
.LC1:
	.string	"Maximum number of points allowed: %d\n"
.LC3:
	.string	"Number of Points = %d\n"
.LC4:
	.string	"GPU Host-to-device = %f ms \n"
.LC5:
	.string	"GPU execution time = %f ms \n"
.LC6:
	.string	"GPU Device-to-host = %f ms \n"
.LC7:
	.string	"Minimum distance (GPU) = %e\n"
.LC9:
	.string	"CPU execution time = %f ms\n"
.LC10:
	.string	"Minimum distance (CPU) = %e\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%edi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rsi, %rbx
	subq	$152, %rsp
	.cfi_def_cfa_offset 208
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	48(%rsp), %rdi
	xorl	%eax, %eax
	movl	%eax, 20(%rsp)
	call	cudaEventCreate@PLT
	leaq	56(%rsp), %rdi
	call	cudaEventCreate@PLT
	cmpl	$2, %ebp
	je	.L9
	movq	(%rbx), %rdx
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L21
.L9:
	movq	8(%rbx), %rdi
	call	atoi@PLT
	movl	%eax, num_points(%rip)
	cmpl	$1048576, %eax
	jle	.L10
	movl	$1048576, %edx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L21:
	xorl	%edi, %edi
	call	exit@PLT
.L10:
	sall	$2, %eax
	xorl	%r14d, %r14d
	movslq	%eax, %rbx
	movq	%rbx, %rdi
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %r12
	call	malloc@PLT
	movl	$4, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movq	%rax, %r13
.L11:
	cmpl	%r14d, num_points(%rip)
	jle	.L22
	leaq	20(%rsp), %r15
	movq	%r15, %rdi
	call	rand_r@PLT
	movq	%r15, %rdi
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC2(%rip), %xmm0
	movss	%xmm0, (%r12,%r14,4)
	call	rand_r@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC2(%rip), %xmm0
	movss	%xmm0, 0(%rbp,%r14,4)
	incq	%r14
	jmp	.L11
.L22:
	leaq	24(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	32(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	40(%rsp), %rdi
	movl	$4, %esi
	call	cudaMalloc@PLT
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	24(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r12, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	32(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	56(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	leaq	96(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	$16, %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	num_points(%rip), %eax
	movl	$1, 88(%rsp)
	movl	$16, threads(%rip)
	cltd
	idivl	%ecx
	movl	$268435457, %edx
	movl	$1, %ecx
	salq	$4, %rdx
	movq	%rdx, 80(%rsp)
	movl	%eax, 64(%rsp)
	movl	%eax, blocks(%rip)
	movabsq	$4294967297, %rax
	movq	%rax, 68(%rsp)
	movq	64(%rsp), %rdi
	movl	72(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L13
	movl	num_points(%rip), %ecx
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdi
	call	_Z41__device_stub__Z16minimum_distancePfS_S_iPfS_S_i
.L13:
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	movl	$1, %ebx
	call	cudaEventRecord@PLT
	movq	56(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	leaq	100(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	40(%rsp), %rsi
	movl	$2, %ecx
	movq	%r13, %rdi
	movl	$4, %edx
	call	cudaMemcpy@PLT
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	56(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	leaq	104(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movl	num_points(%rip), %edx
	leaq	.LC3(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	96(%rsp), %xmm0
	call	__printf_chk@PLT
	movl	$2, %edi
	movb	$1, %al
	leaq	.LC5(%rip), %rsi
	cvtss2sd	100(%rsp), %xmm0
	call	__printf_chk@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	104(%rsp), %xmm0
	call	__printf_chk@PLT
	movl	$2, %edi
	movb	$1, %al
	leaq	.LC7(%rip), %rsi
	cvtss2sd	0(%r13), %xmm0
	call	__printf_chk@PLT
	leaq	64(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movss	4(%r12), %xmm0
	movss	4(%rbp), %xmm1
	subss	(%r12), %xmm0
	subss	0(%rbp), %xmm1
	mulss	%xmm0, %xmm0
	mulss	%xmm1, %xmm1
	addss	%xmm1, %xmm0
	call	sqrtf@PLT
	movss	%xmm0, 12(%rsp)
.L14:
	movl	num_points(%rip), %r15d
	leal	-1(%rbx), %eax
	cmpl	%eax, %r15d
	jle	.L23
	movq	%rbx, %r14
.L17:
	cmpl	%r14d, %r15d
	jle	.L24
	movss	(%r12,%r14,4), %xmm0
	movss	0(%rbp,%r14,4), %xmm1
	incq	%r14
	subss	-4(%r12,%rbx,4), %xmm0
	subss	-4(%rbp,%rbx,4), %xmm1
	mulss	%xmm0, %xmm0
	mulss	%xmm1, %xmm1
	addss	%xmm1, %xmm0
	call	sqrtf@PLT
	minss	12(%rsp), %xmm0
	movss	%xmm0, 12(%rsp)
	jmp	.L17
.L24:
	incq	%rbx
	jmp	.L14
.L23:
	xorl	%esi, %esi
	leaq	80(%rsp), %rdi
	call	gettimeofday@PLT
	movq	80(%rsp), %rax
	subq	64(%rsp), %rax
	leaq	.LC9(%rip), %rsi
	imulq	$1000, %rax, %rax
	movl	$2, %edi
	cvtsi2sdq	%rax, %xmm0
	movq	88(%rsp), %rax
	subq	72(%rsp), %rax
	cvtsi2sdq	%rax, %xmm1
	mulsd	.LC8(%rip), %xmm1
	movb	$1, %al
	addsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 108(%rsp)
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	12(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L18
	call	__stack_chk_fail@PLT
.L18:
	addq	$152, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	main, .-main
	.section	.rodata.str1.1
.LC11:
	.string	"_Z16minimum_distancePfS_S_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2056:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC11(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z16minimum_distancePfS_S_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2056:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.globl	num_points
	.bss
	.align 4
	.type	num_points, @object
	.size	num_points, 4
num_points:
	.zero	4
	.globl	threads
	.align 4
	.type	threads, @object
	.size	threads, 4
threads:
	.zero	4
	.globl	blocks
	.align 4
	.type	blocks, @object
	.size	blocks, 4
blocks:
	.zero	4
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC2:
	.long	805306368
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC8:
	.long	-1598689907
	.long	1051772663
	.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:

//
// CUDA code to compute minimum distance between n points
//

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

#define MAX_POINTS 1048576
#define BLOCK_SIZE 1024
#define M_LOG2E 1.44269504088896340736 //log2(e)

inline long double my_log(const long double x){
    return  log(x) * M_LOG2E;
}

int blocks;     //log2(num_points);
int threads;    //num_points/blocks;
int num_points;

// ---------------------------------------------------------------------------- 
// Kernel Function to compute distance between all pairs of points
// Input: 
//  X: X[i] = x-coordinate of the ith point
//  Y: Y[i] = y-coordinate of the ith point
//  n: number of points
//
// Output: 
//  D: D[0] = minimum distance
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// Compute minimum distance on host to check device computation
// Need to:  modify the kernel functino to compute the minimum distance.
// You are allowed to make other changes to the code that facilitate
// parallelization on the GPU
//
// 1. You need to develop CUDA-based parallel code to compute the
// distance between the closest pair of points on a GPU
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// References
// ----------------------------------------------------------------------------
/* 
__global__ void VecAdd(float* A, float* B, float* C, int N) 
{ 
 int i = blockDim.x * blockIdx.x + threadIdx.x; 
 if (i < N) 
 C[i] = A[i] + B[i]; 
} 
*/

/*
__global__ void MatMult(float* A, float* B, float* C, int N) 
{ 
int i = blockIdx.x * blockDim.x + threadIdx.x; 
 int j = blockIdx.y * blockDim.y + threadIdx.y; 
 if (i < N && j < N) 
 C[i][j] = A[i][j] + B[i][j];
 }
 */
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// Kernel function code goes here
// ---------------------------------------------------------------------------- 
__global__ void minimum_distance(float * X, float * Y, float * D, int n) 
{
    //CUDA version of the code
    int i = blockIdx.y * blockDim.y + threadIdx.y;
    //int j = blockIdx.x * blockDim.x + threadIdx.x + 1;

    if(threadIdx.y == 0)
    {
        D[0] = 10;
    }

    extern __shared__ float mins[]; 
    __threadfence();

    //if (i < n && j < n) 
    if(i<n)
    {
        for (int j = i+1; j < n; j++) 
        {
            float dx = X[j]-X[i];
            float dy = Y[j]-Y[i];
            float Dij = sqrtf(dx*dx+dy*dy);
            
            //__threadfence();

            //mins local to the block
            mins[threadIdx.x] = Dij;

            __syncthreads();
        }
    }

    //let the last block handle finding the actual minimum
    if(i >= n)
    {
        //sort mins to 
        for(unsigned int s = blockDim.y/2; s > 0; s >>= 1) 
        {

            if(threadIdx.y < s)
            {
                if (mins[threadIdx.y] > mins[threadIdx.y + s]) 
                {
                    mins[threadIdx.y] = mins[threadIdx.y + s];
                }
            }
            __syncthreads();
        }

        //now update global minimum
        if(threadIdx.y == 0)
        {
            if(D[0] > mins[0])
            {
                D[0] = mins[0];
            }
        }
    }
    //D[0] = -15;
} 
// ---------------------------------------------------------------------------- 

// ---------------------------------------------------------------------------- 
// Main program - initializes points and computes minimum distance 
// between the points
// ---------------------------------------------------------------------------- 
int main(int argc, char* argv[]) {

    // Host Data
    float * hVx;            // host x-coordinate array
    float * hVy;            // host y-coordinate array
    float * hmin_dist;      // minimum value on host

    // Device Data
    float * dVx;            // device x-coordinate array
    float * dVy;            // device x-coordinate array
    float * dmin_dist;      // minimum value on device

    int i, j, size;// num_points, blocks, threads; 
    float dx, dy, Dij, distance;
    unsigned int seed = 0;

    hipEvent_t start, stop;        // GPU timing variables

    struct timeval cpu_start, cpu_stop; // CPU timing variables

    float time_array[10]; 

    // Timing initializations
    hipEventCreate(&start);
    hipEventCreate(&stop);

    // Check input
    if (argc != 2) {
    printf("Use: %s <number of points>\n", argv[0]);  
    exit(0);
    }
    if ((num_points = atoi(argv[argc-1])) > MAX_POINTS) {
    printf("Maximum number of points allowed: %d\n", MAX_POINTS);
    exit(0);
    } 

    // Allocate host coordinate arrays 
    size = num_points * sizeof(float); 
    hVx = (float *) malloc(size); 
    hVy = (float *) malloc(size);
    hmin_dist = (float *) malloc(sizeof(float));

    // Initialize points
    for (i = 0; i < num_points; i++) {
        hVx[i] = (float)(rand_r(&seed))/(float)(RAND_MAX);
        hVy[i] = (float)(rand_r(&seed))/(float)(RAND_MAX);
    }

    // Allocate device coordinate arrays
    hipMalloc(&dVx, size);
    hipMalloc(&dVy, size);
    hipMalloc(&dmin_dist, sizeof(float));

    // Copy coordinate arrays from host memory to device memory 
    hipEventRecord( start, 0 ); 

    hipMemcpy(dVx, hVx, size, hipMemcpyHostToDevice);
    hipMemcpy(dVy, hVy, size, hipMemcpyHostToDevice);

    hipEventRecord(stop, 0);
    hipEventSynchronize(stop);
    hipEventElapsedTime(&(time_array[0]), start, stop);

    // Invoke kernel
    hipEventRecord(start, 0); 

    // ------------------------------------------------------------
    //
    // Invoke kernel function here
    //
    // ------------------------------------------------------------
    //choose the number of blocks and points
    threads = 16;                      //num_points/blocks;
    blocks = num_points/threads;       //my_log(num_points);
    minimum_distance<<<(blocks), threads>>>(dVx, dVy, dmin_dist, num_points);

    //alternative declaration
    //dim3 block(16,16);
    //dim3 grid((num_points + 15)/16, (num_points + 15)/16);
    //minimum_distance<<<grid, block>>>(dVx, dVy, dmin_dist, num_points);

    hipEventRecord(stop, 0);
    hipEventSynchronize(stop);
    hipEventElapsedTime(&(time_array[1]), start, stop);

    // Copy result from device memory to host memory 
    hipEventRecord(start, 0); 

    //this was wrong! converting size to sizeof(float)
    hipMemcpy(hmin_dist, dmin_dist, sizeof(float), hipMemcpyDeviceToHost);

    hipEventRecord(stop, 0);
    hipEventSynchronize(stop);
    hipEventElapsedTime(&(time_array[2]), start, stop);

    printf("Number of Points = %d\n", num_points); 
    printf("GPU Host-to-device = %f ms \n", time_array[0]);
    printf("GPU execution time = %f ms \n", time_array[1]);
    printf("GPU Device-to-host = %f ms \n", time_array[2]);
    printf("Minimum distance (GPU) = %e\n", hmin_dist[0]);

    gettimeofday(&cpu_start, NULL);

    dx = hVx[1]-hVx[0];
    dy = hVy[1]-hVy[0];
    distance = sqrtf(dx*dx+dy*dy);
    
    for (i = 0; i < num_points; i++) {
       for (j = i+1; j < num_points; j++) {
           dx = hVx[j]-hVx[i];
           dy = hVy[j]-hVy[i];
           Dij = sqrtf(dx*dx+dy*dy);
           if (distance > Dij) distance = Dij;
       }
    }

    gettimeofday(&cpu_stop, NULL); 
    time_array[3] = 1000*(cpu_stop.tv_sec-cpu_start.tv_sec)
    +0.000001*(cpu_stop.tv_usec-cpu_start.tv_usec);
    printf("CPU execution time = %f ms\n", time_array[3]);
    printf("Minimum distance (CPU) = %e\n", distance);

    // Free device memory 
    hipFree(dVx);
    hipFree(dVy);
    hipFree(dmin_dist);

    // Free host memory 
    free(hVx);
    free(hVy);
    free(hmin_dist);

    return 0;
}  
// ---------------------------------------------------------------------------- 

	.text
	.file	"sidney.hip"
	.globl	_Z31__device_stub__minimum_distancePfS_S_i # -- Begin function _Z31__device_stub__minimum_distancePfS_S_i
	.type	_Z31__device_stub__minimum_distancePfS_S_i,@function
_Z31__device_stub__minimum_distancePfS_S_i: # @_Z31__device_stub__minimum_distancePfS_S_i
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z16minimum_distancePfS_S_i, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z31__device_stub__minimum_distancePfS_S_i, .Lfunc_end0-_Z31__device_stub__minimum_distancePfS_S_i
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0x30000000                      # float 4.65661287E-10
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI1_1:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	movl	%edi, %ebp
	movl	$0, 28(%rsp)
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	movq	%rsp, %rdi
	callq	hipEventCreate
	cmpl	$2, %ebp
	jne	.LBB1_16
# %bb.1:
	movq	8(%rbx), %rdi
	callq	atoi
	movl	%eax, num_points(%rip)
	cmpl	$1048577, %eax                  # imm = 0x100001
	jge	.LBB1_17
# %bb.2:
	movl	%eax, %r13d
	leal	(,%r13,4), %eax
	movslq	%eax, %r12
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %rbx
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %r14
	movl	$4, %edi
	callq	malloc
	movq	%rax, %r15
	testl	%r13d, %r13d
	jle	.LBB1_5
# %bb.3:                                # %.lr.ph.preheader
	xorl	%ebp, %ebp
	leaq	28(%rsp), %r13
.LBB1_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%r13, %rdi
	callq	rand_r
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	.LCPI1_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	mulss	%xmm1, %xmm0
	movss	%xmm0, (%rbx,%rbp,4)
	movq	%r13, %rdi
	callq	rand_r
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	movss	%xmm0, (%r14,%rbp,4)
	incq	%rbp
	movslq	num_points(%rip), %rax
	cmpq	%rax, %rbp
	jl	.LBB1_4
.LBB1_5:                                # %._crit_edge
	leaq	40(%rsp), %r13
	movq	%r13, %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	leaq	32(%rsp), %rbp
	movq	%rbp, %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	leaq	16(%rsp), %rdi
	movl	$4, %esi
	callq	hipMalloc
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%r13), %rdi
	movq	%rbx, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%rbp), %rdi
	movq	%r14, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rsp), %rdi
	callq	hipEventSynchronize
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdx
	leaq	64(%rsp), %rdi
	callq	hipEventElapsedTime
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	$16, threads(%rip)
	movl	num_points(%rip), %eax
	leal	15(%rax), %edi
	testl	%eax, %eax
	cmovnsl	%eax, %edi
	sarl	$4, %edi
	movl	%edi, blocks(%rip)
	btsq	$32, %rdi
	movabsq	$4294967296, %rdx               # imm = 0x100000000
	orq	$16, %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_7
# %bb.6:
	movq	40(%rsp), %rdi
	movq	32(%rsp), %rsi
	movq	16(%rsp), %rdx
	movl	num_points(%rip), %ecx
	callq	_Z31__device_stub__minimum_distancePfS_S_i
.LBB1_7:
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rsp), %rdi
	callq	hipEventSynchronize
	leaq	68(%rsp), %rdi
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdx
	callq	hipEventElapsedTime
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	16(%rsp), %rsi
	movl	$4, %edx
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rsp), %rdi
	callq	hipEventSynchronize
	leaq	72(%rsp), %r12
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdx
	movq	%r12, %rdi
	callq	hipEventElapsedTime
	movl	num_points(%rip), %esi
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	cvtss2sd	-8(%r12), %xmm0
	movl	$.L.str.3, %edi
	movb	$1, %al
	callq	printf
	xorps	%xmm0, %xmm0
	cvtss2sd	-4(%r12), %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r12), %xmm0
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r15), %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	leaq	48(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movss	4(%rbx), %xmm0                  # xmm0 = mem[0],zero,zero,zero
	subss	(%rbx), %xmm0
	movss	4(%r14), %xmm1                  # xmm1 = mem[0],zero,zero,zero
	subss	(%r14), %xmm1
	mulss	%xmm0, %xmm0
	mulss	%xmm1, %xmm1
	addss	%xmm0, %xmm1
	xorps	%xmm0, %xmm0
	sqrtss	%xmm1, %xmm0
	movl	num_points(%rip), %eax
	testl	%eax, %eax
	jle	.LBB1_14
# %bb.8:                                # %.lr.ph72
	xorl	%edi, %edi
	movq	%rax, %rcx
	movq	%rbx, %rdx
	movq	%r14, %rsi
.LBB1_9:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_11 Depth 2
	movq	%rdi, %r8
	incq	%rdi
	cmpq	%rax, %rdi
	jae	.LBB1_12
# %bb.10:                               # %.lr.ph68
                                        #   in Loop: Header=BB1_9 Depth=1
	movss	(%rbx,%r8,4), %xmm1             # xmm1 = mem[0],zero,zero,zero
	movss	(%r14,%r8,4), %xmm2             # xmm2 = mem[0],zero,zero,zero
	movl	$1, %r8d
.LBB1_11:                               #   Parent Loop BB1_9 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rdx,%r8,4), %xmm3             # xmm3 = mem[0],zero,zero,zero
	subss	%xmm1, %xmm3
	movss	(%rsi,%r8,4), %xmm4             # xmm4 = mem[0],zero,zero,zero
	subss	%xmm2, %xmm4
	mulss	%xmm3, %xmm3
	mulss	%xmm4, %xmm4
	addss	%xmm3, %xmm4
	xorps	%xmm3, %xmm3
	sqrtss	%xmm4, %xmm3
	minss	%xmm0, %xmm3
	incq	%r8
	movaps	%xmm3, %xmm0
	cmpq	%r8, %rcx
	jne	.LBB1_11
	jmp	.LBB1_13
.LBB1_12:                               #   in Loop: Header=BB1_9 Depth=1
	movaps	%xmm0, %xmm3
.LBB1_13:                               # %.loopexit
                                        #   in Loop: Header=BB1_9 Depth=1
	addq	$4, %rsi
	addq	$4, %rdx
	decq	%rcx
	movaps	%xmm3, %xmm0
	cmpq	%rax, %rdi
	jne	.LBB1_9
	jmp	.LBB1_15
.LBB1_14:
	movaps	%xmm0, %xmm3
.LBB1_15:                               # %._crit_edge73
	movss	%xmm3, 24(%rsp)                 # 4-byte Spill
	leaq	104(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r12), %rax
	subq	48(%rsp), %rax
	movq	8(%r12), %rcx
	imulq	$1000, %rax, %rax               # imm = 0x3E8
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	subq	56(%rsp), %rcx
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rcx, %xmm1
	mulsd	.LCPI1_1(%rip), %xmm1
	addsd	%xmm0, %xmm1
	xorps	%xmm0, %xmm0
	cvtsd2ss	%xmm1, %xmm0
	movss	%xmm0, 76(%rsp)
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.7, %edi
	movb	$1, %al
	callq	printf
	xorps	%xmm0, %xmm0
	cvtss2sd	24(%rsp), %xmm0                 # 4-byte Folded Reload
	movl	$.L.str.8, %edi
	movb	$1, %al
	callq	printf
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$120, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB1_16:
	.cfi_def_cfa_offset 176
	movq	(%rbx), %rsi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB1_18
.LBB1_17:
	movl	$.L.str.1, %edi
	movl	$1048576, %esi                  # imm = 0x100000
	xorl	%eax, %eax
	callq	printf
.LBB1_18:
	xorl	%edi, %edi
	callq	exit
.Lfunc_end1:
	.size	main, .Lfunc_end1-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	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z16minimum_distancePfS_S_i, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__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	.LBB3_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
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	blocks,@object                  # @blocks
	.bss
	.globl	blocks
	.p2align	2, 0x0
blocks:
	.long	0                               # 0x0
	.size	blocks, 4

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

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

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

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Use: %s <number of points>\n"
	.size	.L.str, 28

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Maximum number of points allowed: %d\n"
	.size	.L.str.1, 38

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Number of Points = %d\n"
	.size	.L.str.2, 23

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"GPU Host-to-device = %f ms \n"
	.size	.L.str.3, 29

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"GPU execution time = %f ms \n"
	.size	.L.str.4, 29

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"GPU Device-to-host = %f ms \n"
	.size	.L.str.5, 29

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Minimum distance (GPU) = %e\n"
	.size	.L.str.6, 29

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"CPU execution time = %f ms\n"
	.size	.L.str.7, 28

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Minimum distance (CPU) = %e\n"
	.size	.L.str.8, 29

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z16minimum_distancePfS_S_i"
	.size	.L__unnamed_1, 28

	.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 _Z31__device_stub__minimum_distancePfS_S_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z16minimum_distancePfS_S_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z16minimum_distancePfS_S_i ; -- Begin function _Z16minimum_distancePfS_S_i
	.globl	_Z16minimum_distancePfS_S_i
	.p2align	8
	.type	_Z16minimum_distancePfS_S_i,@function
_Z16minimum_distancePfS_S_i:            ; @_Z16minimum_distancePfS_S_i
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x2c
	s_load_b64 s[4:5], s[0:1], 0x10
	v_bfe_u32 v1, v0, 10, 10
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_eq_u32_e32 vcc_lo, 0, v1
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s2, s2, 16
	s_and_saveexec_b32 s3, vcc_lo
	s_cbranch_execz .LBB0_2
; %bb.1:
	v_dual_mov_b32 v2, 0 :: v_dual_mov_b32 v3, 0x41200000
	global_store_b32 v2, v3, s[4:5]
.LBB0_2:
	s_or_b32 exec_lo, exec_lo, s3
	s_load_b32 s3, s[0:1], 0x18
	s_and_b32 s7, 0xffff, s2
	s_mov_b32 s6, exec_lo
	v_mad_u64_u32 v[2:3], null, s15, s7, v[1:2]
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	buffer_gl1_inv
	buffer_gl0_inv
	v_cmpx_le_i32_e64 s3, v2
	s_xor_b32 s6, exec_lo, s6
	s_cbranch_execz .LBB0_13
; %bb.3:                                ; %.critedge
	s_cmp_lt_u32 s7, 2
	s_cbranch_scc1 .LBB0_9
; %bb.4:                                ; %.lr.ph41
	v_lshl_add_u32 v0, v1, 2, 0
.LBB0_5:                                ; =>This Inner Loop Header: Depth=1
	s_mov_b32 s8, s7
	s_lshr_b32 s7, s7, 1
	s_mov_b32 s9, exec_lo
	v_cmpx_gt_u32_e64 s7, v1
	s_cbranch_execz .LBB0_8
; %bb.6:                                ;   in Loop: Header=BB0_5 Depth=1
	v_lshl_add_u32 v2, s7, 2, v0
	ds_load_b32 v3, v0
	ds_load_b32 v2, v2
	s_waitcnt lgkmcnt(0)
	v_cmp_gt_f32_e64 s2, v3, v2
	s_delay_alu instid0(VALU_DEP_1)
	s_and_b32 exec_lo, exec_lo, s2
	s_cbranch_execz .LBB0_8
; %bb.7:                                ;   in Loop: Header=BB0_5 Depth=1
	ds_store_b32 v0, v2
.LBB0_8:                                ;   in Loop: Header=BB0_5 Depth=1
	s_or_b32 exec_lo, exec_lo, s9
	s_cmp_lt_u32 s8, 4
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc0 .LBB0_5
.LBB0_9:                                ; %._crit_edge42
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB0_12
; %bb.10:
	v_mov_b32_e32 v0, 0
	global_load_b32 v2, v0, s[4:5]
	ds_load_b32 v1, v0
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_cmp_ngt_f32_e32 vcc_lo, v2, v1
	s_cbranch_vccnz .LBB0_12
; %bb.11:
	global_store_b32 v0, v1, s[4:5]
.LBB0_12:                               ; %Flow57
	s_or_b32 exec_lo, exec_lo, s2
                                        ; implicit-def: $vgpr2_vgpr3
                                        ; implicit-def: $vgpr0
.LBB0_13:                               ; %Flow63
	s_and_not1_saveexec_b32 s2, s6
	s_cbranch_execz .LBB0_17
; %bb.14:                               ; %.preheader
	v_add_nc_u32_e32 v8, 1, v2
	s_mov_b32 s2, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e64 s3, v8
	s_cbranch_execz .LBB0_17
; %bb.15:                               ; %.lr.ph
	s_load_b128 s[4:7], s[0:1], 0x0
	v_ashrrev_i32_e32 v3, 31, v2
	v_and_b32_e32 v0, 0x3ff, v0
	s_mov_b32 s2, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[2:3], 2, v[2:3]
	v_lshl_add_u32 v9, v0, 2, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_u32 v6, vcc_lo, v2, 4
	v_add_co_ci_u32_e32 v7, vcc_lo, 0, v3, vcc_lo
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v0, vcc_lo, s4, v2
	v_add_co_ci_u32_e32 v1, vcc_lo, s5, v3, vcc_lo
	v_add_co_u32 v2, vcc_lo, s6, v2
	v_add_co_ci_u32_e32 v3, vcc_lo, s7, v3, vcc_lo
	v_add_co_u32 v4, vcc_lo, s4, v6
	v_add_co_ci_u32_e32 v5, vcc_lo, s5, v7, vcc_lo
	v_add_co_u32 v6, vcc_lo, s6, v6
	v_add_co_ci_u32_e32 v7, vcc_lo, s7, v7, vcc_lo
.LBB0_16:                               ; =>This Inner Loop Header: Depth=1
	global_load_b32 v10, v[6:7], off
	global_load_b32 v11, v[2:3], off
	global_load_b32 v12, v[4:5], off
	global_load_b32 v13, v[0:1], off
	v_add_nc_u32_e32 v8, 1, v8
	v_add_co_u32 v4, s0, v4, 4
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_add_co_ci_u32_e64 v5, s0, 0, v5, s0
	v_add_co_u32 v6, s0, v6, 4
	v_add_co_ci_u32_e64 v7, s0, 0, v7, s0
	s_waitcnt vmcnt(0)
	v_dual_sub_f32 v10, v10, v11 :: v_dual_sub_f32 v11, v12, v13
	v_cmp_le_i32_e64 s0, s3, v8
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v10, v10, v10
	s_or_b32 s2, s0, s2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v10, v11, v11
	v_mul_f32_e32 v11, 0x4f800000, v10
	v_cmp_gt_f32_e32 vcc_lo, 0xf800000, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e32 v10, v10, v11, 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)
	v_cndmask_b32_e32 v10, v11, v10, vcc_lo
	ds_store_b32 v9, v10
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_and_not1_b32 exec_lo, exec_lo, s2
	s_cbranch_execnz .LBB0_16
.LBB0_17:                               ; %._crit_edge
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z16minimum_distancePfS_S_i
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.amdhsa_user_sgpr_count 14
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 16
		.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	_Z16minimum_distancePfS_S_i, .Lfunc_end0-_Z16minimum_distancePfS_S_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 760
; NumSgprs: 18
; NumVgprs: 16
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 16
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
      - .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:           _Z16minimum_distancePfS_S_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z16minimum_distancePfS_S_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     16
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
