// File: /workspace/github_cpp_makefile_star2_O3/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 0x42d4(%rip), %r14      # 0x9fd0
leaq 0x14f9(%rip), %rsi      # 0x71fc
movl $0xe, %edx
movq %r14, %rdi
callq 0x2240
movq %rbx, %rdi
movq %r15, %rsi
callq 0x22c0
movq 0x42b6(%rip), %rcx      # 0x9fd8
leaq -0x38(%rbp), %rbx
movl $0x617, %edi            # imm = 0x617
movl $0x1b, %esi
movl $0x1b, %edx
movq %rbx, %r8
callq 0x2330
movq %rbx, %rdi
callq 0x2280
movq 0x426c(%rip), %r15      # 0x9fb8
leaq -0x40(%rbp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x22b0
leaq -0x2c(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2260
leaq -0x3b0(%rbp), %r15
leaq -0x3c(%rbp), %rsi
movq %r15, %rdi
callq 0x2230
movl (%rbx), %esi
movq %r14, %rdi
callq 0x2340
movq %rax, %r14
leaq 0x147a(%rip), %rsi      # 0x720b
movl $0xc, %edx
movq %rax, %rdi
callq 0x2240
movl (%r12), %esi
movq %r14, %rdi
callq 0x2340
movq %rax, %r14
leaq 0x1464(%rip), %rsi      # 0x7218
movl $0x6, %edx
movq %rax, %rdi
callq 0x2240
movq %r15, %rdi
callq 0x2100
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2240
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2150
cmpl $0x0, (%rbx)
je 0x5f6f
movl $0x291b4, %edi          # imm = 0x291B4
callq 0x21f0
movq %rax, %rbx
movl $0x291b4, %edx          # imm = 0x291B4
movq %rax, %rdi
xorl %esi, %esi
callq 0x2120
leaq -0x52720(%rbp), %rdi
callq 0x4e24
leaq -0x78(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1328(%rip), %rsi      # 0x7166
leaq 0x132a(%rip), %rdx      # 0x716f
leaq -0x88(%rbp), %rdi
callq 0x62e4
leaq -0x52720(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x5276
movq -0x88(%rbp), %rdi
cmpq %r14, %rdi
je 0x5e7c
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x58(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1331(%rip), %rsi      # 0x71bc
leaq 0x1332(%rip), %rdx      # 0x71c4
leaq -0x68(%rbp), %rdi
callq 0x62e4
leaq -0x52720(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x55ca
movq -0x68(%rbp), %rdi
cmpq %r14, %rdi
je 0x5ec0
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x52720(%rbp), %rdi
movl $0x4b0, %esi            # imm = 0x4B0
callq 0x5028
leaq -0x52720(%rbp), %rdi
callq 0x51f8
xorl %ecx, %ecx
movq %rbx, %rdx
movq (%rax,%rcx,8), %rsi
xorl %edi, %edi
movl (%rsi,%rdi,4), %r8d
movl %r8d, (%rdx,%rdi,4)
incq %rdi
cmpq $0x1b, %rdi
jne 0x5ee8
incq %rcx
addq $0x6c, %rdx
cmpq $0x617, %rcx            # imm = 0x617
jne 0x5ee2
movq -0x38(%rbp), %rdx
movq 0x40a4(%rip), %r9       # 0x9fb8
movq %rbx, %rdi
movl $0x1, %esi
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x20c0
movl $0x4, %edi
callq 0x21f0
leaq -0xa8(%rbp), %rcx
movq %rcx, (%rsp)
movq 0x4093(%rip), %rdx      # 0x9fd8
movq 0x406c(%rip), %r9       # 0x9fb8
movq %rax, %rdi
movl $0x1, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2190
leaq -0x52720(%rbp), %rdi
callq 0x5918
jmp 0x6269
leaq -0x52720(%rbp), %rbx
movl $0x52368, %edx          # imm = 0x52368
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2120
leaq 0x1293(%rip), %rsi      # 0x721f
leaq -0x2a8(%rbp), %r14
movq %r14, %rdi
movl $0x10, %edx
callq 0x2220
leaq 0x1283(%rip), %rsi      # 0x722a
movl $0x8, %edx
movq %r14, %rdi
callq 0x2240
leaq 0x124e(%rip), %rsi      # 0x7209
leaq -0x2a8(%rbp), %rdi
movl $0x1, %edx
callq 0x2240
cmpl $0x2, -0x2c(%rbp)
jl 0x6083
movl $0x1, %r14d
movq 0x3fd5(%rip), %r15      # 0x9fb8
leaq -0x52720(%rbp), %r13
movl $0x291b4, %edi          # imm = 0x291B4
callq 0x21f0
movq %rax, %r12
movl $0x291b4, %edx          # imm = 0x291B4
movq %rax, %rdi
xorl %esi, %esi
callq 0x2120
movq -0x38(%rbp), %rdx
leaq -0xa8(%rbp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movl $0x1, %esi
movl %r14d, %ecx
movl $0x1, %r8d
movq %r15, %r9
callq 0x2190
movq %r13, %rax
movq %r12, %rcx
xorl %edx, %edx
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsi2sdl (%rcx,%rsi,4), %xmm0
addsd (%rax,%rsi,8), %xmm0
movsd %xmm0, (%rax,%rsi,8)
incq %rsi
cmpq $0x1b, %rsi
jne 0x6038
incq %rdx
addq $0x6c, %rcx
addq $0xd8, %rax
cmpq $0x617, %rdx            # imm = 0x617
jne 0x6036
movl $0x291b4, %esi          # imm = 0x291B4
movq %r12, %rdi
callq 0x2200
incl %r14d
cmpl -0x2c(%rbp), %r14d
jl 0x5fea
movq 0x3f46(%rip), %rdi      # 0x9fd0
leaq 0x11a2(%rip), %rsi      # 0x7233
movl $0x1e, %edx
callq 0x2240
leaq -0x3b0(%rbp), %r14
movq %r14, %rdi
callq 0x2100
movq 0x3f1f(%rip), %rdi      # 0x9fd0
movq %r14, %rsi
movq %rax, %rdx
callq 0x2240
movq 0x3f0d(%rip), %r14      # 0x9fd0
leaq 0xfc0(%rip), %rsi       # 0x708a
movl $0x4, %edx
movq %r14, %rdi
callq 0x2240
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq 0x3edb(%rip), %rdi      # 0x9fd0
callq 0x2040
movq %rax, %rdi
callq 0x2150
xorl %r14d, %r14d
leaq 0x10fd(%rip), %r13      # 0x7209
movl $0xfffffff6, %eax       # imm = 0xFFFFFFF6
xorl %ecx, %ecx
xorl %r15d, %r15d
movsd (%rbx,%rcx,8), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
cvttsd2si %xmm0, %edx
cmovael %edx, %eax
cmovael %ecx, %r15d
incq %rcx
cmpq $0x1b, %rcx
jne 0x6116
leaq -0x2a8(%rbp), %rdi
movl %r14d, %esi
callq 0x2340
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0x10f7(%rip), %rsi      # 0x7252
callq 0x2240
movq %r12, %rdi
movl %r15d, %esi
callq 0x2340
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2240
incq %r14
addq $0xd8, %rbx
cmpq $0x617, %r14            # imm = 0x617
jne 0x610c
movq 0x3e37(%rip), %rbx      # 0x9fd0
leaq 0x10b4(%rip), %rsi      # 0x7254
movl $0xc, %edx
movq %rbx, %rdi
callq 0x2240
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq 0x3e05(%rip), %rdi      # 0x9fd0
callq 0x2040
movq %rax, %rdi
callq 0x2150
leaq -0x2a8(%rbp), %rdi
callq 0x22d0
movl $0x4, %edi
callq 0x21f0
movq %rax, %rbx
movl $0x17, (%rax)
cmpl $0x2, -0x2c(%rbp)
jl 0x6233
movl $0x1, %r14d
movq 0x3dce(%rip), %r15      # 0x9fd8
movq 0x3da7(%rip), %r12      # 0x9fb8
movq %rbx, %rdi
movl $0x1, %esi
movq %r15, %rdx
movl %r14d, %ecx
xorl %r8d, %r8d
movq %r12, %r9
callq 0x20c0
incl %r14d
cmpl -0x2c(%rbp), %r14d
jl 0x6211
movq 0x3d8e(%rip), %rax      # 0x9fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x2a0(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x2a8(%rbp,%rcx)
callq 0x21b0
leaq -0x1b0(%rbp), %rdi
callq 0x20b0
xorl %eax, %eax
addq $0x52708, %rsp          # imm = 0x52708
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x62cd
jmp 0x62b6
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r14, %rdi
je 0x62b9
movq -0x58(%rbp), %rsi
jmp 0x62a8
jmp 0x62b6
movq %rax, %rbx
movq -0x88(%rbp), %rdi
cmpq %r14, %rdi
je 0x62b9
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x62b9
jmp 0x62b6
jmp 0x62b6
movq %rax, %rbx
leaq -0x52720(%rbp), %rdi
callq 0x5918
jmp 0x62dc
jmp 0x62cd
jmp 0x62cd
jmp 0x62cd
movq %rax, %rbx
leaq -0x2a8(%rbp), %rdi
callq 0x22f0
movq %rbx, %rdi
callq 0x2350