1

This is my code in Fortran 90

program final
   implicit none
   real, dimension(421,422) :: a
   real :: temp,factor
   real, dimension (421) :: soln
   integer :: i, rmax, pivot, row,n, O,P,COL,k,a_j,n2,a_i,t

   open(unit=177241,file="input_21.txt",status="old")
   open(unit=20,file="output.txt",status="unknown")

   do i =1,421
      read (177241,*) (a(i,:))
   end do
   do i = 1,421
      print *,a(i,13)
   end do
   do T=1,421
      n=421
      a_i=n

      n2=422
      a_j=n2

     !pivoting
      do pivot = T,n-1
         print *, 'pivot=',pivot
         rmax = pivot
         do row = pivot,a_i
            if (ABS(a(rmax,pivot)) < ABS(a(row,pivot)) ) then
               rmax=row
            end if
         end do
         if (rmax /= pivot) then
            do k=1, a_j
               temp = a(rmax,k)
               a(rmax,k) = a(pivot,k)
               a(pivot,k) = temp
            end do
         end if      
  !elimination      
         do row = pivot+1, n
            if ( a(pivot,pivot) /= 0 ) then
               factor = (a(row,pivot)) / (a(pivot,pivot))
               print *,'factor=',factor
               do col = 1, n2
                  a(row, col) = a(row,col) - a(pivot,col) * factor
                  print *,'a=',a(row,col)
               end do
            end if
         end do     
      end do
   end do
   !back-substitution
   soln(n2) = 0
   do o=n,1,-1
      do p=n2,o+1,-1
         a(o,n2) = a(o,n2) - soln(p)*a(o,p)
      end do
      soln(o) = a(o,n2) / a(o,o)
   end do        
   do i=1,n
      print *,soln(i)
      write (121,*) soln(i)
   end do
end program final

The input file is 441x442 matrix txt file. I think this can read that input file but it's going to be a infinite loop on here

if ( a(pivot,pivot) /= 0 ) then
   factor = (a(row,pivot)) / (a(pivot,pivot))
   print *,'factor=',factor    
   do col = 1, n2
      a(row, col) = a(row,col) - a(pivot,col) * factor

How can I solve this problem?

Kyle Kanos
  • 3,257
  • 2
  • 23
  • 37
Jamie
  • 11
  • 2

1 Answers1

3

If you have this properly indented, you will see the following loop structure (ignoring the math portion):

    do T=1,421
       do pivot=T,420
          do row=pivot+1,421
             do col=1,422
                print *,a
             enddo
          enddo
       enddo
    enddo

It is not an infinite loop you have, you are just printing a(row,col) to the screen 522,326,280 times (and if you put that to a file, which I do not recommend, you will have an 11 GB file in your folder!). If you comment out the print statements regarding a and factor but leaving in the one for pivot, it will run in about 3 minutes. Neglecting all print statements, it runs in just a few seconds.

Needless to say, you probably should not be putting anything more than 10 lines to the screen.

Kyle Kanos
  • 3,257
  • 2
  • 23
  • 37