【问题标题】:Fortran best way to improve write to file speed for large arraysFortran 提高大型数组写入文件速度的最佳方法
【发布时间】:2021-04-13 13:23:31
【问题描述】:

我有一个非常大的数组,大小为 20,000,000,我想写入一个未格式化的文件。 它是一个自相关函数。

在不写入文件的情况下使用 -O4 优化编译标志非常快。 但是一旦我写入文件,似乎需要一天多的时间才能完成。

最后是 f90 程序。下面是没有写入文件和写入文件的输出。

很明显,写入数组的单个元素大约需要 10 毫秒。

20,000,000 x 0.01 = 200,000 秒 = 3,333 分钟 = 55 小时

读取只需要 45 秒,怎么可能需要这么长时间来写入文件?我可以做些什么来提高速度?

备注

系统:Ubuntu 20.04

编译行:fortran -o acorr.exe -O4 acorr.f90

无文件写入

elapsed time for reading:    43.4389992    
 Size of Jx:     20000000
 Loop Start Time:    43.5009995    
 correlation time magnitude 1e0 elapsed time:    43.5009995    
 correlation time magnitude 1e1 elapsed time:    43.5009995    
 correlation time magnitude 1e2 elapsed time:    43.5009995    
 correlation time magnitude 1e3 elapsed time:    43.5009995    
 correlation time magnitude 1e4 elapsed time:    43.5009995    
 correlation time magnitude 1e5 elapsed time:    43.5009995    
 correlation time magnitude 1e6 elapsed time:    43.5029984    
 correlation time magnitude 1e7 elapsed time:    43.5190010    
 elapsed time:    43.5369987  

使用文件写入

 elapsed time for reading:    43.6349983    
 Size of Jx:     20000000
 Loop Start Time:    43.6949997    
 correlation time magnitude 1e0 elapsed time:    43.7319984    
 correlation time magnitude 1e1 elapsed time:    43.8969994    
 correlation time magnitude 1e2 elapsed time:    45.4980011    
 correlation time magnitude 1e3 elapsed time:    61.5289993

acorr.f90

PROGRAM acorr
    
    real:: a,b,c,d, sum, mean, var
    integer:: i,j, jsize,beginning, rate, end, end1
    real, dimension(20000000):: Jx, Jxm, corr
    integer:: skip_lines = 4
    call system_clock(beginning, rate)
    
    !reading file
    open(10, file='DiamHeat.log', status='old')
    do i = 1,skip_lines
        read(10,*)
    end do
    do i = 1, 20000000
        read(10,*) a, b, Jx(i), c, d
    end do
    call system_clock(end)
    print *, "elapsed time for reading: ", real(end - beginning) / real(rate)
    close(10)
    !finished reading

    open(20, file='acorr.txt', form='UNFORMATTED')
    jsize = size(Jx)
    print *, "Size of Jx: ", jsize
    !print *, dot_product(Jx(10:jsize),Jx(1:jsize-10))

    !calculate mean
    mean = sum(Jx)/jsize
    Jxm(:) = Jx(:)-mean

    !calculate variance
    var = dot_product(Jxm,Jxm)/jsize
    
    !begin autocorrelation calc
    call system_clock(end1)
    print *, "Loop Start Time: ", real(end1 - beginning) / real(rate)
    do i =0,jsize-1
        !calculation
        corr(i+1) = dot_product(Jxm(i+1:jsize),Jxm(1:jsize-i))/var/(jsize-i)

        !clock timing
        if(i == 1) then
            call system_clock(end)
            print *, "correlation time magnitude 1e0 elapsed time: ", real(end - beginning) / real(rate)
        else if(i == 10) then
            call system_clock(end)
            print *, "correlation time magnitude 1e1 elapsed time: ", real(end - beginning) / real(rate)
        else if(i == 100) then
            call system_clock(end)
            print *, "correlation time magnitude 1e2 elapsed time: ", real(end - beginning) / real(rate)
        else if(i == 1000) then
            call system_clock(end)
            print *, "correlation time magnitude 1e3 elapsed time: ", real(end - beginning) / real(rate)
        else if(i == 10000) then
            call system_clock(end)
            print *, "correlation time magnitude 1e4 elapsed time: ", real(end - beginning) / real(rate)
        else if(i == 100000) then
            call system_clock(end)
            print *, "correlation time magnitude 1e5 elapsed time: ", real(end - beginning) / real(rate)
        else if(i == 1000000) then
            call system_clock(end)
            print *, "correlation time magnitude 1e6 elapsed time: ", real(end - beginning) / real(rate)
        else if(i == 10000000) then
            call system_clock(end)
            print *, "correlation time magnitude 1e7 elapsed time: ", real(end - beginning) / real(rate)
        end if 

    end do
    
    write(20,*) corr
    close(20)
    call system_clock(end)
    print *, "elapsed time: ", real(end - beginning) / real(rate)
END PROGRAM

【问题讨论】:

  • write(20,*) corr格式化 写作,而不是未格式化。这与您的公开声明不一致。此外,这种写入不会发生在循环的迭代中,所以你确定你看到了什么吗?
  • 尝试使用 fromatted 和 unformatted,并没有改变任何东西。是的,这就是我所看到的。很可能是由于 -On 优化编译器,但我不确定。
  • -On 选项几乎可以肯定对 I/O 速度几乎没有影响。看看stackoverflow.com/questions/43637321/… 看看是否有帮助。
  • @ddwong 确切地说,您所说的“尝试使用 fromatted 和 unformatted”是什么意思?您是否尝试了未格式化的 write 语句,或者只是更改了 open 语句?
  • 如果最后没有corr 的输出,编译器可能会决定(特别是在高优化级别)不去计算这些值。你也许可以在最后做一个print *, sum(corr) 看看效果。

标签: optimization fortran gfortran


【解决方案1】:

正如@francescalus 评论的那样,除非将其用于其他目的,否则编译器似乎会跳过计算。 添加

print*, sum corr 

在循环之后似乎使程序计算循环中的点积。这只需要很长时间,但它正在以最佳容量计算。

再次感谢@francescalus

【讨论】:

    猜你喜欢
    • 2014-08-15
    • 2012-12-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多