// File: /workspace/github_cpp_makefile_star2_O2/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, 0x40(%rsp)
movabsq $0xe0000000b, %rax      # imm = 0xE0000000B
leaq 0x260(%rsp), %rsi
movq %rax, (%rsi)
movl $0x11, 0x8(%rsi)
leaq 0x148(%rsp), %rbx
pushq $0x3
popq %rdx
leaq 0x160(%rsp), %rcx
movq %rbx, %rdi
callq 0x3ada
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x3b10
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x362c
leaq 0x70(%rsp), %rdi
callq 0x422c
movq 0x30(%rsp), %rbx
subq 0x28(%rsp), %rbx
leaq 0x188dd(%rip), %rdi     # 0x1c040
callq 0x32d0
leaq 0x50(%rsp), %rdi
leaq 0x260(%rsp), %rdx
movq %rax, %rsi
callq 0x3b5e
leaq 0x188c1(%rip), %rdx     # 0x1c045
leaq 0x128(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x3b99
leaq 0x188d2(%rip), %rdx     # 0x1c06f
leaq 0x108(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x3b99
leaq 0x188d6(%rip), %rdx     # 0x1c08c
leaq 0xe8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x3b99
leaq 0xc8(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x3330
leaq 0xa8(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x3330
leaq 0x88(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x3330
leaq 0x260(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
leaq 0x88(%rsp), %rcx
pushq $0x14
popq %r8
pushq $0x32
popq %r9
callq 0x3bd2
leaq 0x88(%rsp), %rdi
callq 0x3100
leaq 0xa8(%rsp), %rdi
callq 0x3100
leaq 0xc8(%rsp), %rdi
callq 0x3100
leaq 0x1c(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3350
callq 0x32f0
movsd %xmm0, 0x20(%rsp)
movq 0x3672c(%rip), %rdi     # 0x39fa8
leaq 0x18(%rsp), %rsi
callq 0x32b0
movq 0x3671b(%rip), %rdi     # 0x39fa8
leaq 0x14(%rsp), %rsi
callq 0x3340
leaq 0x160(%rsp), %rdi
leaq 0x4c(%rsp), %rsi
callq 0x3280
shrq $0x3, %rbx
cvtsi2sd %ebx, %xmm0
movl 0x18(%rsp), %ebp
cvtsi2sd %ebp, %xmm1
divsd %xmm1, %xmm0
callq 0x3400
cvttsd2si %xmm0, %ecx
imull %ecx, %ebp
subl %ebp, %ebx
movl 0x14(%rsp), %edx
leal 0x1(%rcx), %edi
movl %edx, %esi
subl %ebx, %esi
jge 0x38e4
movl %edi, %eax
imull %edx, %eax
incl %edx
imull %edi, %edx
jmp 0x38f7
imull %edi, %ebx
movl %esi, %eax
imull %ecx, %eax
addl %ebx, %eax
incl %esi
imull %ecx, %esi
addl %ebx, %esi
movl %esi, %edx
movslq %eax, %r12
movslq %edx, %r13
leaq 0x260(%rsp), %r15
xorl %ebx, %ebx
cmpq %r13, %r12
jge 0x3977
movq 0x28(%rsp), %rax
movl (%rax,%r12,8), %r14d
movl 0x4(%rax,%r12,8), %ebp
movl 0x14(%rsp), %ecx
leaq 0x1877f(%rip), %rdi     # 0x1c0a4
movl %r14d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x3030
movl $0x4, (%rsp)
movq %r15, %rdi
movl %r14d, %esi
movl %ebp, %edx
pushq $0x20
popq %rcx
pushq $0x38
popq %r8
pushq $0x7f
popq %r9
movsd 0x186b5(%rip), %xmm0    # 0x1c008
movsd 0x186b5(%rip), %xmm1    # 0x1c010
movsd 0x186b5(%rip), %xmm2    # 0x1c018
callq 0x620c
movq %r15, %rdi
callq 0xa87a
incl %ebx
incq %r12
jmp 0x3907
callq 0x32f0
movl 0x14(%rsp), %edx
subsd 0x20(%rsp), %xmm0
leaq 0x1873d(%rip), %rdi     # 0x1c0ca
leaq 0x160(%rsp), %rsi
movl %ebx, %ecx
movb $0x1, %al
callq 0x3030
callq 0x31d0
leaq 0x260(%rsp), %rdi
callq 0x3c92
leaq 0xe8(%rsp), %rdi
callq 0x3100
leaq 0x108(%rsp), %rdi
callq 0x3100
leaq 0x128(%rsp), %rdi
callq 0x3100
leaq 0x50(%rsp), %rdi
callq 0x3100
leaq 0x28(%rsp), %rdi
callq 0x401e
leaq 0x148(%rsp), %rdi
callq 0x422c
xorl %eax, %eax
addq $0x4c8, %rsp            # imm = 0x4C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3a79
jmp 0x3a79
jmp 0x3a79
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x3100
jmp 0x3a27
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x3100
jmp 0x3a39
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x3100
jmp 0x3a89
movq %rax, %rbx
jmp 0x3a89
movq %rax, %rbx
jmp 0x3a96
movq %rax, %rbx
jmp 0x3aa3
movq %rax, %rbx
jmp 0x3ab0
movq %rax, %rbx
jmp 0x3aba
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x422c
jmp 0x3ac4
movq %rax, %rbx
jmp 0x3ac4
jmp 0x3a79
jmp 0x3a79
movq %rax, %rbx
leaq 0x260(%rsp), %rdi
callq 0x3c92
leaq 0xe8(%rsp), %rdi
callq 0x3100
leaq 0x108(%rsp), %rdi
callq 0x3100
leaq 0x128(%rsp), %rdi
callq 0x3100
leaq 0x50(%rsp), %rdi
callq 0x3100
leaq 0x28(%rsp), %rdi
callq 0x401e
leaq 0x148(%rsp), %rdi
callq 0x422c
movq %rbx, %rdi
callq 0x3430
nop