Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PROGRAM main
- COMMON /size/ N1, ial
- PARAMETER(mem=429496730) !Num of values (1,6GB)
- DIMENSION A(mem) !factual array
- c ia ( 1 : N1+1 )
- c di ( N1+2 : 2*N1+1 )
- c al ( 2*N1+2 : 2*N1+1+ial )
- c vec( 2*N1+2+ial : 3*N1+1+ial )
- c res( 3*N1+2+ial : 4*N1+1+ial )
- c Where ial = ia(N1+1)-1 (number of al() elements)
- BYTE i_read_flag=0 !=1 when the matrix has been read
- CALL menu
- 1 READ *, icom
- SELECT CASE (icom)
- CASE (1)
- CALL read_sizes
- CALL dat_to_txt
- CALL input (A(1), A(N1+2), mem)
- i_read_flag=1
- PRINT *, 'Done.'
- CASE (2)
- CALL read_sizes
- c CALL txt_to_dat
- CALL direct_input (A(1), A(N1+2), mem)
- i_read_flag=1
- PRINT *, 'Done.'
- CASE (3)
- IF(i_read_flag .NE. 1) GOTO 3
- CALL multiply (A(1), A(N1+2), A(2*N1+2),
- + A(2*N1+2+ial), A(3*N1+2+ial))
- CALL output(A(3*N1+ial+2))
- PRINT *, 'Done.'
- CASE (4)
- IF(i_read_flag .NE. 1) GOTO 3
- CALL full_matrix_out(A(1), A(N1+2), A(2*N1+2))
- PRINT *, 'Done.'
- CASE (5)
- CALL generate_matrix
- PRINT *, 'Done.'
- CASE (6)
- GOTO 2
- CASE DEFAULT
- PRINT *, 'Unexpected command.'
- PRINT *, 'Done.'
- END SELECT
- GOTO 1
- 3 PRINT *, 'Input the matrix first.'
- GOTO 1
- 2 END
- SUBROUTINE menu
- PRINT *, '1. Read matrix from .txt.'
- PRINT *, '2. Read matrix from direct access files.'
- PRINT *, '3. Multyply matrix by vector.'
- PRINT *, '4. Output of the entered matrix in dense format.'
- PRINT *, '5. Generate new matrix (into direct access files).'
- PRINT *, '6. Exit.'
- END
- c------------------ Reading matrix and vector dimensions --------------------
- SUBROUTINE read_sizes
- COMMON /size/ N1, ial
- OPEN (1, FILE='N1N2.txt', ERR=1)
- c N1 - matrix size, N2 - vector size
- READ (1, *) N1, N2
- IF (N1 .ne. N2 .OR. N1 .EQ. 0) STOP 'Matrix N1 != Vector N2.'
- IF (N1 .EQ. 0) STOP 'Matrix size = 0.'
- CLOSE(1)
- GOTO 2
- 1 STOP 'Input error: check the input files.'
- 2 END
- c--------------------- Input data from .txt files ---------------------------
- SUBROUTINE input(ia, A, mem)
- COMMON /size/ N1, ial
- c formal arrays:
- DIMENSION ia(*)
- DIMENSION A(*) ! (di, ia, al, vec)
- OPEN (1, FILE='ia.txt', ERR=1)
- OPEN (2, FILE='di.txt', ERR=1)
- OPEN (3, FILE='al.txt', ERR=1)
- OPEN (4, FILE='vec.txt', ERR=1)
- READ (1, *) (ia(i), i=1, N1+1) !ia(N1+1)
- ial=ia(N1+1)-1 !num of al() elements
- IF(5*N1+ial+1 .GT. mem) STOP 'Not enough memory.'
- READ (2, *) (A(i), i=1, N1) !di(N1)
- READ (3, *) (A(i), i=N1+1, N1+ial) !al(ia(N1+1)-1)
- READ (4, *) (A(i), i=N1+ial+1, 2*N1+ial) !vec(N1)
- CLOSE(1)
- CLOSE(2)
- CLOSE(3)
- CLOSE(4)
- GOTO 2
- 1 STOP 'Input error: check the input files.'
- 2 END
- c------------------ Input data from direct access files ---------------------
- SUBROUTINE direct_input(ia, A, mem)
- COMMON /size/ N1, ial
- c A( di, ia, al, vec )
- DIMENSION ia(*)
- DIMENSION A(*) !formal arrays
- OPEN (1, FILE='ia.dat', ACCESS='DIRECT', RECL=4, ERR=1)
- OPEN (2, FILE='di.dat', ACCESS='DIRECT', RECL=4, ERR=1)
- OPEN (3, FILE='al.dat', ACCESS='DIRECT', RECL=4, ERR=1)
- OPEN (4, FILE='vec.dat', ACCESS='DIRECT', RECL=4, ERR=1)
- DO i=1, N1+1
- READ(1, REC=i) ia(i)
- ENDDO
- ial=ia(N1+1)-1 !num of al elements
- IF(5*N1+ial+1 .GT. mem) STOP 'Not enough memory.'
- DO i=1, N1
- READ(2, REC=i) A(i) !di(N1)
- ENDDO
- DO i=1, ial
- READ(3, REC=i) A(N1+i) !al(ia(N1+1)-1)
- ENDDO
- DO i=1, N1
- READ(4, REC=i) A(N1+ial+i) !vec(N1)
- ENDDO
- CLOSE(1)
- CLOSE(2)
- CLOSE(3)
- CLOSE(4)
- GOTO 2
- 1 STOP 'Input error: check the input files.'
- 2 END
- c--------------- Multiply matrix (profile format) on vector -----------------
- SUBROUTINE multiply (ia, di, al, vec, res)
- COMMON /size/ N1, ial
- DIMENSION ia(*), di(*), al(*), vec(*), res(*)
- DO i=1, N1
- DO j=ia(i), ia(i+1)-1
- ind=i+j-ia(i+1)
- res(i) = res(i) + al(j) * vec(ind)
- res(ind) = res(ind) + al(j) * vec(i)
- ENDDO
- res(i) = res(i) + di(i) * vec(i)
- ENDDO
- END
- c-------------------------- Output vector - result --------------------------
- SUBROUTINE output(res)
- COMMON /size/ N1, ial
- DIMENSION res(*)
- OPEN(1, FILE='Result.txt', ERR=1)
- WRITE(1, *)'result = ( '
- DO i=1, N1
- WRITE(1, 10) res(i)
- ENDDO
- WRITE(1, *)')'
- CLOSE(1)
- 10 FORMAT(F10.2)
- GOTO 2
- 1 STOP 'Output error: check the output file.'
- 2 END
- c---------------- Full matrix output (99 * 99 max size) -------------------
- SUBROUTINE full_matrix_out(ia, di, al)
- COMMON /size/ N1, ial
- PARAMETER(Nmax=99)
- DIMENSION Fm(Nmax, Nmax)
- DIMENSION ia(*), di(*), al(*)
- DO i=1, N1
- DO j = ia(i), ia(i+1)-1
- ind = i+j-ia(i+1)
- Fm(i, ind) = al(j)
- Fm(ind, i) = al(j)
- ENDDO
- Fm(i, i) = di(i)
- ENDDO
- OPEN (1, FILE='full_matrix.txt', ERR=1)
- DO i=1, N1
- DO j=1, N1
- WRITE(1, 10) Fm(i, j)
- ENDDO
- WRITE (1, 11)
- ENDDO
- CLOSE(1)
- GOTO 2
- 1 STOP 'Error: failed to create the output file.'
- c 10 FORMAT (F5.2\)
- 10 FORMAT(F5.2)
- 11 FORMAT (1x)
- 2 END
- c------------- Generate new matrix (into direct access files) ---------------
- SUBROUTINE generate_matrix
- PRINT *, 'Enter size:'
- PRINT *, '(Or enter 0 to read size of matrix from file.)'
- READ *, m_size
- OPEN(1, FILE = 'N1N2.txt', ERR=1)
- OPEN(2, FILE = 'vec.dat', ACCESS='DIRECT', RECL=4, ERR=2)
- OPEN(3, FILE = 'di.dat', ACCESS='DIRECT', RECL=4, ERR=2)
- OPEN(4, FILE = 'ia.dat', ACCESS='DIRECT', RECL=4, ERR=2)
- OPEN(5, FILE = 'al.dat', ACCESS='DIRECT', RECL=4, ERR=2)
- IF(m_size .GT. 0) GOTO 5
- READ(1, *) m_size
- 5 WRITE(1, 10) m_size, m_size
- DO i = 1, m_size
- WRITE(2, REC=i) i/2.0
- WRITE(3, REC=i) i/10
- ENDDO
- WRITE(4, REC=1) 1
- it = 1
- ik = 1
- DO WHILE (ik .LT. m_size)
- IF (MOD(it, 3) .EQ. 0) GOTO 20
- WRITE(4, REC=ik) it
- ik = ik + 1
- 20 it = it + 1
- ENDDO
- WRITE(4, REC=ik+1) it
- DO i=1, it
- WRITE(5, REC=i) i/10
- ENDDO
- CLOSE(1)
- CLOSE(2)
- CLOSE(3)
- CLOSE(4)
- CLOSE(5)
- GOTO 3
- 10 FORMAT(I2)
- 1 STOP 'Error: something went wrong with N1N2.txt'
- 2 STOP 'Error: something went wrong with input files.'
- 3 END
- c-------------- Converting .txt files to direct access files ----------------
- SUBROUTINE txt_to_dat
- COMMON /size/ N1, ial
- PARAMETER(Nmax=999)
- DIMENSION ia(Nmax+1), di(Nmax), al(Nmax*Nmax), vec(Nmax)
- OPEN (1, FILE='ia.txt', ERR=1)
- OPEN (2, FILE='di.txt', ERR=1)
- OPEN (3, FILE='al.txt', ERR=1)
- OPEN (4, FILE='vec.txt', ERR=1)
- OPEN (5, FILE='ia.dat', ACCESS='DIRECT', RECL=4, ERR=2)
- OPEN (6, FILE='di.dat', ACCESS='DIRECT', RECL=4, ERR=2)
- OPEN (7, FILE='al.dat', ACCESS='DIRECT', RECL=4, ERR=2)
- OPEN (8, FILE='vec.dat', ACCESS='DIRECT', RECL=4, ERR=2)
- READ (1, *) (ia(i), i=1, N1+1)
- DO i=1, N1+1
- WRITE (5, REC=i) ia(i)
- ENDDO
- ial = ia(N1+1)-1
- READ (2, *) (di(i), i=1, N1)
- DO i=1, N1
- WRITE (6, REC=i) di(i)
- ENDDO
- READ (3, *) (al(i), i=1, ial)
- DO i=1, ial
- WRITE (7, REC=i) al(i)
- ENDDO
- READ (4, *) (vec(i), i=1, N1)
- DO i=1, N1
- WRITE (8, REC=i) vec(i)
- ENDDO
- CLOSE(1)
- CLOSE(2)
- CLOSE(3)
- CLOSE(4)
- CLOSE(5)
- CLOSE(6)
- CLOSE(7)
- CLOSE(8)
- GOTO 3
- 1 STOP 'Input error: check the input files.'
- 2 STOP 'Output error: failed to create direct access file.'
- 3 END
- c-------------- Converting direct access files to .txt files ----------------
- SUBROUTINE dat_to_txt
- COMMON /size/ N1, ial
- PARAMETER(Nmax=999)
- DIMENSION ia(Nmax+1), di(Nmax), al(Nmax*Nmax), vec(Nmax)
- OPEN (1, FILE='ia.txt', ERR=1)
- OPEN (2, FILE='di.txt', ERR=1)
- OPEN (3, FILE='al.txt', ERR=1)
- OPEN (4, FILE='vec.txt', ERR=1)
- OPEN (5, FILE='ia.dat', ACCESS='DIRECT', RECL=4, ERR=2)
- OPEN (6, FILE='di.dat', ACCESS='DIRECT', RECL=4, ERR=2)
- OPEN (7, FILE='al.dat', ACCESS='DIRECT', RECL=4, ERR=2)
- OPEN (8, FILE='vec.dat', ACCESS='DIRECT', RECL=4, ERR=2)
- DO i=1, N1+1
- READ (5, REC = i) ia(i)
- WRITE (1, *) ia(i)
- ENDDO
- ial = ia(N1+1)-1
- DO i=1, N1
- READ (6, REC = i) di(i)
- WRITE (2, *) di(i)
- ENDDO
- DO i=1, ial
- READ (7, REC = i) al(i)
- WRITE (3, *) al(i)
- ENDDO
- DO i=1, N1
- READ (8, REC = i) vec(i)
- WRITE (4, *) vec(i)
- ENDDO
- CLOSE(1)
- CLOSE(2)
- CLOSE(3)
- CLOSE(4)
- CLOSE(5)
- CLOSE(6)
- CLOSE(7)
- CLOSE(8)
- GOTO 3
- 1 STOP 'Input error: check the input files.'
- 2 STOP 'Output error: failed to create direct access file.'
- 3 END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement