// File: /workspace/github_cpp_makefile_star2_O2/liujan[P]MPI_multi_thread_randomforest/main.cpp
// Function: main
int main(int argc,  char * argv[]) {
	// insert code here...
	std::cout << "Hello, World!\n";

	int mypid, size, namelen;
    char name[MPI_MAX_PROCESSOR_NAME];
    MPI_Status status;
    MPI_Init(&argc, &argv);

    MPI_Datatype ctype;

    new_type(&ctype);

    MPI_Comm_rank(MPI_COMM_WORLD, &mypid); 
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Get_processor_name(name, &namelen);
    cout << mypid << " of process " << size << " from " << name << endl;   


    if (mypid == 0){
        double result[numOfTest][numOfTypes+1];
        memset(result, 0, sizeof(result));
        ofstream out("result.csv");
        out << "Id,label" << "\n";

        for (int i = 1; i < size; i++) {
            struct Sample *sample = new Sample();
            MPI_Recv(sample, 1, ctype, i, 1, MPI_COMM_WORLD, &status);

            for (int j = 0; j < numOfTest; j++){
                for (int k = 0; k <= numOfTypes; k++){
                    result[j][k] += sample->result[j][k];
                }
            }
            delete sample;
        }
        cout << "writing to file result.csv of " << name << " ..."<< endl;
        for (int i = 0; i < numOfTest; i++){
            int max_value = -10, index = 0;
            for (int j = 0; j <= numOfTypes; j++){
                if (result[i][j] >= max_value){
                    max_value = result[i][j];
                    index = j;
                }
            }
            out << i << "," << index << "\n";
        }
        cout << "writing over" << endl;
        out.close();

        int *tmp = new int;
        *tmp = 23;
        for (int i = 1; i < size; i++)
            MPI_Send(tmp, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
    }
    else{
        struct Sample *sample = new Sample();

        RandomForest forest;
        forest.readTrainSample("train.csv");
		forest.readTestSample("test.csv");
	    forest.buildTree(1200);
	    int **result = forest.predict();

        for (int i = 0; i < numOfTest; i++) {
            for (int j = 0; j <= numOfTypes; j++){
                sample->result[i][j] = result[i][j];
            }
        }
        
        MPI_Send(sample, 1, ctype, 0, 1, MPI_COMM_WORLD);
        int *tmp = new int;
        MPI_Recv(tmp, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
        
    }


	
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x52708, %rsp          # imm = 0x52708
leaq -0x44(%rbp), %rbx
movl %edi, (%rbx)
leaq -0x90(%rbp), %r15
movq %rsi, (%r15)
movq 0x468d(%rip), %r14      # 0x9fd0
leaq 0x8b2(%rip), %rsi       # 0x61fc
movq %r14, %rdi
callq 0x21e0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x22b0
leaq -0x38(%rbp), %rdi
callq 0x58e8
movq 0x4653(%rip), %r15      # 0x9fc0
leaq -0x40(%rbp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x22a0
leaq -0x2c(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2250
leaq -0x3b0(%rbp), %r15
leaq -0x3c(%rbp), %rsi
movq %r15, %rdi
callq 0x2230
movl (%rbx), %esi
movq %r14, %rdi
callq 0x2330
leaq 0x85c(%rip), %rsi       # 0x620b
movq %rax, %rdi
callq 0x21e0
movl (%r12), %esi
movq %rax, %rdi
callq 0x2330
leaq 0x84e(%rip), %rsi       # 0x6218
movq %rax, %rdi
callq 0x21e0
movq %rax, %rdi
movq %r15, %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
cmpl $0x0, (%rbx)
je 0x5b29
movl $0x291b4, %edi          # imm = 0x291B4
callq 0x21f0
movq %rax, %rbx
movl $0x291b4, %edx          # imm = 0x291B4
movq %rax, %rdi
xorl %esi, %esi
callq 0x2130
leaq -0x52720(%rbp), %rdi
callq 0x4e2c
leaq 0x749(%rip), %rsi       # 0x6166
leaq -0x88(%rbp), %rdi
leaq -0x2a8(%rbp), %rdx
callq 0x5dc0
leaq -0x52720(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x51c8
leaq -0x88(%rbp), %rdi
callq 0x2100
leaq 0x766(%rip), %rsi       # 0x61bc
leaq -0x68(%rbp), %rdi
leaq -0x2a8(%rbp), %rdx
callq 0x5dc0
leaq -0x52720(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x53f8
leaq -0x68(%rbp), %rdi
callq 0x2100
leaq -0x52720(%rbp), %rdi
movl $0x4b0, %esi            # imm = 0x4B0
callq 0x4fe6
leaq -0x52720(%rbp), %rdi
callq 0x5188
xorl %ecx, %ecx
movq %rbx, %rdx
cmpq $0x617, %rcx            # imm = 0x617
je 0x5acc
movq (%rax,%rcx,8), %rsi
xorl %edi, %edi
cmpq $0x1b, %rdi
je 0x5ac3
movl (%rsi,%rdi,4), %r8d
movl %r8d, (%rdx,%rdi,4)
incq %rdi
jmp 0x5ab0
incq %rcx
addq $0x6c, %rdx
jmp 0x5aa1
movq -0x38(%rbp), %rdx
movq 0x44e9(%rip), %r9       # 0x9fc0
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %ecx, %ecx
movl %esi, %r8d
callq 0x20b0
pushq $0x4
popq %rdi
callq 0x21f0
leaq -0xa8(%rbp), %rcx
movq %rcx, (%rsp)
movq 0x44d7(%rip), %rdx      # 0x9fd8
movq 0x44b8(%rip), %r9       # 0x9fc0
pushq $0x1
popq %rsi
movq %rax, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2190
leaq -0x52720(%rbp), %rdi
callq 0x561e
jmp 0x5d5a
leaq -0x52720(%rbp), %rdi
movl $0x52368, %edx          # imm = 0x52368
xorl %esi, %esi
callq 0x2130
leaq 0x6dc(%rip), %rsi       # 0x621f
leaq -0x2a8(%rbp), %r14
pushq $0x10
popq %rdx
movq %r14, %rdi
callq 0x2220
leaq 0x6ce(%rip), %rsi       # 0x622a
movq %r14, %rdi
callq 0x21e0
leaq 0x69e(%rip), %rsi       # 0x6209
movq %rax, %rdi
callq 0x21e0
pushq $0x1
popq %r14
leaq -0xa8(%rbp), %rbx
movq 0x443b(%rip), %r15      # 0x9fc0
movl %r14d, %r12d
cmpl -0x2c(%rbp), %r12d
jge 0x5c1f
movl $0x291b4, %edi          # imm = 0x291B4
callq 0x21f0
movq %rax, %r13
movl $0x291b4, %edx          # imm = 0x291B4
movq %rax, %rdi
xorl %esi, %esi
callq 0x2130
movq -0x38(%rbp), %rdx
movq %rbx, (%rsp)
movq %r13, %rdi
movl %r14d, %esi
movl %r12d, %ecx
movl %r14d, %r8d
movq %r15, %r9
callq 0x2190
leaq -0x52720(%rbp), %rax
movq %r13, %rcx
xorl %edx, %edx
cmpq $0x617, %rdx            # imm = 0x617
je 0x5c0a
xorl %esi, %esi
cmpq $0x1b, %rsi
je 0x5bfb
cvtsi2sdl (%rcx,%rsi,4), %xmm0
addsd (%rax,%rsi,8), %xmm0
movsd %xmm0, (%rax,%rsi,8)
incq %rsi
jmp 0x5be1
incq %rdx
addq $0x6c, %rcx
addq $0xd8, %rax
jmp 0x5bd6
movl $0x291b4, %esi          # imm = 0x291B4
movq %r13, %rdi
callq 0x2200
incl %r12d
jmp 0x5b88
movq 0x43aa(%rip), %rdi      # 0x9fd0
leaq 0x606(%rip), %rsi       # 0x6233
callq 0x21e0
leaq -0x3b0(%rbp), %rsi
movq %rax, %rdi
callq 0x21e0
leaq 0x442(%rip), %rsi       # 0x608a
movq %rax, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
leaq -0x52720(%rbp), %r14
xorl %ebx, %ebx
leaq 0x5ea(%rip), %r15       # 0x6252
leaq 0x59a(%rip), %r12       # 0x6209
cmpq $0x617, %rbx            # imm = 0x617
je 0x5cdf
xorl %eax, %eax
pushq $-0xa
popq %rcx
xorl %r13d, %r13d
cmpq $0x1b, %rax
je 0x5ca4
movsd (%r14,%rax,8), %xmm0
cvtsi2sd %ecx, %xmm1
ucomisd %xmm1, %xmm0
cvttsd2si %xmm0, %edx
cmovael %edx, %ecx
cmovael %eax, %r13d
incq %rax
jmp 0x5c80
leaq -0x2a8(%rbp), %rdi
movl %ebx, %esi
callq 0x2330
movq %rax, %rdi
movq %r15, %rsi
callq 0x21e0
movq %rax, %rdi
movl %r13d, %esi
callq 0x2330
movq %rax, %rdi
movq %r12, %rsi
callq 0x21e0
incq %rbx
addq $0xd8, %r14
jmp 0x5c6f
movq 0x42ea(%rip), %rdi      # 0x9fd0
leaq 0x567(%rip), %rsi       # 0x6254
callq 0x21e0
movq %rax, %rdi
callq 0x2110
leaq -0x2a8(%rbp), %rdi
callq 0x22c0
pushq $0x4
popq %rdi
callq 0x21f0
movq %rax, %rbx
movl $0x17, (%rax)
pushq $0x1
popq %r14
movq 0x42b6(%rip), %r15      # 0x9fd8
movq 0x4297(%rip), %r12      # 0x9fc0
movl %r14d, %r13d
cmpl -0x2c(%rbp), %r13d
jge 0x5d4e
movq %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
movl %r13d, %ecx
xorl %r8d, %r8d
movq %r12, %r9
callq 0x20b0
incl %r13d
jmp 0x5d2c
leaq -0x2a8(%rbp), %rdi
callq 0x22e0
xorl %eax, %eax
addq $0x52708, %rsp          # imm = 0x52708
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5da9
jmp 0x5d92
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
jmp 0x5d87
jmp 0x5d92
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x2100
jmp 0x5d95
jmp 0x5d92
jmp 0x5d92
movq %rax, %rbx
leaq -0x52720(%rbp), %rdi
callq 0x561e
jmp 0x5db8
jmp 0x5da9
jmp 0x5da9
jmp 0x5da9
movq %rax, %rbx
leaq -0x2a8(%rbp), %rdi
callq 0x22e0
movq %rbx, %rdi
callq 0x2340