"Fossies" - the Fresh Open Source Software Archive

Member "laspack/examples/matropt/matropt.c" (8 Aug 1995, 10965 Bytes) of package /linux/privat/old/laspack.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /****************************************************************************/
    2 /*                                matropt.c                                 */
    3 /****************************************************************************/
    4 /*                                                                          */
    5 /* MATRix operation OPTimization for laspack                                */
    6 /*                                                                          */
    7 /* Copyright (C) 1992-1995 Tomas Skalicky. All rights reserved.             */
    8 /*                                                                          */
    9 /****************************************************************************/
   10 /*                                                                          */
   11 /*        ANY USE OF THIS CODE CONSTITUTES ACCEPTANCE OF THE TERMS          */
   12 /*              OF THE COPYRIGHT NOTICE (SEE FILE COPYRGHT.H)               */
   13 /*                                                                          */
   14 /****************************************************************************/
   15 
   16 #include <stdio.h>
   17 #include <stdlib.h>
   18 #include <string.h>
   19 #include <time.h>
   20 #include <math.h>
   21 
   22 #include <laspack/errhandl.h>
   23 #include <laspack/vector.h>
   24 #include <laspack/qmatrix.h>
   25 #include <laspack/operats.h>
   26 #include <laspack/version.h>
   27 #include <laspack/copyrght.h>
   28 
   29 #include <xc/getopts.h>
   30 
   31 #include "testproc.h"
   32 
   33 #define MAX_TEST_NR 10
   34 
   35 static void GenMatr(QMatrix *Q);
   36 static void PrintHelp(void);
   37 
   38 int main(int argc, char **argv)
   39 {
   40     double Factor, ClockLASPack = 0.0, Clock[MAX_TEST_NR + 1];
   41     int TestNo, NoCycles, Test, Cycle;
   42     clock_t BegClock, EndClock;
   43     size_t Dim;
   44     Boolean Help;
   45     QMatrix A;
   46     Vector x, y;
   47 
   48     OptTabType OptTab;
   49 
   50     /* initial message */
   51     fprintf(stderr, "matropt             Version %s\n", LASPACK_VERSION);
   52     fprintf(stderr, "                    (C) 1992-1995 Tomas Skalicky\n");
   53     fprintf(stderr, "                    Use option -h for help.\n");
   54 
   55     /* generate options table */
   56     OptTab.No = 3;
   57     OptTab.Descr = (OptDescrType *)malloc(OptTab.No * sizeof(OptDescrType));
   58     if (OptTab.Descr != NULL) {
   59         /* boolean option */
   60         OptTab.Descr[0].KeyChar = 'd';
   61         OptTab.Descr[0].Type = SizeOptType;
   62         OptTab.Descr[0].Variable = (void *)&Dim;
   63 
   64         /* int option */
   65         OptTab.Descr[1].KeyChar = 'c';
   66         OptTab.Descr[1].Type = IntOptType;
   67         OptTab.Descr[1].Variable = (void *)&NoCycles;
   68 
   69         /* option for help */
   70         OptTab.Descr[2].KeyChar = 'h';
   71         OptTab.Descr[2].Type = BoolOptType;
   72         OptTab.Descr[2].Variable = (void *)&Help;
   73 
   74         /* initialize variables with default values */
   75 #if defined(__MSDOS__) || defined(MSDOS)
   76         /* the most MS-DOS compilers use size_t of 16 bits size, which rescricts
   77        arrays to maximal 65536 / 8 = 8192 double elements */
   78     Dim = 1000;
   79 #else
   80         Dim = 10000;
   81 #endif      
   82         NoCycles = 10;
   83         Help = False;
   84 
   85         /* analyse options in the command line */
   86         GetOpts(&argc, argv, &OptTab);
   87         if (OptResult() == OptOK && !Help) {
   88             /* construction of test matrix and vectors */
   89             /* rem.: matrix A have to be nonsymmetric, because 
   90                the test procedures can not handle other types */ 
   91             Q_Constr(&A, "A", Dim, False, Rowws, Normal, True);
   92             V_Constr(&x, "x", Dim, Normal, True);
   93             V_Constr(&y, "y", Dim, Normal, True);
   94             GenMatr(&A);
   95 
   96             printf("\n");
   97             printf("This program tests efficiency of several implementations\n");
   98             printf("of the matrix by vector product:\n\n");
   99             printf("    <Vector 2> = <Matrix> <Vector 1>.\n");
  100         fprintf(stderr, "\n");
  101 
  102             Factor = 1.0 / CLOCKS_PER_SEC / NoCycles;
  103 
  104             Test = 0;
  105 
  106             /* implementation as in the current LASPack version */
  107 
  108             if (LASResult() == LASOK) {
  109             fprintf(stderr, ".");
  110                 V_SetAllCmp(&x, 0.0);
  111                 V_SetAllCmp(&y, 0.0);
  112 
  113                 Asgn_VV(&y, Mul_QV(&A, &x));
  114             
  115                 BegClock = clock();
  116                 for(Cycle = 1; Cycle <= NoCycles; Cycle++) {
  117                     Asgn_VV(&y, Mul_QV(&A, &x));
  118                 }
  119                 EndClock = clock();
  120                 ClockLASPack = (double)(EndClock - BegClock) * Factor;
  121             }
  122 
  123             /* a very simple implementation */
  124 
  125             Test++;
  126             if (LASResult() == LASOK) {
  127             fprintf(stderr, ".");
  128                 V_SetAllCmp(&x, 0.0);
  129                 V_SetAllCmp(&y, 0.0);
  130 
  131                 BegClock = clock();
  132                 for(Cycle = 1; Cycle <= NoCycles; Cycle++) {
  133                     Asgn_VV(&y, Test1_QV(&A, &x));
  134                 }
  135                 EndClock = clock();
  136                 Clock[Test] = (double)(EndClock - BegClock) * Factor;
  137             }
  138 
  139             /* implementation like LASPack version 0.06 */
  140 
  141             Test++;
  142             if (LASResult() == LASOK) {
  143             fprintf(stderr, ".");
  144                 V_SetAllCmp(&x, 0.0);
  145                 V_SetAllCmp(&y, 0.0);
  146 
  147                 BegClock = clock();
  148                 for(Cycle = 1; Cycle <= NoCycles; Cycle++) {
  149                     Asgn_VV(&y, Test2_QV(&A, &x));
  150                 }
  151                 EndClock = clock();
  152                 Clock[Test] = (double)(EndClock - BegClock) * Factor;
  153             }
  154     
  155             /* implementation using local variables and pointers,
  156                ascended counting of matrix elements */
  157 
  158             Test++;
  159             if (LASResult() == LASOK) {
  160             fprintf(stderr, ".");
  161                 V_SetAllCmp(&x, 0.0);
  162                 V_SetAllCmp(&y, 0.0);
  163 
  164                 BegClock = clock();
  165                 for(Cycle = 1; Cycle <= NoCycles; Cycle++) {
  166                     Asgn_VV(&y, Test3_QV(&A, &x));
  167                 }
  168                 EndClock = clock();
  169                 Clock[Test] = (double)(EndClock - BegClock) * Factor;
  170             }
  171 
  172             /* implementation using local variables and pointers,
  173                descended counting of matrix elements */
  174 
  175             Test++;
  176             if (LASResult() == LASOK) {
  177             fprintf(stderr, ".");
  178                 V_SetAllCmp(&x, 0.0);
  179                 V_SetAllCmp(&y, 0.0);
  180 
  181                 BegClock = clock();
  182                 for(Cycle = 1; Cycle <= NoCycles; Cycle++) {
  183                     Asgn_VV(&y, Test4_QV(&A, &x));
  184                 }
  185                 EndClock = clock();
  186                 Clock[Test] = (double)(EndClock - BegClock) * Factor;
  187             }
  188 
  189             fprintf(stderr, "\n");
  190             TestNo = Test;
  191 
  192             /* output of results */
  193 
  194             if (LASResult() == LASOK) {
  195                 printf("\n");
  196                 printf("Results:    (for dimension %lu, cycles %d)\n",
  197                     (unsigned long)Dim, NoCycles);
  198                 printf("--------\n\n");
  199                 printf(" implementation    CPU time\n");
  200                 printf("-----------------------------------------\n");
  201                 if (ClockLASPack > 1e-5)
  202                     printf("    LASPack    %12.3e s = 100.0 %%\n", ClockLASPack);
  203                 for (Test = 1; Test <= TestNo; Test++) {
  204                     if (Clock[Test] > 1e-5)
  205                         printf("      %2d       %12.3e s = %5.1f %%\n",
  206                             Test, Clock[Test], 100.0 * Clock[Test] / ClockLASPack);
  207                 }
  208                 printf("-----------------------------------------\n\n");
  209                 printf("For details to the implementations look at the file\n");
  210         printf("laspack/examples/matropt/testproc.c.\n");
  211         printf("The current LASPack version corresponds to the implementation 3.\n");
  212         printf("This is, in comparison with LASPack, simplified and can be therefore\n");
  213         printf("a little faster.\n");
  214             }
  215 
  216             /* LASPack error messages */
  217             if (LASResult() != LASOK) {
  218                 fprintf(stderr, "\n");
  219                 fprintf(stderr, "LASPack error: ");
  220                 WriteLASErrDescr(stderr);     
  221             }
  222 
  223             /* destruction of test matrix and vectors */
  224             Q_Destr(&A);
  225             V_Destr(&x);
  226             V_Destr(&y);
  227         } else {
  228             /* error messages */
  229             if (OptResult() == OptNotDefErr || OptResult() == OptSyntaxErr || Help) {
  230                 fprintf(stderr, "\n");
  231                 PrintHelp();
  232             }
  233             if (OptResult() == OptDescrErr) {
  234                 fprintf(stderr, "\n");
  235                 fprintf(stderr, "Description of an option faulty.\n");
  236             }
  237         }
  238         
  239         if (OptTab.Descr != NULL) 
  240             free(OptTab.Descr);
  241     } else {
  242         /* error message */
  243         fprintf(stderr, "\n");
  244         fprintf(stderr, "Not enought memory for analysis of command line options.\n");
  245     }
  246     
  247     return(0);
  248 }
  249 
  250 static void GenMatr(QMatrix *Q)
  251 /* generate matrix Q with a given number of subdiagonals 
  252    for both triangular parts of the matrix */
  253 {
  254     size_t NoSubdiag = 3;
  255 
  256     size_t Dim, RoC, Len1, Len2, Entry, Subdiag, Pos;
  257     Boolean Symmetry;
  258     ElOrderType ElOrder;
  259 
  260     if (LASResult() == LASOK) {
  261         Dim = Q_GetDim(Q);
  262     Symmetry = Q_GetSymmetry(Q);
  263     ElOrder = Q_GetElOrder(Q);
  264         for (RoC = 1; RoC <= Dim; RoC++) {
  265             if (RoC + NoSubdiag <= Dim)
  266             Len1 = NoSubdiag;
  267         else
  268             Len1 = Dim - RoC;
  269         if (RoC > NoSubdiag)
  270             Len2 = NoSubdiag;
  271         else
  272             Len2 = RoC - 1;
  273         if (Symmetry) {
  274             if (ElOrder == Rowws)
  275                     Q_SetLen(Q, RoC, Len1 + 1);
  276             if (ElOrder == Clmws)
  277                     Q_SetLen(Q, RoC, Len2 + 1);
  278         } else {
  279                 Q_SetLen(Q, RoC, Len1 + Len2 + 1);
  280         }
  281             if (LASResult() == LASOK) {
  282             /* main diagonal entry */
  283             Entry = 0;
  284         Q__SetEntry(Q, RoC, Entry, RoC, 1.0);
  285         /* entries for subdiagonals in both triagonal parts */
  286             for (Subdiag = 1; Subdiag <= NoSubdiag ; Subdiag++) {
  287             Pos = RoC + Subdiag;
  288                 if ((!Symmetry || (ElOrder == Rowws)) && Pos <= Dim) {
  289                 Entry++;
  290                     Q__SetEntry(Q, RoC, Entry, Pos, 1.0);
  291             }
  292                 if ((!Symmetry || (ElOrder == Clmws)) && RoC > Subdiag) {
  293                 Pos = RoC - Subdiag;
  294                     Entry++;
  295                         Q__SetEntry(Q, RoC, Entry, Pos, 1.0);
  296                 }
  297         }
  298         }
  299     }
  300     }
  301 }
  302 
  303 static void PrintHelp(void)
  304 /* print help for syntax and avaible options */
  305 {
  306     fprintf(stderr, "Syntax:\n");
  307     fprintf(stderr, "  matropt [-d<dim>] [-c<cycles>] [-h]\n\n");
  308     fprintf(stderr, "Options:\n");
  309     fprintf(stderr, "  -d  set matrix dimension to <dim>, default is %lu\n",
  310 #if defined(__MSDOS__) || defined(MSDOS)
  311         (unsigned long)1000);
  312 #else
  313             (unsigned long)10000);
  314 #endif      
  315     fprintf(stderr, "  -c  set number of cycles to <cycles>, default is 10\n");
  316     fprintf(stderr, "  -h  print this help\n");
  317 }
  318