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 | |
---|
19 | using namespace std; |
---|
20 | |
---|
21 | poly zeroPoly = pISet(0); |
---|
22 | int nonZeroCounter = 0; |
---|
23 | |
---|
24 | void testOneMinor(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage, |
---|
25 | int minorSize, int randomSeed, int cacheEntries, int cacheWeight); |
---|
26 | void 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); |
---|
28 | void 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 | |
---|
32 | void 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.\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 | */ |
---|
79 | void 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 | siSeed = randomSeed; // random seed for ensuring reproducability of experiments |
---|
83 | for (int i = 0; i < theSize; i++) { |
---|
84 | if ((siRand() % 100) < zeroPercentage) |
---|
85 | theMatrix[i] = 0; |
---|
86 | else |
---|
87 | theMatrix[i] = 1 + (siRand() % entryBound); // ranges from 1 to entryBound, including both |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | void 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 | */ |
---|
107 | int testIntMinors (const int dummy) { |
---|
108 | // for output of non-zero minors into file |
---|
109 | PrettyPrinter prpr("minor_output_complete_ints.txt", "minor_output_results_int.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 == 92868; to this end, it needs to compute 1632 minors: |
---|
120 | testAllMinorsUntil(prpr, "Test III", 100, 60, 10, 10, 6, 471, 300, 10000, 92868, true, 10000); |
---|
121 | +prpr; +prpr; |
---|
122 | |
---|
123 | // looks for the first non-zero minor (6x6); to this end, it needs to compute 229 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 == -43065; to this end, it needs to compute 23 minors: |
---|
128 | testAllMinorsUntil(prpr, "Test V", 100, 60, 10, 10, 6, 471, 300, 10000, -43065, true, 10000); |
---|
129 | |
---|
130 | return 0; |
---|
131 | } |
---|
132 | |
---|
133 | void 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 | |
---|
142 | int testAllPolyMinors(matrix mat, int minorSize, int strategies, int cacheEntries, int 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 | |
---|
329 | ideal testAllPolyMinorsAsIdeal(matrix mat, int minorSize, int strategy, int cacheEntries, int cacheWeight) |
---|
330 | { |
---|
331 | // counters + auxiliary stuff |
---|
332 | int totalMultiplications = 0; |
---|
333 | int totalAdditions = 0; |
---|
334 | int totalMultiplicationsAccumulated = 0; |
---|
335 | int 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 | idSkipZeroes(iii); // remove zero generators (resulting from block-wise allocation of memory) |
---|
396 | |
---|
397 | PrintLn(); PrintS("numbers of performed operations"); |
---|
398 | PrintLn(); PrintS(" polynomial-to-polynomial multiplications: "); |
---|
399 | sprintf(h, "%d", totalMultiplications); PrintS(h); |
---|
400 | PrintLn(); PrintS(" polynomial-to-polynomial additions: "); |
---|
401 | sprintf(h, "%d", totalAdditions); PrintS(h); |
---|
402 | PrintLn(); PrintS(" (polynomial-to-polynomial multiplications without cache would be: "); |
---|
403 | sprintf(h, "%d", totalMultiplicationsAccumulated); PrintS(h); PrintS(")"); |
---|
404 | PrintLn(); PrintS(" (polynomial-to-polynomial additions without cache would be: "); |
---|
405 | sprintf(h, "%d", totalAdditionsAccumulated); PrintS(h); PrintS(")"); |
---|
406 | PrintLn(); PrintLn(); |
---|
407 | |
---|
408 | return iii; |
---|
409 | } |
---|
410 | |
---|
411 | /** |
---|
412 | * A method for testing the computation of one minor; without and with cache, respectively.<br> |
---|
413 | * All results should be equal no matter whether we do or do not use a cache. Neither should |
---|
414 | * the cache strategy influence the mathematical value of the minor. |
---|
415 | */ |
---|
416 | void testOneMinor(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage, |
---|
417 | int minorSize, int randomSeed, int cacheEntries, int cacheWeight) { |
---|
418 | int start, end; |
---|
419 | |
---|
420 | prpr < testHeader; |
---|
421 | +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; /* underlines the header string */ |
---|
422 | +prpr < "Testing the computation of one minor without and with cache, respectively."; |
---|
423 | +prpr < "For computing with cache, 5 different caching strategies will be deployed."; |
---|
424 | |
---|
425 | int* myMatrix = new int[rowCount * columnCount]; |
---|
426 | fillRandomMatrix(rowCount, columnCount, randomSeed, zeroPercentage, entryBound, myMatrix); |
---|
427 | |
---|
428 | IntMinorProcessor mp; |
---|
429 | mp.defineMatrix(rowCount, columnCount, myMatrix); |
---|
430 | |
---|
431 | int myRowIndices[minorSize]; for (int i = 0; i < minorSize; i++) myRowIndices[i] = i; |
---|
432 | int myColumnIndices[minorSize]; for (int i = 0; i < minorSize; i++) myColumnIndices[i] = columnCount - minorSize + i; |
---|
433 | |
---|
434 | // We would like to printout mp. For that, we need to provide complete information of |
---|
435 | // what minors we intend to compute later on. |
---|
436 | mp.defineSubMatrix(minorSize, myRowIndices, minorSize, myColumnIndices); |
---|
437 | mp.setMinorSize(minorSize); |
---|
438 | |
---|
439 | +prpr; +prpr < mp.toString(); +prpr; +prpr; |
---|
440 | |
---|
441 | // compute the minor without cache: |
---|
442 | prpr << "Results - " << testHeader << " - no cache"; |
---|
443 | start = clock(); |
---|
444 | IntMinorValue mv = mp.getMinor(minorSize, myRowIndices, myColumnIndices); |
---|
445 | end = clock(); |
---|
446 | ++prpr << "value of minor = " << mv.toString(); |
---|
447 | ++prpr << "(time = " << (end - start) << " msec)"; |
---|
448 | |
---|
449 | // define the cache: |
---|
450 | Cache<MinorKey, IntMinorValue> cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight); |
---|
451 | |
---|
452 | // compute minor using the cache, for all implemented caching strategies: |
---|
453 | for (int strategy = 1; strategy <= 5; strategy++) { |
---|
454 | // clear cache: |
---|
455 | cch.clear(); |
---|
456 | mp.defineSubMatrix(minorSize, myRowIndices, minorSize, myColumnIndices); |
---|
457 | |
---|
458 | // compute the minor using the cache and current strategy |
---|
459 | IntMinorValue::SetRankingStrategy(strategy); |
---|
460 | start = clock(); |
---|
461 | mv = mp.getMinor(minorSize, myRowIndices, myColumnIndices, cch); |
---|
462 | end = clock(); |
---|
463 | |
---|
464 | ++prpr; ++prpr << "Results - " << testHeader; |
---|
465 | prpr << " - using cache - deploying caching strategy #" << strategy; |
---|
466 | ++prpr << "value of minor = " << mv.toString(); |
---|
467 | ++prpr << "(time = " << (end - start) << " msec)"; |
---|
468 | +prpr < "The cache looks like this:"; |
---|
469 | +prpr < cch.toString(); |
---|
470 | } |
---|
471 | delete [] myMatrix; |
---|
472 | } |
---|
473 | |
---|
474 | /** |
---|
475 | * A method for testing the computation of all minors of a given size within a pre-defined |
---|
476 | * sub-matrix of an underlying matrix.<br> |
---|
477 | * Again, we do this first without cache, and later using a cache, respectively.<br> |
---|
478 | * All results should be equal no matter whether we do or do not use a cache. Neither should the cache strategy |
---|
479 | * influence the mathematical value of the minor. |
---|
480 | */ |
---|
481 | void testAllMinors(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage, |
---|
482 | int minorRows, int minorColumns, int minorSize, int randomSeed, int cacheEntries, int cacheWeight) { |
---|
483 | long totalTimeStart, totalTime, printTimeStart, printTime; |
---|
484 | |
---|
485 | prpr < testHeader; |
---|
486 | +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; // underlines the header string |
---|
487 | +prpr < "Testing the successive computation of all minors of a given size without and with cache, respectively."; |
---|
488 | +prpr < "In the case of computing with cache, 5 different caching strategies will be deployed."; |
---|
489 | |
---|
490 | int* myMatrix = new int[rowCount * columnCount]; |
---|
491 | fillRandomMatrix(rowCount, columnCount, randomSeed, zeroPercentage, entryBound, myMatrix); |
---|
492 | |
---|
493 | IntMinorProcessor mp; |
---|
494 | mp.defineMatrix(rowCount, columnCount, myMatrix); |
---|
495 | |
---|
496 | int myRowIndices[minorRows]; for (int i = 0; i < minorRows; i++) myRowIndices[i] = i; |
---|
497 | int myColumnIndices[minorColumns]; for (int i = 0; i < minorColumns; i++) myColumnIndices[i] = columnCount - minorColumns + i; |
---|
498 | |
---|
499 | // setting sub-matrix and size of minors of interest within that sub-matrix: |
---|
500 | mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices); |
---|
501 | mp.setMinorSize(minorSize); |
---|
502 | |
---|
503 | +prpr; +prpr < mp.toString(); |
---|
504 | |
---|
505 | // define the cache: |
---|
506 | Cache<MinorKey, IntMinorValue> cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight); |
---|
507 | |
---|
508 | // container for all upcoming results |
---|
509 | IntMinorValue theMinor; |
---|
510 | |
---|
511 | // counters... |
---|
512 | int k = 1; |
---|
513 | int totalMultiplications = 0; |
---|
514 | int totalAdditions = 0; |
---|
515 | int totalMultiplicationsAccumulated = 0; |
---|
516 | int totalAdditionsAccumulated = 0; |
---|
517 | |
---|
518 | // target for retrieving and writing momentary row and column indices: |
---|
519 | int myIndexArray[32]; |
---|
520 | |
---|
521 | +prpr; +prpr < "Results - " < testHeader < " - no cache"; |
---|
522 | +prpr < "computing all minors of size " < minorSize < "x" < minorSize; |
---|
523 | |
---|
524 | printTime = 0; totalTimeStart = clock(); |
---|
525 | // iteration over all minors of size "minorSize x minorSize" |
---|
526 | while (mp.hasNextMinor()) { |
---|
527 | // retrieving the minor: |
---|
528 | theMinor = mp.getNextMinor(); |
---|
529 | printTimeStart = clock(); |
---|
530 | |
---|
531 | // updating counters: |
---|
532 | totalMultiplications += theMinor.getMultiplications(); |
---|
533 | totalAdditions += theMinor.getAdditions(); |
---|
534 | totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications(); |
---|
535 | totalAdditionsAccumulated += theMinor.getAccumulatedAdditions(); |
---|
536 | |
---|
537 | // writing current row indices: |
---|
538 | mp.getCurrentRowIndices(myIndexArray); |
---|
539 | +prpr < k++ < ". minor (rows: "; |
---|
540 | for (int i = 0; i < minorSize; i++) { |
---|
541 | if (i != 0) prpr < ", "; |
---|
542 | prpr < myIndexArray[i]; |
---|
543 | }; |
---|
544 | |
---|
545 | // writing current column indices: |
---|
546 | mp.getCurrentColumnIndices(myIndexArray); |
---|
547 | prpr < "; columns: "; |
---|
548 | for (int i = 0; i < minorSize; i++) { |
---|
549 | if (i != 0) prpr < ", "; |
---|
550 | prpr < myIndexArray[i]; |
---|
551 | }; |
---|
552 | prpr < ") = "; |
---|
553 | |
---|
554 | // write the actual value of the minor: |
---|
555 | prpr < theMinor.toString(); |
---|
556 | printTime += clock() - printTimeStart; |
---|
557 | }; |
---|
558 | totalTime = clock() - totalTimeStart - printTime; |
---|
559 | // writing summarized information |
---|
560 | ++prpr; ++prpr << "Operation counters - " << testHeader << " - no cache"; |
---|
561 | ++prpr << "performed in total " << totalMultiplications << " multiplications and "; |
---|
562 | prpr << totalAdditions << " additions"; |
---|
563 | ++prpr << "(time = " << totalTime << " msec)"; |
---|
564 | |
---|
565 | for (int strategy = 1; strategy <= 5; strategy++) { |
---|
566 | // setting sub-matrix, size of minors of interest within that sub-matrix, and strategy: |
---|
567 | mp.defineSubMatrix(minorRows, myRowIndices, minorColumns, myColumnIndices); |
---|
568 | mp.setMinorSize(minorSize); |
---|
569 | IntMinorValue::SetRankingStrategy(strategy); |
---|
570 | |
---|
571 | // counters... |
---|
572 | k = 1; |
---|
573 | totalMultiplications = 0; |
---|
574 | totalAdditions = 0; |
---|
575 | totalMultiplicationsAccumulated = 0; |
---|
576 | totalAdditionsAccumulated = 0; |
---|
577 | |
---|
578 | // cleaning up and redefinition of the cache: |
---|
579 | cch.clear(); |
---|
580 | cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight); |
---|
581 | |
---|
582 | +prpr; +prpr < "Results - " < testHeader < " - using cache - deploying caching strategy #" < strategy; |
---|
583 | +prpr < "computing all minors of size " < minorSize < "x" < minorSize; |
---|
584 | |
---|
585 | printTime = 0; totalTimeStart = clock(); |
---|
586 | // iteration over all minors of size "minorSize x minorSize" |
---|
587 | while (mp.hasNextMinor()) { |
---|
588 | // retrieving the minor: |
---|
589 | theMinor = mp.getNextMinor(cch); |
---|
590 | printTimeStart = clock(); |
---|
591 | |
---|
592 | // updating counters: |
---|
593 | totalMultiplications += theMinor.getMultiplications(); |
---|
594 | totalAdditions += theMinor.getAdditions(); |
---|
595 | totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications(); |
---|
596 | totalAdditionsAccumulated += theMinor.getAccumulatedAdditions(); |
---|
597 | |
---|
598 | // writing current row indices: |
---|
599 | mp.getCurrentRowIndices(myIndexArray); |
---|
600 | +prpr < k++ < ". minor (rows: "; |
---|
601 | for (int i = 0; i < minorSize; i++) { |
---|
602 | if (i != 0) prpr < ", "; |
---|
603 | prpr < myIndexArray[i]; |
---|
604 | }; |
---|
605 | |
---|
606 | // writing current column indices: |
---|
607 | mp.getCurrentColumnIndices(myIndexArray); |
---|
608 | prpr < "; columns: "; |
---|
609 | for (int i = 0; i < minorSize; i++) { |
---|
610 | if (i != 0) prpr < ", "; |
---|
611 | prpr < myIndexArray[i]; |
---|
612 | }; |
---|
613 | prpr < ") = "; |
---|
614 | |
---|
615 | // writeMinor the actual value of the minor: |
---|
616 | prpr < theMinor.toString(); |
---|
617 | printTime += clock() - printTimeStart; |
---|
618 | }; |
---|
619 | totalTime = clock() - totalTimeStart - printTime; |
---|
620 | // writing summarized information |
---|
621 | ++prpr; ++prpr << "Operation counters - " << testHeader << " - using cache - deploying caching strategy #" << strategy; |
---|
622 | ++prpr << "performed in total " << totalMultiplications << " multiplications and "; |
---|
623 | prpr << totalAdditions << " additions"; |
---|
624 | ++prpr << "(computation without reuse would need " << totalMultiplicationsAccumulated << " and "; |
---|
625 | prpr << totalAdditionsAccumulated << " additions)"; |
---|
626 | ++prpr << "(time = " << totalTime << " msec)"; |
---|
627 | +prpr < "The cache looks like this:"; |
---|
628 | +prpr < cch.toString(); |
---|
629 | } |
---|
630 | delete [] myMatrix; |
---|
631 | } |
---|
632 | |
---|
633 | /** |
---|
634 | * A method for testing the computation of all minors of a given size within a pre-defined |
---|
635 | * sub-matrix of an underlying large matrix.<br> |
---|
636 | * Again, we do this first without cache, and later using a cache, respectively.<br> |
---|
637 | * All results should be equal no matter whether we do or do not use a cache. Neither should the cache strategy |
---|
638 | * influence the mathematical value of the minor. |
---|
639 | * zeroP: this is the probability for zero entries in the matrix; |
---|
640 | * all other matrix entries will range from 1 to entryBound and be equally distributed |
---|
641 | */ |
---|
642 | void testAllMinorsUntil(PrettyPrinter& prpr, string testHeader, int rowCount, int columnCount, int entryBound, int zeroPercentage, |
---|
643 | int minorSize, int randomSeed, int cacheEntries, int cacheWeight, int targetMinor, |
---|
644 | bool checkForEquality, int maxLoops) { |
---|
645 | long totalTimeStart, totalTime, printTimeStart, printTime; |
---|
646 | |
---|
647 | prpr < testHeader; |
---|
648 | +prpr; for (int i = 0; i < int(testHeader.size()); i++) prpr < "="; // underlines the header string |
---|
649 | +prpr < "Testing the successive computation of minors of a given size without and with "; |
---|
650 | +prpr < "cache, respectively, until a minor with a certain value is found."; |
---|
651 | +prpr < "In the case of computing with cache, 5 different caching strategies will be deployed."; |
---|
652 | |
---|
653 | +prpr; +prpr < "In this test, the matrix is " < rowCount < " x " < columnCount < "."; |
---|
654 | +prpr < "The minor we are looking for is " < minorSize < " x " < minorSize < ", and "; |
---|
655 | prpr < "is supposed to have a value of "; |
---|
656 | if (!checkForEquality) prpr < "<> "; |
---|
657 | prpr < targetMinor < "."; |
---|
658 | +prpr < "As an upper bound for the number of loops, at most " < maxLoops < " minors will be computed."; |
---|
659 | |
---|
660 | int* myMatrix = new int[rowCount * columnCount]; |
---|
661 | fillRandomMatrix(rowCount, columnCount, randomSeed, zeroPercentage, entryBound, myMatrix); |
---|
662 | |
---|
663 | IntMinorProcessor mp; |
---|
664 | mp.defineMatrix(rowCount, columnCount, myMatrix); |
---|
665 | |
---|
666 | int myRowIndices[rowCount]; for (int i = 0; i < rowCount; i++) myRowIndices[i] = i; // choosing all rows |
---|
667 | int myColumnIndices[columnCount]; for (int i = 0; i < columnCount; i++) myColumnIndices[i] = i; // choosing all columns |
---|
668 | |
---|
669 | // define the cache: |
---|
670 | Cache<MinorKey, IntMinorValue> cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight); |
---|
671 | |
---|
672 | // container for all upcoming results |
---|
673 | IntMinorValue theMinor; |
---|
674 | |
---|
675 | // counters... |
---|
676 | int k = 1; |
---|
677 | int totalMultiplications = 0; |
---|
678 | int totalAdditions = 0; |
---|
679 | int totalMultiplicationsAccumulated = 0; |
---|
680 | int totalAdditionsAccumulated = 0; |
---|
681 | |
---|
682 | // setting sub-matrix and size of minors of interest within that sub-matrix: |
---|
683 | mp.defineSubMatrix(rowCount, myRowIndices, columnCount, myColumnIndices); |
---|
684 | mp.setMinorSize(minorSize); |
---|
685 | |
---|
686 | +prpr; +prpr < mp.toString(); |
---|
687 | |
---|
688 | +prpr < "Results - " < testHeader < " - no cache"; |
---|
689 | +prpr < "computing all minors of size " < minorSize < "x" < minorSize; |
---|
690 | prpr < " until first minor with specified value is found:"; |
---|
691 | |
---|
692 | bool minorFound = false; |
---|
693 | int loops = 0; |
---|
694 | printTime = 0; totalTimeStart = clock(); |
---|
695 | // iteration over all minors of size "minorSize x minorSize" |
---|
696 | while (mp.hasNextMinor() && (!minorFound) && (loops < maxLoops)) { |
---|
697 | |
---|
698 | // retrieving the minor: |
---|
699 | theMinor = mp.getNextMinor(); |
---|
700 | printTimeStart = clock(); |
---|
701 | minorFound = (checkForEquality ? (theMinor.getResult() == targetMinor) : (theMinor.getResult() != targetMinor)); |
---|
702 | |
---|
703 | // updating counters: |
---|
704 | totalMultiplications += theMinor.getMultiplications(); |
---|
705 | totalAdditions += theMinor.getAdditions(); |
---|
706 | totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications(); |
---|
707 | totalAdditionsAccumulated += theMinor.getAccumulatedAdditions(); |
---|
708 | |
---|
709 | // writing current minor |
---|
710 | +prpr < k++ < ". minor = " < theMinor.getResult(); |
---|
711 | |
---|
712 | loops++; |
---|
713 | printTime += clock() - printTimeStart; |
---|
714 | }; |
---|
715 | totalTime = clock() - totalTimeStart - printTime; |
---|
716 | // writing summarized information |
---|
717 | ++prpr; ++prpr << "Operation counters - " << testHeader << " - no cache"; |
---|
718 | ++prpr << "performed in total " << totalMultiplications << " multiplications and "; |
---|
719 | prpr << totalAdditions << " additions"; |
---|
720 | ++prpr << "(time = " << totalTime << " msec)"; |
---|
721 | |
---|
722 | for (int strategy = 1; strategy <= 5; strategy++) { |
---|
723 | // setting sub-matrix, size of minors of interest within that sub-matrix, and strategy: |
---|
724 | mp.defineSubMatrix(rowCount, myRowIndices, columnCount, myColumnIndices); |
---|
725 | mp.setMinorSize(minorSize); |
---|
726 | IntMinorValue::SetRankingStrategy(strategy); |
---|
727 | |
---|
728 | // counters... |
---|
729 | k = 1; |
---|
730 | totalMultiplications = 0; |
---|
731 | totalAdditions = 0; |
---|
732 | totalMultiplicationsAccumulated = 0; |
---|
733 | totalAdditionsAccumulated = 0; |
---|
734 | |
---|
735 | // cleaning up and redefinition of the cache: |
---|
736 | cch.clear(); |
---|
737 | cch = Cache<MinorKey, IntMinorValue>(cacheEntries, cacheWeight); |
---|
738 | |
---|
739 | +prpr; +prpr < testHeader < " - using cache - deploying caching strategy #" < strategy; |
---|
740 | +prpr < "computing all minors of size " < minorSize < "x" < minorSize; |
---|
741 | prpr < " until first minor with specified value is found:"; |
---|
742 | |
---|
743 | int loops = 0; |
---|
744 | bool minorFound = false; |
---|
745 | printTime = 0; totalTimeStart = clock(); |
---|
746 | // iteration over all minors of size "minorSize x minorSize" |
---|
747 | while (mp.hasNextMinor() && (!minorFound) && (loops < maxLoops)) { |
---|
748 | |
---|
749 | // retrieving the minor: |
---|
750 | theMinor = mp.getNextMinor(cch); |
---|
751 | printTimeStart = clock(); |
---|
752 | minorFound = (checkForEquality ? (theMinor.getResult() == targetMinor) : (theMinor.getResult() != targetMinor)); |
---|
753 | |
---|
754 | // updating counters: |
---|
755 | totalMultiplications += theMinor.getMultiplications(); |
---|
756 | totalAdditions += theMinor.getAdditions(); |
---|
757 | totalMultiplicationsAccumulated += theMinor.getAccumulatedMultiplications(); |
---|
758 | totalAdditionsAccumulated += theMinor.getAccumulatedAdditions(); |
---|
759 | |
---|
760 | // writing current minor |
---|
761 | +prpr < k++ < ". minor = " < theMinor.getResult(); |
---|
762 | |
---|
763 | loops++; |
---|
764 | printTime += clock() - printTimeStart; |
---|
765 | }; |
---|
766 | totalTime = clock() - totalTimeStart - printTime; |
---|
767 | // writing summarized information |
---|
768 | ++prpr; ++prpr << "Operation counters - " << testHeader << " - using cache - deploying caching strategy #" << strategy; |
---|
769 | ++prpr << "performed in total " << totalMultiplications << " multiplications and "; |
---|
770 | prpr << totalAdditions << " additions"; |
---|
771 | ++prpr << "(computation without reuse would need " << totalMultiplicationsAccumulated << " and "; |
---|
772 | prpr << totalAdditionsAccumulated << " additions)"; |
---|
773 | ++prpr << "(time = " << totalTime << " msec)"; |
---|
774 | +prpr < "The cache has " < cch.getNumberOfEntries() < " (of max. " < cacheEntries < ") entries and a weight of "; |
---|
775 | prpr < cch.getWeight() < " (of max. " < cacheWeight < ")."; |
---|
776 | } |
---|
777 | delete [] myMatrix; |
---|
778 | } |
---|
779 | |
---|
780 | #endif // HAVE_MINOR |
---|