开发者

Fortran: 32 bit / 64 bit performance portability

I've been starting to use Fortran (95) for some numerical code (generating python modules). Here is a simple example:

subroutine bincount (x,c,n,m)
  implicit none
  integer, intent(in) :: n,m
  integer, dimension(0:n-1), intent(in) :: x
  integer, dimension(0:m-1), intent(out) :: c
  integer :: i开发者_如何学编程

  c = 0
  do i = 0, n-1
    c(x(i)) = c(x(i)) + 1 
  end do
end

I've found that this performs very well in 32 bit, but when compiled as x86_64 it is about 5x slower (macbook pro core2duo, snow leopard, gfortran 4.2.3 from r.research.att.com). I finally realised this might be due to using 32bit integer type instead of the native type, and indeed when I replace with integer*8, the 64 bit performance is only 25% worse than the 32bit one.

Why is using a 32 bit integer so much slower on a 64 bit machine? Are there any implicit casts going on with the indexing that I might not be aware of?

Is it always the case that 64 bit will be slower than 32 bit for this type of code (I was surprised at this) - or is there a chance I could get the 64 bit compiled version running the same speed or faster?

(main question) Is there any way to declare a (integer) variable to be the 'native' type... ie 32 bit when compiled 32 bit, 64 bit when compiled 64 bit in modern fortran. Without this it seems like it is impossible to write portable fortran code that won't be much slower depending on how its compiled - and I think this means I will have to stop using fortran for my project. I have looked at kind and selected_kind but not been able to find anything that does this.

[Edit: the large performance hit was from the f2py wrapper copying the array to cast it from 64 bit int to 32 bit int, so nothing inherent to the fortran.]


The answer to your 'main question' is to select the correct compiler option to have the default integer declared with 32 or 64 bits. I never use gfortran (I prefer g95, even better a paid-for compiler) so I Googled and it seems that -fdefault-integer-8 is the option you need.

Like you I'm surprised that the 64 bit version is slower than the 32 bit version. I don't have anything illuminating on that point.


Really have also tried using a 64-bit to run watfor 77 but mine was completely impossible.I got a gf-FOR-compiler for my 64-bit and tried some options on google ans use later given an option to use gcc-mp 4.3 and gfortran 4.3. Version which was still slow. I'll advice you use a 32-bit machine, which is fortran compactible to run your programs or de-grade your 64-bit to a 32-bit to run your progs faster and accurate. Let keep on researching so as to get a 64-bit machine runing campactibly with WATFOR77 and Subroutines progs.


While I haven't done careful studies, I haven't seen such large speed differences.

I suggest trying a newer version of gfortran. Version 4.2 is earlier (gfortran started with 4.0) and considered obsolete. 4.3 and 4.4 are much improved and have more features. 4.4 is the current non-beta version. An easy way to obtain them on a Mac is via MacPorts: the gcc43 and gcc44 packages include gfortran. The compilers are installed as gcc-mp-4.3, gfortran-mp-4.3, etc., so as not to conflict with other versions. Or you can try the latest build of 4.5 from the gfortran wiki page.

Intel fortran is sometimes significantly faster than gfortran.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