home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16496 < prev    next >
Encoding:
Text File  |  1992-11-14  |  3.6 KB  |  79 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!swrinde!sdd.hp.com!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!hamblin.math.byu.edu!hellgate.utah.edu!lanl!cochiti.lanl.gov!jlg
  3. From: jlg@cochiti.lanl.gov (J. Giles)
  4. Subject: Re: Reasons for using C vs. Fortran or vice/versa
  5. Message-ID: <1992Nov13.191249.8704@newshost.lanl.gov>
  6. Sender: news@newshost.lanl.gov
  7. Organization: Los Alamos National Laboratory
  8. References: <1992Nov12.135901.15191@ccd.harris.com> <1992Nov12.205823.13951@u.washington.edu> <gay.721614205@sfu.ca> <1992Nov13.043250.28205@sbcs.sunysb.edu>
  9. Distribution: usa
  10. Date: Fri, 13 Nov 1992 19:12:49 GMT
  11. Lines: 66
  12.  
  13. In article <1992Nov13.043250.28205@sbcs.sunysb.edu>, rhorn@csws8.ic.sunysb.edu (Robert Horn) writes:
  14. |> rons@hardy.u.washington.edu (Ronald Schoenberg) writes:
  15. |> >  Fortran arrays are column echelon for a
  16. |> >reason, and C arrays are row echelon slowing down matrix operations
  17. |> >relative to Fortran.     
  18. |> 
  19. |> Wouldn't FORTRAN arrays actually be slower? consider the normal
  20. |> 
  21. |>   for(index = 0; index < kRows; index++) {
  22. |>     for(jndex = 0; jndex < kColumns; jndex++) {
  23. |>       myArray[index][jndex] ...
  24. |>     }
  25. |>   }
  26. |> 
  27. |> one normally accesses matrices in a row major fashion [...]
  28.  
  29. No.  One normally switches back and forth and accesses arrays in
  30. row-major order, column-major order, sergent-major order, etc. in 
  31. different parts of the program.  Consider the typical matrix multiply: 
  32. at least one of the three arrays (two operands and a result) will
  33. be traversed in each of the two (row- vs. column-major) orders.
  34.  
  35. Further, striding by constant amount on most machines is equally
  36. fast regardless of whether the stride is one or more than one.
  37.  
  38. |> [...]                                                  (index changes less
  39. |> often than jndex), so if your array is stored row major, and your cache is
  40. |> nice enough to bring in the words around what you need (since you may well
  41. |> access these as well), [...]
  42.  
  43. In a cached machine, the choice of order can effect speed.  However, as
  44. I pointed out, the choice of order is more often dictated by what you're
  45. doing.  *AND* if either order is acceptable in you algorithm (as in your 
  46. example above), the *user* is perfectly free to nest the loops in whatever 
  47. order is best - or to declare the arrays with their indices reversed.
  48.  
  49. |> [...]         Also, the compiler can compute myArray[index] at the top of
  50. |> the loop and use an offset from that to compute the location of the jndexth
  51. |> element.  [...]
  52.  
  53. Yes, that trick *is* applied to Fortran loops regardless of what 
  54. the stride on the inner loop is - and has been for 30 years.
  55.  
  56. |> [...]
  57. |> I've always heard that column major matrices were one of the stupidest aspects
  58. |> of FORTRAN.
  59.  
  60. I've always *known* the choice was arbitrary.  It is somewhat
  61. less than completely irrelevant as a *speed* issue.
  62.  
  63. Among *bad* choices for array implementation are the idea that 
  64. multidimensional arrays are the same as arrays-of-arrays (and 
  65. choosing a notation which maintains this pretence) and converting 
  66. arrays to raw pointers when passing them as arguments: both properties 
  67. C arrays have.  The *real* array slowdown comes from the fact that 
  68. pointers passed into a procedure as arguments in C are allowed to be 
  69. aliased and little or no optimization can occur when such aliasing 
  70. *might* be present.  Fortran array arguments are (internally)
  71. passed much the same way, but Fortran doesn't allow the arguments
  72. to be aliased - so in Fortran all the usual optimizations can be
  73. applied always.  To be sure, neither language allows the best
  74. solution of allowing the user explicit control over whether 
  75. aliasing is present or not.
  76.  
  77. -- 
  78. J. Giles
  79.