#include <cuda.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <cmath>
#include <climits>
#include <iostream>
#include <chrono>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
using namespace std;

#define MAX_THREAD_PER_BLOCK 512
#define DEBUG_PRINT

/*
  PARALLEL NN - VERSION 2
*/

typedef unsigned short int usint;

const usint num_dimensions = 16;
const usint numPointsTest = 1000;
const usint numPointsTrain = 19000;
const usint streamCount = 4;

struct Coordinates {
	usint points[num_dimensions];
};

__device__ float getDistance(const Coordinates & coord1, const Coordinates & coord2) {
	float square_sum = 0;
	for (int i = 0; i < num_dimensions; i++) {
		const int c1 = coord1.points[i];
		const int c2 = coord2.points[i];
		square_sum += (c1 - c2)*(c1 - c2);
	}
	return sqrt(square_sum);
}

__global__ void nearestNeighbor(Coordinates * trainCoords, Coordinates * testCoords, const usint sizeTest, const usint sizeTrain, usint * nearestNeighbors) {
	const usint threadId = blockIdx.x*blockDim.x + threadIdx.x;
	if (threadId < sizeTest) { // DEBUG
		usint nearestNeighbor = 0;
		usint nearestDistance = USHRT_MAX;
		for (int trainCoordInd = 0; trainCoordInd < sizeTrain; trainCoordInd++) {
			float currentDistance = getDistance(trainCoords[trainCoordInd], testCoords[threadId]);
			if (currentDistance < nearestDistance) {
				nearestNeighbor = trainCoordInd;
				nearestDistance = currentDistance;
			}
		}
		nearestNeighbors[threadId] = nearestNeighbor;
	}
}

bool checkError(const cudaError_t & error, const char * msg = "") {
	if (error != cudaSuccess) {
		printf("CUDA ERROR: %s\n", msg);
		cout << error << endl;
		exit(1);
	}
	return true;
}

int main() {
	// 1 - INITIALIZE READ STREAMS
	const char * testFile = "test.txt";
	const char * trainFile = "train.txt";
	FILE * test_is = fopen(testFile, "r"), * train_is = fopen(trainFile, "r");
	if (!test_is) {
		cerr << "Cannot open " << testFile << endl;
		exit(1);
	}
	if (!train_is) {
		cerr << "Cannot open " << trainFile << endl;
		exit(1);
	}

	cudaSetDevice(0); // initialize CUDA context
	cout << "\t--------------------\n";
	chrono::high_resolution_clock::time_point begin = chrono::high_resolution_clock::now(), temp, end;

	// 2 - SET EXECUTION PARAMETERS
	cudaStream_t streams[streamCount]; // create four CUDA streams
	cudaError_t cudaError;

	usint numThreadsPerBlock = numPointsTest;
	usint numBlocks = 1;
	if (numPointsTest > MAX_THREAD_PER_BLOCK) {
		numBlocks = std::ceil(static_cast<double>(numPointsTest) / MAX_THREAD_PER_BLOCK);
		numThreadsPerBlock = MAX_THREAD_PER_BLOCK;
	}
	numThreadsPerBlock /= streamCount;
	cout << "Kernels will be called with " << numBlocks << " blocks with " << numThreadsPerBlock << " threads each\n";

	// 3 - READ TRAIN COORDINATES FROM FILE STREAMS
	// device pointers
	Coordinates * d_testCoordinates[streamCount], *d_trainCoordinates;
	usint * d_nearestNeighbors[streamCount];
	// host pointers
	Coordinates * h_testCoordinates[streamCount], *h_trainCoordinates;
	usint * h_nearestNeighbors[streamCount];

	cudaError = cudaMallocHost((void**)&h_trainCoordinates, numPointsTrain * sizeof(Coordinates));
	checkError(cudaError, "cudamallochost - h_trainCoordinates");

	// read train points to host
	for (int i = 0; i < numPointsTrain; i++) {
		fscanf(train_is, "%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n", &h_trainCoordinates[i].points[0], &h_trainCoordinates[i].points[1], &h_trainCoordinates[i].points[2],
			&h_trainCoordinates[i].points[3], &h_trainCoordinates[i].points[4], &h_trainCoordinates[i].points[5], &h_trainCoordinates[i].points[6], &h_trainCoordinates[i].points[7],
			&h_trainCoordinates[i].points[8], &h_trainCoordinates[i].points[9], &h_trainCoordinates[i].points[10], &h_trainCoordinates[i].points[11], &h_trainCoordinates[i].points[12],
			&h_trainCoordinates[i].points[13], &h_trainCoordinates[i].points[14], &h_trainCoordinates[i].points[15]);
	}
	cout << "done reading training coordinates to host pinned memory" << endl;

	// copy train coordinates to device
	cudaError = cudaMalloc((void**)&d_trainCoordinates, numPointsTrain * sizeof(Coordinates));
	checkError(cudaError, "cudaMalloc - d_trainCoordinates");
	cudaError = cudaMemcpy(d_trainCoordinates, h_trainCoordinates, numPointsTrain * sizeof(Coordinates), cudaMemcpyHostToDevice);
	checkError(cudaError, "cudaMemcpyAsync - d_trainCoordinates");
	
	chrono::high_resolution_clock::time_point kernel_start = chrono::high_resolution_clock::now();
	for (usint stream = 0; stream < streamCount; stream++) {
		// 1 - create stream
		cudaStreamCreate(&streams[stream]);

		// 2 - Host memory - allocate memory on host for results and test coordinates
		cudaError = cudaMallocHost((void**)&h_nearestNeighbors[stream], (numPointsTest / streamCount) * sizeof(usint));
		checkError(cudaError, "cudamallochost - h_nearestneighbors");
		cudaError = cudaMallocHost((void**)&h_testCoordinates[stream], (numPointsTest / streamCount) * sizeof(Coordinates));
		checkError(cudaError, "cudamallochost - h_testCoordinates");

		// 3 - Host memory - read test points
		for (int i = 0; i < numPointsTest / streamCount; i++) {
			fscanf(test_is, "%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d", &h_testCoordinates[stream][i].points[0], &h_testCoordinates[stream][i].points[1], &h_testCoordinates[stream][i].points[2],
				&h_testCoordinates[stream][i].points[3], &h_testCoordinates[stream][i].points[4], &h_testCoordinates[stream][i].points[5], &h_testCoordinates[stream][i].points[6], &h_testCoordinates[stream][i].points[7],
				&h_testCoordinates[stream][i].points[8], &h_testCoordinates[stream][i].points[9], &h_testCoordinates[stream][i].points[10], &h_testCoordinates[stream][i].points[11], &h_testCoordinates[stream][i].points[12],
				&h_testCoordinates[stream][i].points[13], &h_testCoordinates[stream][i].points[14], &h_testCoordinates[stream][i].points[15]);
		}
		
		// 4 - Device memory - allocate space for test coordiantes and result array for this stream to write its results to
		cudaError = cudaMalloc((void**)&d_testCoordinates[stream], (numPointsTest / streamCount) * sizeof(Coordinates));
		checkError(cudaError, "cudaMalloc - d_testCoordiantes");
		cudaError = cudaMalloc((void**)&d_nearestNeighbors[stream], (numPointsTest / streamCount) * sizeof(usint));
		checkError(cudaError, "cudaMalloc - d_nearestNeighbors");
	
		// 5 - copy test coordinates to device in async
		temp = chrono::high_resolution_clock::now();
		cudaError = cudaMemcpyAsync(d_testCoordinates[stream], h_testCoordinates[stream], (numPointsTest / streamCount) * sizeof(Coordinates), cudaMemcpyHostToDevice, streams[stream]);
		checkError(cudaError, "cudaMemcpy - d_testCoordinates");
		end = chrono::high_resolution_clock::now();
		cout << "data copied to device memory [" << chrono::duration_cast<chrono::milliseconds>(end - temp).count() << " ms]\n"
			<< "executing kernel with " << numBlocks << " blocks with " << numThreadsPerBlock << " threads each" << endl;
		
		// 6 - Inovke kernel for current stream
		usint *& currentResultArray = d_nearestNeighbors[stream];
		nearestNeighbor<<< numBlocks, numThreadsPerBlock, 0, streams[stream] >>>(d_trainCoordinates, d_testCoordinates[stream], numPointsTest / streamCount, numPointsTrain, currentResultArray);
		cudaError = cudaMemcpyAsync(h_nearestNeighbors[stream], d_nearestNeighbors[stream], (numPointsTest / streamCount) * sizeof(usint), cudaMemcpyDeviceToHost, streams[stream]);
		checkError(cudaError, "cudaMemcpy - h_nearestNeighbors");
	}
	
	// Wait for GPU to terminate and fetch results
	cudaError = cudaGetLastError();
	checkError(cudaError, "before deviceSync() error!");
	cudaDeviceSynchronize();
	end = chrono::high_resolution_clock::now();
	cout << "Computation + read test data: " << chrono::duration_cast<chrono::milliseconds>(end - kernel_start).count() << " ms\n";
	cout << "\t--------------------\n";
	end = chrono::high_resolution_clock::now();
	
	ofstream os("output.txt");
	
	for (int stream = 0; stream < streamCount; stream++) {
		for (int i = 0; i < numPointsTest / streamCount; i++) {
			os << h_nearestNeighbors[stream][i] << endl;
		}
	}
	
	end = chrono::high_resolution_clock::now();
	cout << "\t--------------------\nTotal time: " << chrono::duration_cast<chrono::milliseconds>(end - begin).count() << " ms\nterminating\n";
	
	return 0;
}

	.file	"tmpxft_0037ae29_00000000-6_source2.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3876:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3876:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.align 2
	.type	_ZNSolsEPFRSoS_E.isra.0, @function
