// File: /workspace/github_cpp_makefile_star2_O1/WeihuaZhu[P]hpc_prog2/main.cpp
// Function: main
int main(int argc, char *argv[])
{
   // set up MPI
   MPI_Init(&argc, &argv);

   // get communicator size
   MPI_Comm comm = MPI_COMM_WORLD;
   int p;
   MPI_Comm_size(comm, &p);

   // Ax = b
   std::vector<double> A;
   std::vector<double> b;
   std::vector<double> x;


   /**********************************
    *  Parse command line arguments  *
    **********************************/
   bool write_output = false;
   std::string outfile_name;
   int n;
   bool rand_input = false;
   double difficulty = 0.5;
   std::string fileA;
   std::string fileB;
   if (argc < 3)
   {
      print_usage();
      exit(EXIT_FAILURE);
   }
   if (std::string(argv[1]) == "-n")
   {
      // randomly generate input
      n = atoi(argv[2]);
      if (!(n > 0))
      {
         print_usage();
         exit(EXIT_FAILURE);
      }
      if (argc == 5)
      {
         if (std::string(argv[3]) != "-d")
         {
            print_usage();
            exit(EXIT_FAILURE);
         }
         std::istringstream iss(argv[4]);
         iss >> difficulty;
      }
      rand_input = true;
   }
   else
   {
      if (argc != 4)
      {
         print_usage();
         exit(EXIT_FAILURE);
      }

      // get output filename
      outfile_name = std::string(argv[3]);
      write_output = true;
   }


   // start timer
   //   we omit the file loading and argument parsing from the runtime
   //   timings, we measure the time needed by the processor with rank 0
   struct timespec t_start, t_end;
   if (p > 1)
   {
      // get the dimensions
      int q = (int)sqrt(p);
      if (p != q*q)
      {
         throw std::runtime_error("The number of MPI processes must be a perfect square");
      }
      // create 2D cartesian grid for the processors (enable reordering)
      MPI_Comm grid_comm;
      int dims[2] = {q, q};
      int periods[2] = {0, 0};
      MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, 1, &grid_comm);
      // get the rank of process with coordinates (0,0)
      int rank00;
      int myrank;
      int coords[2] = {0, 0};
      MPI_Cart_rank(grid_comm, coords, &rank00);
      MPI_Comm_rank(grid_comm, &myrank);

      // process (0,0) loads input
      if (myrank == rank00)
      {
         if (rand_input) {
            // generate random input of the given size
            A = diag_dom_rand(n, difficulty);
            b = randn(n);
         } else {
            // read input from file
            A = read_binary_file<double>(argv[1]);
            b = read_binary_file<double>(argv[2]);
            n = b.size();
            if (A.size() != n*(size_t)n)
            {
               throw std::runtime_error("The input dimensions are not matching");
            }
         }
      }
      MPI_Bcast(&n, 1, MPI_INT, rank00, grid_comm);

      // start timer
      clock_gettime(CLOCK_MONOTONIC,  &t_start);

      // allocate output and run the parallel jacobi implementation
      if (myrank == rank00)
         x = std::vector<double>(n);
      mpi_jacobi(n, &A[0], &b[0], &x[0], grid_comm);

      if (myrank == rank00)
      {
         // get time
         clock_gettime(CLOCK_MONOTONIC,  &t_end);
         // time in seconds
         double time_secs = (t_end.tv_sec - t_start.tv_sec)
            + (double) (t_end.tv_nsec - t_start.tv_nsec) * 1e-9;
         // output time
         std::cerr << time_secs << std::endl;
         // write output
         if (write_output)
         {
            write_binary_file(outfile_name.c_str(), x);
         }
      }
   }
   else
   {
      std::cerr << "[WARNING]: Running the sequential solver. Start with mpirun to execute the parallel version." << std::endl;

      // get input
      if (rand_input) {
         // generate random input of the given size
         A = diag_dom_rand(n, difficulty);
         b = randn(n);
      } else {
         // read input from file
         A = read_binary_file<double>(argv[1]);
         b = read_binary_file<double>(argv[2]);
         n = b.size();
         if (A.size() != n*(size_t)n)
         {
            throw std::runtime_error("The input dimensions are not matching");
         }
      }

      clock_gettime(CLOCK_MONOTONIC,  &t_start);
      // sequential jacobi
      x = std::vector<double>(n);
      jacobi(n, &A[0], &b[0], &x[0]);
      // get time
      clock_gettime(CLOCK_MONOTONIC,  &t_end);
      // time in seconds
      double time_secs = (t_end.tv_sec - t_start.tv_sec)
         + (double) (t_end.tv_nsec - t_start.tv_nsec) * 1e-9;
      // output time
      std::cerr << time_secs << std::endl;
      // write output
      if (write_output)
      {
         write_binary_file(outfile_name.c_str(), x);
      }
   }

   // finalize MPI
   MPI_Finalize();
   return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
