// File: /workspace/github_cpp_makefile_star2_O3/isaacbrodsky[P]K-means-plus-plus/k-means-multi.cpp
// Function: Cluster_set::Initialize_plus_plus()
void Cluster_set::Initialize_plus_plus(void) {
	// Initializes using K-means++.

	// Number of instances in the input data
	size_t szData = vclInput_data.size();
	size_t uIndex;
	// Points that have already been selected as starting points.
	// true indicates already selected, false not selected
	// as a starting point.
	vector<bool> vbSkipPoints;
	vector<float> vfDistance;
	int iSelectedPoints;
	int iAttribute_index;
	float fTotalDistance;
	float fRandomDistance;

	vbSkipPoints.resize(szData);
	vfDistance.resize(szData);

	// Select the first data instance as the initial mean
	// (by copying it into the list of means and marking
	// it as selected in our list of points to skip.)
	for (iAttribute_index = 0; iAttribute_index < iAttribute_ct; iAttribute_index++){ // read attributes
		vvfMeans[0][iAttribute_index]
			= vclInput_data[0].vfAttribute[iAttribute_index];
	} // for
	vbSkipPoints[0] = true;
	iSelectedPoints = 1;

	// While we don't have enough starting clusters
	for (; iSelectedPoints < iK_count; iSelectedPoints++) {
		fTotalDistance = 0;

		if (iNumPlusPlusThreads == 1)
		{
			//Don't bother creating more threads.
			fTotalDistance = Initialize_plus_plus_process(0, szData, iSelectedPoints, vfDistance, vbSkipPoints);
		}
		else
		{
			// local variables
			unsigned uPerThread;
			unsigned uDataStart;
			int iThread_index;
			vector<future<float>> vtThreads;

			uDataStart = 0;
			uPerThread = szData / iNumThreads;

			//Split the dataset into parts and launch as individual
			//threads.
			for (iThread_index = 0; iThread_index < iNumPlusPlusThreads; iThread_index++)
			{
				//Force the last thread to take remaining data
				if (iThread_index == iNumPlusPlusThreads - 1)
				{
					uPerThread = szData - uDataStart;
				}

				vtThreads.push_back(async(launch::async, [=, &vfDistance, &vbSkipPoints](unsigned uStart, unsigned uLength) {
					return Initialize_plus_plus_process(uStart, uLength, iSelectedPoints, vfDistance, vbSkipPoints); }, uDataStart, uPerThread));

				uDataStart += uPerThread;
			} // Launch all threads

			//Wait for all threads to complete.
			for (iThread_index = 0; iThread_index < iNumPlusPlusThreads; iThread_index++)
			{
				fTotalDistance += vtThreads[iThread_index].get();
			} // Wait for all threads
		}

		// Determine which instance to take as a new starting cluster
		// First generate a random number uniformly between [0, fTotalDistance)
		// using mtRandom as the PRNG.
		fRandomDistance = uniform_real_distribution<float>(0, fTotalDistance)(mtRandom);
		for (uIndex = 0; uIndex < szData; uIndex++) {
			// Don't consider points already selected as starting points.
			if (!vbSkipPoints[uIndex]) {
				// Data instances with a larger distance will have a higher effect
				// here, biasing the k-means++ algorithm toward selecting them.
				fRandomDistance -= vfDistance[uIndex];
				if (fRandomDistance <= 0) {
					// Select this point as a starting point
					// (by copying it into the means vector
					// and marking it as used in our own
					// vector of points to skip.)
					for (iAttribute_index = 0; iAttribute_index < iAttribute_ct; iAttribute_index++){
						vvfMeans[iSelectedPoints][iAttribute_index]
							= vclInput_data[uIndex].vfAttribute[iAttribute_index];
					} // for
					vbSkipPoints[uIndex] = true;
					
					// Stop selecting the next starting point
					break;
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %r12
movq 0x80(%rdi), %r15
movq 0x88(%rdi), %r13
movq %r13, %rbx
subq %r15, %rbx
sarq $0x6, %rbx
xorl %eax, %eax
leaq 0xc0(%rsp), %rdi
movq %rax, (%rdi)
movl %eax, 0x8(%rdi)
movq %rax, 0x10(%rdi)
movl %eax, 0x18(%rdi)
movq %rax, 0x20(%rdi)
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq %rbx, %rsi
xorl %edx, %edx
callq 0x570a
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x5578
movslq 0x9c(%r12), %rax
testq %rax, %rax
jle 0x4c56
movq (%r12), %rdx
movq 0x80(%r12), %rcx
movq (%rcx), %rcx
movq (%rdx), %rdx
xorl %esi, %esi
movss (%rcx,%rsi,4), %xmm0
movss %xmm0, (%rdx,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x4c44
movq 0xc0(%rsp), %rax
orq $0x1, (%rax)
cmpl $0x2, 0x30(%r12)
jl 0x503a
movabsq $-0x8000000000000000, %rbp # imm = 0x8000000000000000
cmpq $0x1, %rbx
movq %rbx, %r14
adcq $0x0, %r14
leaq 0xa8(%r12), %rax
movq %rax, 0x98(%rsp)
movl $0x1, %eax
movq %rax, 0x10(%rsp)
leaq 0x3f(%rbp), %rax
movq %rax, 0xa0(%rsp)
movq %rbx, 0x58(%rsp)
movq %r15, 0x88(%rsp)
movq %r13, 0x80(%rsp)
movq %r14, 0x90(%rsp)
movl 0x1430(%r12), %ecx
cmpl $0x1, %ecx
jne 0x4cfc
movq %r12, %rdi
xorl %esi, %esi
movl %ebx, %edx
movq 0x10(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0xc0(%rsp), %r9
callq 0x4ade
movss %xmm0, 0xc(%rsp)
jmp 0x4f47
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
jl 0x4f30
movslq 0x1434(%r12), %rsi
xorl %r13d, %r13d
movq %rbx, %rax
xorl %edx, %edx
divq %rsi
movq %rax, %r14
xorl %ebx, %ebx
movl %r14d, %eax
decl %ecx
movq 0x58(%rsp), %r14
subl %ebx, %r14d
cmpl %ecx, %r13d
cmovnel %eax, %r14d
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movl $0x70, %edi
callq 0x32e0
movq %rax, %r15
movq %r13, 0xa8(%rsp)
movq %r12, %r13
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r15)
leaq 0x6df3(%rip), %rax      # 0xbb70
movq %rax, (%r15)
leaq 0x10(%r15), %rbp
xorl %eax, %eax
movq %rax, 0x18(%r15)
movl %eax, 0x20(%r15)
movb $0x0, 0x24(%r15)
movl %eax, 0x28(%r15)
movq %rax, 0x30(%r15)
movl %eax, 0x38(%r15)
leaq 0x6e1a(%rip), %rax      # 0xbbc0
movq %rax, 0x10(%r15)
movl $0x18, %edi
callq 0x32e0
movq %rax, %r12
movq %rax, %rdi
callq 0x670e
movq %r12, 0x40(%r15)
movl %r14d, 0x48(%r15)
movl %ebx, 0x4c(%r15)
leaq 0x20(%rsp), %rax
movq %rax, 0x50(%r15)
leaq 0xc0(%rsp), %rax
movq %rax, 0x58(%r15)
movq %r13, %r12
movq %r13, 0x60(%r15)
movq 0x10(%rsp), %rax
movl %eax, 0x68(%r15)
movq $0x0, 0xb0(%rsp)
movl $0x20, %edi
callq 0x32e0
leaq 0x6e0b(%rip), %rcx      # 0xbc18
movq %rcx, (%rax)
movq %rbp, 0x8(%rax)
leaq 0x4a5(%rip), %rcx       # 0x52c0
movq %rcx, 0x10(%rax)
movq $0x0, 0x18(%rax)
movq %rax, 0x18(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0x3280
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x4e50
movq (%rdi), %rax
callq *0x8(%rax)
cmpq $0x0, 0x30(%r15)
jne 0x5080
movq 0xb0(%rsp), %rax
movq %rax, 0x30(%r15)
movq %rbp, 0x40(%rsp)
movq %r15, 0x48(%rsp)
leaq 0xe8(%rsp), %r15
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0x6cac
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x4e95
callq 0x58ea
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0x6d1e
movq 0xa8(%rsp), %r13
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x4ebc
callq 0x58ea
addl %r14d, %ebx
incl %r13d
movl 0x1430(%r12), %ecx
cmpl %ecx, %r13d
jl 0x4d34
testl %ecx, %ecx
movq 0x88(%rsp), %r15
movq 0x80(%rsp), %r13
movabsq $-0x8000000000000000, %rbp # imm = 0x8000000000000000
jle 0x4f30
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x60(%rsp), %rdi
addq %rbx, %rdi
callq 0x5778
movss 0xc(%rsp), %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0xc(%rsp)
incq %r14
movslq 0x1430(%r12), %rax
addq $0x10, %rbx
cmpq %rax, %r14
jl 0x4eff
leaq 0x60(%rsp), %rdi
callq 0x57d4
movq 0x58(%rsp), %rbx
movq 0x90(%rsp), %r14
movq 0x98(%rsp), %rdi
callq 0x6f92
cmpq %r15, %r13
je 0x501f
mulss 0xc(%rsp), %xmm0
addss 0x3165(%rip), %xmm0     # 0x80d0
movq 0xc0(%rsp), %rdx
movq 0x20(%rsp), %rsi
xorl %eax, %eax
xorl %ecx, %ecx
leaq 0x3f(%rcx), %r9
testq %rcx, %rcx
cmovnsq %rcx, %r9
sarq $0x6, %r9
movq %rcx, %r8
andq 0xa0(%rsp), %r8
xorl %edi, %edi
cmpq %rbp, %r8
setbe %dil
movl $0x1, %r8d
shlq %cl, %r8
leaq (%rdx,%r9,8), %r9
movq -0x8(%r9,%rdi,8), %r10
btq %rcx, %r10
jb 0x4fc4
subss (%rsi,%rcx,4), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jae 0x4fd2
incq %rcx
addq $0x40, %rax
cmpq %rcx, %r14
jne 0x4f7c
jmp 0x501f
movslq 0x9c(%r12), %rcx
testq %rcx, %rcx
jle 0x5010
movq (%r12), %rdx
movq 0x80(%r12), %rsi
movq (%rsi,%rax), %rax
movq 0x10(%rsp), %rsi
leaq (%rsi,%rsi,2), %rsi
movq (%rdx,%rsi,8), %rdx
xorl %esi, %esi
movss (%rax,%rsi,4), %xmm0
movss %xmm0, (%rdx,%rsi,4)
incq %rsi
cmpq %rsi, %rcx
jne 0x4ffe
leaq -0x8(,%rdi,8), %rax
orq %r8, %r10
movq %r10, (%r9,%rax)
movq 0x10(%rsp), %rdx
incq %rdx
movslq 0x30(%r12), %rax
movq %rdx, 0x10(%rsp)
cmpq %rax, %rdx
jl 0x4cc6
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x5051
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x506e
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3090
jmp 0x5087
movq %rax, %rbx
jmp 0x514f
movq %rax, %rbx
jmp 0x5145
movq %rdx, %r14
movq %rax, %rbx
jmp 0x50d6
movq %rdx, %r14
movq %rax, %rbx
jmp 0x50eb
movq %rdx, %r14
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x50ca
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x50ca
movq %rax, %rbx
jmp 0x5136
movq %rdx, %r14
movq %rax, %rbx
movq %r15, %rdi
addq $0x40, %rdi
callq 0x6730
movq %rbp, %rdi
callq 0x678c
movl $0x70, %esi
movq %r15, %rdi
callq 0x32f0
cmpl $0x1, %r14d
jne 0x5136
movq %rbx, %rdi
callq 0x30e0
leaq 0x10(%rax), %rbx
callq 0x3150
leaq 0xb0(%rsp), %rsi
movl $0xb, (%rsi)
movq %rax, 0x8(%rsi)
movq %rbx, %rdi
callq 0x66d2
callq 0x3390
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
jmp 0x513b
movq %rax, %rbx
callq 0x33e0
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x5145
callq 0x58ea
leaq 0x60(%rsp), %rdi
callq 0x57d4
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x5166
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x5183
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
movq %rbx, %rdi
callq 0x3450
movq %rax, %rdi
callq 0x3997
nop