_ZNSolsEPFRSoS_E.isra.0:
.LFB4633:
	.cfi_startproc
	jmp	*%rsi
	.cfi_endproc
.LFE4633:
	.size	_ZNSolsEPFRSoS_E.isra.0, .-_ZNSolsEPFRSoS_E.isra.0
	.globl	_Z11getDistanceRK11CoordinatesS1_
	.type	_Z11getDistanceRK11CoordinatesS1_, @function
_Z11getDistanceRK11CoordinatesS1_:
.LFB3865:
	.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
.LFE3865:
	.size	_Z11getDistanceRK11CoordinatesS1_, .-_Z11getDistanceRK11CoordinatesS1_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"CUDA ERROR: %s\n"
	.text
	.globl	_Z10checkErrorRK9cudaErrorPKc
	.type	_Z10checkErrorRK9cudaErrorPKc, @function
_Z10checkErrorRK9cudaErrorPKc:
.LFB3866:
	.cfi_startproc
	endbr64
	cmpl	$0, (%rdi)
	je	.L6
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rsi, %rdx
	movq	%rdi, %rbx
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	(%rbx), %esi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZNSolsEi@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	$1, %edi
	call	exit@PLT
.L6:
	.cfi_def_cfa_offset 8
	.cfi_restore 3
	movb	$1, %al
	ret
	.cfi_endproc
.LFE3866:
	.size	_Z10checkErrorRK9cudaErrorPKc, .-_Z10checkErrorRK9cudaErrorPKc
	.globl	_Z54__device_stub__Z15nearestNeighborP11CoordinatesS0_ttPtP11CoordinatesS0_ttPt
	.type	_Z54__device_stub__Z15nearestNeighborP11CoordinatesS0_ttPtP11CoordinatesS0_ttPt, @function
