1

I realize this question has been asked before, but not in the context of IO. Is there any reason to believe that:

!compiler can tell that it should write the whole array at once?
!but perhaps compiler allocates/frees temporary array?
write(UNIT) (/( arr(i), i=1,N )/)

would be any more efficient than:

!compiler does lots of IO here?
do i=1,N
   write(UNIT) arr(i)
enddo

for a file which is opened as:

open(unit=UNIT,access='STREAM',file=fname,status='UNKNOWN')

There is a possibly that this will be used with compiler options to turn off buffered writing as well ...

4

2 に答える 2

3

@HighPerformanceMark で提案されているように、ここに私が設定した簡単なベンチマークがあります。

gfortran の使用:

  program main
  implicit none
  include 'mpif.h'
  integer, parameter :: N = 1000000 
  integer :: unit = 22
  integer i
  real*8 arr(N)
  real*8 t1
  integer repeat
  external test1
  external test2
  external test3

  repeat=15

  call MPI_INIT(i)

  arr = 0
  call timeit(test1,repeat,arr,N,t1)
  print*,t1/repeat

  call timeit(test2,repeat,arr,N,t1)
  print*,t1/repeat

  call timeit(test3,repeat,arr,N,t1)
  print*,t1/repeat

  call MPI_Finalize(i)

  end

  subroutine timeit(sub,repeat,arr,size,time)
  include 'mpif.h'
  external sub
  integer repeat
  integer size
  real*8 time,t1
  real*8 arr(size)
  integer i
  time = 0
  do i=1,repeat
     open(unit=10,access='STREAM',file='test1',status='UNKNOWN')
     t1 = mpi_wtime()
     call sub(10,arr,size)
     time = time + (mpi_wtime()-t1)
     close(10)
  enddo

  return
  end

  subroutine test1(ou,a,N)
  integer N
  real*8 a(N)
  integer ou
  integer i
  do i=1,N
     write(ou),a(i)
  enddo
  return
  end

  subroutine test2(ou,a,N)
  integer N
  real*8 a(N)
  integer ou
  integer i
  write(ou),(a(i),i=1,N)
  return
  end

  subroutine test3(ou,a,N)
  integer N
  real*8 a(N)
  integer ou
  write(ou),a(1:N)
  return
  end

私の結果は(バッファリングされた):

temp $ GFORTRAN_UNBUFFERED_ALL=1 mpirun -np 1 ./test
   6.2392100652058922     
   3.3046503861745200     
   9.76902325948079409E-002

(バッファなし):

temp $ GFORTRAN_UNBUFFERED_ALL=0 mpirun -np 1 ./test
  2.7789104779561362     
  0.15584923426310221     
  9.82964992523193415E-002
于 2012-09-24T16:58:07.587 に答える
1

gfortran (4.7.2 20120921) と ifort (13.0.0.079 Build 20120731) の両方を使用して、上記のベンチマーク コードをコンパイルして実行しました。私の結果は次のとおりです。

グフォルトラン

          UNBUFFERED                BUFFERED
test1:    1.2614487171173097        0.20308602650960286     
test2:    1.0525423844655355        3.4633986155192059E-002
test3:    5.9630711873372398E-003   6.0543696085611975E-003

フォート

          UNBUFFERED                BUFFERED
test1:    1.33864809672038          0.171342913309733
test2:    6.001885732014974E-003    6.095488866170247E-003
test3:    5.962880452473959E-003    6.007925669352213E-003

どちらの場合も (最適化フラグが設定されていない場合) 、明示的なループがtest1最も不利であるように思われます。write(ou), (a(i), i=1, N)さらに、インテル® コンパイラーでは、実行する(ケース 2) かwrite(ou), a(1:N)(ケース 3、この場合と同じ )かにかかわらず、実行時間に大きな違いはありませんwrite(ou), a

ところで、このシングルスレッド プロセスでは、fortran 90 (または 95?) 組み込みサブルーチンcpu_timeを使用することもできます。これは、すべてのスレッドを合計し、時間を秒単位で返します。それ以外の場合はsystem_clock、経過したクロック サイクル数とクロック レートを整数として、おそらくより高い精度で返すことができる もあります。

于 2012-10-22T16:36:57.707 に答える