source: git/Singular/TestMinors.cc @ 1e6734

spielwiese
Last change on this file since 1e6734 was 1e6734, checked in by Frank Seelisch <seelisch@…>, 14 years ago
pCopy bug removed git-svn-id: file:///usr/local/Singular/svn/trunk@12253 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 = NULL;
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, pCopy(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, pCopy(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  PrintLn(); PrintS("numbers of performed operations");
401  PrintLn(); PrintS("   polynomial-to-polynomial multiplications: ");
402  sprintf(h, "%d", totalMultiplications); PrintS(h);
403  PrintLn(); PrintS("   polynomial-to-polynomial additions: ");
404  sprintf(h, "%d", totalAdditions); PrintS(h);
405  PrintLn(); PrintS("   (polynomial-to-polynomial multiplications without cache would be: ");
406  sprintf(h, "%d", totalMultiplicationsAccumulated); PrintS(h); PrintS(")");
407  PrintLn(); PrintS("   (polynomial-to-polynomial additions without cache would be: ");
408  sprintf(h, "%d", totalAdditionsAccumulated); PrintS(h); PrintS(")");
409  PrintLn(); PrintLn();
410  return iii;
411}
412
413ideal testAllIntMinorsAsIdeal(matrix mat, int minorSize, int strategy, int cacheEntries, int cacheWeight, int characteristic)
414{
415    // counters + auxiliary stuff
416  int totalMultiplications = 0;
417  int totalAdditions = 0;
418  int totalMultiplicationsAccumulated = 0;
419  int totalAdditionsAccumulated = 0;
420  char h[30];
421
422  int rowCount = mat->nrows;
423  int columnCount = mat->ncols;
424
425  poly* myPolyMatrix = (poly*)(mat->m);
426  int theSize = rowCount * columnCount;
427  int myIntMatrix[theSize]; int vv;
428  for (int i = 0; i < theSize; i++)
429  {
430    vv = 0;
431    if (myPolyMatrix[i] != NULL)
432    {
433      vv = n_Int(pGetCoeff(myPolyMatrix[i]), currRing);
434      if (characteristic != 0) vv = vv % characteristic;
435    }
436    myIntMatrix[i] = vv;
437  }
438
439  IntMinorProcessor mp;
440  mp.defineMatrix(rowCount, columnCount, myIntMatrix);
441
442  /* The next lines are for defining the sub-matrix of myIntMatrix
443     from which we want to compute all k x k - minors.
444     In the given setting, we want the entire matrix to form the sub-matrix. */
445  int minorRows = rowCount;
446  int minorColumns = columnCount;
447  int myRowIndices[minorRows];  for (int i = 0; i < minorRows; i++) myRowIndices[i] = i;
448  int myColumnIndices[minorColumns]; for (int i = 0; i < minorColumns; i++) myColumnIndices[i] = i;
449
450  // setting sub-matrix and size of minors of interest within that sub-matrix:
451  mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
452  mp.setMinorSize(minorSize);
453
454  // containers for all upcoming results
455  IntMinorValue theMinor;
456  int theValue;
457  ideal iii = idInit(1, 0);
458
459  if (strategy == 0)
460  {
461    PrintLn(); PrintS("new code uses no cache");
462    // iteration over all minors of size "minorSize x minorSize"
463    while (mp.hasNextMinor()) {
464      // retrieving the minor:
465      theMinor = mp.getNextMinor(characteristic);
466      theValue = theMinor.getResult();
467      totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
468      totalMultiplications += theMinor.getMultiplications();
469      totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
470      totalAdditions += theMinor.getAdditions();
471      idInsertPoly(iii, pISet(theValue)); // will include theValue only if it is not zero
472    }
473  }
474  else
475  {
476    PrintLn(); PrintS("new code uses cache with caching strategy ");
477    sprintf(h, "%d", strategy); PrintS(h);
478    MinorValue::SetRankingStrategy(strategy);
479    Cache<MinorKey, IntMinorValue> cch(cacheEntries, cacheWeight);
480    // iteration over all minors of size "minorSize x minorSize"
481    while (mp.hasNextMinor()) {
482      // retrieving the minor:
483      theMinor = mp.getNextMinor(cch, characteristic);
484      theValue = theMinor.getResult();
485      totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
486      totalMultiplications += theMinor.getMultiplications();
487      totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
488      totalAdditions += theMinor.getAdditions();
489      idInsertPoly(iii, pISet(theValue)); // will include theValue only if it is not zero
490    }
491  }
492  idSkipZeroes(iii);  // remove zero generators (resulting from block-wise allocation of memory)
493
494  PrintLn(); PrintS("numbers of performed operations");
495  PrintLn(); PrintS("   multiplications: ");
496  sprintf(h, "%d", totalMultiplications); PrintS(h);
497  PrintLn(); PrintS("   additions: ");
498  sprintf(h, "%d", totalAdditions); PrintS(h);
499  PrintLn(); PrintS("   (multiplications without cache would be: ");
500  sprintf(h, "%d", totalMultiplicationsAccumulated); PrintS(h); PrintS(")");
501  PrintLn(); PrintS("   (additions without cache would be: ");
502  sprintf(h, "%d", totalAdditionsAccumulated); PrintS(h); PrintS(")");
503  PrintLn(); PrintLn();
504
505  return iii;
506}
507
508/**
509* A method for testing the computation of one minor; without and with cache, respectively.<br>
510* All results should be equal no matter whether we do or do not use a cache. Neither should
511* the cache strategy influence the mathematical value of the minor.
512*/
513void testOneMinor(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
514                  int minorSize, int randomSeed, int cacheEntries, int cacheWeight) {
515    int start, end;
516
517    prpr < testHeader;
518    +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; /* underlines the header string */
519    +prpr < "Testing the computation of one minor without and with cache, respectively.";
520    +prpr < "For computing with cache, 5 different caching strategies will be deployed.";
521
522    int* myMatrix = new int[rowCount * columnCount];
523    fillRandomMatrix(rowCount, columnCount, randomSeed, zeroPercentage, entryBound, myMatrix);
524
525    IntMinorProcessor mp;
526    mp.defineMatrix(rowCount, columnCount, myMatrix);
527
528    int myRowIndices[minorSize];  for (int i = 0; i < minorSize; i++) myRowIndices[i] = i;
529    int myColumnIndices[minorSize]; for (int i = 0; i < minorSize; i++) myColumnIndices[i] = columnCount - minorSize + i;
530
531    // We would like to printout mp. For that, we need to provide complete information of
532    // what minors we intend to compute later on.
533    mp.defineSubMatrix(minorSize, myRowIndices, minorSize, myColumnIndices);
534    mp.setMinorSize(minorSize);
535
536    +prpr; +prpr < mp.toString(); +prpr; +prpr;
537
538    // compute the minor without cache:
539    prpr << "Results - " << testHeader << " - no cache";
540    start = clock();
541    IntMinorValue mv = mp.getMinor(minorSize, myRowIndices, myColumnIndices, 0);
542    end = clock();
543    ++prpr << "value of minor = " << mv.toString();
544    ++prpr << "(time = " << (end - start) << " msec)";
545
546    // define the cache:
547    Cache<MinorKey, IntMinorValue> cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight);
548
549    // compute minor using the cache, for all implemented caching strategies:
550    for (int strategy = 1; strategy <= 5; strategy++) {
551        // clear cache:
552        cch.clear();
553        mp.defineSubMatrix(minorSize, myRowIndices, minorSize, myColumnIndices);
554
555        // compute the minor using the cache and current strategy
556        IntMinorValue::SetRankingStrategy(strategy);
557        start = clock();
558        mv = mp.getMinor(minorSize, myRowIndices, myColumnIndices, cch, 0);
559        end = clock();
560
561        ++prpr; ++prpr << "Results - " << testHeader;
562        prpr << " - using cache - deploying caching strategy #" << strategy;
563        ++prpr << "value of minor = " << mv.toString();
564        ++prpr << "(time = " << (end - start) << " msec)";
565        +prpr < "The cache looks like this:";
566        +prpr < cch.toString();
567    }
568    delete [] myMatrix;
569}
570
571/**
572* A method for testing the computation of all minors of a given size within a pre-defined
573* sub-matrix of an underlying matrix.<br>
574* Again, we do this first without cache, and later using a cache, respectively.<br>
575* All results should be equal no matter whether we do or do not use a cache. Neither should the cache strategy
576* influence the mathematical value of the minor.
577*/
578void testAllMinors(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
579                   int minorRows, int minorColumns, int minorSize, int randomSeed, int cacheEntries, int cacheWeight) {
580    clock_t totalTimeStart, totalTime, printTimeStart, printTime;
581
582    prpr < testHeader;
583    +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; // underlines the header string
584    +prpr < "Testing the successive computation of all minors of a given size without and with cache, respectively.";
585    +prpr < "In the case of computing with cache, 5 different caching strategies will be deployed.";
586
587    int* myMatrix = new int[rowCount * columnCount];
588    fillRandomMatrix(rowCount, columnCount, randomSeed, zeroPercentage, entryBound, myMatrix);
589
590    IntMinorProcessor mp;
591    mp.defineMatrix(rowCount, columnCount, myMatrix);
592
593    int myRowIndices[minorRows];  for (int i = 0; i < minorRows; i++) myRowIndices[i] = i;
594    int myColumnIndices[minorColumns]; for (int i = 0; i < minorColumns; i++) myColumnIndices[i] = columnCount - minorColumns + i;
595
596    // setting sub-matrix and size of minors of interest within that sub-matrix:
597    mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
598    mp.setMinorSize(minorSize);
599
600    +prpr; +prpr < mp.toString();
601
602    // define the cache:
603    Cache<MinorKey, IntMinorValue> cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight);
604
605    // container for all upcoming results
606    IntMinorValue theMinor;
607
608    // counters...
609    int k = 1;
610    int totalMultiplications = 0;
611    int totalAdditions = 0;
612    int totalMultiplicationsAccumulated = 0;
613    int totalAdditionsAccumulated = 0;
614
615    // target for retrieving and writing momentary row and column indices:
616    int myIndexArray[32];
617
618    +prpr; +prpr < "Results - " < testHeader < " - no cache";
619    +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
620
621    printTime = 0; totalTimeStart = clock();
622    // iteration over all minors of size "minorSize x minorSize"
623    while (mp.hasNextMinor()) {
624        // retrieving the minor:
625        theMinor = mp.getNextMinor(0);
626        printTimeStart = clock();
627
628        // updating counters:
629        totalMultiplications += theMinor.getMultiplications();
630        totalAdditions += theMinor.getAdditions();
631        totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
632        totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
633
634        // writing current row indices:
635        mp.getCurrentRowIndices(myIndexArray);
636        +prpr < k++ < ". minor (rows: ";
637        for (int i = 0; i < minorSize; i++) {
638            if (i != 0) prpr < ", ";
639            prpr < myIndexArray[i];
640        };
641
642        // writing current column indices:
643        mp.getCurrentColumnIndices(myIndexArray);
644        prpr < "; columns: ";
645        for (int i = 0; i < minorSize; i++) {
646            if (i != 0) prpr < ", ";
647            prpr < myIndexArray[i];
648        };
649        prpr < ") = ";
650
651        // write the actual value of the minor:
652        prpr < theMinor.toString();
653        printTime += clock() - printTimeStart;
654    };
655    totalTime = clock() - totalTimeStart - printTime;
656    // writing summarized information
657    ++prpr; ++prpr << "Operation counters - " << testHeader << " - no cache";
658    ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
659    prpr << totalAdditions << " additions";
660    ++prpr << "(time = " << ((totalTime * 1000) / CLOCKS_PER_SEC) << " msec)";
661
662    for (int strategy = 1; strategy <= 5; strategy++) {
663        // setting sub-matrix, size of minors of interest within that sub-matrix, and strategy:
664        mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices);
665        mp.setMinorSize(minorSize);
666        IntMinorValue::SetRankingStrategy(strategy);
667
668        // counters...
669        k = 1;
670        totalMultiplications = 0;
671        totalAdditions = 0;
672        totalMultiplicationsAccumulated = 0;
673        totalAdditionsAccumulated = 0;
674
675        // cleaning up and redefinition of the cache:
676        cch.clear();
677        cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight);
678
679        +prpr; +prpr < "Results - " < testHeader < " - using cache - deploying caching strategy #" < strategy;
680        +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
681
682        printTime = 0; totalTimeStart = clock();
683        // iteration over all minors of size "minorSize x minorSize"
684        while (mp.hasNextMinor()) {
685            // retrieving the minor:
686            theMinor = mp.getNextMinor(cch, 0);
687            printTimeStart = clock();
688
689            // updating counters:
690            totalMultiplications += theMinor.getMultiplications();
691            totalAdditions += theMinor.getAdditions();
692            totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
693            totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
694
695            // writing current row indices:
696            mp.getCurrentRowIndices(myIndexArray);
697            +prpr < k++ < ". minor (rows: ";
698            for (int i = 0; i < minorSize; i++) {
699                if (i != 0) prpr < ", ";
700                prpr < myIndexArray[i];
701            };
702
703            // writing current column indices:
704            mp.getCurrentColumnIndices(myIndexArray);
705            prpr < "; columns: ";
706            for (int i = 0; i < minorSize; i++) {
707                if (i != 0) prpr < ", ";
708                prpr < myIndexArray[i];
709            };
710            prpr < ") = ";
711
712            // writeMinor the actual value of the minor:
713            prpr < theMinor.toString();
714            printTime += clock() - printTimeStart;
715        };
716        totalTime = clock() - totalTimeStart - printTime;
717        // writing summarized information
718        ++prpr; ++prpr << "Operation counters - " << testHeader << " - using cache - deploying caching strategy #" << strategy;
719        ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
720        prpr << totalAdditions << " additions";
721        ++prpr << "(computation without reuse would need " << totalMultiplicationsAccumulated << " and ";
722        prpr << totalAdditionsAccumulated << " additions)";
723        ++prpr << "(time = " << ((totalTime * 1000) / CLOCKS_PER_SEC) << " msec)";
724        +prpr < "The cache looks like this:";
725        +prpr < cch.toString();
726    }
727    delete [] myMatrix;
728}
729
730/**
731* A method for testing the computation of all minors of a given size within a pre-defined
732* sub-matrix of an underlying large matrix.<br>
733* Again, we do this first without cache, and later using a cache, respectively.<br>
734* All results should be equal no matter whether we do or do not use a cache. Neither should the cache strategy
735* influence the mathematical value of the minor.
736* zeroP: this is the probability for zero entries in the matrix;
737*        all other matrix entries will range from 1 to entryBound and be equally distributed
738*/
739void testAllMinorsUntil(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage,
740                        int minorSize, int randomSeed, int cacheEntries, int cacheWeight, int targetMinor,
741                        bool checkForEquality, int maxLoops) {
742    clock_t totalTimeStart, totalTime, printTimeStart, printTime;
743
744    prpr < testHeader;
745    +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; // underlines the header string
746    +prpr < "Testing the successive computation of minors of a given size without and with ";
747    +prpr < "cache, respectively, until a minor with a certain value is found.";
748    +prpr < "In the case of computing with cache, 5 different caching strategies will be deployed.";
749
750    +prpr; +prpr < "In this test, the matrix is " < rowCount < " x " < columnCount < ".";
751    +prpr < "The minor we are looking for is " < minorSize < " x " < minorSize < ", and ";
752    prpr < "is supposed to have a value of ";
753    if (!checkForEquality) prpr < "<> ";
754    prpr < targetMinor < ".";
755    +prpr < "As an upper bound for the number of loops, at most " < maxLoops < " minors will be computed.";
756
757    int* myMatrix = new int[rowCount * columnCount];
758    fillRandomMatrix(rowCount, columnCount, randomSeed, zeroPercentage, entryBound, myMatrix);
759
760    IntMinorProcessor mp;
761    mp.defineMatrix(rowCount, columnCount, myMatrix);
762
763    int myRowIndices[rowCount]; for (int i = 0; i < rowCount; i++) myRowIndices[i] = i; // choosing all rows
764    int myColumnIndices[columnCount]; for (int i = 0; i < columnCount; i++) myColumnIndices[i] = i; // choosing all columns
765
766    // define the cache:
767    Cache<MinorKey, IntMinorValue> cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight);
768
769    // container for all upcoming results
770    IntMinorValue theMinor;
771
772    // counters...
773    int k = 1;
774    int totalMultiplications = 0;
775    int totalAdditions = 0;
776    int totalMultiplicationsAccumulated = 0;
777    int totalAdditionsAccumulated = 0;
778
779    // setting sub-matrix and size of minors of interest within that sub-matrix:
780    mp.defineSubMatrix(rowCount, myRowIndices, columnCount, myColumnIndices);
781    mp.setMinorSize(minorSize);
782
783    +prpr; +prpr < mp.toString();
784
785    +prpr < "Results - " < testHeader < " - no cache";
786    +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
787    prpr < " until first minor with specified value is found:";
788
789    bool minorFound = false;
790    int loops = 0;
791    printTime = 0; totalTimeStart = clock();
792    // iteration over all minors of size "minorSize x minorSize"
793    while (mp.hasNextMinor() && (!minorFound) && (loops < maxLoops)) {
794
795        // retrieving the minor:
796        theMinor = mp.getNextMinor(0);
797        printTimeStart = clock();
798        minorFound = (checkForEquality ? (theMinor.getResult() == targetMinor) : (theMinor.getResult() != targetMinor));
799
800        // updating counters:
801        totalMultiplications += theMinor.getMultiplications();
802        totalAdditions += theMinor.getAdditions();
803        totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
804        totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
805
806        // writing current minor
807        +prpr < k++ < ". minor = " < theMinor.getResult();
808
809        loops++;
810        printTime += clock() - printTimeStart;
811    };
812    totalTime = clock() - totalTimeStart - printTime;
813    // writing summarized information
814    ++prpr; ++prpr << "Operation counters - " << testHeader << " - no cache";
815    ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
816    prpr << totalAdditions << " additions";
817    ++prpr << "(time = " << ((totalTime * 1000) / CLOCKS_PER_SEC) << " msec)";
818
819    for (int strategy = 1; strategy <= 5; strategy++) {
820        // setting sub-matrix, size of minors of interest within that sub-matrix, and strategy:
821        mp.defineSubMatrix(rowCount, myRowIndices, columnCount, myColumnIndices);
822        mp.setMinorSize(minorSize);
823        IntMinorValue::SetRankingStrategy(strategy);
824
825        // counters...
826        k = 1;
827        totalMultiplications = 0;
828        totalAdditions = 0;
829        totalMultiplicationsAccumulated = 0;
830        totalAdditionsAccumulated = 0;
831
832        // cleaning up and redefinition of the cache:
833        cch.clear();
834        cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight);
835
836        +prpr; +prpr < testHeader < " - using cache - deploying caching strategy #" < strategy;
837        +prpr < "computing all minors of size " < minorSize < "x" < minorSize;
838        prpr < " until first minor with specified value is found:";
839
840        int loops = 0;
841        bool minorFound = false;
842        printTime = 0; totalTimeStart = clock();
843        // iteration over all minors of size "minorSize x minorSize"
844        while (mp.hasNextMinor() && (!minorFound) && (loops < maxLoops)) {
845
846            // retrieving the minor:
847            theMinor = mp.getNextMinor(cch, 0);
848            printTimeStart = clock();
849            minorFound = (checkForEquality ? (theMinor.getResult() == targetMinor) : (theMinor.getResult() != targetMinor));
850
851            // updating counters:
852            totalMultiplications += theMinor.getMultiplications();
853            totalAdditions += theMinor.getAdditions();
854            totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications();
855            totalAdditionsAccumulated += theMinor.getAccumulatedAdditions();
856
857            // writing current minor
858            +prpr < k++ < ". minor = " < theMinor.getResult();
859
860            loops++;
861            printTime += clock() - printTimeStart;
862        };
863        totalTime = clock() - totalTimeStart - printTime;
864        // writing summarized information
865        ++prpr; ++prpr << "Operation counters - " << testHeader << " - using cache - deploying caching strategy #" << strategy;
866        ++prpr << "performed in total " << totalMultiplications << " multiplications and ";
867        prpr << totalAdditions << " additions";
868        ++prpr << "(computation without reuse would need " << totalMultiplicationsAccumulated << " and ";
869        prpr << totalAdditionsAccumulated << " additions)";
870        ++prpr << "(time = " << ((totalTime * 1000) / CLOCKS_PER_SEC) << " msec)";
871        +prpr < "The cache has " < cch.getNumberOfEntries() < " (of max. " < cacheEntries < ") entries and a weight of ";
872        prpr < cch.getWeight() < " (of max. " < cacheWeight < ").";
873    }
874    delete [] myMatrix;
875}
876
877#endif // HAVE_MINOR
Note: See TracBrowser for help on using the repository browser.