// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x2c0, %rsp            # imm = 0x2C0
leaq 0x2c(%rsp), %rbx
movl %edi, (%rbx)
leaq 0x10(%rsp), %rax
movq %rsi, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2320
movq 0x567b(%rip), %rdi      # 0x7fa8
leaq 0x5c(%rsp), %rsi
callq 0x2280
andq $0x0, 0x40(%rsp)
andq $0x0, 0x70(%rsp)
andq $0x0, 0xb0(%rsp)
xorpd %xmm0, %xmm0
leaq 0xd8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
movabsq $0x3fe0000000000000, %rax # imm = 0x3FE0000000000000
movq %rax, 0x50(%rsp)
leaq 0x130(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0x60(%rsp)
movb %cl, (%rax)
leaq 0x110(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movapd %xmm0, 0xa0(%rsp)
movb %cl, (%rax)
cmpl $0x2, (%rbx)
jle 0x302a
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x140(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x31ee
leaq 0x2832(%rip), %rsi      # 0x520f
leaq 0x140(%rsp), %r14
movq %r14, %rdi
callq 0x31e1
movl %eax, %ebx
movq %r14, %rdi
callq 0x2120
testb %bl, %bl
je 0x2ace
movq 0x10(%rsp), %r15
movq 0x10(%r15), %rdi
callq 0x2360
movl %eax, 0x8(%rsp)
testl %eax, %eax
jle 0x3031
movl %eax, %r14d
cmpl $0x5, 0x2c(%rsp)
jne 0x2b17
movq 0x18(%r15), %rsi
leaq 0x140(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x31ee
leaq 0x27cb(%rip), %rsi      # 0x5212
leaq 0x140(%rsp), %r15
movq %r15, %rdi
callq 0x3229
movl %eax, %ebp
movq %r15, %rdi
callq 0x2120
testb %bpl, %bpl
jne 0x3071
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rsi
leaq 0x80(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x31ee
leaq 0x140(%rsp), %rdi
leaq 0x80(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x23b0
leaq 0x80(%rsp), %rdi
callq 0x2120
leaq 0x140(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x2180
leaq 0x140(%rsp), %rdi
callq 0x2130
jmp 0x2b17
cmpl $0x4, 0x2c(%rsp)
jne 0x3038
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rsi
leaq 0x140(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x31ee
leaq 0xc8(%rsp), %rdi
leaq 0x140(%rsp), %r14
movq %r14, %rsi
callq 0x22b0
movq %r14, %rdi
callq 0x2120
movl 0x5c(%rsp), %eax
cmpl $0x2, %eax
jl 0x2c1f
cvtsi2sd %eax, %xmm0
sqrtsd %xmm0, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, %edx
imull %ecx, %edx
cmpl %edx, %eax
jne 0x303f
leaq 0xf8(%rsp), %rdx
movl %ecx, (%rdx)
movl %ecx, 0x4(%rdx)
leaq 0xf0(%rsp), %rcx
andq $0x0, (%rcx)
movq 0x544b(%rip), %rdi      # 0x7fa8
pushq $0x2
popq %rsi
pushq $0x1
popq %r8
leaq 0x20(%rsp), %r9
callq 0x2040
leaq 0xe8(%rsp), %rsi
andq $0x0, (%rsi)
movq 0x20(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x2400
movq 0x20(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x2310
movl 0x18(%rsp), %eax
cmpl 0xc(%rsp), %eax
jne 0x2eba
testb %bl, %bl
je 0x2e25
movsd 0x50(%rsp), %xmm0
leaq 0x140(%rsp), %rdi
movl %r14d, %esi
callq 0x2777
leaq 0x30(%rsp), %rdi
leaq 0x140(%rsp), %r15
movq %r15, %rsi
callq 0x3686
movq %r15, %rdi
callq 0x3514
leaq 0x140(%rsp), %rdi
movsd 0x2447(%rip), %xmm1     # 0x5038
xorpd %xmm0, %xmm0
movl %r14d, %esi
callq 0x2714
leaq 0x60(%rsp), %rdi
leaq 0x140(%rsp), %r14
movq %r14, %rsi
callq 0x3686
movq %r14, %rdi
callq 0x3514
jmp 0x2eba
movq 0x53c2(%rip), %rdi      # 0x7fe8
leaq 0x2643(%rip), %rsi      # 0x5270
callq 0x2240
movq %rax, %rdi
callq 0x2140
testb %bl, %bl
je 0x2caf
movsd 0x50(%rsp), %xmm0
leaq 0x140(%rsp), %rdi
movl %r14d, %esi
callq 0x2777
leaq 0x30(%rsp), %rdi
leaq 0x140(%rsp), %r15
movq %r15, %rsi
callq 0x3686
movq %r15, %rdi
callq 0x3514
leaq 0x140(%rsp), %rdi
movsd 0x23b7(%rip), %xmm1     # 0x5038
xorpd %xmm0, %xmm0
movl %r14d, %esi
callq 0x2714
leaq 0x60(%rsp), %rdi
leaq 0x140(%rsp), %r15
movq %r15, %rsi
callq 0x3686
movq %r15, %rdi
callq 0x3514
jmp 0x2d46
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x140(%rsp), %rdi
callq 0x3233
leaq 0x30(%rsp), %rdi
leaq 0x140(%rsp), %r14
movq %r14, %rsi
callq 0x3686
movq %r14, %rdi
callq 0x3514
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
leaq 0x140(%rsp), %rdi
callq 0x3233
leaq 0x60(%rsp), %r15
leaq 0x140(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x3686
movq %r14, %rdi
callq 0x3514
movq 0x8(%r15), %r14
subq (%r15), %r14
shrq $0x3, %r14
movl %r14d, 0x8(%rsp)
movq 0x38(%rsp), %rax
subq 0x30(%rsp), %rax
sarq $0x3, %rax
movslq %r14d, %rcx
imulq %rcx, %rcx
cmpq %rcx, %rax
jne 0x307e
pushq $0x1
popq %rdi
leaq 0x80(%rsp), %rsi
callq 0x2230
movslq %r14d, %rsi
leaq 0x140(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x3190
leaq 0xa0(%rsp), %r15
leaq 0x140(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3686
movq %r12, %rdi
callq 0x3514
movq 0x30(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq (%r15), %rcx
pushq $0x64
popq %r8
movsd 0x2299(%rip), %xmm0     # 0x5040
movl %r14d, %edi
callq 0x379d
pushq $0x1
popq %rdi
leaq 0xb8(%rsp), %r14
movq %r14, %rsi
callq 0x2230
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq 0x80(%rsp), %rax
cvtsi2sd %rax, %xmm1
subq 0x88(%rsp), %rcx
cvtsi2sd %rcx, %xmm0
mulsd 0x225d(%rip), %xmm0     # 0x5048
addsd %xmm1, %xmm0
movq 0x51f2(%rip), %rdi      # 0x7fe8
callq 0x2390
movq %rax, %rdi
callq 0x2140
testb %bl, %bl
jne 0x2fcb
movq 0xc8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x33be
jmp 0x2fcb
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x140(%rsp), %rdi
callq 0x3233
leaq 0x30(%rsp), %rdi
leaq 0x140(%rsp), %r14
movq %r14, %rsi
callq 0x3686
movq %r14, %rdi
callq 0x3514
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
leaq 0x140(%rsp), %rdi
callq 0x3233
leaq 0x60(%rsp), %r14
leaq 0x140(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x3686
movq %r15, %rdi
callq 0x3514
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
movl %eax, 0x8(%rsp)
movq 0x38(%rsp), %rcx
subq 0x30(%rsp), %rcx
sarq $0x3, %rcx
cltq
imulq %rax, %rax
cmpq %rax, %rcx
jne 0x30b0
movl 0xc(%rsp), %ecx
movq 0x20(%rsp), %r8
movq 0x50fe(%rip), %rdx      # 0x7fc8
leaq 0x8(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x2050
pushq $0x1
popq %rdi
leaq 0x80(%rsp), %rsi
callq 0x2230
movl 0x18(%rsp), %eax
cmpl 0xc(%rsp), %eax
jne 0x2f28
movslq 0x8(%rsp), %rsi
leaq 0x140(%rsp), %rdi
leaq 0x1f(%rsp), %rdx
callq 0x3190
leaq 0xa0(%rsp), %rdi
leaq 0x140(%rsp), %r14
movq %r14, %rsi
callq 0x3686
movq %r14, %rdi
callq 0x3514
movl 0x8(%rsp), %edi
movq 0x30(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movq 0x20(%rsp), %r8
pushq $0x64
popq %r9
movsd 0x20f1(%rip), %xmm0     # 0x5040
callq 0x4817
movl 0x18(%rsp), %eax
cmpl 0xc(%rsp), %eax
jne 0x2fcb
pushq $0x1
popq %rdi
leaq 0xb8(%rsp), %r14
movq %r14, %rsi
callq 0x2230
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq 0x80(%rsp), %rax
cvtsi2sd %rax, %xmm1
subq 0x88(%rsp), %rcx
cvtsi2sd %rcx, %xmm0
mulsd 0x20ae(%rip), %xmm0     # 0x5048
addsd %xmm1, %xmm0
movq 0x5043(%rip), %rdi      # 0x7fe8
callq 0x2390
movq %rax, %rdi
callq 0x2140
testb %bl, %bl
jne 0x2fcb
movq 0xc8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x33be
callq 0x21b0
leaq 0x100(%rsp), %rdi
callq 0x2120
leaq 0x120(%rsp), %rdi
callq 0x2120
leaq 0xc8(%rsp), %rdi
callq 0x2120
leaq 0xa0(%rsp), %rdi
callq 0x3514
leaq 0x60(%rsp), %rdi
callq 0x3514
leaq 0x30(%rsp), %rdi
callq 0x3514
xorl %eax, %eax
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x2853
jmp 0x3076
callq 0x2853
jmp 0x3076
callq 0x2853
jmp 0x3076
pushq $0x10
popq %rdi
callq 0x2150
movq %rax, %r14
leaq 0x21c4(%rip), %rsi      # 0x5215
movq %rax, %rdi
callq 0x20f0
movq 0x4f90(%rip), %rsi      # 0x7ff0
movq 0x4f59(%rip), %rdx      # 0x7fc0
movq %r14, %rdi
callq 0x23c0
jmp 0x30e0
callq 0x2853
pushq $0x1
popq %rdi
callq 0x22a0
pushq $0x10
popq %rdi
callq 0x2150
movq %rax, %r14
leaq 0x21ba(%rip), %rsi      # 0x524a
movq %rax, %rdi
callq 0x20f0
movq 0x4f51(%rip), %rsi      # 0x7ff0
movq 0x4f1a(%rip), %rdx      # 0x7fc0
movq %r14, %rdi
callq 0x23c0
jmp 0x30e0
pushq $0x10
popq %rdi
callq 0x2150
movq %rax, %r14
leaq 0x2188(%rip), %rsi      # 0x524a
movq %rax, %rdi
callq 0x20f0
movq 0x4f1f(%rip), %rsi      # 0x7ff0
movq 0x4ee8(%rip), %rdx      # 0x7fc0
movq %r14, %rdi
callq 0x23c0
jmp 0x311e
jmp 0x313d
jmp 0x313d
jmp 0x311e
jmp 0x313d
jmp 0x313d
jmp 0x313d
jmp 0x313d
jmp 0x313d
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x2130
jmp 0x3140
jmp 0x313d
jmp 0x313d
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x2120
jmp 0x3140
jmp 0x313d
jmp 0x313d
movq %rax, %rbx
movq %r14, %rdi
callq 0x21a0
jmp 0x3140
jmp 0x313d
jmp 0x313d
jmp 0x313d
jmp 0x313d
jmp 0x313d
jmp 0x313d
jmp 0x313d
jmp 0x313d
jmp 0x313d
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x2120
leaq 0x120(%rsp), %rdi
callq 0x2120
leaq 0xc8(%rsp), %rdi
callq 0x2120
leaq 0xa0(%rsp), %rdi
callq 0x3514
leaq 0x60(%rsp), %rdi
callq 0x3514
leaq 0x30(%rsp), %rdi
callq 0x3514
movq %rbx, %rdi
callq 0x23e0