// File: /workspace/github_cpp_makefile_star2_O1/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 $0xe8, %rsp
movq %rdi, %r13
movq 0x80(%rdi), %rbx
movq 0x88(%rdi), %r14
movq %r14, %r15
subq %rbx, %r15
sarq $0x6, %r15
xorl %eax, %eax
leaq 0x38(%rsp), %rdi
movq %rax, (%rdi)
movl %eax, 0x8(%rdi)
movq %rax, 0x10(%rdi)
movl %eax, 0x18(%rdi)
movq %rax, 0x20(%rdi)
movq %rax, 0x70(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq %r15, %rsi
xorl %edx, %edx
callq 0x5a00
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0x586a
movslq 0x9c(%r13), %rax
testq %rax, %rax
jle 0x4c75
movq (%r13), %rdx
movq 0x80(%r13), %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 0x4c63
movq 0x38(%rsp), %rax
orq $0x1, (%rax)
cmpl $0x2, 0x30(%r13)
jl 0x5147
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
leaq 0xa8(%r13), %rbp
cmpq $0x1, %r15
movq %r15, %rax
adcq $0x0, %rax
movq %rax, 0xb8(%rsp)
movl $0x1, %eax
movq %rax, 0x8(%rsp)
leaq 0x3f(%r12), %rax
movq %rax, 0xc0(%rsp)
movq %r13, 0x88(%rsp)
movq %rbx, 0xb0(%rsp)
movq %r14, 0xa8(%rsp)
movq %r15, 0x28(%rsp)
movq %rbp, 0x80(%rsp)
cmpl $0x1, 0x1430(%r13)
jne 0x4d1a
movq %r13, %rdi
xorl %esi, %esi
movl %r15d, %edx
movq 0x8(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0x38(%rsp), %r9
callq 0x4b02
movss %xmm0, 0x4(%rsp)
jmp 0x505a
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movq $0x0, 0xa0(%rsp)
movl 0x1430(%r13), %ecx
testl %ecx, %ecx
jle 0x4fb9
movslq 0x1434(%r13), %rsi
xorl %ebp, %ebp
movq 0x28(%rsp), %rax
xorl %edx, %edx
divq %rsi
movq %rax, %r14
xorl %ebx, %ebx
movl %r14d, %eax
decl %ecx
movq 0x28(%rsp), %r14
subl %ebx, %r14d
cmpl %ecx, %ebp
cmovnel %eax, %r14d
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movl $0x70, %edi
callq 0x32e0
movq %rax, %r12
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r12)
leaq 0x6d0a(%rip), %rax      # 0xbaa0
movq %rax, (%r12)
leaq 0x10(%r12), %r15
xorl %eax, %eax
movl %eax, 0x28(%r12)
movq %rax, 0x18(%r12)
movq %rax, 0x1d(%r12)
movq %rax, 0x30(%r12)
movl %eax, 0x38(%r12)
leaq 0x6d2f(%rip), %rax      # 0xbaf0
movq %rax, 0x10(%r12)
movl $0x18, %edi
callq 0x32e0
movq %rax, %r13
movq %rax, %rdi
callq 0x69f6
movq %r13, 0x40(%r12)
movl %r14d, 0x48(%r12)
movl %ebx, 0x4c(%r12)
leaq 0x60(%rsp), %rax
movq %rax, 0x50(%r12)
leaq 0x38(%rsp), %rax
movq %rax, 0x58(%r12)
movq 0x88(%rsp), %r13
movq %r13, 0x60(%r12)
movq 0x8(%rsp), %rax
movl %eax, 0x68(%r12)
movq $0x0, 0xd8(%rsp)
movl $0x20, %edi
callq 0x32e0
leaq 0x6d16(%rip), %rcx      # 0xbb48
movq %rcx, (%rax)
movq %r15, 0x8(%rax)
leaq 0x5e6(%rip), %rcx       # 0x5426
movq %rcx, 0x10(%rax)
movq $0x0, 0x18(%rax)
movq %rax, 0x30(%rsp)
leaq 0xd8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
callq 0x3280
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x4e75
movq (%rdi), %rax
callq *0x8(%rax)
cmpq $0x0, 0x30(%r12)
jne 0x51a0
movq 0xd8(%rsp), %rax
movq %rax, 0x30(%r12)
movq %r15, 0x10(%rsp)
movq 0x18(%rsp), %rdi
movq %r12, 0x18(%rsp)
testq %rdi, %rdi
je 0x4ea7
callq 0x5bd8
cmpq $0x0, 0x10(%rsp)
jne 0x4f5d
movl $0x60, %edi
callq 0x32e0
movq %rax, %r15
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r15)
leaq 0x6cb3(%rip), %rax      # 0xbb88
movq %rax, (%r15)
movq %r15, %r12
addq $0x10, %r12
movl $0x0, 0x28(%r15)
xorl %eax, %eax
movq %rax, 0x18(%r15)
movq %rax, 0x1d(%r15)
leaq 0x6ce0(%rip), %rax      # 0xbbd8
movq %rax, 0x10(%r15)
movl $0x18, %edi
callq 0x32e0
movq %rax, %r13
movq %rax, %rdi
callq 0x69f6
movq %r13, 0x30(%r15)
movl %r14d, 0x38(%r15)
movl %ebx, 0x3c(%r15)
leaq 0x60(%rsp), %rax
movq %rax, 0x40(%r15)
leaq 0x38(%rsp), %rax
movq %rax, 0x48(%r15)
movq 0x88(%rsp), %r13
movq %r13, 0x50(%r15)
movq 0x8(%rsp), %rax
movl %eax, 0x58(%r15)
movq %r12, 0x10(%rsp)
movq 0x18(%rsp), %rdi
movq %r15, 0x18(%rsp)
testq %rdi, %rdi
je 0x4f5d
callq 0x5bd8
leaq 0xc8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x6f98
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x4f7e
callq 0x5bd8
leaq 0x90(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x700a
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x4fa5
callq 0x5bd8
addl %r14d, %ebx
incl %ebp
movl 0x1430(%r13), %ecx
cmpl %ecx, %ebp
jl 0x4d58
cmpl $0x0, 0x1430(%r13)
jle 0x501d
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x28(%rsp), %r15
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
movq 0x80(%rsp), %rbp
movq 0x90(%rsp), %rdi
addq %rbx, %rdi
callq 0x5a6e
movss 0x4(%rsp), %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0x4(%rsp)
incq %r14
movslq 0x1430(%r13), %rax
addq $0x10, %rbx
cmpq %rax, %r14
jl 0x4fe8
jmp 0x503d
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
movq 0x28(%rsp), %r15
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
movq 0x80(%rsp), %rbp
leaq 0x90(%rsp), %rdi
callq 0x5aca
movq 0xb0(%rsp), %rbx
movq 0xa8(%rsp), %r14
movq %rbp, %rdi
callq 0x729a
cmpq %rbx, %r14
je 0x512d
mulss 0x4(%rsp), %xmm0
addss 0x3027(%rip), %xmm0     # 0x80a0
movq 0x38(%rsp), %rdx
movq 0x60(%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 0xc0(%rsp), %r8
xorl %edi, %edi
cmpq %r12, %r8
setbe %dil
movl $0x1, %r8d
shlq %cl, %r8
leaq (%rdx,%r9,8), %r9
movq -0x8(%r9,%rdi,8), %r10
btq %rcx, %r10
jb 0x50cf
subss (%rsi,%rcx,4), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jae 0x50e2
incq %rcx
addq $0x40, %rax
cmpq %rcx, 0xb8(%rsp)
jne 0x5087
jmp 0x512d
movslq 0x9c(%r13), %rcx
testq %rcx, %rcx
jle 0x511e
movq (%r13), %rdx
movq 0x80(%r13), %rsi
movq (%rsi,%rax), %rax
movq 0x8(%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 0x510c
leaq -0x8(,%rdi,8), %rax
orq %r8, %r10
movq %r10, (%r9,%rax)
movq 0x8(%rsp), %rdx
incq %rdx
movslq 0x30(%r13), %rax
movq %rdx, 0x8(%rsp)
cmpq %rax, %rdx
jl 0x4ce9
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x515e
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x518e
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
xorl %eax, %eax
movq %rax, 0x38(%rsp)
movl %eax, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movl %eax, 0x50(%rsp)
movq %rax, 0x58(%rsp)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3090
jmp 0x51a7
movq %rax, %rbx
jmp 0x529d
movq %rax, %rbx
movq %r12, %rdi
callq 0x6aa8
movl $0x60, %esi
movq %r15, %rdi
callq 0x32f0
jmp 0x5281
jmp 0x51ce
movq %rax, %rbx
jmp 0x5281
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
jmp 0x5286
movq %rdx, %r14
movq %rax, %rbx
jmp 0x5229
movq %rdx, %r14
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x5204
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, 0x30(%rsp)
jmp 0x521d
movq %rdx, %r14
movq %rax, %rbx
jmp 0x523e
movq %rdx, %r14
movq %rax, %rbx
movq %r12, %rdi
addq $0x40, %rdi
callq 0x6a18
movq %r15, %rdi
callq 0x6a74
movl $0x70, %esi
movq %r12, %rdi
callq 0x32f0
cmpl $0x1, %r14d
jne 0x5281
movq %rbx, %rdi
callq 0x30e0
leaq 0x10(%rax), %rbx
callq 0x3150
leaq 0xd8(%rsp), %rsi
movl $0xb, (%rsi)
movq %rax, 0x8(%rsi)
movq %rbx, %rdi
callq 0x69b2
callq 0x3390
movq %rax, %rbx
jmp 0x5290
movq %rax, %rbx
callq 0x33e0
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x5290
callq 0x5bd8
leaq 0x90(%rsp), %rdi
callq 0x5aca
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x52b4
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x52e4
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x32f0
xorl %eax, %eax
movq %rax, 0x38(%rsp)
movl %eax, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movl %eax, 0x50(%rsp)
movq %rax, 0x58(%rsp)
movq %rbx, %rdi
callq 0x3450
movq %rax, %rdi
callq 0x3991