#include <iostream>
#include <numeric>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cuda.h>
#include <curand.h>
#include <math.h>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <ctime>

static void CheckCudaErrorAux (const char *, unsigned, const char *, cudaError_t);
void sequentialKMeans(int N, int K, int EPS, float*devData);

#define N 80000
#define K 200
#define EPS 0.000001

#define CUDA_CHECK_RETURN(value) CheckCudaErrorAux(__FILE__,__LINE__, #value, value)
#define CUDA_CALL(x) do { if((x)!=cudaSuccess) { printf("Error at %s:%d\n",__FILE__,__LINE__);return EXIT_FAILURE;}} while(0)
#define CURAND_CALL(x) do { if((x)!=CURAND_STATUS_SUCCESS) {printf("Error at %s:%d\n",__FILE__,__LINE__); return EXIT_FAILURE;}} while(0)
using namespace std;


int init(float *vectorsDev){
	curandGenerator_t gen;
	CURAND_CALL(curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_DEFAULT));
	CURAND_CALL(curandSetPseudoRandomGeneratorSeed(gen, 234ULL));
	CURAND_CALL(curandGenerateUniform(gen, vectorsDev, 2*N));
	CURAND_CALL(curandDestroyGenerator(gen));
	return 0;
}

int main(int argc, char* argv[]){
	std::clock_t start;
	double duration;

	int iter = 0;
	float *devData, *hostData, *mean;
	bool stopCriterion = false;

	short *clusters;
	float oldMeanX, oldMeanY;

	hostData = (float*) malloc(2*N*sizeof(float));
	mean = (float*) malloc(2*K*sizeof(float));
	clusters = (short*) malloc(N*sizeof(short));


	CUDA_CALL(cudaMalloc((float **)&devData, 2*N*sizeof(float)));
	init(devData);
	CUDA_CHECK_RETURN(cudaMemcpy(hostData, devData, 2*N*sizeof(float), cudaMemcpyDeviceToHost));
	cudaDeviceSynchronize();

	memcpy(mean, hostData, 2*K*sizeof(float));

	start = std::clock();
	while (!stopCriterion) {
		stopCriterion = true;
		/*
		 * distanze e minimo
		 */
		for (int v = 0; v < N; v++) {
			float minDistance = 3.0;
			short minIndex = -1;
			float distance = 0;
			for (int c = 0; c < K; c++) {
				distance = pow((hostData[2*v] - mean[2*c]),2)+pow((hostData[2*v+1] - mean[2*c+1]),2);

				if (distance < minDistance) {
					minIndex = c;
					minDistance = distance;
				}

			}
			clusters[v] = minIndex;
		}
		/*
		 * nuova media
		 */
		for (int i = 0; i < K; i++) {
			int numComponents = 0;
			float *arraySum = (float*)calloc(2, sizeof(float));
			for (int j = 0; j < N; j++) {
				if (clusters[j] == i) {
					numComponents++;
					arraySum[0] += hostData[2*j];
					arraySum[1] += hostData[2*j+1];

				}
			}
			oldMeanX = mean[2*i];
			mean[2*i] = arraySum[0] / numComponents;
			oldMeanY = mean[2*i+1];
			mean[2*i+1] = arraySum[1] / numComponents;
			if(abs(mean[2*i] - oldMeanX) > EPS || abs(mean[2*i+1] - oldMeanY) > EPS){
				stopCriterion = false;
			}
		}

		iter++;
	}
	printf("Numero di iterazioni: %d\r\n", iter);
	duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
	std::cout<<"durata: "<< duration <<'\n';

	free(hostData);
	free(mean);
	free(clusters);

	CUDA_CHECK_RETURN(cudaFree(devData));

	return EXIT_SUCCESS;
}

/**
 * Check the return value of the CUDA runtime API call and exit
 * the application if the call has failed.
 */
static void CheckCudaErrorAux (const char *file, unsigned line, const char *statement, cudaError_t err)
{
	if (err == cudaSuccess)
		return;
	std::cerr << statement<<" returned " << cudaGetErrorString(err) << "("<<err<< ") at "<<file<<":"<<line << std::endl;
	exit (1);
}


	.file	"tmpxft_0025a804_00000000-6_KMeansSequenziale.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3867:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3867:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	" returned "
