// File: /workspace/github_cpp_makefile_star2_O2/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, %r13
movq 0x88(%rdi), %r14
subq 0x80(%rdi), %r14
leaq 0xd0(%rsp), %rdi
andq $0x0, (%rdi)
andl $0x0, 0x8(%rdi)
andq $0x0, 0x10(%rdi)
sarq $0x6, %r14
andl $0x0, 0x18(%rdi)
andq $0x0, 0x20(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
andq $0x0, 0x80(%rsp)
movq %r14, %rsi
xorl %edx, %edx
callq 0x5434
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x530e
movl 0x9c(%r13), %eax
movq (%r13), %rcx
movq 0x80(%r13), %rdx
xorl %esi, %esi
testl %eax, %eax
cmovlel %esi, %eax
cmpq %rsi, %rax
je 0x47cf
movq (%rdx), %rdi
movss (%rdi,%rsi,4), %xmm0
movq (%rcx), %rdi
movss %xmm0, (%rdi,%rsi,4)
incq %rsi
jmp 0x47b5
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x54a2
orq %rdx, (%rax)
leaq 0xa8(%r13), %rbp
pushq $0x1
popq %rcx
movq %r14, 0x90(%rsp)
movq %rbp, 0xb8(%rsp)
movslq 0x30(%r13), %rax
movq %rcx, 0x28(%rsp)
cmpq %rax, %rcx
jge 0x4c7d
movl 0x1430(%r13), %ecx
cmpl $0x1, %ecx
jne 0x4840
movq %r13, %rdi
xorl %esi, %esi
movl %r14d, %edx
movq 0x28(%rsp), %rcx
leaq 0x70(%rsp), %r8
movq %r15, %r9
callq 0x4632
leaq 0x50(%rsp), %rbx
jmp 0x4bc4
andq $0x0, 0x60(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movslq 0x1434(%r13), %rsi
movq $0x0, (%rsp)
movq %r14, %rax
xorl %edx, %edx
divq %rsi
movq %rax, %rbp
xorl %r15d, %r15d
cmpl %ecx, %r15d
jge 0x4b5c
decl %ecx
movl %r14d, %eax
subl (%rsp), %eax
cmpl %ecx, %r15d
cmovel %eax, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
pushq $0x70
popq %rdi
callq 0x32e0
movq %rax, %r12
movq %r13, %r14
leaq 0xf(%rsp), %rax
movq %rax, 0x10(%rsp)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r12)
leaq 0x8218(%rip), %rax      # 0xcad0
movq %rax, (%r12)
andq $0x0, 0x18(%r12)
movq %r12, 0x18(%rsp)
andl $0x0, 0x20(%r12)
movb $0x0, 0x24(%r12)
andl $0x0, 0x28(%r12)
leaq 0x10(%r12), %r13
andq $0x0, 0x30(%r12)
andl $0x0, 0x38(%r12)
leaq 0x822f(%rip), %rax      # 0xcb20
movq %rax, 0x10(%r12)
pushq $0x18
popq %rdi
callq 0x32e0
movq %rax, %rbx
movq %rax, %rdi
callq 0x653c
movq %rbx, 0x40(%r12)
movl %ebp, 0x48(%r12)
movq (%rsp), %rax
movl %eax, 0x4c(%r12)
leaq 0x70(%rsp), %rax
movq %rax, 0x50(%r12)
leaq 0xd0(%rsp), %rax
movq %rax, 0x58(%r12)
movq %r14, 0x60(%r12)
movq 0x28(%rsp), %rax
movl %eax, 0x68(%r12)
andq $0x0, 0x98(%rsp)
pushq $0x20
popq %rdi
callq 0x32e0
leaq 0x821e(%rip), %rcx      # 0xcb78
movq %rcx, (%rax)
movq %r13, 0x8(%rax)
leaq 0x5a6(%rip), %rcx       # 0x4f0e
movq %rcx, 0x10(%rax)
andq $0x0, 0x18(%rax)
movq %rax, 0x48(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x48(%rsp), %rsi
xorl %edx, %edx
callq 0x3280
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x499a
movq (%rdi), %rax
callq *0x8(%rax)
movq %r12, %rdi
addq $0x30, %rdi
leaq 0x98(%rsp), %rbx
movq %rbx, %rsi
callq 0x655e
movq %rbx, %rdi
callq 0x5556
andq $0x0, 0x18(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x4e92
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps 0x30(%rsp), %xmm0
movq %r13, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movaps %xmm0, 0x10(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x55d8
leaq 0xa8(%rsp), %rdi
callq 0x55d8
cmpq $0x0, 0x30(%rsp)
jne 0x4afc
pushq $0x60
popq %rdi
callq 0x32e0
movq %rax, %r12
leaq 0x48(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r12, 0x18(%rsp)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r12)
leaq 0x817d(%rip), %rax      # 0xcbb8
movq %rax, (%r12)
movq %r12, %r13
addq $0x10, %r13
andq $0x0, 0x18(%r12)
andl $0x0, 0x20(%r12)
movb $0x0, 0x24(%r12)
andl $0x0, 0x28(%r12)
leaq 0x81a3(%rip), %rax      # 0xcc08
movq %rax, 0x10(%r12)
pushq $0x18
popq %rdi
callq 0x32e0
movq %rax, %rbx
movq %rax, %rdi
callq 0x653c
movq %rbx, 0x30(%r12)
movl %ebp, 0x38(%r12)
movq (%rsp), %rax
movl %eax, 0x3c(%r12)
leaq 0x70(%rsp), %rax
movq %rax, 0x40(%r12)
leaq 0xd0(%rsp), %rax
movq %rax, 0x48(%r12)
movq %r14, 0x50(%r12)
movq 0x28(%rsp), %rax
movl %eax, 0x58(%r12)
andq $0x0, 0x18(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x5134
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps 0x30(%rsp), %xmm0
movq %r13, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movaps %xmm0, 0x10(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x55d8
leaq 0xa8(%rsp), %rdi
callq 0x55d8
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
leaq 0x30(%rsp), %rsi
callq 0x6b8a
leaq 0x50(%rsp), %rbx
leaq 0x38(%rsp), %rdi
callq 0x55d8
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6c1e
leaq 0xc8(%rsp), %rdi
callq 0x55d8
movq (%rsp), %rax
addl %ebp, %eax
movq %rax, (%rsp)
incl %r15d
movq %r14, %r13
movl 0x1430(%r14), %ecx
movq 0x90(%rsp), %r14
jmp 0x486b
xorps %xmm0, %xmm0
xorl %ebx, %ebx
xorl %r14d, %r14d
leaq 0xd0(%rsp), %r15
movq 0xb8(%rsp), %rbp
movss %xmm0, (%rsp)
movslq %ecx, %rax
cmpq %rax, %r14
jge 0x4baa
movq 0x50(%rsp), %rdi
addq %rbx, %rdi
callq 0x54dc
movss (%rsp), %xmm1
addss %xmm0, %xmm1
movaps %xmm1, %xmm0
incq %r14
movl 0x1430(%r13), %ecx
addq $0x10, %rbx
jmp 0x4b74
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x551c
movq 0x90(%rsp), %r14
movss (%rsp), %xmm0
andl $0x0, 0x50(%rsp)
movss %xmm0, 0x54(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x553e
movss %xmm0, (%rsp)
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x4c70
movq %r15, %rdi
movq %r12, %rsi
callq 0x54a2
testq %rdx, (%rax)
jne 0x4c1a
movq 0x70(%rsp), %rax
movss (%rsp), %xmm1
subss (%rax,%r12,4), %xmm1
xorps %xmm0, %xmm0
movss %xmm1, (%rsp)
ucomiss %xmm1, %xmm0
jae 0x4c23
incq %r12
addq $0x40, %rbx
jmp 0x4be4
movl 0x9c(%r13), %eax
movq 0x80(%r13), %rcx
imulq $0x18, 0x28(%rsp), %rdx
addq (%r13), %rdx
testl %eax, %eax
movl $0x0, %esi
cmovlel %esi, %eax
xorl %esi, %esi
cmpq %rsi, %rax
je 0x4c62
movq (%rcx,%rbx), %rdi
movss (%rdi,%rsi,4), %xmm0
movq (%rdx), %rdi
movss %xmm0, (%rdi,%rsi,4)
incq %rsi
jmp 0x4c47
movq %r15, %rdi
movq %r12, %rsi
callq 0x54a2
orq %rdx, (%rax)
movq 0x28(%rsp), %rcx
incq %rcx
jmp 0x47fe
leaq 0x70(%rsp), %rdi
callq 0x37f0
leaq 0xd0(%rsp), %rdi
callq 0x5546
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4d7d
jmp 0x4d7d
jmp 0x4d7d
jmp 0x4d7d
jmp 0x4d7d
jmp 0x4ceb
movq %rax, %rbx
movq %r13, %rdi
callq 0x65d8
leaq 0x10(%rsp), %rdi
callq 0x5134
jmp 0x4d8a
movq %rax, %rbx
jmp 0x4d94
movq %rdx, %r14
movq %rax, %rbx
jmp 0x4d21
movq %rax, %rbx
jmp 0x4d8a
movq %rdx, %r14
movq %rax, %rbx
jmp 0x4d2d
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
jmp 0x4d8f
movq %rdx, %r14
movq %rax, %rbx
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x4d21
movq (%rdi), %rax
callq *0x8(%rax)
addq $0x40, %r12
movq %r12, %rdi
callq 0x6578
movq %r13, %rdi
callq 0x65a2
leaq 0x10(%rsp), %rdi
callq 0x4e92
jmp 0x4d47
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x4d8a
movq %rbx, %rdi
callq 0x30d0
leaq 0x10(%rax), %rbx
callq 0x3150
leaq 0xa0(%rsp), %rsi
movl $0xb, (%rsi)
movq %rax, 0x8(%rsi)
movq %rbx, %rdi
callq 0x6531
callq 0x33a0
movq %rax, %rbx
jmp 0x4d9e
movq %rax, %rbx
callq 0x33e0
leaq 0x38(%rsp), %rdi
callq 0x55d8
leaq 0x50(%rsp), %rdi
callq 0x551c
leaq 0x70(%rsp), %rdi
callq 0x37f0
leaq 0xd0(%rsp), %rdi
callq 0x5546
movq %rbx, %rdi
callq 0x3450
movq %rax, %rdi
callq 0x378b
nop