// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4c8, %rsp            # imm = 0x4C8
movl %edi, 0x1c(%rsp)
movq %rsi, 0xd8(%rsp)
movabsq $0xe0000000b, %rax      # imm = 0xE0000000B
leaq 0x260(%rsp), %rsi
movq %rax, (%rsi)
movl $0x11, 0x8(%rsi)
leaq 0x148(%rsp), %rbx
leaq 0x160(%rsp), %rcx
movl $0x3, %edx
movq %rbx, %rdi
callq 0x3dae
leaq 0xc0(%rsp), %rdi
movq %rbx, %rsi
callq 0x3df4
leaq 0xa0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x368c
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x37ee
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x32a0
movq 0xa8(%rsp), %rbx
subq 0xa0(%rsp), %rbx
leaq 0x1883b(%rip), %rdi     # 0x1c040
callq 0x3320
leaq 0x80(%rsp), %rdi
leaq 0x260(%rsp), %rdx
movq %rax, %rsi
callq 0x3e74
leaq 0x70(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
addq %rsi, %rdx
leaq 0x60(%rsp), %rdi
callq 0x4336
leaq 0x187f6(%rip), %rsi     # 0x1c045
leaq 0x60(%rsp), %rdi
callq 0x34a0
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x4336
leaq 0x187e9(%rip), %rsi     # 0x1c06f
leaq 0x40(%rsp), %rdi
callq 0x34a0
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
addq %rsi, %rdx
leaq 0x20(%rsp), %rdi
callq 0x4336
leaq 0x187cf(%rip), %rsi     # 0x1c08c
leaq 0x20(%rsp), %rdi
callq 0x34a0
leaq 0x138(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
addq %rsi, %rdx
leaq 0x128(%rsp), %rdi
callq 0x4336
leaq 0x118(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
leaq 0x108(%rsp), %rdi
callq 0x4336
leaq 0xf8(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
leaq 0xe8(%rsp), %rdi
callq 0x4336
leaq 0x260(%rsp), %rdi
leaq 0x128(%rsp), %rsi
leaq 0x108(%rsp), %rdx
leaq 0xe8(%rsp), %rcx
movl $0x14, %r8d
movl $0x32, %r9d
callq 0x3eb2
movq 0xe8(%rsp), %rdi
cmpq %r12, %rdi
je 0x3988
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x32a0
movq 0x108(%rsp), %rdi
cmpq %r15, %rdi
je 0x39a5
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x32a0
movq 0x128(%rsp), %rdi
cmpq %r14, %rdi
je 0x39c2
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x32a0
leaq 0x1c(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x3380
callq 0x3340
movsd %xmm0, 0xb8(%rsp)
movq 0x2b5b7(%rip), %rdi     # 0x2efa0
leaq 0x18(%rsp), %rsi
callq 0x3300
movq 0x2b5a6(%rip), %rdi     # 0x2efa0
leaq 0x10(%rsp), %rsi
callq 0x3370
leaq 0x160(%rsp), %rdi
leaq 0xe4(%rsp), %rsi
callq 0x32c0
shrq $0x3, %rbx
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movl 0x18(%rsp), %ebp
cvtsi2sd %ebp, %xmm1
divsd %xmm1, %xmm0
callq 0x3420
cvttsd2si %xmm0, %ecx
imull %ecx, %ebp
subl %ebp, %ebx
movl 0x10(%rsp), %edi
leal 0x1(%rcx), %esi
movl %edi, %edx
subl %ebx, %edx
jge 0x3a57
movl %esi, %eax
imull %edi, %eax
incl %edi
imull %esi, %edi
jmp 0x3a6a
imull %esi, %ebx
movl %edx, %eax
imull %ecx, %eax
addl %ebx, %eax
incl %edx
imull %ecx, %edx
addl %ebx, %edx
movl %edx, %edi
xorl %ebx, %ebx
subl %eax, %edi
jle 0x3b07
movslq %eax, %r12
leaq 0x18626(%rip), %rbx     # 0x1c0a4
leaq 0x260(%rsp), %r14
movl %edi, 0x14(%rsp)
movl %edi, %r13d
movq 0xa0(%rsp), %rax
movl (%rax,%r12,8), %ebp
movl 0x4(%rax,%r12,8), %r15d
movl 0x10(%rsp), %ecx
movq %rbx, %rdi
movl %ebp, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x3040
movl $0x4, (%rsp)
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %edx
movl $0x20, %ecx
movl $0x38, %r8d
movl $0x7f, %r9d
movsd 0x1852f(%rip), %xmm0    # 0x1c008
movsd 0x1852f(%rip), %xmm1    # 0x1c010
movsd 0x1852f(%rip), %xmm2    # 0x1c018
callq 0x69fc
movq %r14, %rdi
callq 0xec20
incq %r12
decl %r13d
jne 0x3a8d
movl 0x14(%rsp), %ebx
leaq 0x70(%rsp), %r13
callq 0x3340
movl 0x10(%rsp), %edx
subsd 0xb8(%rsp), %xmm0
leaq 0x185aa(%rip), %rdi     # 0x1c0ca
leaq 0x160(%rsp), %rsi
movl %ebx, %ecx
movb $0x1, %al
callq 0x3040
callq 0x31f0
leaq 0x260(%rsp), %rdi
callq 0x4010
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3b5f
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x32a0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3b7b
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32a0
movq 0x60(%rsp), %rdi
cmpq %r13, %rdi
je 0x3b92
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x32a0
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3bb3
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x32a0
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x3bd0
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x32a0
movq 0x148(%rsp), %rdi
testq %rdi, %rdi
je 0x3bed
movq 0x158(%rsp), %rsi
subq %rdi, %rsi
callq 0x32a0
xorl %eax, %eax
addq $0x4c8, %rsp            # imm = 0x4C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3ce6
jmp 0x3ce6
jmp 0x3ce6
movq %rax, %rbx
movq 0xe8(%rsp), %rdi
cmpq %r12, %rdi
je 0x3c35
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x32a0
jmp 0x3c35
movq %rax, %rbx
movq 0x108(%rsp), %rdi
cmpq %r15, %rdi
je 0x3c57
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x32a0
jmp 0x3c57
movq %rax, %rbx
movq 0x128(%rsp), %rdi
cmpq %r14, %rdi
je 0x3cf6
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x32a0
jmp 0x3cf6
jmp 0x3c7c
movq %rax, %rbx
jmp 0x3cf6
jmp 0x3c83
movq %rax, %rbx
jmp 0x3d12
movq %rax, %rbx
jmp 0x3d2e
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r13, %rdi
jne 0x3d3d
jmp 0x3d4a
movq %rax, %rbx
jmp 0x3d4a
movq %rax, %rbx
jmp 0x3d6b
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x3d88
movq 0xd0(%rsp), %rsi
jmp 0x3d80
movq %rax, %rbx
jmp 0x3d88
jmp 0x3ce6
jmp 0x3ce6
movq %rax, %rbx
leaq 0x260(%rsp), %rdi
callq 0x4010
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3d12
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x32a0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3d2e
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32a0
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x3d4a
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x32a0
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3d6b
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x32a0
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x3d88
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x32a0
movq 0x148(%rsp), %rdi
testq %rdi, %rdi
je 0x3da5
movq 0x158(%rsp), %rsi
subq %rdi, %rsi
callq 0x32a0
movq %rbx, %rdi
callq 0x3450
nop