source: git/Singular/TestMinors.cc @ 60e4be

spielwiese
Last change on this file since 60e4be was 60e4be, checked in by Frank Seelisch <seelisch@…>, 14 years ago
bug fix git-svn-id: file:///usr/local/Singular/svn/trunk@12183 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 37.6 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, int 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, int cacheWeight);
28void testAllMinorsUntil(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
29                        int minorSize, int randomSeed, int cacheEntries, int 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_ints.txt'");
37  PrintS("\nincluding all results and runtimes, and a much more detailed file");
38  PrintS("\n'minor_output_complete_ints.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.");
72  PrintS("\n\nType 'ideal i = system(\"minors\", m, k, strategy, nCache, wCache, ch);'");
73  PrintS("\nto obtain the ideal generated by all k x k non-zero minors of the");
74  PrintS("\ninteger matrix m modulo ch. (For the remaining parameters, see the");
75  PrintS("\nprevious call.)\n\n");
76}
77
78/**
79* A method for obtaining a random matrix with specified properties.
80* The returned structure is 1-dimensional, i.e., the matrix from
81* top to bottom, left to right.
82*/
83void fillRandomMatrix(const int rowCount, const int columnCount, const int randomSeed,
84                      const int zeroPercentage, const int entryBound, int* theMatrix) {
85    int theSize = rowCount * columnCount;
86    siSeed = randomSeed; // random seed for ensuring reproducability of experiments
87    for (int i = 0; i < theSize; i++) {
88        if ((siRand() % 100) < zeroPercentage)
89            theMatrix[i] = 0;
90        else
91            theMatrix[i] = 1 + (siRand() % entryBound); // ranges from 1 to entryBound, including both
92    }
93}
94
95void writeTheMinorIfNonZero(PrettyPrinter& pm, const PolyMinorValue& pmv) {
96    if (!pEqualPolys(pmv.getResult(), zeroPoly))
97    {
98      nonZeroCounter++;
99      +pm < nonZeroCounter < ". " < pString(pmv.getResult());
100    }
101}
102
103/**
104* A method for testing the implementation.<br>
105* All intermediate and final results will be printed to both the console and
106* into files with the names specified by filenameForCompleteOutput and
107* filenameForResultOutput, which both reside in the path of the compiled executable.
108* @param argc not used
109* @param *argv[] not used
110*/
111int testIntMinors (const int dummy) {
112  // for output of non-zero minors into file
113  PrettyPrinter prpr("minor_output_complete_ints.txt", "minor_output_results_ints.txt", false, false, -1, "   ");
114
115  // computes just one minor:
116  testOneMinor(prpr, "Test I", 7, 10, 50, 20, 5, 471, 70, 1000);
117  +prpr; +prpr;
118
119  // computes all (1470) minors of a specified size (6x6):
120  testAllMinors(prpr, "Test II", 7, 10, 50, 20, 7, 10, 6, 471, 200, 10000);
121  +prpr; +prpr;
122
123  // looks for minor == 92868; to this end, it needs to compute 1632 minors:
124  testAllMinorsUntil(prpr, "Test III", 100, 60, 10, 10, 6, 471, 300, 10000, 92868, true, 10000);
125  +prpr; +prpr;
126
127  // looks for the first non-zero minor (6x6); to this end, it needs to compute 229 minors:
128  testAllMinorsUntil(prpr, "Test IV", 100, 60, 10, 75, 6, 4712, 300, 10000, 0, false, 10000);
129  +prpr; +prpr;
130
131  // looks for minor == -43065; to this end, it needs to compute 23 minors:
132  testAllMinorsUntil(prpr, "Test V", 100, 60, 10, 10, 6, 471, 300, 10000, -43065, true, 10000);
133
134  return 0;
135}
136
137void testStuff (const poly p)
138{
139  PrintLn(); PrintS("poly = "); PrintS(pString(p));
140  PrintLn(); PrintS("length of poly = ");
141  char h[10];
142  sprintf(h, "%d", pLength(p));
143  PrintS(h);
144}
145
146int testAllPolyMinors(matrix mat, int minorSize, int strategies, int cacheEntries, int cacheWeight,
147                      int dumpMinors, int dumpResults, int dumpComplete, int dumpConsole) {
148  // for pretty printing and file output of results and runtimes
149  PrettyPrinter prpr(dumpComplete == 1 ? "minor_output_complete_polys.txt" : "",
150                     dumpResults == 1 ? "minor_output_results_polys.txt" : "",
151                     false, false, dumpConsole == 1 ? 0 : -1, "   ");
152
153  // for output of non-zero minors into file
154  PrettyPrinter pm(dumpMinors == 1 ? "non_zero_poly_minors.txt" : "", "", false, false, -1, "   ");
155
156  int rowCount = mat->nrows;
157  int columnCount = mat->ncols;
158  clock_t totalTimeStart, totalTime, printTimeStart, printTime;
159  string testHeader = "COMPUTE ALL MINORS IN A POLY MATRIX";
160
161  prpr < testHeader;
162  +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; // underlines the header string
163  +prpr < "Testing the successive computation of all minors of a given size without and with cache, respectively.";
164  +prpr < "In the case of computing with cache, different caching strategies may be deployed.";
165  +prpr < "The provided matrix is expected to have SINGULAR polys as entries.";
166
167  poly* myPolyMatrix = (poly*)(mat->m);
168  PolyMinorProcessor mp;
169  mp.defineMatrix(rowCount, columnCount, myPolyMatrix);
170
171  /* The next lines are for defining the sub-matrix of myPolyMatrix
172     from which we want to compute all k x k - minors.
173     In the given setting, we want the entire matrix to form the sub-matrix. */
174  int minorRows = rowCount;
175  int minorColumns = columnCount;
176  int myRowIndices[minorRows];  for (int i = 0; i < minorRows; i++) myRowIndices[i] = i;
177  int myColumnIndices[minorColumns]; for (int i = 0; i < minorColumns; i++) myColumnIndices[i] = i;
178
179  // setting sub-matrix and size of minors of interest within that sub-matrix:
180  mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
181  mp.setMinorSize(minorSize);
182
183  // define the cache:
184  Cache<MinorKey, PolyMinorValue> cch(cacheEntries, cacheWeight);
185
186  // container for all upcoming results
187  PolyMinorValue theMinor;
188
189  // counters...
190  int k = 1;
191  int totalMultiplications = 0;
192  int totalAdditions = 0;
193  int totalMultiplicationsAccumulated = 0;
194  int totalAdditionsAccumulated = 0;
195
196  // target for retrieving and writing momentary row and column indices:
197  int myIndexArray[32];
198
199  if (strategies % 10 == 0)
200  {
201    strategies = strategies / 10;
202    +prpr; +prpr < "Results - " < testHeader < " - no cache";
203    +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
204
205    pm < "non-zero minors - no cache\n==========================";
206    nonZeroCounter = 0;
207
208    printTime = 0; totalTimeStart = clock();
209    // iteration over all minors of size "minorSize x minorSize"
210    while (mp.hasNextMinor()) {
211        // retrieving the minor:
212        theMinor = mp.getNextMinor();
213        printTimeStart = clock();
214        writeTheMinorIfNonZero(pm, theMinor);
215
216        // updating counters:
217        totalMultiplications += theMinor.getMultiplications();
218        totalAdditions += theMinor.getAdditions();
219        totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
220        totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
221 
222        // writing current row indices:
223        mp.getCurrentRowIndices(myIndexArray);
224        +prpr; prpr < k++ < ". minor (rows: ";
225        for (int i = 0; i < minorSize; i++) {
226            if (i != 0) prpr < ", ";
227            prpr < myIndexArray[i];
228        };
229 
230        // writing current column indices:
231        mp.getCurrentColumnIndices(myIndexArray);
232        prpr < "; columns: ";
233        for (int i = 0; i < minorSize; i++) {
234            if (i != 0) prpr < ", ";
235            prpr < myIndexArray[i];
236        };
237        prpr < ") = ";
238 
239        // write the actual value of the minor:
240        prpr < theMinor.toString();
241        printTime += clock() - printTimeStart;
242    };
243    totalTime = clock() - totalTimeStart - printTime;
244
245    // writing summarized information
246    ++prpr; ++prpr << "Operation counters - " << testHeader << " - no cache";
247    ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
248    prpr << totalAdditions << " additions";
249    ++prpr << "number of non-zero minors = " << nonZeroCounter;
250    ++prpr << "(time = " << ((totalTime * 1000) / CLOCKS_PER_SEC) << " msec)";
251  }
252
253  for (int strategy = 1; strategy <= 5; strategy++) {
254    if (strategies % 10 == strategy)
255    {
256      strategies = strategies / 10;
257      // setting sub-matrix, size of minors of interest within that sub-matrix, and strategy:
258      mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
259      mp.setMinorSize(minorSize);
260      MinorValue::SetRankingStrategy(strategy);
261 
262      // counters...
263      k = 1;
264      totalMultiplications = 0;
265      totalAdditions = 0;
266      totalMultiplicationsAccumulated = 0;
267      totalAdditionsAccumulated = 0;
268 
269      // cleaning up and redefinition of the cache:
270      cch.clear();
271      Cache<MinorKey, PolyMinorValue> cch(cacheEntries, cacheWeight);
272 
273      +prpr; +prpr <  "Results - " < testHeader < " - using cache - deploying caching strategy #" < strategy;
274      +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
275
276      pm < "\n\nnon-zero minors - using cache - deploying caching strategy #" < strategy
277         < "\n=============================================================";
278      nonZeroCounter = 0;
279 
280      printTime = 0; totalTimeStart = clock();
281      // iteration over all minors of size "minorSize x minorSize"
282      while (mp.hasNextMinor()) {
283          // retrieving the minor:
284          theMinor = mp.getNextMinor(cch);
285          printTimeStart = clock();
286          writeTheMinorIfNonZero(pm, theMinor);
287 
288          // updating counters:
289          totalMultiplications += theMinor.getMultiplications();
290          totalAdditions += theMinor.getAdditions();
291          totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
292          totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
293
294          // writing current row indices:
295          mp.getCurrentRowIndices(myIndexArray);
296          +prpr < k++ < ". minor (rows: ";
297          for (int i = 0; i < minorSize; i++) {
298              if (i != 0) prpr < ", ";
299              prpr < myIndexArray[i];
300          };
301 
302          // writing current column indices:
303          mp.getCurrentColumnIndices(myIndexArray);
304          prpr < "; columns: ";
305          for (int i = 0; i < minorSize; i++) {
306              if (i != 0) prpr < ", ";
307              prpr < myIndexArray[i];
308          };
309          prpr < ") = ";
310 
311          // writeMinor the actual value of the minor:
312          prpr < theMinor.toString();
313          printTime += clock() - printTimeStart;
314      };
315      totalTime = clock() - totalTimeStart - printTime;
316 
317      // writing summarized information
318      ++prpr; ++prpr << "Operation counters - " << testHeader << " - using cache - deploying caching strategy #" << strategy;
319      ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
320      prpr << totalAdditions << " additions";
321      ++prpr << "(computation without reuse would need " << totalMultiplicationsAccumulated << " and ";
322      prpr << totalAdditionsAccumulated << " additions)";
323      ++prpr << "number of non-zero minors = " << nonZeroCounter;
324      ++prpr << "(time = " << ((totalTime * 1000) / CLOCKS_PER_SEC) << " msec)";
325      +prpr < "The cache looks like this:";
326      +prpr < cch.toString();
327    }
328  }
329
330  return 0;
331}
332
333ideal testAllPolyMinorsAsIdeal(matrix mat, int minorSize, int strategy, int cacheEntries, int cacheWeight)
334{
335    // counters + auxiliary stuff
336  int totalMultiplications = 0;
337  int totalAdditions = 0;
338  int totalMultiplicationsAccumulated = 0;
339  int totalAdditionsAccumulated = 0;
340  char h[30];
341
342  int rowCount = mat->nrows;
343  int columnCount = mat->ncols;
344
345  poly* myPolyMatrix = (poly*)(mat->m);
346  PolyMinorProcessor mp;
347  mp.defineMatrix(rowCount, columnCount, myPolyMatrix);
348
349  /* The next lines are for defining the sub-matrix of myPolyMatrix
350     from which we want to compute all k x k - minors.
351     In the given setting, we want the entire matrix to form the sub-matrix. */
352  int minorRows = rowCount;
353  int minorColumns = columnCount;
354  int myRowIndices[minorRows];  for (int i = 0; i < minorRows; i++) myRowIndices[i] = i;
355  int myColumnIndices[minorColumns]; for (int i = 0; i < minorColumns; i++) myColumnIndices[i] = i;
356
357  // setting sub-matrix and size of minors of interest within that sub-matrix:
358  mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
359  mp.setMinorSize(minorSize);
360
361  // containers for all upcoming results
362  PolyMinorValue theMinor;
363  poly po;
364  ideal iii = idInit(1, 0);
365
366  if (strategy == 0)
367  {
368    PrintLn(); PrintS("new code uses no cache");
369    // iteration over all minors of size "minorSize x minorSize"
370    while (mp.hasNextMinor()) {
371      // retrieving the minor:
372      theMinor = mp.getNextMinor();
373      po = theMinor.getResult();
374      totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
375      totalMultiplications += theMinor.getMultiplications();
376      totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
377      totalAdditions += theMinor.getAdditions();
378      idInsertPoly(iii, po); // will include po only if it is not the zero polynomial
379    }
380  }
381  else
382  {
383    PrintLn(); PrintS("new code uses cache with caching strategy ");
384    sprintf(h, "%d", strategy); PrintS(h);
385    MinorValue::SetRankingStrategy(strategy);
386    Cache<MinorKey, PolyMinorValue> cch(cacheEntries, cacheWeight);
387    // iteration over all minors of size "minorSize x minorSize"
388    while (mp.hasNextMinor()) {
389      // retrieving the minor:
390      theMinor = mp.getNextMinor(cch);
391      po = theMinor.getResult();
392      totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
393      totalMultiplications += theMinor.getMultiplications();
394      totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
395      totalAdditions += theMinor.getAdditions();
396      idInsertPoly(iii, po); // will include po only if it is not the zero polynomial
397    }
398  }
399  idSkipZeroes(iii);  // remove zero generators (resulting from block-wise allocation of memory)
400
401  PrintLn(); PrintS("numbers of performed operations");
402  PrintLn(); PrintS("   polynomial-to-polynomial multiplications: ");
403  sprintf(h, "%d", totalMultiplications); PrintS(h);
404  PrintLn(); PrintS("   polynomial-to-polynomial additions: ");
405  sprintf(h, "%d", totalAdditions); PrintS(h);
406  PrintLn(); PrintS("   (polynomial-to-polynomial multiplications without cache would be: ");
407  sprintf(h, "%d", totalMultiplicationsAccumulated); PrintS(h); PrintS(")");
408  PrintLn(); PrintS("   (polynomial-to-polynomial additions without cache would be: ");
409  sprintf(h, "%d", totalAdditionsAccumulated); PrintS(h); PrintS(")");
410  PrintLn(); PrintLn();
411
412  return iii;
413}
414
415ideal testAllIntMinorsAsIdeal(matrix mat, int minorSize, int strategy, int cacheEntries, int cacheWeight, int characteristic)
416{
417    // counters + auxiliary stuff
418  int totalMultiplications = 0;
419  int totalAdditions = 0;
420  int totalMultiplicationsAccumulated = 0;
421  int totalAdditionsAccumulated = 0;
422  char h[30];
423
424  int rowCount = mat->nrows;
425  int columnCount = mat->ncols;
426
427  poly* myPolyMatrix = (poly*)(mat->m);
428  int theSize = rowCount * columnCount;
429  int myIntMatrix[theSize]; int vv;
430  for (int i = 0; i < theSize; i++)
431  {
432    vv = 0;
433    if (myPolyMatrix[i] != NULL)
434    {
435      vv = n_Int(pGetCoeff(myPolyMatrix[i]), currRing);
436      if (characteristic != 0) vv = vv % characteristic;
437    }
438    myIntMatrix[i] = vv;
439  }
440
441  IntMinorProcessor mp;
442  mp.defineMatrix(rowCount, columnCount, myIntMatrix);
443
444  /* The next lines are for defining the sub-matrix of myIntMatrix
445     from which we want to compute all k x k - minors.
446     In the given setting, we want the entire matrix to form the sub-matrix. */
447  int minorRows = rowCount;
448  int minorColumns = columnCount;
449  int myRowIndices[minorRows];  for (int i = 0; i < minorRows; i++) myRowIndices[i] = i;
450  int myColumnIndices[minorColumns]; for (int i = 0; i < minorColumns; i++) myColumnIndices[i] = i;
451
452  // setting sub-matrix and size of minors of interest within that sub-matrix:
453  mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
454  mp.setMinorSize(minorSize);
455
456  // containers for all upcoming results
457  IntMinorValue theMinor;
458  int theValue;
459  ideal iii = idInit(1, 0);
460
461  if (strategy == 0)
462  {
463    PrintLn(); PrintS("new code uses no cache");
464    // iteration over all minors of size "minorSize x minorSize"
465    while (mp.hasNextMinor()) {
466      // retrieving the minor:
467      theMinor = mp.getNextMinor(characteristic);
468      theValue = theMinor.getResult();
469      totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
470      totalMultiplications += theMinor.getMultiplications();
471      totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
472      totalAdditions += theMinor.getAdditions();
473      idInsertPoly(iii, pISet(theValue)); // will include theValue only if it is not zero
474    }
475  }
476  else
477  {
478    PrintLn(); PrintS("new code uses cache with caching strategy ");
479    sprintf(h, "%d", strategy); PrintS(h);
480    MinorValue::SetRankingStrategy(strategy);
481    Cache<MinorKey, IntMinorValue> cch(cacheEntries, cacheWeight);
482    // iteration over all minors of size "minorSize x minorSize"
483    while (mp.hasNextMinor()) {
484      // retrieving the minor:
485      theMinor = mp.getNextMinor(cch, characteristic);
486      theValue = theMinor.getResult();
487      totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
488      totalMultiplications += theMinor.getMultiplications();
489      totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
490      totalAdditions += theMinor.getAdditions();
491      idInsertPoly(iii, pISet(theValue)); // will include theValue only if it is not zero
492    }
493  }
494  idSkipZeroes(iii);  // remove zero generators (resulting from block-wise allocation of memory)
495
496  PrintLn(); PrintS("numbers of performed operations");
497  PrintLn(); PrintS("   multiplications: ");
498  sprintf(h, "%d", totalMultiplications); PrintS(h);
499  PrintLn(); PrintS("   additions: ");
500  sprintf(h, "%d", totalAdditions); PrintS(h);
501  PrintLn(); PrintS("   (multiplications without cache would be: ");
502  sprintf(h, "%d", totalMultiplicationsAccumulated); PrintS(h); PrintS(")");
503  PrintLn(); PrintS("   (additions without cache would be: ");
504  sprintf(h, "%d", totalAdditionsAccumulated); PrintS(h); PrintS(")");
505  PrintLn(); PrintLn();
506
507  return iii;
508}
509
510/**
511* A method for testing the computation of one minor; without and with cache, respectively.<br>
512* All results should be equal no matter whether we do or do not use a cache. Neither should
513* the cache strategy influence the mathematical value of the minor.
514*/
515void testOneMinor(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
516                  int minorSize, int randomSeed, int cacheEntries, int cacheWeight) {
517    int start, end;
518
519    prpr < testHeader;
520    +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; /* underlines the header string */
521    +prpr < "Testing the computation of one minor without and with cache, respectively.";
522    +prpr < "For computing with cache, 5 different caching strategies will be deployed.";
523
524    int* myMatrix = new int[rowCount * columnCount];
525    fillRandomMatrix(rowCount, columnCount, randomSeed, zeroPercentage, entryBound, myMatrix);
526
527    IntMinorProcessor mp;
528    mp.defineMatrix(rowCount, columnCount, myMatrix);
529
530    int myRowIndices[minorSize];  for (int i = 0; i < minorSize; i++) myRowIndices[i] = i;
531    int myColumnIndices[minorSize]; for (int i = 0; i < minorSize; i++) myColumnIndices[i] = columnCount - minorSize + i;
532
533    // We would like to printout mp. For that, we need to provide complete information of
534    // what minors we intend to compute later on.
535    mp.defineSubMatrix(minorSize, myRowIndices, minorSize, myColumnIndices);
536    mp.setMinorSize(minorSize);
537
538    +prpr; +prpr < mp.toString(); +prpr; +prpr;
539
540    // compute the minor without cache:
541    prpr << "Results - " << testHeader << " - no cache";
542    start = clock();
543    IntMinorValue mv = mp.getMinor(minorSize, myRowIndices, myColumnIndices, 0);
544    end = clock();
545    ++prpr << "value of minor = " << mv.toString();
546    ++prpr << "(time = " << (end - start) << " msec)";
547
548    // define the cache:
549    Cache<MinorKey, IntMinorValue> cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight);
550
551    // compute minor using the cache, for all implemented caching strategies:
552    for (int strategy = 1; strategy <= 5; strategy++) {
553        // clear cache:
554        cch.clear();
555        mp.defineSubMatrix(minorSize, myRowIndices, minorSize, myColumnIndices);
556
557        // compute the minor using the cache and current strategy
558        IntMinorValue::SetRankingStrategy(strategy);
559        start = clock();
560        mv = mp.getMinor(minorSize, myRowIndices, myColumnIndices, cch, 0);
561        end = clock();
562
563        ++prpr; ++prpr << "Results - " << testHeader;
564        prpr << " - using cache - deploying caching strategy #" << strategy;
565        ++prpr << "value of minor = " << mv.toString();
566        ++prpr << "(time = " << (end - start) << " msec)";
567        +prpr < "The cache looks like this:";
568        +prpr < cch.toString();
569    }
570    delete [] myMatrix;
571}
572
573/**
574* A method for testing the computation of all minors of a given size within a pre-defined
575* sub-matrix of an underlying matrix.<br>
576* Again, we do this first without cache, and later using a cache, respectively.<br>
577* All results should be equal no matter whether we do or do not use a cache. Neither should the cache strategy
578* influence the mathematical value of the minor.
579*/
580void testAllMinors(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
581                   int minorRows, int minorColumns, int minorSize, int randomSeed, int cacheEntries, int cacheWeight) {
582    clock_t totalTimeStart, totalTime, printTimeStart, printTime;
583
584    prpr < testHeader;
585    +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; // underlines the header string
586    +prpr < "Testing the successive computation of all minors of a given size without and with cache, respectively.";
587    +prpr < "In the case of computing with cache, 5 different caching strategies will be deployed.";
588
589    int* myMatrix = new int[rowCount * columnCount];
590    fillRandomMatrix(rowCount, columnCount, randomSeed, zeroPercentage, entryBound, myMatrix);
591
592    IntMinorProcessor mp;
593    mp.defineMatrix(rowCount, columnCount, myMatrix);
594
595    int myRowIndices[minorRows];  for (int i = 0; i < minorRows; i++) myRowIndices[i] = i;
596    int myColumnIndices[minorColumns]; for (int i = 0; i < minorColumns; i++) myColumnIndices[i] = columnCount - minorColumns + i;
597
598    // setting sub-matrix and size of minors of interest within that sub-matrix:
599    mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
600    mp.setMinorSize(minorSize);
601
602    +prpr; +prpr < mp.toString();
603
604    // define the cache:
605    Cache<MinorKey, IntMinorValue> cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight);
606
607    // container for all upcoming results
608    IntMinorValue theMinor;
609
610    // counters...
611    int k = 1;
612    int totalMultiplications = 0;
613    int totalAdditions = 0;
614    int totalMultiplicationsAccumulated = 0;
615    int totalAdditionsAccumulated = 0;
616
617    // target for retrieving and writing momentary row and column indices:
618    int myIndexArray[32];
619
620    +prpr; +prpr < "Results - " < testHeader < " - no cache";
621    +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
622
623    printTime = 0; totalTimeStart = clock();
624    // iteration over all minors of size "minorSize x minorSize"
625    while (mp.hasNextMinor()) {
626        // retrieving the minor:
627        theMinor = mp.getNextMinor(0);
628        printTimeStart = clock();
629
630        // updating counters:
631        totalMultiplications += theMinor.getMultiplications();
632        totalAdditions += theMinor.getAdditions();
633        totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
634        totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
635
636        // writing current row indices:
637        mp.getCurrentRowIndices(myIndexArray);
638        +prpr < k++ < ". minor (rows: ";
639        for (int i = 0; i < minorSize; i++) {
640            if (i != 0) prpr < ", ";
641            prpr < myIndexArray[i];
642        };
643
644        // writing current column indices:
645        mp.getCurrentColumnIndices(myIndexArray);
646        prpr < "; columns: ";
647        for (int i = 0; i < minorSize; i++) {
648            if (i != 0) prpr < ", ";
649            prpr < myIndexArray[i];
650        };
651        prpr < ") = ";
652
653        // write the actual value of the minor:
654        prpr < theMinor.toString();
655        printTime += clock() - printTimeStart;
656    };
657    totalTime = clock() - totalTimeStart - printTime;
658    // writing summarized information
659    ++prpr; ++prpr << "Operation counters - " << testHeader << " - no cache";
660    ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
661    prpr << totalAdditions << " additions";
662    ++prpr << "(time = " << ((totalTime * 1000) / CLOCKS_PER_SEC) << " msec)";
663
664    for (int strategy = 1; strategy <= 5; strategy++) {
665        // setting sub-matrix, size of minors of interest within that sub-matrix, and strategy:
666        mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
667        mp.setMinorSize(minorSize);
668        IntMinorValue::SetRankingStrategy(strategy);
669
670        // counters...
671        k = 1;
672        totalMultiplications = 0;
673        totalAdditions = 0;
674        totalMultiplicationsAccumulated = 0;
675        totalAdditionsAccumulated = 0;
676
677        // cleaning up and redefinition of the cache:
678        cch.clear();
679        cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight);
680
681        +prpr; +prpr < "Results - " < testHeader < " - using cache - deploying caching strategy #" < strategy;
682        +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
683
684        printTime = 0; totalTimeStart = clock();
685        // iteration over all minors of size "minorSize x minorSize"
686        while (mp.hasNextMinor()) {
687            // retrieving the minor:
688            theMinor = mp.getNextMinor(cch, 0);
689            printTimeStart = clock();
690
691            // updating counters:
692            totalMultiplications += theMinor.getMultiplications();
693            totalAdditions += theMinor.getAdditions();
694            totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
695            totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
696
697            // writing current row indices:
698            mp.getCurrentRowIndices(myIndexArray);
699            +prpr < k++ < ". minor (rows: ";
700            for (int i = 0; i < minorSize; i++) {
701                if (i != 0) prpr < ", ";
702                prpr < myIndexArray[i];
703            };
704
705            // writing current column indices:
706            mp.getCurrentColumnIndices(myIndexArray);
707            prpr < "; columns: ";
708            for (int i = 0; i < minorSize; i++) {
709                if (i != 0) prpr < ", ";
710                prpr < myIndexArray[i];
711            };
712            prpr < ") = ";
713
714            // writeMinor the actual value of the minor:
715            prpr < theMinor.toString();
716            printTime += clock() - printTimeStart;
717        };
718        totalTime = clock() - totalTimeStart - printTime;
719        // writing summarized information
720        ++prpr; ++prpr << "Operation counters - " << testHeader << " - using cache - deploying caching strategy #" << strategy;
721        ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
722        prpr << totalAdditions << " additions";
723        ++prpr << "(computation without reuse would need " << totalMultiplicationsAccumulated << " and ";
724        prpr << totalAdditionsAccumulated << " additions)";
725        ++prpr << "(time = " << ((totalTime * 1000) / CLOCKS_PER_SEC) << " msec)";
726        +prpr < "The cache looks like this:";
727        +prpr < cch.toString();
728    }
729    delete [] myMatrix;
730}
731
732/**
733* A method for testing the computation of all minors of a given size within a pre-defined
734* sub-matrix of an underlying large matrix.<br>
735* Again, we do this first without cache, and later using a cache, respectively.<br>
736* All results should be equal no matter whether we do or do not use a cache. Neither should the cache strategy
737* influence the mathematical value of the minor.
738* zeroP: this is the probability for zero entries in the matrix;
739*        all other matrix entries will range from 1 to entryBound and be equally distributed
740*/
741void testAllMinorsUntil(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
742                        int minorSize, int randomSeed, int cacheEntries, int cacheWeight, int targetMinor,
743                        bool checkForEquality, int maxLoops) {
744    clock_t totalTimeStart, totalTime, printTimeStart, printTime;
745
746    prpr < testHeader;
747    +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; // underlines the header string
748    +prpr < "Testing the successive computation of minors of a given size without and with ";
749    +prpr < "cache, respectively, until a minor with a certain value is found.";
750    +prpr < "In the case of computing with cache, 5 different caching strategies will be deployed.";
751
752    +prpr; +prpr < "In this test, the matrix is " < rowCount < " x " < columnCount < ".";
753    +prpr < "The minor we are looking for is " < minorSize < " x " < minorSize < ", and ";
754    prpr < "is supposed to have a value of ";
755    if (!checkForEquality) prpr < "<> ";
756    prpr < targetMinor < ".";
757    +prpr < "As an upper bound for the number of loops, at most " < maxLoops < " minors will be computed.";
758
759    int* myMatrix = new int[rowCount * columnCount];
760    fillRandomMatrix(rowCount, columnCount, randomSeed, zeroPercentage, entryBound, myMatrix);
761
762    IntMinorProcessor mp;
763    mp.defineMatrix(rowCount, columnCount, myMatrix);
764
765    int myRowIndices[rowCount]; for (int i = 0; i < rowCount; i++) myRowIndices[i] = i; // choosing all rows
766    int myColumnIndices[columnCount]; for (int i = 0; i < columnCount; i++) myColumnIndices[i] = i; // choosing all columns
767
768    // define the cache:
769    Cache<MinorKey, IntMinorValue> cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight);
770
771    // container for all upcoming results
772    IntMinorValue theMinor;
773
774    // counters...
775    int k = 1;
776    int totalMultiplications = 0;
777    int totalAdditions = 0;
778    int totalMultiplicationsAccumulated = 0;
779    int totalAdditionsAccumulated = 0;
780
781    // setting sub-matrix and size of minors of interest within that sub-matrix:
782    mp.defineSubMatrix(rowCount, myRowIndices, columnCount, myColumnIndices);
783    mp.setMinorSize(minorSize);
784
785    +prpr; +prpr < mp.toString();
786
787    +prpr < "Results - " < testHeader < " - no cache";
788    +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
789    prpr < " until first minor with specified value is found:";
790
791    bool minorFound = false;
792    int loops = 0;
793    printTime = 0; totalTimeStart = clock();
794    // iteration over all minors of size "minorSize x minorSize"
795    while (mp.hasNextMinor() && (!minorFound) && (loops < maxLoops)) {
796
797        // retrieving the minor:
798        theMinor = mp.getNextMinor(0);
799        printTimeStart = clock();
800        minorFound = (checkForEquality ? (theMinor.getResult() == targetMinor) : (theMinor.getResult() != targetMinor));
801
802        // updating counters:
803        totalMultiplications += theMinor.getMultiplications();
804        totalAdditions += theMinor.getAdditions();
805        totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
806        totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
807
808        // writing current minor
809        +prpr < k++ < ". minor = " < theMinor.getResult();
810
811        loops++;
812        printTime += clock() - printTimeStart;
813    };
814    totalTime = clock() - totalTimeStart - printTime;
815    // writing summarized information
816    ++prpr; ++prpr << "Operation counters - " << testHeader << " - no cache";
817    ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
818    prpr << totalAdditions << " additions";
819    ++prpr << "(time = " << ((totalTime * 1000) / CLOCKS_PER_SEC) << " msec)";
820
821    for (int strategy = 1; strategy <= 5; strategy++) {
822        // setting sub-matrix, size of minors of interest within that sub-matrix, and strategy:
823        mp.defineSubMatrix(rowCount, myRowIndices, columnCount, myColumnIndices);
824        mp.setMinorSize(minorSize);
825        IntMinorValue::SetRankingStrategy(strategy);
826
827        // counters...
828        k = 1;
829        totalMultiplications = 0;
830        totalAdditions = 0;
831        totalMultiplicationsAccumulated = 0;
832        totalAdditionsAccumulated = 0;
833
834        // cleaning up and redefinition of the cache:
835        cch.clear();
836        cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight);
837
838        +prpr; +prpr < testHeader < " - using cache - deploying caching strategy #" < strategy;
839        +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
840        prpr < " until first minor with specified value is found:";
841
842        int loops = 0;
843        bool minorFound = false;
844        printTime = 0; totalTimeStart = clock();
845        // iteration over all minors of size "minorSize x minorSize"
846        while (mp.hasNextMinor() && (!minorFound) && (loops < maxLoops)) {
847
848            // retrieving the minor:
849            theMinor = mp.getNextMinor(cch, 0);
850            printTimeStart = clock();
851            minorFound = (checkForEquality ? (theMinor.getResult() == targetMinor) : (theMinor.getResult() != targetMinor));
852
853            // updating counters:
854            totalMultiplications += theMinor.getMultiplications();
855            totalAdditions += theMinor.getAdditions();
856            totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
857            totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
858
859            // writing current minor
860            +prpr < k++ < ". minor = " < theMinor.getResult();
861
862            loops++;
863            printTime += clock() - printTimeStart;
864        };
865        totalTime = clock() - totalTimeStart - printTime;
866        // writing summarized information
867        ++prpr; ++prpr << "Operation counters - " << testHeader << " - using cache - deploying caching strategy #" << strategy;
868        ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
869        prpr << totalAdditions << " additions";
870        ++prpr << "(computation without reuse would need " << totalMultiplicationsAccumulated << " and ";
871        prpr << totalAdditionsAccumulated << " additions)";
872        ++prpr << "(time = " << ((totalTime * 1000) / CLOCKS_PER_SEC) << " msec)";
873        +prpr < "The cache has " < cch.getNumberOfEntries() < " (of max. " < cacheEntries < ") entries and a weight of ";
874        prpr < cch.getWeight() < " (of max. " < cacheWeight < ").";
875    }
876    delete [] myMatrix;
877}
878
879#endif // HAVE_MINOR
Note: See TracBrowser for help on using the repository browser.