.LC1:
	.string	"("
.LC2:
	.string	") at "
.LC3:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/lucadinidue/ParallelKMeans/master/KMeansSequenziale.cu"
.LC4:
	.string	":"
	.text
	.type	_ZL17CheckCudaErrorAuxPKcjS0_9cudaError.part.0.constprop.0, @function
_ZL17CheckCudaErrorAuxPKcjS0_9cudaError.part.0.constprop.0:
.LFB4542:
	.cfi_startproc
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	%edx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movl	%edi, %ebx
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	leaq	.LC0(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	%ebp, %edi
	movq	%rax, %r12
	call	cudaGetErrorString@PLT
	movq	%r12, %rdi
	movq	%rax, %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	leaq	.LC1(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	%ebp, %esi
	movq	%rax, %rdi
	call	_ZNSolsEi@PLT
	leaq	.LC2(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	leaq	.LC3(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	leaq	.LC4(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	%ebx, %esi
	movq	%rax, %rdi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movl	$1, %edi
	call	exit@PLT
	.cfi_endproc
.LFE4542:
	.size	_ZL17CheckCudaErrorAuxPKcjS0_9cudaError.part.0.constprop.0, .-_ZL17CheckCudaErrorAuxPKcjS0_9cudaError.part.0.constprop.0
	.section	.rodata.str1.1
.LC5:
	.string	"Error at %s:%d\n"
	.text
	.globl	_Z4initPf
	.type	_Z4initPf, @function
_Z4initPf:
.LFB3862:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	$100, %esi
	movq	%rdi, %rbx
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rdi
	call	curandCreateGenerator@PLT
	movl	$29, %ecx
	testl	%eax, %eax
	jne	.L14
	movq	(%rsp), %rdi
	movl	$234, %esi
	call	curandSetPseudoRandomGeneratorSeed@PLT
	testl	%eax, %eax
	je	.L7
	movl	$30, %ecx
.L14:
	leaq	.LC3(%rip), %rdx
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
.L15:
	call	__printf_chk@PLT
	movl	$1, %eax
	jmp	.L4
.L7:
	movq	(%rsp), %rdi
	movl	$160000, %edx
	movq	%rbx, %rsi
	call	curandGenerateUniform@PLT
	movl	$31, %ecx
	testl	%eax, %eax
	jne	.L14
	movq	(%rsp), %rdi
	call	curandDestroyGenerator@PLT
	movl	%eax, %edx
	xorl	%eax, %eax
	testl	%edx, %edx
	je	.L4
	movl	$32, %ecx
	leaq	.LC3(%rip), %rdx
	movl	$2, %edi
	leaq	.LC5(%rip), %rsi
	jmp	.L15
.L4:
	movq	8(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3862:
	.size	_Z4initPf, .-_Z4initPf
	.section	.rodata.str1.1
.LC7:
	.string	"cudaMemcpy(hostData, devData, 2*N*sizeof(float), cudaMemcpyDeviceToHost)"
.LC10:
	.string	"Numero di iterazioni: %d\r\n"
.LC12:
	.string	"durata: "
.LC13:
	.string	"cudaFree(devData)"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3863:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$640000, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$1600, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movl	$160000, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	leaq	32(%rsp), %rdi
	movl	$640000, %esi
	movq	%rax, %r12
	call	cudaMalloc@PLT
	testl	%eax, %eax
	je	.L18
	movl	$52, %ecx
	leaq	.LC3(%rip), %rdx
	leaq	.LC5(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	$1, %eax
	jmp	.L17
.L18:
	movq	32(%rsp), %rdi
	call	_Z4initPf
	movq	32(%rsp), %rsi
	movl	$640000, %edx
	movq	%rbp, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movl	%eax, %edx
	testl	%eax, %eax
	je	.L20
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC7(%rip), %rsi
	movl	$54, %edi
	je	.L47
	jmp	.L48
.L20:
	call	cudaDeviceSynchronize@PLT
	movl	$400, %ecx
	movq	%rbx, %rdi
	movq	%rbp, %rsi
	rep movsl
	xorl	%r14d, %r14d
	call	clock@PLT
	movq	%rax, %r13
.L22:
	xorl	%eax, %eax
.L31:
	movss	0(%rbp,%rax,8), %xmm5
	movss	4(%rbp,%rax,8), %xmm4
	xorl	%edx, %edx
	orl	$-1, %ecx
	movss	.LC6(%rip), %xmm3
.L25:
	movaps	%xmm5, %xmm0
	movaps	%xmm4, %xmm1
	subss	(%rbx,%rdx,8), %xmm0
	subss	4(%rbx,%rdx,8), %xmm1
	cvtss2sd	%xmm0, %xmm0
	mulsd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	mulsd	%xmm1, %xmm1
	addsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	comiss	%xmm0, %xmm3
	minss	%xmm3, %xmm0
	cmova	%edx, %ecx
	incq	%rdx
	movaps	%xmm0, %xmm3
	cmpq	$200, %rdx
	jne	.L25
	movw	%cx, (%r12,%rax,2)
	incq	%rax
	cmpq	$80000, %rax
	jne	.L31
	xorl	%r15d, %r15d
	movb	$1, %dl
.L26:
	movl	$4, %esi
	movl	$2, %edi
	movb	%dl, 8(%rsp)
	call	calloc@PLT
	movb	8(%rsp), %dl
	xorl	%ecx, %ecx
	xorl	%esi, %esi
	movss	.LC8(%rip), %xmm2
.L28:
	movswl	(%r12,%rcx,2), %edi
	cmpl	%r15d, %edi
	jne	.L27
	movss	(%rax), %xmm0
	addss	0(%rbp,%rcx,8), %xmm0
	incl	%esi
	movss	%xmm0, (%rax)
	movss	4(%rbp,%rcx,8), %xmm0
	addss	4(%rax), %xmm0
	movss	%xmm0, 4(%rax)
.L27:
	incq	%rcx
	cmpq	$80000, %rcx
	jne	.L28
	cvtsi2ssl	%esi, %xmm3
	movss	(%rax), %xmm1
	movss	4(%rax), %xmm0
	movss	(%rbx,%r15,8), %xmm5
	movss	4(%rbx,%r15,8), %xmm4
	divss	%xmm3, %xmm1
	divss	%xmm3, %xmm0
	movsd	.LC9(%rip), %xmm3
	movss	%xmm1, (%rbx,%r15,8)
	subss	%xmm5, %xmm1
	andps	%xmm2, %xmm1
	cvtss2sd	%xmm1, %xmm1
	comisd	%xmm3, %xmm1
	movss	%xmm0, 4(%rbx,%r15,8)
	ja	.L37
	subss	%xmm4, %xmm0
	xorl	%eax, %eax
	andps	%xmm2, %xmm0
	cvtss2sd	%xmm0, %xmm0
	ucomisd	%xmm3, %xmm0
	cmova	%eax, %edx
	jmp	.L29
.L37:
	xorl	%edx, %edx
.L29:
	incq	%r15
	cmpq	$200, %r15
	jne	.L26
	incl	%r14d
	testb	%dl, %dl
	je	.L22
	movl	%r14d, %edx
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	call	clock@PLT
	leaq	.LC12(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	subq	%r13, %rax
	cvtsi2sdq	%rax, %xmm0
	divsd	.LC11(%rip), %xmm0
	movsd	%xmm0, 8(%rsp)
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movsd	8(%rsp), %xmm0
	movq	%rax, %rdi
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	movb	$10, 31(%rsp)
	movq	%rax, %rdi
	movq	(%rax), %rax
	movq	-24(%rax), %rax
	cmpq	$0, 16(%rdi,%rax)
	je	.L32
	leaq	31(%rsp), %rsi
	movl	$1, %edx
	call	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l@PLT
	jmp	.L33
.L32:
	movl	$10, %esi
	call	_ZNSo3putEc@PLT
.L33:
	movq	%rbp, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movl	%eax, %edx
	xorl	%eax, %eax
	testl	%edx, %edx
	je	.L17
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC13(%rip), %rsi
	movl	$113, %edi
	jne	.L48
.L47:
	call	_ZL17CheckCudaErrorAuxPKcjS0_9cudaError.part.0.constprop.0
.L17:
	movq	40(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L35
.L48:
	call	__stack_chk_fail@PLT
.L35:
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	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
.LFE3863:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3890:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3890:
	.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
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC6:
	.long	1077936128
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC8:
	.long	2147483647
	.long	0
	.long	0
	.long	0
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC9:
	.long	-1598689907
	.long	1051772663
	.align 8
.LC11:
	.long	0
	.long	1093567616
	.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 <iostream>
#include <numeric>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <hip/hip_runtime.h>
#include <hiprand/hiprand.h>
#include <math.h>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <ctime>

static void CheckCudaErrorAux (const char *, unsigned, const char *, hipError_t);
void sequentialKMeans(int N, int K, int EPS, float*devData);

#define N 80000
#define K 200
#define EPS 0.000001

#define CUDA_CHECK_RETURN(value) CheckCudaErrorAux(__FILE__,__LINE__, #value, value)
#define CUDA_CALL(x) do { if((x)!=hipSuccess) { printf("Error at %s:%d\n",__FILE__,__LINE__);return EXIT_FAILURE;}} while(0)
#define CURAND_CALL(x) do { if((x)!=HIPRAND_STATUS_SUCCESS) {printf("Error at %s:%d\n",__FILE__,__LINE__); return EXIT_FAILURE;}} while(0)
using namespace std;


int init(float *vectorsDev){
	hiprandGenerator_t gen;
	CURAND_CALL(hiprandCreateGenerator(&gen, HIPRAND_RNG_PSEUDO_DEFAULT));
	CURAND_CALL(hiprandSetPseudoRandomGeneratorSeed(gen, 234ULL));
	CURAND_CALL(hiprandGenerateUniform(gen, vectorsDev, 2*N));
	CURAND_CALL(hiprandDestroyGenerator(gen));
	return 0;
}

int main(int argc, char* argv[]){
	std::clock_t start;
	double duration;

	int iter = 0;
	float *devData, *hostData, *mean;
	bool stopCriterion = false;

	short *clusters;
	float oldMeanX, oldMeanY;

	hostData = (float*) malloc(2*N*sizeof(float));
	mean = (float*) malloc(2*K*sizeof(float));
	clusters = (short*) malloc(N*sizeof(short));


	CUDA_CALL(hipMalloc((float **)&devData, 2*N*sizeof(float)));
	init(devData);
	CUDA_CHECK_RETURN(hipMemcpy(hostData, devData, 2*N*sizeof(float), hipMemcpyDeviceToHost));
	hipDeviceSynchronize();

	memcpy(mean, hostData, 2*K*sizeof(float));

	start = std::clock();
	while (!stopCriterion) {
		stopCriterion = true;
		/*
		 * distanze e minimo
		 */
		for (int v = 0; v < N; v++) {
			float minDistance = 3.0;
			short minIndex = -1;
			float distance = 0;
			for (int c = 0; c < K; c++) {
				distance = pow((hostData[2*v] - mean[2*c]),2)+pow((hostData[2*v+1] - mean[2*c+1]),2);

				if (distance < minDistance) {
					minIndex = c;
					minDistance = distance;
				}

			}
			clusters[v] = minIndex;
		}
		/*
		 * nuova media
		 */
		for (int i = 0; i < K; i++) {
			int numComponents = 0;
			float *arraySum = (float*)calloc(2, sizeof(float));
			for (int j = 0; j < N; j++) {
				if (clusters[j] == i) {
					numComponents++;
					arraySum[0] += hostData[2*j];
					arraySum[1] += hostData[2*j+1];

				}
			}
			oldMeanX = mean[2*i];
			mean[2*i] = arraySum[0] / numComponents;
			oldMeanY = mean[2*i+1];
			mean[2*i+1] = arraySum[1] / numComponents;
			if(abs(mean[2*i] - oldMeanX) > EPS || abs(mean[2*i+1] - oldMeanY) > EPS){
				stopCriterion = false;
			}
		}

		iter++;
	}
	printf("Numero di iterazioni: %d\r\n", iter);
	duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
	std::cout<<"durata: "<< duration <<'\n';

	free(hostData);
	free(mean);
	free(clusters);

	CUDA_CHECK_RETURN(hipFree(devData));

	return EXIT_SUCCESS;
}

/**
 * Check the return value of the CUDA runtime API call and exit
 * the application if the call has failed.
 */
static void CheckCudaErrorAux (const char *file, unsigned line, const char *statement, hipError_t err)
{
	if (err == hipSuccess)
		return;
	std::cerr << statement<<" returned " << hipGetErrorString(err) << "("<<err<< ") at "<<file<<":"<<line << std::endl;
	exit (1);
}


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

                                        # End of file scope inline assembly
	.globl	_Z4initPf                       # -- Begin function _Z4initPf
	.type	_Z4initPf,@function
_Z4initPf:                              # @_Z4initPf
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	leaq	8(%rsp), %rdi
	movl	$400, %esi                      # imm = 0x190
	callq	hiprandCreateGenerator
	movl	$29, %edx
	testl	%eax, %eax
	jne	.LBB0_5
# %bb.1:
	movq	8(%rsp), %rdi
	movl	$234, %esi
	callq	hiprandSetPseudoRandomGeneratorSeed
	movl	$30, %edx
	testl	%eax, %eax
	jne	.LBB0_5
# %bb.2:
	movq	8(%rsp), %rdi
	movl	$160000, %edx                   # imm = 0x27100
	movq	%rbx, %rsi
	callq	hiprandGenerateUniform
	movl	$31, %edx
	testl	%eax, %eax
	jne	.LBB0_5
# %bb.3:
	movq	8(%rsp), %rdi
	callq	hiprandDestroyGenerator
	movl	$32, %edx
	testl	%eax, %eax
	je	.LBB0_4
.LBB0_5:                                # %.sink.split
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %eax
.LBB0_6:
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB0_4:
	.cfi_def_cfa_offset 32
	xorl	%eax, %eax
	jmp	.LBB0_6
.Lfunc_end0:
	.size	_Z4initPf, .Lfunc_end0-_Z4initPf
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0x40400000                      # float 3
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI1_1:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI1_2:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
.LCPI1_3:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$32, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$640000, %edi                   # imm = 0x9C400
	callq	malloc
	movq	%rax, %rbx
	movl	$1600, %edi                     # imm = 0x640
	callq	malloc
	movq	%rax, %r14
	movl	$160000, %edi                   # imm = 0x27100
	callq	malloc
	movq	%rax, %r15
	leaq	16(%rsp), %rdi
	movl	$640000, %esi                   # imm = 0x9C400
	callq	hipMalloc
	testl	%eax, %eax
	je	.LBB1_2
# %bb.1:
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	movl	$52, %edx
	xorl	%eax, %eax
	callq	printf
	movl	$1, %eax
	jmp	.LBB1_21
.LBB1_2:
	movq	16(%rsp), %rdi
	callq	_Z4initPf
	movq	16(%rsp), %rsi
	movl	$640000, %edx                   # imm = 0x9C400
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movl	$.L.str.2, %esi
	movl	$54, %edi
	movl	%eax, %edx
	callq	_ZL17CheckCudaErrorAuxPKcjS0_10hipError_t
	callq	hipDeviceSynchronize
	movl	$1600, %edx                     # imm = 0x640
	movq	%r14, %rdi
	movq	%rbx, %rsi
	callq	memcpy@PLT
	xorl	%r13d, %r13d
	callq	clock
	movq	%rax, %r12
	movss	.LCPI1_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	movaps	.LCPI1_1(%rip), %xmm1           # xmm1 = [NaN,NaN,NaN,NaN]
	movsd	.LCPI1_2(%rip), %xmm2           # xmm2 = mem[0],zero
.LBB1_3:                                # %.preheader78
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
                                        #       Child Loop BB1_5 Depth 3
                                        #     Child Loop BB1_8 Depth 2
                                        #       Child Loop BB1_9 Depth 3
	xorl	%eax, %eax
.LBB1_4:                                # %.preheader
                                        #   Parent Loop BB1_3 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB1_5 Depth 3
	movss	(%rbx,%rax,8), %xmm3            # xmm3 = mem[0],zero,zero,zero
	movss	4(%rbx,%rax,8), %xmm4           # xmm4 = mem[0],zero,zero,zero
	movw	$-1, %cx
	xorl	%edx, %edx
	movaps	%xmm0, %xmm5
.LBB1_5:                                #   Parent Loop BB1_3 Depth=1
                                        #     Parent Loop BB1_4 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movaps	%xmm3, %xmm6
	subss	(%r14,%rdx,8), %xmm6
	cvtss2sd	%xmm6, %xmm6
	mulsd	%xmm6, %xmm6
	movaps	%xmm4, %xmm7
	subss	4(%r14,%rdx,8), %xmm7
	cvtss2sd	%xmm7, %xmm7
	mulsd	%xmm7, %xmm7
	addsd	%xmm6, %xmm7
	xorps	%xmm6, %xmm6
	cvtsd2ss	%xmm7, %xmm6
	ucomiss	%xmm6, %xmm5
	minss	%xmm5, %xmm6
	movl	%edx, %esi
	cmovbel	%ecx, %esi
	movl	%esi, %ecx
	incq	%rdx
	movaps	%xmm6, %xmm5
	cmpq	$200, %rdx
	jne	.LBB1_5
# %bb.6:                                #   in Loop: Header=BB1_4 Depth=2
	movw	%cx, (%r15,%rax,2)
	incq	%rax
	cmpq	$80000, %rax                    # imm = 0x13880
	jne	.LBB1_4
# %bb.7:                                # %.preheader77.preheader
                                        #   in Loop: Header=BB1_3 Depth=1
	movb	$1, %al
	xorl	%ecx, %ecx
.LBB1_8:                                # %.preheader77
                                        #   Parent Loop BB1_3 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB1_9 Depth 3
	xorl	%edx, %edx
	xorps	%xmm3, %xmm3
	xorps	%xmm4, %xmm4
	xorl	%esi, %esi
.LBB1_9:                                #   Parent Loop BB1_3 Depth=1
                                        #     Parent Loop BB1_8 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	movswq	(%r15,%rdx,2), %rdi
	movl	%edi, %edi
	cmpq	%rdi, %rcx
	jne	.LBB1_11
# %bb.10:                               #   in Loop: Header=BB1_9 Depth=3
	incl	%esi
	addss	(%rbx,%rdx,8), %xmm4
	addss	4(%rbx,%rdx,8), %xmm3
.LBB1_11:                               #   in Loop: Header=BB1_9 Depth=3
	incq	%rdx
	cmpq	$80000, %rdx                    # imm = 0x13880
	jne	.LBB1_9
# %bb.12:                               #   in Loop: Header=BB1_8 Depth=2
	xorps	%xmm5, %xmm5
	cvtsi2ss	%esi, %xmm5
	divss	%xmm5, %xmm4
	movaps	%xmm4, %xmm6
	subss	(%r14,%rcx,8), %xmm6
	movss	%xmm4, (%r14,%rcx,8)
	movss	4(%r14,%rcx,8), %xmm4           # xmm4 = mem[0],zero,zero,zero
	divss	%xmm5, %xmm3
	movss	%xmm3, 4(%r14,%rcx,8)
	andps	%xmm1, %xmm6
	xorps	%xmm5, %xmm5
	cvtss2sd	%xmm6, %xmm5
	ucomisd	%xmm2, %xmm5
	ja	.LBB1_14
# %bb.13:                               #   in Loop: Header=BB1_8 Depth=2
	subss	%xmm4, %xmm3
	andps	%xmm1, %xmm3
	cvtss2sd	%xmm3, %xmm3
	ucomisd	%xmm2, %xmm3
	jbe	.LBB1_15
.LBB1_14:                               #   in Loop: Header=BB1_8 Depth=2
	xorl	%eax, %eax
.LBB1_15:                               #   in Loop: Header=BB1_8 Depth=2
	incq	%rcx
	cmpq	$200, %rcx
	jne	.LBB1_8
# %bb.16:                               #   in Loop: Header=BB1_3 Depth=1
	incl	%r13d
	testb	$1, %al
	je	.LBB1_3
# %bb.17:
	movl	$.L.str.3, %edi
	movl	%r13d, %esi
	xorl	%eax, %eax
	callq	printf
	callq	clock
	subq	%r12, %rax
	cvtsi2sd	%rax, %xmm0
	divsd	.LCPI1_3(%rip), %xmm0
	movsd	%xmm0, 24(%rsp)                 # 8-byte Spill
	movl	$_ZSt4cout, %edi
	movl	$.L.str.4, %esi
	movl	$8, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	$_ZSt4cout, %edi
	movsd	24(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	callq	_ZNSo9_M_insertIdEERSoT_
	movb	$10, 15(%rsp)
	movq	(%rax), %rcx
	movq	-24(%rcx), %rcx
	cmpq	$0, 16(%rax,%rcx)
	je	.LBB1_19
# %bb.18:
	leaq	15(%rsp), %rsi
	movl	$1, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	jmp	.LBB1_20
.LBB1_19:
	movq	%rax, %rdi
	movl	$10, %esi
	callq	_ZNSo3putEc
.LBB1_20:                               # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c.exit
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	16(%rsp), %rdi
	callq	hipFree
	movl	$.L.str.5, %esi
	movl	$113, %edi
	movl	%eax, %edx
	callq	_ZL17CheckCudaErrorAuxPKcjS0_10hipError_t
	xorl	%eax, %eax
.LBB1_21:
	addq	$32, %rsp
	.cfi_def_cfa_offset 48
	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_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	_ZL17CheckCudaErrorAuxPKcjS0_10hipError_t,@function # -- Begin function _ZL17CheckCudaErrorAuxPKcjS0_10hipError_t
_ZL17CheckCudaErrorAuxPKcjS0_10hipError_t: # @_ZL17CheckCudaErrorAuxPKcjS0_10hipError_t
	.cfi_startproc
# %bb.0:
	testl	%edx, %edx
	jne	.LBB2_2
# %bb.1:
	retq
.LBB2_2:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %rbp, -16
	movl	%edx, %ebp
	movl	%edi, %ebx
	movl	$_ZSt4cerr, %edi
	callq	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
	movl	$.L.str.6, %esi
	movq	%rax, %rdi
	callq	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
	movq	%rax, %r14
	movl	%ebp, %edi
	callq	hipGetErrorString
	movq	%r14, %rdi
	movq	%rax, %rsi
	callq	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
	movl	$.L.str.7, %esi
	movq	%rax, %rdi
	callq	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
	movq	%rax, %rdi
	movl	%ebp, %esi
	callq	_ZNSolsEi
	movl	$.L.str.8, %esi
	movq	%rax, %rdi
	callq	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
	movl	$.L.str.1, %esi
	movq	%rax, %rdi
	callq	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
	movl	$.L.str.9, %esi
	movq	%rax, %rdi
	callq	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
	movq	%rax, %rdi
	movl	%ebx, %esi
	callq	_ZNSolsEj
	movq	%rax, %rdi
	callq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
	movl	$1, %edi
	callq	exit
.Lfunc_end2:
	.size	_ZL17CheckCudaErrorAuxPKcjS0_10hipError_t, .Lfunc_end2-_ZL17CheckCudaErrorAuxPKcjS0_10hipError_t
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Error at %s:%d\n"
	.size	.L.str, 16

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/lucadinidue/ParallelKMeans/master/KMeansSequenziale.hip"
	.size	.L.str.1, 118

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"hipMemcpy(hostData, devData, 2*N*sizeof(float), hipMemcpyDeviceToHost)"
	.size	.L.str.2, 71

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Numero di iterazioni: %d\r\n"
	.size	.L.str.3, 27

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"durata: "
	.size	.L.str.4, 9

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"hipFree(devData)"
	.size	.L.str.5, 17

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	" returned "
	.size	.L.str.6, 11

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"("
	.size	.L.str.7, 2

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	") at "
	.size	.L.str.8, 6

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

	.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 _ZSt4cout
	.addrsig_sym _ZSt4cerr
	.addrsig_sym __hip_cuid_

	.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_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
