// File: /workspace/github_cpp_makefile_star2_O1/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 0x42ac(%rip), %r14      # 0x9fd0
leaq 0x14d1(%rip), %rsi      # 0x71fc
movl $0xe, %edx
movq %r14, %rdi
callq 0x2240
movq %rbx, %rdi
movq %r15, %rsi
callq 0x22c0
movq 0x428e(%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 0x4244(%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 0x1452(%rip), %rsi      # 0x720b
movl $0xc, %edx
movq %rax, %rdi
callq 0x2240
movl (%r12), %esi
movq %r14, %rdi
callq 0x2340
movq %rax, %r14
leaq 0x143c(%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 0x5f97
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 0x4e50
leaq -0x78(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1300(%rip), %rsi      # 0x7166
leaq 0x1302(%rip), %rdx      # 0x716f
leaq -0x88(%rbp), %rdi
callq 0x630c
leaq -0x52720(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x52a2
movq -0x88(%rbp), %rdi
cmpq %r14, %rdi
je 0x5ea4
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x58(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1309(%rip), %rsi      # 0x71bc
leaq 0x130a(%rip), %rdx      # 0x71c4
leaq -0x68(%rbp), %rdi
callq 0x630c
leaq -0x52720(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x55f6
movq -0x68(%rbp), %rdi
cmpq %r14, %rdi
je 0x5ee8
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x52720(%rbp), %rdi
movl $0x4b0, %esi            # imm = 0x4B0
callq 0x5054
leaq -0x52720(%rbp), %rdi
callq 0x5224
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 0x5f10
incq %rcx
addq $0x6c, %rdx
cmpq $0x617, %rcx            # imm = 0x617
jne 0x5f0a
movq -0x38(%rbp), %rdx
movq 0x407c(%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 0x406b(%rip), %rdx      # 0x9fd8
movq 0x4044(%rip), %r9       # 0x9fb8
movq %rax, %rdi
movl $0x1, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2190
leaq -0x52720(%rbp), %rdi
callq 0x5944
jmp 0x6291
leaq -0x52720(%rbp), %rbx
movl $0x52368, %edx          # imm = 0x52368
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2120
leaq 0x126b(%rip), %rsi      # 0x721f
leaq -0x2a8(%rbp), %r14
movq %r14, %rdi
movl $0x10, %edx
callq 0x2220
leaq 0x125b(%rip), %rsi      # 0x722a
movl $0x8, %edx
movq %r14, %rdi
callq 0x2240
leaq 0x1226(%rip), %rsi      # 0x7209
leaq -0x2a8(%rbp), %rdi
movl $0x1, %edx
callq 0x2240
cmpl $0x2, -0x2c(%rbp)
jl 0x60ab
movl $0x1, %r14d
movq 0x3fad(%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 0x6060
incq %rdx
addq $0x6c, %rcx
addq $0xd8, %rax
cmpq $0x617, %rdx            # imm = 0x617
jne 0x605e
movl $0x291b4, %esi          # imm = 0x291B4
movq %r12, %rdi
callq 0x2200
incl %r14d
cmpl -0x2c(%rbp), %r14d
jl 0x6012
movq 0x3f1e(%rip), %rdi      # 0x9fd0
leaq 0x117a(%rip), %rsi      # 0x7233
movl $0x1e, %edx
callq 0x2240
leaq -0x3b0(%rbp), %r14
movq %r14, %rdi
callq 0x2100
movq 0x3ef7(%rip), %rdi      # 0x9fd0
movq %r14, %rsi
movq %rax, %rdx
callq 0x2240
movq 0x3ee5(%rip), %r14      # 0x9fd0
leaq 0xf98(%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 0x3eb3(%rip), %rdi      # 0x9fd0
callq 0x2040
movq %rax, %rdi
callq 0x2150
xorl %r14d, %r14d
leaq 0x10d5(%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 0x613e
leaq -0x2a8(%rbp), %rdi
movl %r14d, %esi
callq 0x2340
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0x10cf(%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 0x6134
movq 0x3e0f(%rip), %rbx      # 0x9fd0
leaq 0x108c(%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 0x3ddd(%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 0x625b
movl $0x1, %r14d
movq 0x3da6(%rip), %r15      # 0x9fd8
movq 0x3d7f(%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 0x6239
movq 0x3d66(%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 0x62f5
jmp 0x62de
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r14, %rdi
je 0x62e1
movq -0x58(%rbp), %rsi
jmp 0x62d0
jmp 0x62de
movq %rax, %rbx
movq -0x88(%rbp), %rdi
cmpq %r14, %rdi
je 0x62e1
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x62e1
jmp 0x62de
jmp 0x62de
movq %rax, %rbx
leaq -0x52720(%rbp), %rdi
callq 0x5944
jmp 0x6304
jmp 0x62f5
jmp 0x62f5
jmp 0x62f5
movq %rax, %rbx
leaq -0x2a8(%rbp), %rdi
callq 0x22f0
movq %rbx, %rdi
callq 0x2350