_Z54__device_stub__Z15nearestNeighborP11CoordinatesS0_ttPtP11CoordinatesS0_ttPt:
.LFB3898:
	.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
	movw	%dx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movw	%cx, 8(%rsp)
	leaq	40(%rsp), %rcx
	movq	%r8, (%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)
	movq	%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	.L11
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z15nearestNeighborP11CoordinatesS0_ttPt(%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
.L11:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3898:
	.size	_Z54__device_stub__Z15nearestNeighborP11CoordinatesS0_ttPtP11CoordinatesS0_ttPt, .-_Z54__device_stub__Z15nearestNeighborP11CoordinatesS0_ttPtP11CoordinatesS0_ttPt
	.globl	_Z15nearestNeighborP11CoordinatesS0_ttPt
	.type	_Z15nearestNeighborP11CoordinatesS0_ttPt, @function
_Z15nearestNeighborP11CoordinatesS0_ttPt:
.LFB3899:
	.cfi_startproc
	endbr64
	movzwl	%cx, %ecx
	movzwl	%dx, %edx
	jmp	_Z54__device_stub__Z15nearestNeighborP11CoordinatesS0_ttPtP11CoordinatesS0_ttPt
	.cfi_endproc
.LFE3899:
	.size	_Z15nearestNeighborP11CoordinatesS0_ttPt, .-_Z15nearestNeighborP11CoordinatesS0_ttPt
	.section	.rodata.str1.1
.LC1:
	.string	"_Z15nearestNeighborP11CoordinatesS0_ttPt"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3901:
	.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	.LC1(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z15nearestNeighborP11CoordinatesS0_ttPt(%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
.LFE3901:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.rodata.str1.1
.LC2:
	.string	"r"
.LC3:
	.string	"test.txt"
.LC4:
	.string	"train.txt"
.LC5:
	.string	"Cannot open "
.LC6:
	.string	"\t--------------------\n"
.LC7:
	.string	"Kernels will be called with "
.LC8:
	.string	" blocks with "
.LC9:
	.string	" threads each\n"
.LC10:
	.string	"cudamallochost - h_trainCoordinates"
.LC11:
	.string	"%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n"
.LC12:
	.string	"done reading training coordinates to host pinned memory"
.LC13:
	.string	"cudaMalloc - d_trainCoordinates"
.LC14:
	.string	"cudaMemcpyAsync - d_trainCoordinates"
.LC15:
	.string	"cudamallochost - h_nearestneighbors"
.LC16:
	.string	"cudamallochost - h_testCoordinates"
.LC17:
	.string	"%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d"
.LC18:
	.string	"cudaMalloc - d_testCoordiantes"
.LC19:
	.string	"cudaMalloc - d_nearestNeighbors"
.LC20:
	.string	"cudaMemcpy - d_testCoordinates"
.LC21:
	.string	"data copied to device memory ["
.LC22:
	.string	" ms]\n"
.LC23:
	.string	"executing kernel with "
.LC24:
	.string	" threads each"
.LC25:
	.string	"cudaMemcpy - h_nearestNeighbors"
.LC26:
	.string	"before deviceSync() error!"
.LC27:
	.string	"Computation + read test data: "
.LC28:
	.string	" ms\n"
.LC29:
	.string	"output.txt"
.LC30:
	.string	"\t--------------------\nTotal time: "
.LC31:
	.string	" ms\nterminating\n"
	.section	.text.startup
	.globl	main
	.type	main, @function
main:
.LFB3867:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA3867
	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
	leaq	.LC4(%rip), %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	leaq	.LC3(%rip), %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	leaq	.LC2(%rip), %rbx
	movq	%rbp, %rdi
	movq	%rbx, %rsi
	subq	$776, %rsp
	.cfi_def_cfa_offset 832
	movq	%fs:40, %rax
	movq	%rax, 760(%rsp)
	xorl	%eax, %eax
.LEHB0:
	call	fopen@PLT
	movq	%rbx, %rsi
	movq	%r12, %rdi
	movq	%rax, 8(%rsp)
	call	fopen@PLT
	cmpq	$0, 8(%rsp)
	jne	.L19
	leaq	.LC5(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rbp, %rsi
	movq	%rax, %rdi
	jmp	.L38
.L19:
	movq	%rax, %rbx
	testq	%rax, %rax
	jne	.L20
	leaq	.LC5(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%r12, %rsi
	movq	%rax, %rdi
.L38:
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	$1, %edi
	call	exit@PLT
.L20:
	xorl	%edi, %edi
	leaq	_ZSt4cout(%rip), %r12
	leaq	44(%rsp), %rbp
	xorl	%r15d, %r15d
	call	cudaSetDevice@PLT
	leaq	.LC6(%rip), %rsi
	movq	%r12, %rdi
	leaq	.LC11(%rip), %r13
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	leaq	.LC7(%rip), %rsi
	movq	%r12, %rdi
	movq	%rax, 16(%rsp)
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	$2, %esi
	movq	%rax, %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	leaq	.LC8(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	$128, %esi
	movq	%rax, %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	leaq	.LC9(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	leaq	56(%rsp), %rdi
	movl	$608000, %esi
	call	cudaMallocHost@PLT
	leaq	.LC10(%rip), %rsi
	movq	%rbp, %rdi
	movl	%eax, 44(%rsp)
	call	_Z10checkErrorRK9cudaErrorPKc
.L21:
	movq	56(%rsp), %rdx
	movq	%r13, %rsi
	movq	%rbx, %rdi
	addq	%r15, %rdx
	addq	$32, %r15
	leaq	30(%rdx), %rax
	leaq	2(%rdx), %rcx
	pushq	%rax
	.cfi_def_cfa_offset 840
	leaq	28(%rdx), %rax
	leaq	6(%rdx), %r9
	pushq	%rax
	.cfi_def_cfa_offset 848
	leaq	26(%rdx), %rax
	leaq	4(%rdx), %r8
	pushq	%rax
	.cfi_def_cfa_offset 856
	leaq	24(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 864
	leaq	22(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 872
	leaq	20(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 880
	leaq	18(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 888
	leaq	16(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 896
	leaq	14(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 904
	leaq	12(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 912
	leaq	10(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 920
	leaq	8(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 928
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	addq	$96, %rsp
	.cfi_def_cfa_offset 832
	cmpq	$608000, %r15
	jne	.L21
	leaq	.LC12(%rip), %rsi
	movq	%r12, %rdi
	leaq	88(%rsp), %r13
	xorl	%r14d, %r14d
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	leaq	48(%rsp), %rdi
	movl	$608000, %esi
	call	cudaMalloc@PLT
	leaq	.LC13(%rip), %rsi
	movq	%rbp, %rdi
	movl	%eax, 44(%rsp)
	call	_Z10checkErrorRK9cudaErrorPKc
	movq	56(%rsp), %rsi
	movq	48(%rsp), %rdi
	movl	$1, %ecx
	movl	$608000, %edx
	call	cudaMemcpy@PLT
	leaq	.LC14(%rip), %rsi
	movq	%rbp, %rdi
	movl	%eax, 44(%rsp)
	call	_Z10checkErrorRK9cudaErrorPKc
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	movq	%rax, 24(%rsp)
.L24:
	leaq	0(%r13,%r14), %rdi
	leaq	44(%rsp), %rbp
	xorl	%r12d, %r12d
	call	cudaStreamCreate@PLT
	leaq	216(%rsp,%r14), %rdi
	movl	$500, %esi
	leaq	184(%rsp), %rbx
	call	cudaMallocHost@PLT
	movq	%rbp, %rdi
	leaq	.LC15(%rip), %rsi
	movl	%eax, 44(%rsp)
	call	_Z10checkErrorRK9cudaErrorPKc
	leaq	(%rbx,%r14), %rdi
	movl	$8000, %esi
	call	cudaMallocHost@PLT
	movq	%rbp, %rdi
	leaq	.LC16(%rip), %rsi
	leaq	.LC17(%rip), %rbp
	movl	%eax, 44(%rsp)
	call	_Z10checkErrorRK9cudaErrorPKc
.L22:
	movq	(%rbx,%r14), %rdx
	movq	%rbp, %rsi
	addq	%r12, %rdx
	addq	$32, %r12
	leaq	30(%rdx), %rax
	leaq	2(%rdx), %rcx
	pushq	%rax
	.cfi_def_cfa_offset 840
	leaq	28(%rdx), %rax
	leaq	6(%rdx), %r9
	pushq	%rax
	.cfi_def_cfa_offset 848
	leaq	26(%rdx), %rax
	leaq	4(%rdx), %r8
	pushq	%rax
	.cfi_def_cfa_offset 856
	leaq	24(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 864
	leaq	22(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 872
	leaq	20(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 880
	leaq	18(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 888
	leaq	16(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 896
	leaq	14(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 904
	leaq	12(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 912
	leaq	10(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 920
	leaq	8(%rdx), %rax
	pushq	%rax
	.cfi_def_cfa_offset 928
	movq	104(%rsp), %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	addq	$96, %rsp
	.cfi_def_cfa_offset 832
	cmpq	$8000, %r12
	jne	.L22
	leaq	44(%rsp), %rbp
	leaq	120(%rsp,%r14), %rdi
	movl	$8000, %esi
	call	cudaMalloc@PLT
	leaq	.LC18(%rip), %rsi
	movq	%rbp, %rdi
	leaq	_ZSt4cout(%rip), %r12
	movl	%eax, 44(%rsp)
	call	_Z10checkErrorRK9cudaErrorPKc
	leaq	152(%rsp,%r14), %rdi
	movl	$500, %esi
	call	cudaMalloc@PLT
	leaq	.LC19(%rip), %rsi
	movq	%rbp, %rdi
	movl	%eax, 44(%rsp)
	call	_Z10checkErrorRK9cudaErrorPKc
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	movq	0(%r13,%r14), %r8
	movq	120(%rsp,%r14), %rdi
	movl	$1, %ecx
	movq	184(%rsp,%r14), %rsi
	movl	$8000, %edx
	movq	%rax, %r15
	call	cudaMemcpyAsync@PLT
	leaq	.LC20(%rip), %rsi
	movq	%rbp, %rdi
	movl	%eax, 44(%rsp)
	call	_Z10checkErrorRK9cudaErrorPKc
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	leaq	.LC21(%rip), %rsi
	movq	%r12, %rdi
	movq	%rax, %rbx
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	$1000000, %ecx
	movq	%rax, %rdi
	movq	%rbx, %rax
	subq	%r15, %rax
	cqto
	idivq	%rcx
	movq	%rax, %rsi
	call	_ZNSo9_M_insertIlEERSoT_@PLT
	leaq	.LC22(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	leaq	.LC23(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	$2, %esi
	movq	%rax, %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	leaq	.LC8(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	$128, %esi
	movq	%rax, %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	leaq	.LC24(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	$33554433, %edx
	movl	$2147483649, %edi
	xorl	%r8d, %r8d
	movq	0(%r13,%r14), %r9
	salq	$7, %rdx
	addq	%rdi, %rdi
	movl	$1, %ecx
	movl	$1, %esi
	movq	%rdx, 76(%rsp)
	movl	$1, 84(%rsp)
	movq	%rdi, 64(%rsp)
	movl	$1, 72(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L23
	movq	120(%rsp,%r14), %rsi
	movq	48(%rsp), %rdi
	movl	$19000, %ecx
	movl	$250, %edx
	movq	152(%rsp,%r14), %r8
	call	_Z54__device_stub__Z15nearestNeighborP11CoordinatesS0_ttPtP11CoordinatesS0_ttPt
.L23:
	movq	0(%r13,%r14), %r8
	movl	$2, %ecx
	movl	$500, %edx
	movq	152(%rsp,%r14), %rsi
	movq	216(%rsp,%r14), %rdi
	addq	$8, %r14
	leaq	216(%rsp), %r15
	call	cudaMemcpyAsync@PLT
	leaq	.LC25(%rip), %rsi
	movq	%rbp, %rdi
	movl	%eax, 44(%rsp)
	call	_Z10checkErrorRK9cudaErrorPKc
	cmpq	$32, %r14
	jne	.L24
	call	cudaGetLastError@PLT
	movq	%rbp, %rdi
	leaq	.LC26(%rip), %rsi
	xorl	%r13d, %r13d
	movl	%eax, 44(%rsp)
	leaq	248(%rsp), %rbp
	call	_Z10checkErrorRK9cudaErrorPKc
	call	cudaDeviceSynchronize@PLT
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	leaq	.LC27(%rip), %rsi
	movq	%r12, %rdi
	movq	%rax, %rbx
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	24(%rsp), %rcx
	movq	%rax, %rdi
	movq	%rbx, %rax
	subq	%rcx, %rax
	movl	$1000000, %ecx
	cqto
	idivq	%rcx
	movq	%rax, %rsi
	call	_ZNSo9_M_insertIlEERSoT_@PLT
	leaq	.LC28(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	leaq	.LC6(%rip), %rsi
	movq	%r12, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	movl	$16, %edx
	leaq	.LC29(%rip), %rsi
	movq	%rbp, %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode@PLT
.LEHE0:
.L25:
	xorl	%ebx, %ebx
.L26:
	movq	(%r15,%r13,8), %rax
	movq	%rbp, %rdi
	movzwl	(%rax,%rbx), %esi
.LEHB1:
	call	_ZNSo9_M_insertImEERSoT_@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	addq	$2, %rbx
	cmpq	$500, %rbx
	jne	.L26
	incq	%r13
	cmpq	$4, %r13
	jne	.L25
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	leaq	.LC30(%rip), %rsi
	movq	%r12, %rdi
	movq	%rax, %rbx
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	16(%rsp), %rcx
	movq	%rax, %rdi
	movq	%rbx, %rax
	subq	%rcx, %rax
	movl	$1000000, %ecx
	cqto
	idivq	%rcx
	movq	%rax, %rsi
	call	_ZNSo9_M_insertIlEERSoT_@PLT
	movq	%rax, %rdi
	leaq	.LC31(%rip), %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
.LEHE1:
	movq	%rbp, %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	760(%rsp), %rax
	subq	%fs:40, %rax
	je	.L30
	jmp	.L36
.L31:
	endbr64
	movq	%rax, %rbx
.L28:
	movq	%rbp, %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	760(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L36
	movq	%rbx, %rdi
.LEHB2:
	call	_Unwind_Resume@PLT
.LEHE2:
.L36:
	call	__stack_chk_fail@PLT
.L30:
	addq	$776, %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
.LFE3867:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA3867:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE3867-.LLSDACSB3867
.LLSDACSB3867:
	.uleb128 .LEHB0-.LFB3867
	.uleb128 .LEHE0-.LEHB0
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB1-.LFB3867
	.uleb128 .LEHE1-.LEHB1
	.uleb128 .L31-.LFB3867
	.uleb128 0
	.uleb128 .LEHB2-.LFB3867
	.uleb128 .LEHE2-.LEHB2
	.uleb128 0
	.uleb128 0
.LLSDACSE3867:
	.section	.text.startup
	.size	main, .-main
	.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
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include <hip/hip_runtime.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <cmath>
#include <climits>
#include <iostream>
#include <chrono>


using namespace std;

#define MAX_THREAD_PER_BLOCK 512
#define DEBUG_PRINT

/*
  PARALLEL NN - VERSION 2
*/

typedef unsigned short int usint;

const usint num_dimensions = 16;
const usint numPointsTest = 1000;
const usint numPointsTrain = 19000;
const usint streamCount = 4;

struct Coordinates {
	usint points[num_dimensions];
};

__device__ float getDistance(const Coordinates & coord1, const Coordinates & coord2) {
	float square_sum = 0;
	for (int i = 0; i < num_dimensions; i++) {
		const int c1 = coord1.points[i];
		const int c2 = coord2.points[i];
		square_sum += (c1 - c2)*(c1 - c2);
	}
	return sqrt(square_sum);
}

__global__ void nearestNeighbor(Coordinates * trainCoords, Coordinates * testCoords, const usint sizeTest, const usint sizeTrain, usint * nearestNeighbors) {
	const usint threadId = blockIdx.x*blockDim.x + threadIdx.x;
	if (threadId < sizeTest) { // DEBUG
		usint nearestNeighbor = 0;
		usint nearestDistance = USHRT_MAX;
		for (int trainCoordInd = 0; trainCoordInd < sizeTrain; trainCoordInd++) {
			float currentDistance = getDistance(trainCoords[trainCoordInd], testCoords[threadId]);
			if (currentDistance < nearestDistance) {
				nearestNeighbor = trainCoordInd;
				nearestDistance = currentDistance;
			}
		}
		nearestNeighbors[threadId] = nearestNeighbor;
	}
}

bool checkError(const hipError_t & error, const char * msg = "") {
	if (error != hipSuccess) {
		printf("CUDA ERROR: %s\n", msg);
		cout << error << endl;
		exit(1);
	}
	return true;
}

int main() {
	// 1 - INITIALIZE READ STREAMS
	const char * testFile = "test.txt";
	const char * trainFile = "train.txt";
	FILE * test_is = fopen(testFile, "r"), * train_is = fopen(trainFile, "r");
	if (!test_is) {
		cerr << "Cannot open " << testFile << endl;
		exit(1);
	}
	if (!train_is) {
		cerr << "Cannot open " << trainFile << endl;
		exit(1);
	}

	hipSetDevice(0); // initialize CUDA context
	cout << "\t--------------------\n";
	chrono::high_resolution_clock::time_point begin = chrono::high_resolution_clock::now(), temp, end;

	// 2 - SET EXECUTION PARAMETERS
	hipStream_t streams[streamCount]; // create four CUDA streams
	hipError_t hipError_t;

	usint numThreadsPerBlock = numPointsTest;
	usint numBlocks = 1;
	if (numPointsTest > MAX_THREAD_PER_BLOCK) {
		numBlocks = std::ceil(static_cast<double>(numPointsTest) / MAX_THREAD_PER_BLOCK);
		numThreadsPerBlock = MAX_THREAD_PER_BLOCK;
	}
	numThreadsPerBlock /= streamCount;
	cout << "Kernels will be called with " << numBlocks << " blocks with " << numThreadsPerBlock << " threads each\n";

	// 3 - READ TRAIN COORDINATES FROM FILE STREAMS
	// device pointers
	Coordinates * d_testCoordinates[streamCount], *d_trainCoordinates;
	usint * d_nearestNeighbors[streamCount];
	// host pointers
	Coordinates * h_testCoordinates[streamCount], *h_trainCoordinates;
	usint * h_nearestNeighbors[streamCount];

	hipError_t = hipHostMalloc((void**)&h_trainCoordinates, numPointsTrain * sizeof(Coordinates), hipHostMallocDefault);
	checkError(hipError_t, "cudamallochost - h_trainCoordinates");

	// read train points to host
	for (int i = 0; i < numPointsTrain; i++) {
		fscanf(train_is, "%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n", &h_trainCoordinates[i].points[0], &h_trainCoordinates[i].points[1], &h_trainCoordinates[i].points[2],
			&h_trainCoordinates[i].points[3], &h_trainCoordinates[i].points[4], &h_trainCoordinates[i].points[5], &h_trainCoordinates[i].points[6], &h_trainCoordinates[i].points[7],
			&h_trainCoordinates[i].points[8], &h_trainCoordinates[i].points[9], &h_trainCoordinates[i].points[10], &h_trainCoordinates[i].points[11], &h_trainCoordinates[i].points[12],
			&h_trainCoordinates[i].points[13], &h_trainCoordinates[i].points[14], &h_trainCoordinates[i].points[15]);
	}
	cout << "done reading training coordinates to host pinned memory" << endl;

	// copy train coordinates to device
	hipError_t = hipMalloc((void**)&d_trainCoordinates, numPointsTrain * sizeof(Coordinates));
	checkError(hipError_t, "hipMalloc - d_trainCoordinates");
	hipError_t = hipMemcpy(d_trainCoordinates, h_trainCoordinates, numPointsTrain * sizeof(Coordinates), hipMemcpyHostToDevice);
	checkError(hipError_t, "hipMemcpyAsync - d_trainCoordinates");
	
	chrono::high_resolution_clock::time_point kernel_start = chrono::high_resolution_clock::now();
	for (usint stream = 0; stream < streamCount; stream++) {
		// 1 - create stream
		hipStreamCreate(&streams[stream]);

		// 2 - Host memory - allocate memory on host for results and test coordinates
		hipError_t = hipHostMalloc((void**)&h_nearestNeighbors[stream], (numPointsTest / streamCount) * sizeof(usint), hipHostMallocDefault);
		checkError(hipError_t, "cudamallochost - h_nearestneighbors");
		hipError_t = hipHostMalloc((void**)&h_testCoordinates[stream], (numPointsTest / streamCount) * sizeof(Coordinates), hipHostMallocDefault);
		checkError(hipError_t, "cudamallochost - h_testCoordinates");

		// 3 - Host memory - read test points
		for (int i = 0; i < numPointsTest / streamCount; i++) {
			fscanf(test_is, "%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d", &h_testCoordinates[stream][i].points[0], &h_testCoordinates[stream][i].points[1], &h_testCoordinates[stream][i].points[2],
				&h_testCoordinates[stream][i].points[3], &h_testCoordinates[stream][i].points[4], &h_testCoordinates[stream][i].points[5], &h_testCoordinates[stream][i].points[6], &h_testCoordinates[stream][i].points[7],
				&h_testCoordinates[stream][i].points[8], &h_testCoordinates[stream][i].points[9], &h_testCoordinates[stream][i].points[10], &h_testCoordinates[stream][i].points[11], &h_testCoordinates[stream][i].points[12],
				&h_testCoordinates[stream][i].points[13], &h_testCoordinates[stream][i].points[14], &h_testCoordinates[stream][i].points[15]);
		}
		
		// 4 - Device memory - allocate space for test coordiantes and result array for this stream to write its results to
		hipError_t = hipMalloc((void**)&d_testCoordinates[stream], (numPointsTest / streamCount) * sizeof(Coordinates));
		checkError(hipError_t, "hipMalloc - d_testCoordiantes");
		hipError_t = hipMalloc((void**)&d_nearestNeighbors[stream], (numPointsTest / streamCount) * sizeof(usint));
		checkError(hipError_t, "hipMalloc - d_nearestNeighbors");
	
		// 5 - copy test coordinates to device in async
		temp = chrono::high_resolution_clock::now();
		hipError_t = hipMemcpyAsync(d_testCoordinates[stream], h_testCoordinates[stream], (numPointsTest / streamCount) * sizeof(Coordinates), hipMemcpyHostToDevice, streams[stream]);
		checkError(hipError_t, "hipMemcpy - d_testCoordinates");
		end = chrono::high_resolution_clock::now();
		cout << "data copied to device memory [" << chrono::duration_cast<chrono::milliseconds>(end - temp).count() << " ms]\n"
			<< "executing kernel with " << numBlocks << " blocks with " << numThreadsPerBlock << " threads each" << endl;
		
		// 6 - Inovke kernel for current stream
		usint *& currentResultArray = d_nearestNeighbors[stream];
		nearestNeighbor<<< numBlocks, numThreadsPerBlock, 0, streams[stream] >>>(d_trainCoordinates, d_testCoordinates[stream], numPointsTest / streamCount, numPointsTrain, currentResultArray);
		hipError_t = hipMemcpyAsync(h_nearestNeighbors[stream], d_nearestNeighbors[stream], (numPointsTest / streamCount) * sizeof(usint), hipMemcpyDeviceToHost, streams[stream]);
		checkError(hipError_t, "hipMemcpy - h_nearestNeighbors");
	}
	
	// Wait for GPU to terminate and fetch results
	hipError_t = hipGetLastError();
	checkError(hipError_t, "before deviceSync() error!");
	hipDeviceSynchronize();
	end = chrono::high_resolution_clock::now();
	cout << "Computation + read test data: " << chrono::duration_cast<chrono::milliseconds>(end - kernel_start).count() << " ms\n";
	cout << "\t--------------------\n";
	end = chrono::high_resolution_clock::now();
	
	ofstream os("output.txt");
	
	for (int stream = 0; stream < streamCount; stream++) {
		for (int i = 0; i < numPointsTest / streamCount; i++) {
			os << h_nearestNeighbors[stream][i] << endl;
		}
	}
	
	end = chrono::high_resolution_clock::now();
	cout << "\t--------------------\nTotal time: " << chrono::duration_cast<chrono::milliseconds>(end - begin).count() << " ms\nterminating\n";
	
	return 0;
}

	.text
	.file	"source2.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z30__device_stub__nearestNeighborP11CoordinatesS0_ttPt # -- Begin function _Z30__device_stub__nearestNeighborP11CoordinatesS0_ttPt
	.type	_Z30__device_stub__nearestNeighborP11CoordinatesS0_ttPt,@function
_Z30__device_stub__nearestNeighborP11CoordinatesS0_ttPt: # @_Z30__device_stub__nearestNeighborP11CoordinatesS0_ttPt
	.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	6(%rsp), %rsi
	movw	%dx, (%rsi)
	leaq	4(%rsp), %rdx
	movw	%cx, (%rdx)
	leaq	24(%rsp), %rcx
	movq	%r8, (%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	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	$_Z15nearestNeighborP11CoordinatesS0_ttPt, %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	_Z30__device_stub__nearestNeighborP11CoordinatesS0_ttPt, .Lfunc_end0-_Z30__device_stub__nearestNeighborP11CoordinatesS0_ttPt
	.cfi_endproc
                                        # -- End function
	.globl	_Z10checkErrorRK10hipError_tPKc # -- Begin function _Z10checkErrorRK10hipError_tPKc
	.type	_Z10checkErrorRK10hipError_tPKc,@function
_Z10checkErrorRK10hipError_tPKc:        # @_Z10checkErrorRK10hipError_tPKc
	.cfi_startproc
# %bb.0:
	cmpl	$0, (%rdi)
	jne	.LBB1_2
# %bb.1:
	movb	$1, %al
	retq
.LBB1_2:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	movl	(%rbx), %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSolsEi
	movq	%rax, %rdi
	callq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z10checkErrorRK10hipError_tPKc, .Lfunc_end1-_Z10checkErrorRK10hipError_tPKc
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %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	$792, %rsp                      # imm = 0x318
	.cfi_def_cfa_offset 848
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.1, %edi
	movl	$.L.str.3, %esi
	callq	fopen
	movq	%rax, %rbx
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.2, %edi
	movl	$.L.str.3, %esi
	callq	fopen
	testq	%rbx, %rbx
	je	.LBB2_1
# %bb.3:
	movq	%rax, %r14
	testq	%rax, %rax
	je	.LBB2_4
# %bb.5:
	movq	%rbx, 96(%rsp)                  # 8-byte Spill
	.cfi_escape 0x2e, 0x00
	xorl	%edi, %edi
	callq	hipSetDevice
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cout, %edi
	movl	$.L.str.5, %esi
	movl	$22, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	.cfi_escape 0x2e, 0x00
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	movq	%rax, 72(%rsp)                  # 8-byte Spill
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cout, %edi
	movl	$.L.str.6, %esi
	movl	$28, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cout, %edi
	movl	$2, %esi
	callq	_ZNSo9_M_insertImEERSoT_
	movq	%rax, %r15
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.7, %esi
	movl	$13, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	.cfi_escape 0x2e, 0x00
	movl	$128, %esi
	movq	%r15, %rdi
	callq	_ZNSo9_M_insertImEERSoT_
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.8, %esi
	movl	$14, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	.cfi_escape 0x2e, 0x00
	leaq	64(%rsp), %rdi
	movl	$608000, %esi                   # imm = 0x94700
	xorl	%edx, %edx
	callq	hipHostMalloc
	leaq	12(%rsp), %rdi
	movl	%eax, (%rdi)
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.9, %esi
	callq	_Z10checkErrorRK10hipError_tPKc
	movl	$30, %r15d
.LBB2_6:                                # =>This Inner Loop Header: Depth=1
	movq	64(%rsp), %rax
	leaq	(%rax,%r15), %rbp
	leaq	(%rax,%r15), %rdx
	addq	$-30, %rdx
	leaq	-28(%rax,%r15), %rcx
	leaq	-26(%rax,%r15), %r8
	leaq	-24(%rax,%r15), %r9
	leaq	-22(%rax,%r15), %rsi
	movq	%rsi, 56(%rsp)                  # 8-byte Spill
	leaq	-20(%rax,%r15), %rsi
	movq	%rsi, 48(%rsp)                  # 8-byte Spill
	leaq	-18(%rax,%r15), %rsi
	movq	%rsi, 40(%rsp)                  # 8-byte Spill
	leaq	-16(%rax,%r15), %rsi
	movq	%rsi, 32(%rsp)                  # 8-byte Spill
	leaq	-14(%rax,%r15), %rsi
	movq	%rsi, 24(%rsp)                  # 8-byte Spill
	leaq	-12(%rax,%r15), %rsi
	movq	%rsi, 16(%rsp)                  # 8-byte Spill
	leaq	-10(%rax,%r15), %r11
	leaq	-8(%rax,%r15), %r12
	leaq	-6(%rax,%r15), %r13
	leaq	-4(%rax,%r15), %rbx
	leaq	-2(%rax,%r15), %r10
	.cfi_escape 0x2e, 0x60
	movl	$.L.str.10, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	pushq	%rbp
	.cfi_adjust_cfa_offset 8
	pushq	%r10
	.cfi_adjust_cfa_offset 8
	pushq	%rbx
	.cfi_adjust_cfa_offset 8
	pushq	%r13
	.cfi_adjust_cfa_offset 8
	pushq	%r12
	.cfi_adjust_cfa_offset 8
	pushq	%r11
	.cfi_adjust_cfa_offset 8
	pushq	64(%rsp)                        # 8-byte Folded Reload
	.cfi_adjust_cfa_offset 8
	pushq	80(%rsp)                        # 8-byte Folded Reload
	.cfi_adjust_cfa_offset 8
	pushq	96(%rsp)                        # 8-byte Folded Reload
	.cfi_adjust_cfa_offset 8
	pushq	112(%rsp)                       # 8-byte Folded Reload
	.cfi_adjust_cfa_offset 8
	pushq	128(%rsp)                       # 8-byte Folded Reload
	.cfi_adjust_cfa_offset 8
	pushq	144(%rsp)                       # 8-byte Folded Reload
	.cfi_adjust_cfa_offset 8
	callq	__isoc23_fscanf
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	addq	$32, %r15
	cmpq	$608030, %r15                   # imm = 0x9471E
	jne	.LBB2_6
# %bb.7:
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cout, %ebx
	movl	$_ZSt4cout, %edi
	movl	$.L.str.11, %esi
	movl	$55, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	.cfi_escape 0x2e, 0x00
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	.cfi_escape 0x2e, 0x00
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	.cfi_escape 0x2e, 0x00
	leaq	104(%rsp), %rbx
	movl	$608000, %esi                   # imm = 0x94700
	movq	%rbx, %rdi
	callq	hipMalloc
	leaq	12(%rsp), %r14
	movl	%eax, (%r14)
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.12, %esi
	movq	%r14, %rdi
	callq	_Z10checkErrorRK10hipError_tPKc
	movq	(%rbx), %rdi
	movq	64(%rsp), %rsi
	.cfi_escape 0x2e, 0x00
	movl	$608000, %edx                   # imm = 0x94700
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, (%r14)
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.13, %esi
	movq	%r14, %rdi
	callq	_Z10checkErrorRK10hipError_tPKc
	.cfi_escape 0x2e, 0x00
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	xorl	%r12d, %r12d
.LBB2_8:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_9 Depth 2
	leaq	(%rsp,%r12,8), %rdi
	addq	$112, %rdi
	.cfi_escape 0x2e, 0x00
	callq	hipStreamCreate
	leaq	(%rsp,%r12,8), %rdi
	addq	$144, %rdi
	.cfi_escape 0x2e, 0x00
	movl	$500, %esi                      # imm = 0x1F4
	xorl	%edx, %edx
	callq	hipHostMalloc
	movl	%eax, 12(%rsp)
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.14, %esi
	movq	%r14, %rdi
	callq	_Z10checkErrorRK10hipError_tPKc
	movq	%r12, 88(%rsp)                  # 8-byte Spill
	leaq	(%rsp,%r12,8), %rdi
	addq	$176, %rdi
	.cfi_escape 0x2e, 0x00
	movl	$8000, %esi                     # imm = 0x1F40
	movq	%rdi, 16(%rsp)                  # 8-byte Spill
	xorl	%edx, %edx
	callq	hipHostMalloc
	movl	%eax, 12(%rsp)
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.15, %esi
	movq	%r14, %rdi
	callq	_Z10checkErrorRK10hipError_tPKc
	movl	$30, %r15d
.LBB2_9:                                #   Parent Loop BB2_8 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	16(%rsp), %rax                  # 8-byte Reload
	movq	(%rax), %rax
	leaq	(%rax,%r15), %r12
	leaq	(%rax,%r15), %rdx
	addq	$-30, %rdx
	leaq	-28(%rax,%r15), %rcx
	leaq	-26(%rax,%r15), %r8
	leaq	-24(%rax,%r15), %r9
	leaq	-22(%rax,%r15), %rsi
	movq	%rsi, 56(%rsp)                  # 8-byte Spill
	leaq	-20(%rax,%r15), %rsi
	movq	%rsi, 48(%rsp)                  # 8-byte Spill
	leaq	-18(%rax,%r15), %rsi
	movq	%rsi, 40(%rsp)                  # 8-byte Spill
	leaq	-16(%rax,%r15), %rsi
	movq	%rsi, 32(%rsp)                  # 8-byte Spill
	leaq	-14(%rax,%r15), %rsi
	movq	%rsi, 24(%rsp)                  # 8-byte Spill
	leaq	-12(%rax,%r15), %rbx
	leaq	-10(%rax,%r15), %r10
	leaq	-8(%rax,%r15), %r11
	leaq	-6(%rax,%r15), %r13
	leaq	-4(%rax,%r15), %rbp
	leaq	-2(%rax,%r15), %r14
	.cfi_escape 0x2e, 0x60
	movl	$.L.str.16, %esi
	movq	96(%rsp), %rdi                  # 8-byte Reload
	xorl	%eax, %eax
	pushq	%r12
	.cfi_adjust_cfa_offset 8
	pushq	%r14
	.cfi_adjust_cfa_offset 8
	pushq	%rbp
	.cfi_adjust_cfa_offset 8
	pushq	%r13
	.cfi_adjust_cfa_offset 8
	pushq	%r11
	.cfi_adjust_cfa_offset 8
	pushq	%r10
	.cfi_adjust_cfa_offset 8
	pushq	%rbx
	.cfi_adjust_cfa_offset 8
	pushq	80(%rsp)                        # 8-byte Folded Reload
	.cfi_adjust_cfa_offset 8
	pushq	96(%rsp)                        # 8-byte Folded Reload
	.cfi_adjust_cfa_offset 8
	pushq	112(%rsp)                       # 8-byte Folded Reload
	.cfi_adjust_cfa_offset 8
	pushq	128(%rsp)                       # 8-byte Folded Reload
	.cfi_adjust_cfa_offset 8
	pushq	144(%rsp)                       # 8-byte Folded Reload
	.cfi_adjust_cfa_offset 8
	callq	__isoc23_fscanf
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	addq	$32, %r15
	cmpq	$8030, %r15                     # imm = 0x1F5E
	jne	.LBB2_9
# %bb.10:                               #   in Loop: Header=BB2_8 Depth=1
	movq	88(%rsp), %r12                  # 8-byte Reload
	leaq	(%rsp,%r12,8), %rbp
	addq	$240, %rbp
	.cfi_escape 0x2e, 0x00
	movl	$8000, %esi                     # imm = 0x1F40
	movq	%rbp, %rdi
	callq	hipMalloc
	movl	%eax, 12(%rsp)
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.17, %esi
	leaq	12(%rsp), %r14
	movq	%r14, %rdi
	callq	_Z10checkErrorRK10hipError_tPKc
	leaq	(%rsp,%r12,8), %rbx
	addq	$208, %rbx
	.cfi_escape 0x2e, 0x00
	movl	$500, %esi                      # imm = 0x1F4
	movq	%rbx, %rdi
	callq	hipMalloc
	movl	%eax, 12(%rsp)
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.18, %esi
	movq	%r14, %rdi
	callq	_Z10checkErrorRK10hipError_tPKc
	.cfi_escape 0x2e, 0x00
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	movq	%rax, %r13
	movq	(%rbp), %rdi
	movq	176(%rsp,%r12,8), %rsi
	movq	112(%rsp,%r12,8), %r8
	.cfi_escape 0x2e, 0x00
	movl	$8000, %edx                     # imm = 0x1F40
	movl	$1, %ecx
	callq	hipMemcpyAsync
	movl	%eax, 12(%rsp)
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.19, %esi
	movq	%r14, %rdi
	callq	_Z10checkErrorRK10hipError_tPKc
	.cfi_escape 0x2e, 0x00
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	movq	%rax, %r15
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cout, %edi
	movl	$.L.str.20, %esi
	movl	$30, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	subq	%r13, %r15
	movq	%r15, %rax
	movabsq	$4835703278458516699, %rcx      # imm = 0x431BDE82D7B634DB
	imulq	%rcx
	movq	%rdx, %rsi
	shrq	$63, %rsi
	sarq	$18, %rdx
	addq	%rdx, %rsi
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertIlEERSoT_
	movq	%rax, %r15
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.21, %esi
	movl	$5, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.22, %esi
	movl	$22, %edx
	movq	%r15, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	.cfi_escape 0x2e, 0x00
	movl	$2, %esi
	movq	%r15, %rdi
	callq	_ZNSo9_M_insertImEERSoT_
	movq	%rax, %r15
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.7, %esi
	movl	$13, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	.cfi_escape 0x2e, 0x00
	movl	$128, %esi
	movq	%r15, %rdi
	callq	_ZNSo9_M_insertImEERSoT_
	movq	%rax, %r15
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.23, %esi
	movl	$13, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	(%r15), %rax
	movq	-24(%rax), %rdi
	addq	%r15, %rdi
	.cfi_escape 0x2e, 0x00
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	.cfi_escape 0x2e, 0x00
	movq	%r15, %rdi
	callq	_ZNSo3putEc
	.cfi_escape 0x2e, 0x00
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movq	112(%rsp,%r12,8), %r9
	movabsq	$4294967298, %rdi               # imm = 0x100000002
	leaq	126(%rdi), %rdx
	.cfi_escape 0x2e, 0x00
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_12
# %bb.11:                               #   in Loop: Header=BB2_8 Depth=1
	movq	104(%rsp), %rdi
	movq	(%rbp), %rsi
	movq	(%rbx), %r8
	.cfi_escape 0x2e, 0x00
	movl	$250, %edx
	movl	$19000, %ecx                    # imm = 0x4A38
	callq	_Z30__device_stub__nearestNeighborP11CoordinatesS0_ttPt
.LBB2_12:                               #   in Loop: Header=BB2_8 Depth=1
	movq	144(%rsp,%r12,8), %rdi
	movq	208(%rsp,%r12,8), %rsi
	movq	112(%rsp,%r12,8), %r8
	.cfi_escape 0x2e, 0x00
	movl	$500, %edx                      # imm = 0x1F4
	movl	$2, %ecx
	callq	hipMemcpyAsync
	movl	%eax, 12(%rsp)
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.24, %esi
	movq	%r14, %rdi
	callq	_Z10checkErrorRK10hipError_tPKc
	incq	%r12
	cmpq	$4, %r12
	jne	.LBB2_8
# %bb.13:
	.cfi_escape 0x2e, 0x00
	callq	hipGetLastError
	leaq	12(%rsp), %rdi
	movl	%eax, (%rdi)
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.25, %esi
	callq	_Z10checkErrorRK10hipError_tPKc
	.cfi_escape 0x2e, 0x00
	callq	hipDeviceSynchronize
	.cfi_escape 0x2e, 0x00
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	movq	%rax, %rbx
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cout, %edi
	movl	$.L.str.26, %esi
	movl	$30, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	subq	80(%rsp), %rbx                  # 8-byte Folded Reload
	movq	%rbx, %rax
	movabsq	$4835703278458516699, %rcx      # imm = 0x431BDE82D7B634DB
	imulq	%rcx
	movq	%rdx, %rsi
	shrq	$63, %rsi
	sarq	$18, %rdx
	addq	%rdx, %rsi
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertIlEERSoT_
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.27, %esi
	movl	$4, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cout, %edi
	movl	$.L.str.5, %esi
	movl	$22, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	.cfi_escape 0x2e, 0x00
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	.cfi_escape 0x2e, 0x00
	leaq	280(%rsp), %rbx
	movl	$.L.str.28, %esi
	movq	%rbx, %rdi
	movl	$16, %edx
	callq	_ZNSt14basic_ofstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode
	xorl	%r15d, %r15d
.LBB2_14:                               # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_15 Depth 2
	xorl	%r12d, %r12d
.LBB2_15:                               #   Parent Loop BB2_14 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	144(%rsp,%r15,8), %rax
	movzwl	(%rax,%r12,2), %esi
.Ltmp0:
	.cfi_escape 0x2e, 0x00
	movq	%rbx, %rdi
	callq	_ZNSo9_M_insertImEERSoT_
.Ltmp1:
# %bb.16:                               # %_ZNSolsEt.exit
                                        #   in Loop: Header=BB2_15 Depth=2
	movq	%rax, %r14
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%r14, %rdi
.Ltmp2:
	.cfi_escape 0x2e, 0x00
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
.Ltmp3:
# %bb.17:                               # %.noexc
                                        #   in Loop: Header=BB2_15 Depth=2
.Ltmp4:
	.cfi_escape 0x2e, 0x00
	movsbl	%al, %esi
	movq	%r14, %rdi
	callq	_ZNSo3putEc
.Ltmp5:
# %bb.18:                               # %.noexc115
                                        #   in Loop: Header=BB2_15 Depth=2
.Ltmp6:
	.cfi_escape 0x2e, 0x00
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
.Ltmp7:
# %bb.19:                               # %_ZNSolsEPFRSoS_E.exit
                                        #   in Loop: Header=BB2_15 Depth=2
	incq	%r12
	cmpq	$250, %r12
	jne	.LBB2_15
# %bb.20:                               #   in Loop: Header=BB2_14 Depth=1
	incq	%r15
	cmpq	$4, %r15
	jne	.LBB2_14
# %bb.21:
	.cfi_escape 0x2e, 0x00
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	movq	%rax, %rbx
.Ltmp9:
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cout, %edi
	movl	$.L.str.29, %esi
	movl	$34, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp10:
# %bb.22:
	subq	72(%rsp), %rbx                  # 8-byte Folded Reload
	movq	%rbx, %rax
	movabsq	$4835703278458516699, %rcx      # imm = 0x431BDE82D7B634DB
	imulq	%rcx
	movq	%rdx, %rsi
	shrq	$63, %rsi
	sarq	$18, %rdx
	addq	%rdx, %rsi
.Ltmp12:
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertIlEERSoT_
.Ltmp13:
# %bb.23:                               # %_ZNSolsEl.exit
.Ltmp14:
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.30, %esi
	movl	$16, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp15:
# %bb.24:                               # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit114
	movq	_ZTTSt14basic_ofstreamIcSt11char_traitsIcEE(%rip), %rax
	leaq	288(%rsp), %rdi
	movq	%rax, -8(%rdi)
	movq	_ZTTSt14basic_ofstreamIcSt11char_traitsIcEE+24(%rip), %rcx
	movq	-24(%rax), %rax
	movq	%rcx, 280(%rsp,%rax)
	.cfi_escape 0x2e, 0x00
	callq	_ZNSt13basic_filebufIcSt11char_traitsIcEED2Ev
	leaq	528(%rsp), %rdi
	.cfi_escape 0x2e, 0x00
	callq	_ZNSt8ios_baseD2Ev
	xorl	%eax, %eax
	addq	$792, %rsp                      # imm = 0x318
	.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
.LBB2_1:
	.cfi_def_cfa_offset 848
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cerr, %edi
	movl	$.L.str.4, %esi
	callq	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.1, %esi
	jmp	.LBB2_2
.LBB2_4:
	.cfi_escape 0x2e, 0x00
	movl	$_ZSt4cerr, %edi
	movl	$.L.str.4, %esi
	callq	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
	.cfi_escape 0x2e, 0x00
	movl	$.L.str.2, %esi
.LBB2_2:
	movq	%rax, %rdi
	callq	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
	.cfi_escape 0x2e, 0x00
	movq	%rax, %rdi
	callq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
	.cfi_escape 0x2e, 0x00
	movl	$1, %edi
	callq	exit
.LBB2_25:
.Ltmp11:
	jmp	.LBB2_27
.LBB2_26:
.Ltmp16:
	jmp	.LBB2_27
.LBB2_28:
.Ltmp8:
.LBB2_27:
	movq	%rax, %rbx
	.cfi_escape 0x2e, 0x00
	leaq	280(%rsp), %rdi
	callq	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev
	.cfi_escape 0x2e, 0x00
	movq	%rbx, %rdi
	callq	_Unwind_Resume@PLT
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table2:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	255                             # @TType Encoding = omit
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Lfunc_begin0-.Lfunc_begin0    # >> Call Site 1 <<
	.uleb128 .Ltmp0-.Lfunc_begin0           #   Call between .Lfunc_begin0 and .Ltmp0
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 2 <<
	.uleb128 .Ltmp7-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp7
	.uleb128 .Ltmp8-.Lfunc_begin0           #     jumps to .Ltmp8
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp9-.Lfunc_begin0           # >> Call Site 3 <<
	.uleb128 .Ltmp10-.Ltmp9                 #   Call between .Ltmp9 and .Ltmp10
	.uleb128 .Ltmp11-.Lfunc_begin0          #     jumps to .Ltmp11
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp12-.Lfunc_begin0          # >> Call Site 4 <<
	.uleb128 .Ltmp15-.Ltmp12                #   Call between .Ltmp12 and .Ltmp15
	.uleb128 .Ltmp16-.Lfunc_begin0          #     jumps to .Ltmp16
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp15-.Lfunc_begin0          # >> Call Site 5 <<
	.uleb128 .Lfunc_end2-.Ltmp15            #   Call between .Ltmp15 and .Lfunc_end2
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
.Lcst_end0:
	.p2align	2, 0x0
                                        # -- End function
	.text
	.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	$_Z15nearestNeighborP11CoordinatesS0_ttPt, %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	_Z15nearestNeighborP11CoordinatesS0_ttPt,@object # @_Z15nearestNeighborP11CoordinatesS0_ttPt
	.section	.rodata,"a",@progbits
	.globl	_Z15nearestNeighborP11CoordinatesS0_ttPt
	.p2align	3, 0x0
_Z15nearestNeighborP11CoordinatesS0_ttPt:
	.quad	_Z30__device_stub__nearestNeighborP11CoordinatesS0_ttPt
	.size	_Z15nearestNeighborP11CoordinatesS0_ttPt, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"CUDA ERROR: %s\n"
	.size	.L.str, 16

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"test.txt"
	.size	.L.str.1, 9

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"train.txt"
	.size	.L.str.2, 10

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

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Cannot open "
	.size	.L.str.4, 13

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"\t--------------------\n"
	.size	.L.str.5, 23

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Kernels will be called with "
	.size	.L.str.6, 29

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	" blocks with "
	.size	.L.str.7, 14

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	" threads each\n"
	.size	.L.str.8, 15

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"cudamallochost - h_trainCoordinates"
	.size	.L.str.9, 36

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n"
	.size	.L.str.10, 64

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"done reading training coordinates to host pinned memory"
	.size	.L.str.11, 56

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"hipMalloc - d_trainCoordinates"
	.size	.L.str.12, 31

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"hipMemcpyAsync - d_trainCoordinates"
	.size	.L.str.13, 36

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"cudamallochost - h_nearestneighbors"
	.size	.L.str.14, 36

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"cudamallochost - h_testCoordinates"
	.size	.L.str.15, 35

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d"
	.size	.L.str.16, 63

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"hipMalloc - d_testCoordiantes"
	.size	.L.str.17, 30

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"hipMalloc - d_nearestNeighbors"
	.size	.L.str.18, 31

	.type	.L.str.19,@object               # @.str.19
.L.str.19:
	.asciz	"hipMemcpy - d_testCoordinates"
	.size	.L.str.19, 30

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"data copied to device memory ["
	.size	.L.str.20, 31

	.type	.L.str.21,@object               # @.str.21
.L.str.21:
	.asciz	" ms]\n"
	.size	.L.str.21, 6

	.type	.L.str.22,@object               # @.str.22
.L.str.22:
	.asciz	"executing kernel with "
	.size	.L.str.22, 23

	.type	.L.str.23,@object               # @.str.23
.L.str.23:
	.asciz	" threads each"
	.size	.L.str.23, 14

	.type	.L.str.24,@object               # @.str.24
.L.str.24:
	.asciz	"hipMemcpy - h_nearestNeighbors"
	.size	.L.str.24, 31

	.type	.L.str.25,@object               # @.str.25
.L.str.25:
	.asciz	"before deviceSync() error!"
	.size	.L.str.25, 27

	.type	.L.str.26,@object               # @.str.26
.L.str.26:
	.asciz	"Computation + read test data: "
	.size	.L.str.26, 31

	.type	.L.str.27,@object               # @.str.27
.L.str.27:
	.asciz	" ms\n"
	.size	.L.str.27, 5

	.type	.L.str.28,@object               # @.str.28
.L.str.28:
	.asciz	"output.txt"
	.size	.L.str.28, 11

	.type	.L.str.29,@object               # @.str.29
.L.str.29:
	.asciz	"\t--------------------\nTotal time: "
	.size	.L.str.29, 35

	.type	.L.str.30,@object               # @.str.30
.L.str.30:
	.asciz	" ms\nterminating\n"
	.size	.L.str.30, 17

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z15nearestNeighborP11CoordinatesS0_ttPt"
	.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 _Z30__device_stub__nearestNeighborP11CoordinatesS0_ttPt
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Unwind_Resume
	.addrsig_sym _Z15nearestNeighborP11CoordinatesS0_ttPt
	.addrsig_sym _ZSt4cout
	.addrsig_sym _ZSt4cerr
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z15nearestNeighborP11CoordinatesS0_ttPt ; -- Begin function _Z15nearestNeighborP11CoordinatesS0_ttPt
	.globl	_Z15nearestNeighborP11CoordinatesS0_ttPt
	.p2align	8
	.type	_Z15nearestNeighborP11CoordinatesS0_ttPt,@function
_Z15nearestNeighborP11CoordinatesS0_ttPt: ; @_Z15nearestNeighborP11CoordinatesS0_ttPt
; %bb.0:
	s_clause 0x1
	s_load_b32 s3, s[0:1], 0x2c
	s_load_b32 s2, s[0:1], 0x10
	s_waitcnt lgkmcnt(0)
	v_mad_u64_u32 v[1:2], null, s15, s3, v[0:1]
	s_and_b32 s3, s2, 0xffff
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_and_b32_e32 v0, 0xffff, v1
	v_cmp_gt_u32_e32 vcc_lo, s3, v0
	s_and_saveexec_b32 s3, vcc_lo
	s_cbranch_execz .LBB0_7
; %bb.1:                                ; %.preheader
	v_mov_b32_e32 v1, 0
	s_lshr_b32 s8, s2, 16
	s_mov_b32 s9, 0
	s_cmp_eq_u32 s8, 0
	s_cbranch_scc1 .LBB0_6
; %bb.2:                                ; %.lr.ph
	s_load_b128 s[4:7], s[0:1], 0x0
	v_dual_mov_b32 v2, 0 :: v_dual_lshlrev_b32 v1, 5, v0
	v_mov_b32_e32 v3, -1
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v4, s2, s6, v1
	v_add_co_ci_u32_e64 v5, null, s7, 0, s2
	v_mov_b32_e32 v1, 0
.LBB0_3:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_4 Depth 2
	v_mov_b32_e32 v6, 0
	s_mov_b64 s[2:3], 0
.LBB0_4:                                ;   Parent Loop BB0_3 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(SALU_CYCLE_1)
	v_add_co_u32 v7, vcc_lo, v4, s2
	v_add_co_ci_u32_e32 v8, vcc_lo, s3, v5, vcc_lo
	s_add_u32 s6, s4, s2
	s_addc_u32 s7, s5, s3
	global_load_u16 v9, v2, s[6:7]
	global_load_u16 v7, v[7:8], off
	s_add_u32 s2, s2, 2
	s_addc_u32 s3, s3, 0
	s_cmp_lg_u32 s2, 32
	s_waitcnt vmcnt(0)
	v_sub_nc_u32_e32 v7, v9, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_i32_i24_e32 v7, v7, v7
	v_cvt_f32_i32_e32 v7, v7
	s_delay_alu instid0(VALU_DEP_1)
	v_add_f32_e32 v6, v6, v7
	s_cbranch_scc1 .LBB0_4
; %bb.5:                                ; %_Z11getDistanceRK11CoordinatesS1_.exit
                                        ;   in Loop: Header=BB0_3 Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v7, 0x4f800000, v6
	v_cmp_gt_f32_e32 vcc_lo, 0xf800000, v6
	v_cndmask_b32_e32 v6, v6, v7, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_2)
	v_sqrt_f32_e32 v7, v6
	s_waitcnt_depctr 0xfff
	v_add_nc_u32_e32 v8, -1, v7
	v_add_nc_u32_e32 v9, 1, v7
	v_fma_f32 v10, -v8, v7, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v11, -v9, v7, v6
	v_cmp_ge_f32_e64 s2, 0, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cndmask_b32_e64 v7, v7, v8, s2
	v_cmp_lt_f32_e64 s2, 0, v11
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e64 v7, v7, v9, s2
	v_mul_f32_e32 v8, 0x37800000, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_dual_cndmask_b32 v7, v7, v8 :: v_dual_and_b32 v8, 0xffff, v3
	v_cmp_class_f32_e64 vcc_lo, v6, 0x260
	v_cndmask_b32_e32 v6, v7, v6, vcc_lo
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cvt_f32_u32_e32 v7, v8
	v_cvt_u32_f32_e32 v8, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_lt_f32_e32 vcc_lo, v6, v7
	v_cndmask_b32_e32 v3, v3, v8, vcc_lo
	v_cndmask_b32_e64 v1, v1, s9, vcc_lo
	s_add_i32 s9, s9, 1
	s_add_u32 s4, s4, 32
	s_addc_u32 s5, s5, 0
	s_cmp_eq_u32 s9, s8
	s_cbranch_scc0 .LBB0_3
.LBB0_6:                                ; %Flow36
	s_load_b64 s[0:1], s[0:1], 0x18
	v_lshlrev_b32_e32 v0, 1, v0
	s_waitcnt lgkmcnt(0)
	global_store_b16 v0, v1, s[0:1]
.LBB0_7:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z15nearestNeighborP11CoordinatesS0_ttPt
		.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 12
		.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	_Z15nearestNeighborP11CoordinatesS0_ttPt, .Lfunc_end0-_Z15nearestNeighborP11CoordinatesS0_ttPt
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 456
; NumSgprs: 18
; NumVgprs: 12
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 12
; 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 __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:           2
        .value_kind:     by_value
      - .offset:         18
        .size:           2
        .value_kind:     by_value
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z15nearestNeighborP11CoordinatesS0_ttPt
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z15nearestNeighborP11CoordinatesS0_ttPt.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     12
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
