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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!stanford.edu!leland.Stanford.EDU!ungar
  3. From: ungar@leland.Stanford.EDU (Jeffrey Ungar)
  4. Subject: Re: Reasons for using C vs. Fortran or vice/versa
  5. Message-ID: <1992Nov13.075506.29134@leland.Stanford.EDU>
  6. Sender: news@leland.Stanford.EDU (Mr News)
  7. Organization: DSG, Stanford University, CA 94305, USA
  8. References: <1992Nov12.205823.13951@u.washington.edu> <gay.721614205@sfu.ca> <1992Nov13.060701.6917@u.washington.edu>
  9. Distribution: usa
  10. Date: Fri, 13 Nov 92 07:55:06 GMT
  11. Lines: 70
  12.  
  13. In article <1992Nov13.060701.6917@u.washington.edu> rons@hardy.u.washington.edu (Ronald Schoenberg) writes:
  14. >
  15. >The most important matrix operation in numerical work is the gaxpy
  16. >("generalized a times x plus y").  Numerical routines are written to
  17. >be rich in gaxpies (see Matrix Computations by Golub and Van Loan for
  18.  
  19. [misleading(?) stuff about column vs. row major order deleted]
  20.  
  21. >
  22. >#include <stdio.h>
  23. >
  24. >void main()
  25. >{
  26. >    float a_col[] = { 1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16 };
  27. >    float a_row[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
  28. >    float x[] = { 1,2,3,4 };
  29. >    float z[4];
  30. >
  31. >    int m = 4, n = 4;
  32. >    int i, j, k;
  33. >
  34. >/*
  35. >** column echelon gaxpy
  36. >*/
  37. >    for (i = 0; i < m; i++) {
  38. >        z[i] = 0;
  39. >        k = i;
  40. >        for (j = 0; j < n; j++) {
  41. >            z[i] += a_col[k] * x[j];
  42. >            k += n;
  43. >        }
  44. >    }
  45.  
  46. This looks OK...
  47.  
  48. >
  49. >/*
  50. >** row echelon gaxpy
  51. >*/
  52. >    for (i = 0; i < m; i++) {  
  53. >        z[i] = 0;
  54. >        for (j = 0; j < n; j++) {
  55. >            k = i * n + j;
  56. >            z[i] += a_row[k] * x[j];
  57. >        }
  58. >    }
  59. >}
  60.  
  61. Well small wonder row major looks slower! It isn't coded
  62. like a C programmer would do it! :-) Why not
  63.  
  64. k=0;
  65. for ( i=0; i<m; i++ ) {
  66.    z[i]=0;
  67.    for ( j=0; j<n; j++ ) {
  68.       z[i]+=a_row[k]*x[j];
  69.       k++;
  70.    }
  71. }
  72.  
  73. I'm sure the reason the BLAS used in M++ uses column major order is 
  74. that it came from Fortran. There do not have to be any
  75. appreciable speed differences resulting from either choice. Row major
  76. order makes the code a little easier to understand from my perspective,
  77. however.
  78.  
  79. Jeff
  80.  
  81.  
  82.  
  83.