Best way to write a large array to file in fortran? Text vs Other

前端 未结 2 1787
暗喜
暗喜 2020-12-05 05:45

I wanted to know what the best way to write a large fortran array ( 5000 x 5000 real single precision numbers) to a file. I am trying to save the results of a numerical calc

2条回答
  •  隐瞒了意图╮
    2020-12-05 06:26

    OPEN the file for reading and writing as "unformatted", and READ and WRITE the data without supplying a format, as demonstrated in the program below.

    program xunformatted
    integer, parameter :: n = 5000, inu = 20, outu = 21
    real               :: x(n,n)
    integer            :: i
    character (len=*), parameter :: out_file = "temp_num"
    call random_seed()
    call random_number(x)
    open (unit=outu,form="unformatted",file=out_file,action="write")
    do i=1,n
       write (outu) x(i,:) ! write one row at a time
    end do
    print*,"sum(x) =",sum(x)
    close (outu)
    open (unit=inu,form="unformatted",file=out_file,action="read")
    x = 0.0
    do i=1,n
       read (inu) x(i,:)   ! read one row at a time
    end do
    print*,"sum(x) =",sum(x)
    end program xunformatted
    

提交回复
热议问题