home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c220 / 4.ddi / DEMOS / MATMULT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-16  |  5.4 KB  |  151 lines

  1. #ifdef TIME
  2. #include <system.cf>    /* Get DOS clock function. */
  3. #else
  4. long clock() {return 0;}
  5. #endif
  6. #include <stdio.h>
  7.  
  8. extern void * malloc(unsigned int);
  9. extern void free(void *);
  10.  
  11. #define Trace_malloc 1
  12.  
  13. unsigned Size_mem() {
  14.    unsigned int Alloc_amount 
  15.        = sizeof(int) == 2 ? 60000 : 0xff_ffff /* 16 mb. */,
  16.        Sum = 0;
  17.    void try() {
  18.       void *p;
  19.       do {
  20.          p = malloc(Alloc_amount);
  21.          Alloc_amount = Alloc_amount / 2;
  22.       } while (p == 0 && Alloc_amount > 256);  
  23.       if (Trace_malloc) printf("Malloc-ed %d: %x\n",Alloc_amount*2,p);   
  24.       if (p != 0) {
  25.            Sum += Alloc_amount*2;
  26.          if (Alloc_amount > 256) try();
  27.          }
  28.       if (p != 0) free(p);
  29.       }
  30.    try();
  31.    return Sum;
  32.    }    
  33.  
  34. char YesNo(char *msg) {
  35.    char response;
  36.    printf("%s (y/n)?",msg);
  37.    scanf(" %c",&response);
  38.    return response == 'y' || response == 'Y';
  39.    } 
  40.  
  41. void print_banner() {
  42.    static char * banner[] = {
  43.     "IMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM;", 
  44.     ":                  Big Matrix Multiply                     :",
  45.     "HMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM<",
  46.     "IMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM;", 
  47.     ":  This program multiplies a square matrix by itself.      :",
  48.     ":  The matrix contains 32-bit integers.  Then the program  :",
  49.     ":  does the same for a matrix containing 32-bit floating-  :",
  50.     ":  point numbers. You can specify the size of the matrix.  :",
  51.     ":  M[i,j] is initialized to i+j.                           :",
  52.     ":DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD:",
  53.     ":  Brought to you by the friendly folks at Phar Lap        :",
  54.     ":               and MetaWare Incorporated.                 :",
  55.     "HMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM<"};
  56.    int I;
  57.    for (I = 0; I < sizeof(banner)/sizeof(char *); I++)
  58.       printf("\t%s\n",banner[I]);
  59.    }
  60.  
  61. void main (int argc, char** argv) {
  62.    int Size;
  63.    long int *Matrix_in, *Matrix_out;
  64.    float *Fmatrix_in, *Fmatrix_out;
  65. /*   long int (*M100_in)[100][100], (*M100_out)[100][100];  */
  66.    void init() {
  67. #     define DEFAULT 100
  68.       /* Get the parameters. */
  69.       /* If one or more arguments, assume default size. */
  70.       if (argc > 1) Size = DEFAULT;
  71.       else {
  72.       L: printf("Enter the number of rows (and columns) in the matrix.\n"
  73.               "Enter 0 for the default of %d.\n"
  74.                 "Now enter your number:",DEFAULT,argv);
  75.            scanf("%d",&Size);
  76.            if (Size == 0) Size = DEFAULT;
  77.            if (Size < 0) {
  78.               printf("Sorry, please try something a little bigger.\n");
  79.               goto L;
  80.               }
  81.          }
  82.    L2:printf("\nAllocating two %d by %d matrices of 32-bit quantities...\n", 
  83.                Size,Size);
  84.       Matrix_in = malloc(Size*Size*sizeof(long int));
  85.       if (Trace_malloc) printf("Malloc-ed %d: %x\n",Size*Size,Matrix_in);
  86.       Matrix_out = malloc(Size*Size*sizeof(long int));
  87.       if (Trace_malloc) printf("Malloc-ed %d: %x\n",Size*Size,Matrix_out);
  88.       if (Matrix_in == 0 || Matrix_out == 0) {
  89.            extern double sqrt(double);
  90.            if (Matrix_in != 0) free(Matrix_in);
  91.            printf("Sorry, I don't seem to have that much memory...hang on...\n");
  92.            printf("I seem to be able to allocate up to %d bytes for my array.\n",
  93.                Size = Size_mem());
  94.            printf("Since I need two matrices, the maximum number of rows is %d.\n",
  95.                Size = sqrt(Size/sizeof(long int)/2));
  96.            if (YesNo("Do you want me to use all of memory")) goto L2;
  97.            else goto L;
  98.            }
  99.       }
  100. #define Subscript(M,R,C) M[(R)*Size+(C)]
  101.    int Row, Col;
  102.    print_banner();   
  103.    init();
  104.  
  105.    long start_time = clock();
  106.    printf("\nInitializing matrix...\n");
  107.    for (Row = 0; Row < Size; Row++)
  108.       for (Col = 0; Col < Size; Col++)
  109.          Subscript(Matrix_in,Row,Col) = Row+Col;
  110.          
  111.    printf("\nMultiplying matrix...\n");
  112.    printf("\n(It will require %ld individual element multiplications.)\n"
  113.          " and %ld multiplications just to compute array subscripts.)\n",
  114.            Size*Size*Size,Size*Size*Size*2+Size*Size);
  115.    for (Row = 0; Row < Size; Row++)
  116.       for (Col = 0; Col < Size; Col++) {
  117.          /* Multiply M[Row,*] by M[*,Col] to get Result[Row,Col]. */
  118.          long int Sum = 0; int R2;
  119.          for (R2 = 0; R2 < Size; R2++) 
  120.             Sum += Subscript(Matrix_in,Row,R2)*Subscript(Matrix_in,R2,Col);
  121.          Subscript(Matrix_out,Row,Col) = Sum;
  122.          }   
  123.  
  124.    if (start_time != 0) 
  125.       printf("That took %d hundredths of a second.\n\n",
  126.            clock()-start_time);
  127.    start_time = clock(); 
  128.            
  129.    printf("\nNow for floating point.  Initializing matrix...\n");
  130.    Fmatrix_in = (float *) Matrix_in;
  131.    Fmatrix_out = (float *) Matrix_out;
  132.    for (Row = 0; Row < Size; Row++)
  133.       for (Col = 0; Col < Size; Col++)
  134.          Subscript(Fmatrix_in,Row,Col) = Row+Col;
  135.          
  136.    printf("\nMultiplying matrix...\n");
  137.    for (Row = 0; Row < Size; Row++)
  138.       for (Col = 0; Col < Size; Col++) {
  139.          /* Multiply M[Row,*] by M[*,Col] to get Result[Row,Col]. */
  140.          float Sum = 0; int R2;
  141.          for (R2 = 0; R2 < Size; R2++) 
  142.             Sum += Subscript(Fmatrix_in,Row,R2)*Subscript(Fmatrix_in,R2,Col);
  143.          Subscript(Fmatrix_out,Row,Col) = Sum;
  144.          }   
  145.    if (start_time != 0) 
  146.    printf("That took %d hundredths of a second.\n\n",
  147.            clock()-start_time);
  148.    
  149.    printf("\nEnd of matrix multiply.\n");
  150.    }
  151.