leaq 0x44(%rsp), %rbx
movl %edi, (%rbx)
leaq 0x10(%rsp), %rax
movq %rsi, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2320
movq 0x4529(%rip), %rdi      # 0x6f98
leaq 0x40(%rsp), %rsi
callq 0x2290
xorl %eax, %eax
movq %rax, 0x30(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
leaq 0x80(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movabsq $0x3fe0000000000000, %rcx # imm = 0x3FE0000000000000
movq %rcx, 0x48(%rsp)
leaq 0xc0(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0xa0(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
cmpl $0x2, (%rbx)
jle 0x3340
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0xe0(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x3576
leaq 0x2715(%rip), %rsi      # 0x520f
leaq 0xe0(%rsp), %rbx
movq %rbx, %rdi
callq 0x20c0
movl %eax, %ebp
movq (%rbx), %rdi
leaq 0xf0(%rsp), %r15
cmpq %r15, %rdi
je 0x2b2c
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x2260
testl %ebp, %ebp
je 0x2b8c
cmpl $0x4, 0x44(%rsp)
jne 0x3347
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rsi
leaq 0xe0(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x3576
leaq 0x70(%rsp), %rdi
leaq 0xe0(%rsp), %rbx
movq %rbx, %rsi
callq 0x22c0
movq (%rbx), %rdi
cmpq %r15, %rdi
je 0x2c80
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x2c80
movq 0x10(%rsp), %rbx
movq 0x10(%rbx), %rdi
callq 0x2370
movl %eax, 0x8(%rsp)
testl %eax, %eax
jle 0x3380
cmpl $0x5, 0x44(%rsp)
jne 0x2c80
movq 0x18(%rbx), %rsi
leaq 0xe0(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x3576
leaq 0x2644(%rip), %rsi      # 0x5212
leaq 0xe0(%rsp), %r14
movq %r14, %rdi
callq 0x20c0
movl %eax, %ebx
movq (%r14), %rdi
cmpq %r15, %rdi
je 0x2bf8
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x2260
testl %ebx, %ebx
jne 0x3387
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rsi
leaq 0x50(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
callq 0x3576
leaq 0xe0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movl $0x8, %edx
callq 0x23c0
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2c4d
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2260
leaq 0xe0(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x2160
movq 0x4352(%rip), %rsi      # 0x6fb8
leaq 0xe0(%rsp), %rdi
callq 0x2340
leaq 0x158(%rsp), %rdi
callq 0x20d0
movl 0x40(%rsp), %eax
cmpl $0x2, %eax
jl 0x2ca7
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2dc9
sqrtsd %xmm0, %xmm0
jmp 0x2dce
xorl %ebx, %ebx
movq 0x4338(%rip), %r13      # 0x6fe8
leaq 0x25b9(%rip), %rsi      # 0x5270
movl $0x5c, %edx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r13, %rdi
callq 0x2280
movq (%r13), %rax
addq -0x18(%rax), %r13
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r13, %rdi
movl $0xa, %esi
callq 0x2240
xorl %ebx, %ebx
movsbl %al, %esi
movq 0x42ef(%rip), %rdi      # 0x6fe8
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
callq 0x2040
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %rax, %rdi
callq 0x2170
testl %ebp, %ebp
je 0x2f29
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x35b3
leaq 0xe0(%rsp), %rdi
movq (%rdi), %rbx
movq 0x8(%rdi), %r13
movq 0x10(%rdi), %r14
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x35b3
movq 0xe0(%rsp), %r15
movq 0xe8(%rsp), %rax
movq 0xf0(%rsp), %r12
subq %r15, %rax
shrq $0x3, %rax
movl %eax, 0x8(%rsp)
subq %rbx, %r13
sarq $0x3, %r13
cltq
imulq %rax, %rax
cmpq %rax, %r13
je 0x2f74
movl $0x10, %edi
callq 0x2120
movq %rax, %rbp
leaq 0x24a4(%rip), %rsi      # 0x524a
movq %rax, %rdi
callq 0x2100
movq 0x423b(%rip), %rsi      # 0x6ff0
movq 0x4204(%rip), %rdx      # 0x6fc0
movq %rbp, %rdi
callq 0x23d0
jmp 0x3380
callq 0x21c0
cvttsd2si %xmm0, %eax
movl %eax, %ecx
imull %eax, %ecx
cmpl %ecx, 0x40(%rsp)
jne 0x334e
leaq 0x270(%rsp), %rdx
movl %eax, (%rdx)
movl %eax, 0x4(%rdx)
leaq 0x268(%rsp), %rcx
movq $0x0, (%rcx)
movq 0x4194(%rip), %rdi      # 0x6f98
leaq 0x38(%rsp), %r9
movl $0x2, %esi
movl $0x1, %r8d
callq 0x2050
leaq 0x260(%rsp), %rsi
movq $0x0, (%rsi)
xorl %ebx, %ebx
movq 0x38(%rsp), %rdi
leaq 0xc(%rsp), %rdx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
callq 0x2410
xorl %ebx, %ebx
movq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rsi
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
callq 0x2310
movl 0x18(%rsp), %eax
cmpl 0xc(%rsp), %eax
jne 0x2f19
testl %ebp, %ebp
je 0x30b6
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x35b3
leaq 0xe0(%rsp), %rdi
movq (%rdi), %rbx
movq 0x8(%rdi), %r13
movq 0x10(%rdi), %r14
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x35b3
movq 0xe0(%rsp), %r15
movq 0xe8(%rsp), %rax
movq 0xf0(%rsp), %r12
subq %r15, %rax
shrq $0x3, %rax
movl %eax, 0x8(%rsp)
subq %rbx, %r13
sarq $0x3, %r13
cltq
imulq %rax, %rax
cmpq %rax, %r13
je 0x3101
movl $0x10, %edi
callq 0x2120
movq %rax, %rbp
leaq 0x2354(%rip), %rsi      # 0x524a
movq %rax, %rdi
callq 0x2100
movq 0x40eb(%rip), %rsi      # 0x6ff0
movq 0x40b4(%rip), %rdx      # 0x6fc0
movq %rbp, %rdi
callq 0x23d0
jmp 0x3380
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x3101
movl 0x8(%rsp), %esi
movsd 0x48(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x27a0
leaq 0xe0(%rsp), %rdi
movq (%rdi), %rbx
movq 0x10(%rdi), %r14
movl 0x8(%rsp), %esi
movsd 0x20dd(%rip), %xmm1     # 0x5038
xorpd %xmm0, %xmm0
callq 0x273a
movq 0xe0(%rsp), %r15
movq 0xf0(%rsp), %r12
leaq 0x50(%rsp), %rsi
movl $0x1, %edi
callq 0x2230
movslq 0x8(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
leaq 0x38(%rsp), %rdx
callq 0x34ec
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rsi
movaps 0xe0(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xe0(%rsp)
movq $0x0, 0xf0(%rsp)
testq %rdi, %rdi
je 0x2fe2
subq %rdi, %rsi
callq 0x2260
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x2fff
movq 0xf0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movl 0x8(%rsp), %edi
movq 0x20(%rsp), %rcx
movsd 0x2030(%rip), %xmm0     # 0x5040
movq %rbx, %rsi
movq %r15, %rdx
movl $0x64, %r8d
callq 0x3ab4
leaq 0xd0(%rsp), %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0x2230
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq 0x50(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
subq 0x58(%rsp), %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x1fe8(%rip), %xmm0     # 0x5048
addsd %xmm1, %xmm0
movq 0x3f7d(%rip), %rdi      # 0x6fe8
callq 0x23a0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2240
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
testl %ebp, %ebp
je 0x327e
movq 0x70(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x37b7
jmp 0x327e
movl 0x8(%rsp), %esi
movsd 0x48(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x27a0
leaq 0xe0(%rsp), %rdi
movq (%rdi), %rbx
movq 0x10(%rdi), %r14
movl 0x8(%rsp), %esi
movsd 0x1f50(%rip), %xmm1     # 0x5038
xorpd %xmm0, %xmm0
callq 0x273a
movq 0xe0(%rsp), %r15
movq 0xf0(%rsp), %r12
movl 0xc(%rsp), %ecx
movq 0x38(%rsp), %r8
movq 0x3eb7(%rip), %rdx      # 0x6fc8
leaq 0x8(%rsp), %rdi
movl $0x1, %esi
callq 0x2060
leaq 0x50(%rsp), %rsi
movl $0x1, %edi
callq 0x2230
movl 0x18(%rsp), %eax
cmpl 0xc(%rsp), %eax
jne 0x31bd
movslq 0x8(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
leaq 0x1f(%rsp), %rdx
callq 0x34ec
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rsi
movapd 0xe0(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x30(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xe0(%rsp)
movq $0x0, 0xf0(%rsp)
testq %rdi, %rdi
je 0x31a0
subq %rdi, %rsi
callq 0x2260
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x31bd
movq 0xf0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movl 0x8(%rsp), %edi
movq 0x20(%rsp), %rcx
movq 0x38(%rsp), %r8
movsd 0x1e6d(%rip), %xmm0     # 0x5040
movq %rbx, %rsi
movq %r15, %rdx
movl $0x64, %r9d
callq 0x4d19
movl 0x18(%rsp), %eax
cmpl 0xc(%rsp), %eax
jne 0x327e
leaq 0xd0(%rsp), %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0x2230
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq 0x50(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
subq 0x58(%rsp), %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x1e17(%rip), %xmm0     # 0x5048
addsd %xmm1, %xmm0
movq 0x3dac(%rip), %rdi      # 0x6fe8
callq 0x23a0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2240
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
testl %ebp, %ebp
je 0x327e
movq 0x70(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x37b7
callq 0x21a0
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x32a8
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x32cd
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x32ef
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3306
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
testq %r15, %r15
je 0x3319
subq %r15, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2260
testq %rbx, %rbx
je 0x332c
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2260
xorl %eax, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2897
jmp 0x338c
callq 0x2897
jmp 0x338c
movl $0x10, %edi
callq 0x2120
movq %rax, %rbx
leaq 0x1eb3(%rip), %rsi      # 0x5215
movq %rax, %rdi
callq 0x2100
movq 0x3c7f(%rip), %rsi      # 0x6ff0
movq 0x3c48(%rip), %rdx      # 0x6fc0
movq %rbx, %rdi
callq 0x23d0
callq 0x2897
jmp 0x338c
callq 0x2897
movl $0x1, %edi
callq 0x22b0
jmp 0x339f
jmp 0x3412
jmp 0x3421
movq %rax, %r13
movq %rbp, %rdi
callq 0x2190
jmp 0x343a
jmp 0x3412
jmp 0x3421
jmp 0x3412
jmp 0x3421
movq %rax, %r13
movq 0x3bf7(%rip), %rsi      # 0x6fb8
leaq 0xe0(%rsp), %rdi
callq 0x2340
leaq 0x158(%rsp), %rdi
callq 0x20d0
jmp 0x3424
movq %rax, %r13
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3424
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x3424
jmp 0x3421
jmp 0x3421
jmp 0x3437
jmp 0x3421
movq %rax, %r13
movq %rbx, %rdi
callq 0x2190
jmp 0x3424
movq %rax, %r13
jmp 0x3429
jmp 0x3421
jmp 0x3437
jmp 0x3421
jmp 0x3421
jmp 0x3421
movq %rax, %r13
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x343a
jmp 0x3437
jmp 0x3437
jmp 0x3437
movq %rax, %r13
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x345f
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x3484
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x34a6
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x34bd
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
testq %r15, %r15
je 0x34d0
subq %r15, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2260
testq %rbx, %rbx
je 0x34e3
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2260
movq %r13, %rdi
callq 0x23f0
nop