source: git/Singular/TestMinors.cc @ 0af9ba

spielwiese
Last change on this file since 0af9ba was 0af9ba, checked in by Frank Seelisch <seelisch@…>, 15 years ago
bug fix git-svn-id: file:///usr/local/Singular/svn/trunk@12163 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 33.4 KB
Line 
1#include "mod2.h"
2
3#ifdef HAVE_MINOR
4
5#include "structs.h"
6#include "polys.h"
7#include "ideals.h"
8#include <Cache.h>
9#include <Minor.h>
10#include <MinorProcessor.h>
11#include <iostream>
12#include <fstream>
13#include <time.h>
14#include "TestMinors.h"
15#include <stdio.h>
16#include "PrettyPrinter.h"
17#include "febase.h"
18
19using namespace std;
20
21poly zeroPoly = pISet(0);
22int nonZeroCounter = 0;
23
24void testOneMinor(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
25                  int minorSize, int randomSeed, int cacheEntries, long cacheWeight);
26void testAllMinors(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
27                   int minorRows, int minorColumns, int minorSize, int randomSeed, int cacheEntries, long cacheWeight);
28void testAllMinorsUntil(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
29                        int minorSize, int randomSeed, int cacheEntries, long cacheWeight, int targetMinor,
30                        bool checkForEquality, int maxLoops);
31
32void minorUsageInfo()
33{
34  PrintS("\nType 'system(\"minors\", 0);' to run 5 default tests with a random");
35  PrintS("\ninteger matrix. This test, for which no ring needs to be declared");
36  PrintS("\nbeforehand, will generate the file 'minor_output_results_longs.txt'");
37  PrintS("\nincluding all results and runtimes, and a much more detailed file");
38  PrintS("\n'minor_output_complete_longs.txt' (both in the folder of the SIN-");
39  PrintS("\nGULAR executable).");
40  PrintS("\n\nType 'system(\"minors\", m, k, strategies, nCache, wCache,");
41  PrintS("\n             dumpMinors, dumpResults, dumpComplete, dumpConsole);'");
42  PrintS("\nto compute all k x k minors of the poly matrix m. Depending on the");
43  PrintS("\ninteger 'strategies', e.g. = '420', these tests will first be per-");
44  PrintS("\nformed without a cache (due to lowest digit '0') and afterwards");
45  PrintS("\nwith a cache, deploying the strategies '2' and '4'. (There are five");
46  PrintS("\nstrategies available; always sort strategies from highest to lowest,");
47  PrintS("\ni.e., use '5210' instead of '2501'.");
48  PrintS("\nWhen a cache is used, it has a maximum number of 'nCache' entries");
49  PrintS("\nand a maximum weight of 'wCache'. (The weight of the cache equals");
50  PrintS("\nthe total number of monomials counted over all cached polynomials.)");
51  PrintS("\nNote that a ring needs to be defined beforehand, and the poly matrix");
52  PrintS("\nm. Set 'dumpMinors' to 1 in order to write all non-zero minors to");
53  PrintS("\nthe file 'non_zero_poly_minors.txt'. When 'dumpResults' equals 1,");
54  PrintS("\nan additional file 'minor_output_results_polys.txt' will be written");
55  PrintS("\nwhich contains an overview over all results and runtimes. Set");
56  PrintS("\n'dumpComplete' to 1 in order to obtain a very detailed file output");
57  PrintS("\nin 'minor_output_complete_polys.txt'. (All files will be created in");
58  PrintS("\nthe folder of the SINGULAR executable.)");
59  PrintS("\nSet 'dumpConsole' to 1 in order to write everything also to the con-");
60  PrintS("\nsole.");
61  PrintS("\n\nType 'ideal i = system(\"minors\", m, k, strategy, nCache, wCache);'");
62  PrintS("\nto obtain the ideal generated by all k x k non-zero minors of the");
63  PrintS("\npoly matrix m. Note again that both ring and matrix must be defined");
64  PrintS("\nbeforehand. (Note that no checks for duplicate ideal generators will");
65  PrintS("\nbe performed.) With 'strategy' == 0, the computation will be run with-");
66  PrintS("\nout cache; otherwise a cache with the respective strategy (1, ..., 5)");
67  PrintS("\nwill be used. In this case, the cache will have at most 'nCache' en-");
68  PrintS("\ntries, i.e. cached sub-minors, and a maximum weight of 'wCache'. (The");
69  PrintS("\nweight of the cache equals the total number of monomials counted over");
70  PrintS("\nall cached polynomials.)");
71  PrintS("\nFor this command, there will be no file or console outputs.\n\n");
72}
73
74/**
75* A method for obtaining a random matrix with specified properties.
76* The returned structure is 1-dimensional, i.e., the matrix from
77* top to bottom, left to right.
78*/
79void fillRandomMatrix(const int rowCount, const int columnCount, const int randomSeed,
80                      const int zeroPercentage, const int entryBound, int* theMatrix) {
81    int theSize = rowCount * columnCount;
82    srand(randomSeed); // random seed for ensuring reproducability of experiments
83    for (int i = 0; i < theSize; i++) {
84        if ((rand() % 100) < zeroPercentage)
85            theMatrix[i] = 0;
86        else
87            theMatrix[i] = 1 + (rand() % entryBound); // ranges from 1 to entryBound, including both
88    }
89}
90
91void writeTheMinorIfNonZero(PrettyPrinter& pm, const PolyMinorValue& pmv) {
92    if (!pEqualPolys(pmv.getResult(), zeroPoly))
93    {
94      nonZeroCounter++;
95      +pm < nonZeroCounter < ". " < pString(pmv.getResult());
96    }
97}
98
99/**
100* A method for testing the implementation.<br>
101* All intermediate and final results will be printed to both the console and
102* into files with the names specified by filenameForCompleteOutput and
103* filenameForResultOutput, which both reside in the path of the compiled executable.
104* @param argc not used
105* @param *argv[] not used
106*/
107int testIntMinors (const int dummy) {
108  // for output of non-zero minors into file
109  PrettyPrinter prpr("minor_output_complete_longs.txt", "minor_output_results_longs.txt", false, false, -1, "   ");
110
111  // computes just one minor:
112  testOneMinor(prpr, "Test I", 7, 10, 50, 20, 5, 471, 70, 1000);
113  +prpr; +prpr;
114
115  // computes all (1470) minors of a specified size (6x6):
116  testAllMinors(prpr, "Test II", 7, 10, 50, 20, 7, 10, 6, 471, 200, 10000);
117  +prpr; +prpr;
118
119  // looks for minor == 3000; to this end, it needs to compute 1001 minors:
120  testAllMinorsUntil(prpr, "Test III", 100, 60, 10, 10, 6, 471, 300, 10000, 3000, true, 10000);
121  +prpr; +prpr;
122
123  // looks for the first non-zero minor (6x6); to this end, it needs to compute 476 minors:
124  testAllMinorsUntil(prpr, "Test IV", 100, 60, 10, 75, 6, 4712, 300, 10000, 0, false, 10000);
125  +prpr; +prpr;
126
127  // looks for minor == 265986; to this end, it needs to compute 16 minors:
128  testAllMinorsUntil(prpr, "Test V", 100, 60, 10, 10, 6, 471, 300, 10000, 265986, true, 10000);
129
130  return 0;
131}
132
133void testStuff (const poly p)
134{
135  PrintLn(); PrintS("poly = "); PrintS(pString(p));
136  PrintLn(); PrintS("length of poly = ");
137  char h[10];
138  sprintf(h, "%d", pLength(p));
139  PrintS(h);
140}
141
142int testAllPolyMinors(matrix mat, int minorSize, int strategies, int cacheEntries, long cacheWeight,
143                      int dumpMinors, int dumpResults, int dumpComplete, int dumpConsole) {
144  // for pretty printing and file output of results and runtimes
145  PrettyPrinter prpr(dumpComplete == 1 ? "minor_output_complete_polys.txt" : "",
146                     dumpResults == 1 ? "minor_output_results_polys.txt" : "",
147                     false, false, dumpConsole == 1 ? 0 : -1, "   ");
148
149  // for output of non-zero minors into file
150  PrettyPrinter pm(dumpMinors == 1 ? "non_zero_poly_minors.txt" : "", "", false, false, -1, "   ");
151
152  int rowCount = mat->nrows;
153  int columnCount = mat->ncols;
154  long totalTimeStart, totalTime, printTimeStart, printTime;
155  string testHeader = "COMPUTE ALL MINORS IN A POLY MATRIX";
156
157  prpr < testHeader;
158  +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; // underlines the header string
159  +prpr < "Testing the successive computation of all minors of a given size without and with cache, respectively.";
160  +prpr < "In the case of computing with cache, different caching strategies may be deployed.";
161  +prpr < "The provided matrix is expected to have SINGULAR polys as entries.";
162
163  poly* myPolyMatrix = (poly*)(mat->m);
164  PolyMinorProcessor mp;
165  mp.defineMatrix(rowCount, columnCount, myPolyMatrix);
166
167  /* The next lines are for defining the sub-matrix of myPolyMatrix
168     from which we want to compute all k x k - minors.
169     In the given setting, we want the entire matrix to form the sub-matrix. */
170  int minorRows = rowCount;
171  int minorColumns = columnCount;
172  int myRowIndices[minorRows];  for (int i = 0; i < minorRows; i++) myRowIndices[i] = i;
173  int myColumnIndices[minorColumns]; for (int i = 0; i < minorColumns; i++) myColumnIndices[i] = i;
174
175  // setting sub-matrix and size of minors of interest within that sub-matrix:
176  mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
177  mp.setMinorSize(minorSize);
178
179  // define the cache:
180  Cache<MinorKey, PolyMinorValue> cch(cacheEntries, cacheWeight);
181
182  // container for all upcoming results
183  PolyMinorValue theMinor;
184
185  // counters...
186  int k = 1;
187  int totalMultiplications = 0;
188  int totalAdditions = 0;
189  int totalMultiplicationsAccumulated = 0;
190  int totalAdditionsAccumulated = 0;
191
192  // target for retrieving and writing momentary row and column indices:
193  int myIndexArray[32];
194
195  if (strategies % 10 == 0)
196  {
197    strategies = strategies / 10;
198    +prpr; +prpr < "Results - " < testHeader < " - no cache";
199    +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
200
201    pm < "non-zero minors - no cache\n==========================";
202    nonZeroCounter = 0;
203
204    printTime = 0; totalTimeStart = clock();
205    // iteration over all minors of size "minorSize x minorSize"
206    while (mp.hasNextMinor()) {
207        // retrieving the minor:
208        theMinor = mp.getNextMinor();
209        printTimeStart = clock();
210        writeTheMinorIfNonZero(pm, theMinor);
211
212        // updating counters:
213        totalMultiplications += theMinor.getMultiplications();
214        totalAdditions += theMinor.getAdditions();
215        totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
216        totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
217 
218        // writing current row indices:
219        mp.getCurrentRowIndices(myIndexArray);
220        +prpr; prpr < k++ < ". minor (rows: ";
221        for (int i = 0; i < minorSize; i++) {
222            if (i != 0) prpr < ", ";
223            prpr < myIndexArray[i];
224        };
225 
226        // writing current column indices:
227        mp.getCurrentColumnIndices(myIndexArray);
228        prpr < "; columns: ";
229        for (int i = 0; i < minorSize; i++) {
230            if (i != 0) prpr < ", ";
231            prpr < myIndexArray[i];
232        };
233        prpr < ") = ";
234 
235        // write the actual value of the minor:
236        prpr < theMinor.toString();
237        printTime += clock() - printTimeStart;
238    };
239    totalTime = clock() - totalTimeStart - printTime;
240
241    // writing summarized information
242    ++prpr; ++prpr << "Operation counters - " << testHeader << " - no cache";
243    ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
244    prpr << totalAdditions << " additions";
245    ++prpr << "number of non-zero minors = " << nonZeroCounter;
246    ++prpr << "(time = " << totalTime << " msec)";
247  }
248
249  for (int strategy = 1; strategy <= 5; strategy++) {
250    if (strategies % 10 == strategy)
251    {
252      strategies = strategies / 10;
253      // setting sub-matrix, size of minors of interest within that sub-matrix, and strategy:
254      mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
255      mp.setMinorSize(minorSize);
256      MinorValue::SetRankingStrategy(strategy);
257 
258      // counters...
259      k = 1;
260      totalMultiplications = 0;
261      totalAdditions = 0;
262      totalMultiplicationsAccumulated = 0;
263      totalAdditionsAccumulated = 0;
264 
265      // cleaning up and redefinition of the cache:
266      cch.clear();
267      Cache<MinorKey, PolyMinorValue> cch(cacheEntries, cacheWeight);
268 
269      +prpr; +prpr <  "Results - " < testHeader < " - using cache - deploying caching strategy #" < strategy;
270      +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
271
272      pm < "\n\nnon-zero minors - using cache - deploying caching strategy #" < strategy
273         < "\n=============================================================";
274      nonZeroCounter = 0;
275 
276      printTime = 0; totalTimeStart = clock();
277      // iteration over all minors of size "minorSize x minorSize"
278      while (mp.hasNextMinor()) {
279          // retrieving the minor:
280          theMinor = mp.getNextMinor(cch);
281          printTimeStart = clock();
282          writeTheMinorIfNonZero(pm, theMinor);
283 
284          // updating counters:
285          totalMultiplications += theMinor.getMultiplications();
286          totalAdditions += theMinor.getAdditions();
287          totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
288          totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
289
290          // writing current row indices:
291          mp.getCurrentRowIndices(myIndexArray);
292          +prpr < k++ < ". minor (rows: ";
293          for (int i = 0; i < minorSize; i++) {
294              if (i != 0) prpr < ", ";
295              prpr < myIndexArray[i];
296          };
297 
298          // writing current column indices:
299          mp.getCurrentColumnIndices(myIndexArray);
300          prpr < "; columns: ";
301          for (int i = 0; i < minorSize; i++) {
302              if (i != 0) prpr < ", ";
303              prpr < myIndexArray[i];
304          };
305          prpr < ") = ";
306 
307          // writeMinor the actual value of the minor:
308          prpr < theMinor.toString();
309          printTime += clock() - printTimeStart;
310      };
311      totalTime = clock() - totalTimeStart - printTime;
312 
313      // writing summarized information
314      ++prpr; ++prpr << "Operation counters - " << testHeader << " - using cache - deploying caching strategy #" << strategy;
315      ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
316      prpr << totalAdditions << " additions";
317      ++prpr << "(computation without reuse would need " << totalMultiplicationsAccumulated << " and ";
318      prpr << totalAdditionsAccumulated << " additions)";
319      ++prpr << "number of non-zero minors = " << nonZeroCounter;
320      ++prpr << "(time = " << totalTime << " msec)";
321      +prpr < "The cache looks like this:";
322      +prpr < cch.toString();
323    }
324  }
325
326  return 0;
327}
328
329ideal testAllPolyMinorsAsIdeal(matrix mat, int minorSize, int strategy, int cacheEntries, long cacheWeight)
330{
331    // counters + auxiliary stuff
332  long totalMultiplications = 0;
333  long totalAdditions = 0;
334  long totalMultiplicationsAccumulated = 0;
335  long totalAdditionsAccumulated = 0;
336  char h[30];
337
338  int rowCount = mat->nrows;
339  int columnCount = mat->ncols;
340
341  poly* myPolyMatrix = (poly*)(mat->m);
342  PolyMinorProcessor mp;
343  mp.defineMatrix(rowCount, columnCount, myPolyMatrix);
344
345  /* The next lines are for defining the sub-matrix of myPolyMatrix
346     from which we want to compute all k x k - minors.
347     In the given setting, we want the entire matrix to form the sub-matrix. */
348  int minorRows = rowCount;
349  int minorColumns = columnCount;
350  int myRowIndices[minorRows];  for (int i = 0; i < minorRows; i++) myRowIndices[i] = i;
351  int myColumnIndices[minorColumns]; for (int i = 0; i < minorColumns; i++) myColumnIndices[i] = i;
352
353  // setting sub-matrix and size of minors of interest within that sub-matrix:
354  mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
355  mp.setMinorSize(minorSize);
356
357  // containers for all upcoming results
358  PolyMinorValue theMinor;
359  poly po;
360  ideal iii = idInit(1, 0);
361
362  if (strategy == 0)
363  {
364    PrintLn(); PrintS("new code uses no cache");
365    // iteration over all minors of size "minorSize x minorSize"
366    while (mp.hasNextMinor()) {
367      // retrieving the minor:
368      theMinor = mp.getNextMinor();
369      po = theMinor.getResult();
370      totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
371      totalMultiplications += theMinor.getMultiplications();
372      totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
373      totalAdditions += theMinor.getAdditions();
374      idInsertPoly(iii, po); // will include po only if it is not the zero polynomial
375    }
376  }
377  else
378  {
379    PrintLn(); PrintS("new code uses cache with caching strategy ");
380    sprintf(h, "%d", strategy); PrintS(h);
381    MinorValue::SetRankingStrategy(strategy);
382    Cache<MinorKey, PolyMinorValue> cch(cacheEntries, cacheWeight);
383    // iteration over all minors of size "minorSize x minorSize"
384    while (mp.hasNextMinor()) {
385      // retrieving the minor:
386      theMinor = mp.getNextMinor(cch);
387      po = theMinor.getResult();
388      totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
389      totalMultiplications += theMinor.getMultiplications();
390      totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
391      totalAdditions += theMinor.getAdditions();
392      idInsertPoly(iii, po); // will include po only if it is not the zero polynomial
393    }
394  }
395 
396  PrintLn(); PrintS("numbers of performed operations");
397  PrintLn(); PrintS("   polynomial-to-polynomial multiplications: ");
398  sprintf(h, "%ld", totalMultiplications); PrintS(h);
399  PrintLn(); PrintS("   polynomial-to-polynomial additions: ");
400  sprintf(h, "%ld", totalAdditions); PrintS(h);
401  PrintLn(); PrintS("   (polynomial-to-polynomial multiplications without cache would be: ");
402  sprintf(h, "%ld", totalMultiplicationsAccumulated); PrintS(h); PrintS(")");
403  PrintLn(); PrintS("   (polynomial-to-polynomial additions without cache would be: ");
404  sprintf(h, "%ld", totalAdditionsAccumulated); PrintS(h); PrintS(")");
405  PrintLn(); PrintLn();
406
407  return iii;
408}
409
410/**
411* A method for testing the computation of one minor; without and with cache, respectively.<br>
412* All results should be equal no matter whether we do or do not use a cache. Neither should
413* the cache strategy influence the mathematical value of the minor.
414*/
415void testOneMinor(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
416                  int minorSize, int randomSeed, int cacheEntries, long cacheWeight) {
417    long start, end;
418
419    prpr < testHeader;
420    +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; /* underlines the header string */
421    +prpr < "Testing the computation of one minor without and with cache, respectively.";
422    +prpr < "For computing with cache, 5 different caching strategies will be deployed.";
423
424    int* myMatrix = new int[rowCount * columnCount];
425    fillRandomMatrix(rowCount, columnCount, randomSeed, zeroPercentage, entryBound, myMatrix);
426
427    LongMinorProcessor mp;
428    mp.defineMatrix(rowCount, columnCount, myMatrix);
429
430    int myRowIndices[minorSize];  for (int i = 0; i < minorSize; i++) myRowIndices[i] = i;
431    int myColumnIndices[minorSize]; for (int i = 0; i < minorSize; i++) myColumnIndices[i] = columnCount - minorSize + i;
432
433    // We would like to printout mp. For that, we need to provide complete information of
434    // what minors we intend to compute later on.
435    mp.defineSubMatrix(minorSize, myRowIndices, minorSize, myColumnIndices);
436    mp.setMinorSize(minorSize);
437
438    +prpr; +prpr < mp.toString(); +prpr; +prpr;
439
440    // compute the minor without cache:
441    prpr << "Results - " << testHeader << " - no cache";
442    start = clock();
443    LongMinorValue mv = mp.getMinor(minorSize, myRowIndices, myColumnIndices);
444    end = clock();
445    ++prpr << "value of minor = " << mv.toString();
446    ++prpr << "(time = " << (end - start) << " msec)";
447
448    // define the cache:
449    Cache<MinorKey, LongMinorValue> cch = Cache<MinorKey, LongMinorValue>(cacheEntries, cacheWeight);
450
451    // compute minor using the cache, for all implemented caching strategies:
452    for (int strategy = 1; strategy <= 5; strategy++) {
453        // clear cache:
454        cch.clear();
455        mp.defineSubMatrix(minorSize, myRowIndices, minorSize, myColumnIndices);
456
457        // compute the minor using the cache and current strategy
458        LongMinorValue::SetRankingStrategy(strategy);
459        start = clock();
460        mv = mp.getMinor(minorSize, myRowIndices, myColumnIndices, cch);
461        end = clock();
462
463        ++prpr; ++prpr << "Results - " << testHeader;
464        prpr << " - using cache - deploying caching strategy #" << strategy;
465        ++prpr << "value of minor = " << mv.toString();
466        ++prpr << "(time = " << (end - start) << " msec)";
467        +prpr < "The cache looks like this:";
468        +prpr < cch.toString();
469    }
470    delete [] myMatrix;
471}
472
473/**
474* A method for testing the computation of all minors of a given size within a pre-defined
475* sub-matrix of an underlying matrix.<br>
476* Again, we do this first without cache, and later using a cache, respectively.<br>
477* All results should be equal no matter whether we do or do not use a cache. Neither should the cache strategy
478* influence the mathematical value of the minor.
479*/
480void testAllMinors(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
481                   int minorRows, int minorColumns, int minorSize, int randomSeed, int cacheEntries, long cacheWeight) {
482    long totalTimeStart, totalTime, printTimeStart, printTime;
483
484    prpr < testHeader;
485    +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; // underlines the header string
486    +prpr < "Testing the successive computation of all minors of a given size without and with cache, respectively.";
487    +prpr < "In the case of computing with cache, 5 different caching strategies will be deployed.";
488
489    int* myMatrix = new int[rowCount * columnCount];
490    fillRandomMatrix(rowCount, columnCount, randomSeed, zeroPercentage, entryBound, myMatrix);
491
492    LongMinorProcessor mp;
493    mp.defineMatrix(rowCount, columnCount, myMatrix);
494
495    int myRowIndices[minorRows];  for (int i = 0; i < minorRows; i++) myRowIndices[i] = i;
496    int myColumnIndices[minorColumns]; for (int i = 0; i < minorColumns; i++) myColumnIndices[i] = columnCount - minorColumns + i;
497
498    // setting sub-matrix and size of minors of interest within that sub-matrix:
499    mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
500    mp.setMinorSize(minorSize);
501
502    +prpr; +prpr < mp.toString();
503
504    // define the cache:
505    Cache<MinorKey, LongMinorValue> cch = Cache<MinorKey, LongMinorValue>(cacheEntries, cacheWeight);
506
507    // container for all upcoming results
508    LongMinorValue theMinor;
509
510    // counters...
511    int k = 1;
512    int totalMultiplications = 0;
513    int totalAdditions = 0;
514    int totalMultiplicationsAccumulated = 0;
515    int totalAdditionsAccumulated = 0;
516
517    // target for retrieving and writing momentary row and column indices:
518    int myIndexArray[32];
519
520    +prpr; +prpr < "Results - " < testHeader < " - no cache";
521    +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
522
523    printTime = 0; totalTimeStart = clock();
524    // iteration over all minors of size "minorSize x minorSize"
525    while (mp.hasNextMinor()) {
526        // retrieving the minor:
527        theMinor = mp.getNextMinor();
528        printTimeStart = clock();
529
530        // updating counters:
531        totalMultiplications += theMinor.getMultiplications();
532        totalAdditions += theMinor.getAdditions();
533        totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
534        totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
535
536        // writing current row indices:
537        mp.getCurrentRowIndices(myIndexArray);
538        +prpr < k++ < ". minor (rows: ";
539        for (int i = 0; i < minorSize; i++) {
540            if (i != 0) prpr < ", ";
541            prpr < myIndexArray[i];
542        };
543
544        // writing current column indices:
545        mp.getCurrentColumnIndices(myIndexArray);
546        prpr < "; columns: ";
547        for (int i = 0; i < minorSize; i++) {
548            if (i != 0) prpr < ", ";
549            prpr < myIndexArray[i];
550        };
551        prpr < ") = ";
552
553        // write the actual value of the minor:
554        prpr < theMinor.toString();
555        printTime += clock() - printTimeStart;
556    };
557    totalTime = clock() - totalTimeStart - printTime;
558    // writing summarized information
559    ++prpr; ++prpr << "Operation counters - " << testHeader << " - no cache";
560    ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
561    prpr << totalAdditions << " additions";
562    ++prpr << "(time = " << totalTime << " msec)";
563
564    for (int strategy = 1; strategy <= 5; strategy++) {
565        // setting sub-matrix, size of minors of interest within that sub-matrix, and strategy:
566        mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
567        mp.setMinorSize(minorSize);
568        LongMinorValue::SetRankingStrategy(strategy);
569
570        // counters...
571        k = 1;
572        totalMultiplications = 0;
573        totalAdditions = 0;
574        totalMultiplicationsAccumulated = 0;
575        totalAdditionsAccumulated = 0;
576
577        // cleaning up and redefinition of the cache:
578        cch.clear();
579        cch = Cache<MinorKey, LongMinorValue>(cacheEntries, cacheWeight);
580
581        +prpr; +prpr < "Results - " < testHeader < " - using cache - deploying caching strategy #" < strategy;
582        +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
583
584        printTime = 0; totalTimeStart = clock();
585        // iteration over all minors of size "minorSize x minorSize"
586        while (mp.hasNextMinor()) {
587            // retrieving the minor:
588            theMinor = mp.getNextMinor(cch);
589            printTimeStart = clock();
590
591            // updating counters:
592            totalMultiplications += theMinor.getMultiplications();
593            totalAdditions += theMinor.getAdditions();
594            totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
595            totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
596
597            // writing current row indices:
598            mp.getCurrentRowIndices(myIndexArray);
599            +prpr < k++ < ". minor (rows: ";
600            for (int i = 0; i < minorSize; i++) {
601                if (i != 0) prpr < ", ";
602                prpr < myIndexArray[i];
603            };
604
605            // writing current column indices:
606            mp.getCurrentColumnIndices(myIndexArray);
607            prpr < "; columns: ";
608            for (int i = 0; i < minorSize; i++) {
609                if (i != 0) prpr < ", ";
610                prpr < myIndexArray[i];
611            };
612            prpr < ") = ";
613
614            // writeMinor the actual value of the minor:
615            prpr < theMinor.toString();
616            printTime += clock() - printTimeStart;
617        };
618        totalTime = clock() - totalTimeStart - printTime;
619        // writing summarized information
620        ++prpr; ++prpr << "Operation counters - " << testHeader << " - using cache - deploying caching strategy #" << strategy;
621        ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
622        prpr << totalAdditions << " additions";
623        ++prpr << "(computation without reuse would need " << totalMultiplicationsAccumulated << " and ";
624        prpr << totalAdditionsAccumulated << " additions)";
625        ++prpr << "(time = " << totalTime << " msec)";
626        +prpr < "The cache looks like this:";
627        +prpr < cch.toString();
628    }
629    delete [] myMatrix;
630}
631
632/**
633* A method for testing the computation of all minors of a given size within a pre-defined
634* sub-matrix of an underlying large matrix.<br>
635* Again, we do this first without cache, and later using a cache, respectively.<br>
636* All results should be equal no matter whether we do or do not use a cache. Neither should the cache strategy
637* influence the mathematical value of the minor.
638* zeroP: this is the probability for zero entries in the matrix;
639*        all other matrix entries will range from 1 to entryBound and be equally distributed
640*/
641void testAllMinorsUntil(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
642                        int minorSize, int randomSeed, int cacheEntries, long cacheWeight, int targetMinor,
643                        bool checkForEquality, int maxLoops) {
644    long totalTimeStart, totalTime, printTimeStart, printTime;
645
646    prpr < testHeader;
647    +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; // underlines the header string
648    +prpr < "Testing the successive computation of minors of a given size without and with ";
649    +prpr < "cache, respectively, until a minor with a certain value is found.";
650    +prpr < "In the case of computing with cache, 5 different caching strategies will be deployed.";
651
652    +prpr; +prpr < "In this test, the matrix is " < rowCount < " x " < columnCount < ".";
653    +prpr < "The minor we are looking for is " < minorSize < " x " < minorSize < ", and ";
654    prpr < "is supposed to have a value of ";
655    if (!checkForEquality) prpr < "<> ";
656    prpr < targetMinor < ".";
657    +prpr < "As an upper bound for the number of loops, at most " < maxLoops < " minors will be computed.";
658
659    int* myMatrix = new int[rowCount * columnCount];
660    fillRandomMatrix(rowCount, columnCount, randomSeed, zeroPercentage, entryBound, myMatrix);
661
662    LongMinorProcessor mp;
663    mp.defineMatrix(rowCount, columnCount, myMatrix);
664
665    int myRowIndices[rowCount]; for (int i = 0; i < rowCount; i++) myRowIndices[i] = i; // choosing all rows
666    int myColumnIndices[columnCount]; for (int i = 0; i < columnCount; i++) myColumnIndices[i] = i; // choosing all columns
667
668    // define the cache:
669    Cache<MinorKey, LongMinorValue> cch = Cache<MinorKey, LongMinorValue>(cacheEntries, cacheWeight);
670
671    // container for all upcoming results
672    LongMinorValue theMinor;
673
674    // counters...
675    int k = 1;
676    int totalMultiplications = 0;
677    int totalAdditions = 0;
678    int totalMultiplicationsAccumulated = 0;
679    int totalAdditionsAccumulated = 0;
680
681    // setting sub-matrix and size of minors of interest within that sub-matrix:
682    mp.defineSubMatrix(rowCount, myRowIndices, columnCount, myColumnIndices);
683    mp.setMinorSize(minorSize);
684
685    +prpr; +prpr < mp.toString();
686
687    +prpr < "Results - " < testHeader < " - no cache";
688    +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
689    prpr < " until first minor with specified value is found:";
690
691    bool minorFound = false;
692    int loops = 0;
693    printTime = 0; totalTimeStart = clock();
694    // iteration over all minors of size "minorSize x minorSize"
695    while (mp.hasNextMinor() && (!minorFound) && (loops < maxLoops)) {
696
697        // retrieving the minor:
698        theMinor = mp.getNextMinor();
699        printTimeStart = clock();
700        minorFound = (checkForEquality ? (theMinor.getResult() == targetMinor) : (theMinor.getResult() != targetMinor));
701
702        // updating counters:
703        totalMultiplications += theMinor.getMultiplications();
704        totalAdditions += theMinor.getAdditions();
705        totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
706        totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
707
708        // writing current minor
709        +prpr < k++ < ". minor = " < theMinor.getResult();
710
711        loops++;
712        printTime += clock() - printTimeStart;
713    };
714    totalTime = clock() - totalTimeStart - printTime;
715    // writing summarized information
716    ++prpr; ++prpr << "Operation counters - " << testHeader << " - no cache";
717    ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
718    prpr << totalAdditions << " additions";
719    ++prpr << "(time = " << totalTime << " msec)";
720
721    for (int strategy = 1; strategy <= 5; strategy++) {
722        // setting sub-matrix, size of minors of interest within that sub-matrix, and strategy:
723        mp.defineSubMatrix(rowCount, myRowIndices, columnCount, myColumnIndices);
724        mp.setMinorSize(minorSize);
725        LongMinorValue::SetRankingStrategy(strategy);
726
727        // counters...
728        k = 1;
729        totalMultiplications = 0;
730        totalAdditions = 0;
731        totalMultiplicationsAccumulated = 0;
732        totalAdditionsAccumulated = 0;
733
734        // cleaning up and redefinition of the cache:
735        cch.clear();
736        cch = Cache<MinorKey, LongMinorValue>(cacheEntries, cacheWeight);
737
738        +prpr; +prpr < testHeader < " - using cache - deploying caching strategy #" < strategy;
739        +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
740        prpr < " until first minor with specified value is found:";
741
742        int loops = 0;
743        bool minorFound = false;
744        printTime = 0; totalTimeStart = clock();
745        // iteration over all minors of size "minorSize x minorSize"
746        while (mp.hasNextMinor() && (!minorFound) && (loops < maxLoops)) {
747
748            // retrieving the minor:
749            theMinor = mp.getNextMinor(cch);
750            printTimeStart = clock();
751            minorFound = (checkForEquality ? (theMinor.getResult() == targetMinor) : (theMinor.getResult() != targetMinor));
752
753            // updating counters:
754            totalMultiplications += theMinor.getMultiplications();
755            totalAdditions += theMinor.getAdditions();
756            totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
757            totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
758
759            // writing current minor
760            +prpr < k++ < ". minor = " < theMinor.getResult();
761
762            loops++;
763            printTime += clock() - printTimeStart;
764        };
765        totalTime = clock() - totalTimeStart - printTime;
766        // writing summarized information
767        ++prpr; ++prpr << "Operation counters - " << testHeader << " - using cache - deploying caching strategy #" << strategy;
768        ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
769        prpr << totalAdditions << " additions";
770        ++prpr << "(computation without reuse would need " << totalMultiplicationsAccumulated << " and ";
771        prpr << totalAdditionsAccumulated << " additions)";
772        ++prpr << "(time = " << totalTime << " msec)";
773        +prpr < "The cache has " < cch.getNumberOfEntries() < " (of max. " < cacheEntries < ") entries and a weight of ";
774        prpr < cch.getWeight() < " (of max. " < cacheWeight < ").";
775    }
776    delete [] myMatrix;
777}
778
779#endif // HAVE_MINOR
Note: See TracBrowser for help on using the repository browser.