// File: Pyboon[P]sage/test_mpi_sage.cpp
// Function: main
int main(int argc, char** argv) {
    vector<int> spots = {11, 14, 17};
    vector<pair<int, int> > file_nums = ParseFileNum(spots);
    int file_size = file_nums.size();
    // config init
    string home_path = getenv("HOME");
    string path_cir = home_path + "/code/sage/data/low_fre_MIMO/cut_cir_bin/";
    string path_antenna = home_path + "/code/sage/data/antenna_bin/";
    string path_result  = home_path + "/code/sage/data/result/";
    IsiSage test_Sage(path_cir, path_antenna, path_result, 20, 50); 

    double start_time, end_time;
    MPI_Init(&argc, &argv);
    start_time = MPI_Wtime();

    // Get the number of processes
    int comm_size;
    MPI_Comm_size(MPI_COMM_WORLD, &comm_size);

    // Get the rank of the process
    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    // Get the name of the processor
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int name_len;
    MPI_Get_processor_name(processor_name, &name_len);

    int local_size = floor(file_size / (double)comm_size);
    int left = file_size - local_size*comm_size;
    int start = 0;
    int end = 0;
    if(rank<left){
        start = (local_size+1) * rank;
        end = (local_size+1) * (rank+1);
    } else {
        start = (local_size+1)*left + (rank-left)*local_size;
        end = (local_size+1)*left + (rank-left+1)*local_size;
    }
    int spot = 0;
    int cycle_idx = 0;
    int count = 0;
    for(int i = start; i < end; ++i){
        spot = file_nums[i].first;
        cycle_idx = file_nums[i].second;
        printf("running spot_%d_cyc_%d from rank %d \n", spot, cycle_idx, rank);
        test_Sage.ConfigInit(spot, cycle_idx);
        test_Sage.run();
        ++count;
    }
    end_time = MPI_Wtime();
    printf("[%s:rank-%d]Calculation Finished, total cycle:%d, elapsed time:%lfs \n", 
    processor_name, rank, count, end_time - start_time);
    
    // Finalize the MPI environment. No more MPI calls can be made after this
    MPI_Finalize();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x560, %rsp            # imm = 0x560
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0xb, -0x44(%rbp)
movl $0xe, -0x40(%rbp)
movl $0x11, -0x3c(%rbp)
leaq -0x44(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x3, -0x30(%rbp)
leaq -0x45(%rbp), %rdi
movq %rdi, -0x530(%rbp)
callq 0xb0f0
movq -0x530(%rbp), %rcx
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0x28(%rbp), %rdi
callq 0xb110
jmp 0xa2d8
leaq -0x45(%rbp), %rdi
callq 0xb190
leaq -0x88(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0xb1b0
jmp 0xa2f3
leaq -0x70(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0xa170
jmp 0xa305
leaq -0x88(%rbp), %rdi
callq 0xb2a0
leaq -0x70(%rbp), %rdi
callq 0xb300
movl %eax, -0x8c(%rbp)
leaq 0x63d09(%rip), %rdi     # 0x6e030
callq 0x9950
movq %rax, -0x540(%rbp)
leaq -0xb1(%rbp), %rdi
movq %rdi, -0x538(%rbp)
callq 0x9dd0
movq -0x540(%rbp), %rsi
movq -0x538(%rbp), %rdx
leaq -0xb0(%rbp), %rdi
callq 0xb320
jmp 0xa362
leaq -0xb1(%rbp), %rdi
callq 0x9840
leaq 0x63cc0(%rip), %rdx     # 0x6e035
leaq -0xd8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0xb3d0
jmp 0xa38a
leaq 0x63cce(%rip), %rdx     # 0x6e05f
leaq -0xf8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0xb3d0
jmp 0xa3a6
leaq 0x63ccf(%rip), %rdx     # 0x6e07c
leaq -0x118(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0xb3d0
jmp 0xa3c2
leaq -0x3a0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x92c0
jmp 0xa3d7
leaq -0x3c0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x92c0
jmp 0xa3ec
leaq -0x3e0(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x92c0
jmp 0xa401
leaq -0x380(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
leaq -0x3c0(%rbp), %rdx
leaq -0x3e0(%rbp), %rcx
movl $0x14, %r8d
movl $0x32, %r9d
callq 0xb450
jmp 0xa430
leaq -0x3e0(%rbp), %rdi
callq 0x9470
leaq -0x3c0(%rbp), %rdi
callq 0x9470
leaq -0x3a0(%rbp), %rdi
callq 0x9470
leaq -0x8(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x9b20
jmp 0xa463
callq 0x99d0
movsd %xmm0, -0x548(%rbp)
jmp 0xa472
movsd -0x548(%rbp), %xmm0
movsd %xmm0, -0x3e8(%rbp)
movq 0xaab0f(%rip), %rdi     # 0xb4f98
leaq -0x3f4(%rbp), %rsi
callq 0x9900
jmp 0xa497
movq 0xaaafa(%rip), %rdi     # 0xb4f98
leaq -0x3f8(%rbp), %rsi
callq 0x9b10
jmp 0xa4ac
leaq -0x500(%rbp), %rdi
leaq -0x504(%rbp), %rsi
callq 0x98d0
jmp 0xa4c1
cvtsi2sdl -0x8c(%rbp), %xmm0
cvtsi2sdl -0x3f4(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x9d20
cvttsd2si %xmm0, %eax
movl %eax, -0x508(%rbp)
movl -0x8c(%rbp), %eax
movl -0x508(%rbp), %ecx
imull -0x3f4(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x50c(%rbp)
movl $0x0, -0x510(%rbp)
movl $0x0, -0x514(%rbp)
movl -0x3f8(%rbp), %eax
cmpl -0x50c(%rbp), %eax
jge 0xa672
movl -0x508(%rbp), %eax
addl $0x1, %eax
imull -0x3f8(%rbp), %eax
movl %eax, -0x510(%rbp)
movl -0x508(%rbp), %eax
addl $0x1, %eax
movl -0x3f8(%rbp), %ecx
addl $0x1, %ecx
imull %ecx, %eax
movl %eax, -0x514(%rbp)
jmp 0xa6cb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x45(%rbp), %rdi
callq 0xb190
jmp 0xa8da
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xa8d1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x88(%rbp), %rdi
callq 0xb2a0
jmp 0xa8d1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xb1(%rbp), %rdi
callq 0x9840
jmp 0xa8c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xa8bc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xa8b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xa8a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xa898
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xa644
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xa638
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x3e0(%rbp), %rdi
callq 0x9470
leaq -0x3c0(%rbp), %rdi
callq 0x9470
leaq -0x3a0(%rbp), %rdi
callq 0x9470
jmp 0xa898
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x380(%rbp), %rdi
callq 0xb590
jmp 0xa898
movl -0x508(%rbp), %eax
addl $0x1, %eax
imull -0x50c(%rbp), %eax
movl -0x3f8(%rbp), %ecx
subl -0x50c(%rbp), %ecx
imull -0x508(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x510(%rbp)
movl -0x508(%rbp), %eax
addl $0x1, %eax
imull -0x50c(%rbp), %eax
movl -0x3f8(%rbp), %ecx
subl -0x50c(%rbp), %ecx
addl $0x1, %ecx
imull -0x508(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x514(%rbp)
movl $0x0, -0x518(%rbp)
movl $0x0, -0x51c(%rbp)
movl $0x0, -0x520(%rbp)
movl -0x510(%rbp), %eax
movl %eax, -0x524(%rbp)
movl -0x524(%rbp), %eax
cmpl -0x514(%rbp), %eax
jge 0xa7e1
movslq -0x524(%rbp), %rsi
leaq -0x70(%rbp), %rdi
movq %rdi, -0x550(%rbp)
callq 0xb570
movq -0x550(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x518(%rbp)
movslq -0x524(%rbp), %rsi
callq 0xb570
movl 0x4(%rax), %eax
movl %eax, -0x51c(%rbp)
movl -0x518(%rbp), %esi
movl -0x51c(%rbp), %edx
movl -0x3f8(%rbp), %ecx
leaq 0x63939(%rip), %rdi     # 0x6e094
xorl %eax, %eax
callq 0x9060
jmp 0xa764
movl -0x518(%rbp), %esi
movl -0x51c(%rbp), %edx
movq %rsp, %rax
movl $0x4, (%rax)
leaq -0x380(%rbp), %rdi
movl $0x20, %ecx
movl $0x38, %r8d
movl $0x7f, %r9d
movsd 0x6386f(%rip), %xmm0    # 0x6e008
movsd 0x6386f(%rip), %xmm1    # 0x6e010
movsd 0x6386f(%rip), %xmm2    # 0x6e018
callq 0x18b20
jmp 0xa7b0
leaq -0x380(%rbp), %rdi
callq 0x1f970
jmp 0xa7be
movl -0x520(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x520(%rbp)
movl -0x524(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x524(%rbp)
jmp 0xa6f5
callq 0x99d0
movsd %xmm0, -0x558(%rbp)
jmp 0xa7f0
movsd -0x558(%rbp), %xmm0
movsd %xmm0, -0x3f0(%rbp)
movl -0x3f8(%rbp), %edx
movl -0x520(%rbp), %ecx
movsd -0x3f0(%rbp), %xmm0
movsd -0x3e8(%rbp), %xmm1
subsd %xmm1, %xmm0
leaq 0x63893(%rip), %rdi     # 0x6e0ba
leaq -0x500(%rbp), %rsi
movb $0x1, %al
callq 0x9060
jmp 0xa837
callq 0x95c0
jmp 0xa83e
leaq -0x380(%rbp), %rdi
callq 0xb590
leaq -0x118(%rbp), %rdi
callq 0x9470
leaq -0xf8(%rbp), %rdi
callq 0x9470
leaq -0xd8(%rbp), %rdi
callq 0x9470
leaq -0xb0(%rbp), %rdi
callq 0x9470
leaq -0x70(%rbp), %rdi
callq 0xb090
leaq -0x28(%rbp), %rdi
callq 0xb2a0
movl -0x4(%rbp), %eax
addq $0x560, %rsp            # imm = 0x560
popq %rbp
retq
leaq -0x118(%rbp), %rdi
callq 0x9470
leaq -0xf8(%rbp), %rdi
callq 0x9470
leaq -0xd8(%rbp), %rdi
callq 0x9470
leaq -0xb0(%rbp), %rdi
callq 0x9470
leaq -0x70(%rbp), %rdi
callq 0xb090
leaq -0x28(%rbp), %rdi
callq 0xb2a0
movq -0x50(%rbp), %rdi
callq 0x9dc0
nopw %cs:(%rax,%rax)
nopl (%rax)