// File: liujan[P]MPI_multi_thread_randomforest/RandomForest.cpp
// Function: RandomForest::buildTree(int)
bool RandomForest::buildTree(int num){
	if (all_samples.empty()){
		cout << "[Error] all sample is empty" << endl;
		return false;
	}

	memset(result, 0, sizeof(result));
	r = new int*[numOfTest];
	for (int i = 0; i < numOfTest; i++){
		r[i] = new int[numOfTypes+1];
		for (int j = 0; j < numOfTypes + 1; j++)
			r[i][j] = 0;
	}

	for (int i = 0; i < num; i+=numOfThread){
		pthread_t thread[numOfThread];
		M *m = new M[numOfThread];
		for (int j = 0; j < numOfThread; j++){
			m[j].rf = this;
			m[j].i = j;
			pthread_create(&thread[j], NULL, build, &(m[j]));
		}
		for (int j = 0; j < numOfThread; j++)
			pthread_join(thread[j], NULL);
		
		delete[] m;
	}
	

	for (int i = 0; i < numOfTest; i++){
		for (int j = 0; j < numOfTypes + 1; j++){
			for (int k = 0; k < numOfThread; k++){
				r[i][j] += result[k][i][j];
			}
		}
	}
	
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x88(%rbp)
callq 0xda40
testb $0x1, %al
jne 0x15888
jmp 0x158b3
movq 0x13741(%rip), %rdi     # 0x28fd0
leaq 0x788f(%rip), %rsi      # 0x1d125
callq 0x9660
movq %rax, %rdi
movq 0x13703(%rip), %rsi     # 0x28fa8
callq 0x96b0
movb $0x0, -0x1(%rbp)
jmp 0x15adb
leaq 0x13e56(%rip), %rdi     # 0x29710
xorl %esi, %esi
movl $0x148da0, %edx         # imm = 0x148DA0
callq 0x9310
movl $0x30b8, %edi           # imm = 0x30B8
callq 0x9030
movq %rax, %rcx
movq -0x88(%rbp), %rax
movq %rcx, 0x30(%rax)
movl $0x0, -0x18(%rbp)
cmpl $0x617, -0x18(%rbp)     # imm = 0x617
jge 0x15951
movl $0x6c, %edi
callq 0x9030
movq %rax, %rdx
movq -0x88(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x18(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x0, -0x1c(%rbp)
cmpl $0x1b, -0x1c(%rbp)
jge 0x15944
movq -0x88(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x1c(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x15915
jmp 0x15946
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x158e5
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x15a3d
movl $0x80, %edi
callq 0x9030
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
cmpl $0x8, -0x6c(%rbp)
jge 0x159e6
movq -0x88(%rbp), %rcx
movq -0x68(%rbp), %rax
movslq -0x6c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl -0x6c(%rbp), %ecx
movq -0x68(%rbp), %rax
movslq -0x6c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movslq -0x6c(%rbp), %rax
leaq -0x60(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
movq -0x68(%rbp), %rcx
movslq -0x6c(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0x416(%rip), %rdx      # 0x155c0
callq 0x9c80
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x15979
movl $0x0, -0x70(%rbp)
cmpl $0x8, -0x70(%rbp)
jge 0x15a10
movslq -0x70(%rbp), %rax
movq -0x60(%rbp,%rax,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x95e0
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x159ed
movq -0x68(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
je 0x15a2d
movq -0x90(%rbp), %rdi
callq 0x98a0
jmp 0x15a2f
movl -0x20(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x20(%rbp)
jmp 0x15958
movl $0x0, -0x74(%rbp)
cmpl $0x617, -0x74(%rbp)     # imm = 0x617
jge 0x15ad7
movl $0x0, -0x78(%rbp)
cmpl $0x1b, -0x78(%rbp)
jge 0x15ac7
movl $0x0, -0x7c(%rbp)
cmpl $0x8, -0x7c(%rbp)
jge 0x15aba
movq -0x88(%rbp), %rax
movslq -0x7c(%rbp), %rdx
leaq 0x13c93(%rip), %rcx     # 0x29710
imulq $0x291b4, %rdx, %rdx    # imm = 0x291B4
addq %rdx, %rcx
movslq -0x74(%rbp), %rdx
imulq $0x6c, %rdx, %rdx
addq %rdx, %rcx
movslq -0x78(%rbp), %rdx
movl (%rcx,%rdx,4), %edx
movq 0x30(%rax), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x78(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x15a65
jmp 0x15abc
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x15a58
jmp 0x15ac9
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x15a44
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)