This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:fall2017:discrete:projects:pnc0 [2017/07/12 20:11] – [Check Results] wedge | haas:fall2017:discrete:projects:pnc0 [2017/10/15 21:11] (current) – [Programs] wedge | ||
---|---|---|---|
Line 1: | Line 1: | ||
<WRAP centeralign round box> | <WRAP centeralign round box> | ||
< | < | ||
- | < | + | < |
</ | </ | ||
- | |||
- | ~~TOC~~ | ||
======Project: | ======Project: | ||
+ | |||
+ | =====Errata===== | ||
+ | With any increasingly complex piece of code or environment, | ||
+ | |||
+ | Any typos, bugs, or other updates/ | ||
+ | |||
+ | ====Revision List==== | ||
+ | * revision 0: initial release (20170712) | ||
+ | * 0.1: there were some stray mentions of " | ||
+ | * 0.2: added an additional requirement to implementation constraints: | ||
+ | * 0.3: added new check/ | ||
+ | * 0.4: added further explanation to " | ||
+ | * 0.5: for increased readability, | ||
+ | * revision #: < | ||
+ | |||
+ | Some changes may involve updates being made available to the project, in which case you'll be prompted with such notification and can run the available updating commands to synchronize your copy of the project with the changes. | ||
=====Objective===== | =====Objective===== | ||
Line 81: | Line 95: | ||
* you know the starting value and the terminating condition, so you have a clear starting and ending point to work with. | * you know the starting value and the terminating condition, so you have a clear starting and ending point to work with. | ||
* I want you to use two **DIFFERENT** kind of loops in your programs. If you use a **for()** loop in your outer loop, I want you to use a **while()** or **do-while()** loop in your inner loop (and whatever combination you end up with). | * I want you to use two **DIFFERENT** kind of loops in your programs. If you use a **for()** loop in your outer loop, I want you to use a **while()** or **do-while()** loop in your inner loop (and whatever combination you end up with). | ||
+ | * I do **NOT** want to see ambiguous, one-letter variables used in your implementation(s). Please use // | ||
+ | * Some good examples of variable names would be: | ||
+ | * **number**: the number being tested | ||
+ | * **factor**: the value being divided into number to test for primality | ||
+ | * **step**: the rate by which some variable is changing | ||
+ | * **qty**: the count of the current tally of primes | ||
+ | * **max**: the maximum count we seek | ||
+ | * **start**: a value we are starting at | ||
+ | * **lower**: a lower bound | ||
+ | * **upper**: an upper bound | ||
+ | * see how much more readable and meaningful these are, especially as compared to **a**, **i**, **n**, **x**? You may even find it helps with debugging and understanding your code better. | ||
* let the loops drive the overall process. Identify prime/ | * let the loops drive the overall process. Identify prime/ | ||
* and remember, the baseline brute force algorithm (**primereg**) may well identify a value as composite, but won't terminate the loop. | * and remember, the baseline brute force algorithm (**primereg**) may well identify a value as composite, but won't terminate the loop. | ||
- | * your timing should start before the loop (just AFTER argument processing), | + | * your timing should start before the loop (just AFTER argument processing), |
+ | * you may **NOT** split **qty** and **range** functionality into two separate code blocks (ie have two sets of two loops). Only the one set as indicated. | ||
=====prime algorithm optimizations===== | =====prime algorithm optimizations===== | ||
To give us a decent appreciation of the subtleties of algorithm development in a theme of programs, I have identified the following optimizations that we will be implementing. | To give us a decent appreciation of the subtleties of algorithm development in a theme of programs, I have identified the following optimizations that we will be implementing. | ||
Line 107: | Line 132: | ||
That is, if your program to implement is **primerego**, | That is, if your program to implement is **primerego**, | ||
- | Some of these optimizations can co-exist easily (break + map, odd + sqrt()), others are conditionally | + | Some of these optimizations can co-exist easily (break + map, odd + sqrt()), others are partially |
Here are the variants you'll be implementing for this project: | Here are the variants you'll be implementing for this project: | ||
Line 134: | Line 159: | ||
This optimization employs the approximated square root trick (**NOT** utilizing an existing square root function, but using simpler logic you implement to approximate the square root point). | This optimization employs the approximated square root trick (**NOT** utilizing an existing square root function, but using simpler logic you implement to approximate the square root point). | ||
+ | ===Further explanation=== | ||
+ | An optimization to the previous process, which used **sqrt()**, this variation will do the exact same thing, but without using the **sqrt()** function. It will approximate the square root. | ||
+ | |||
+ | We know that a square root (especially a whole numbered square root), is when we have whole number factors that are squared. But in addition, only considering the whole number aspect of the square root, we start seeing series of values with the same whole square root value: | ||
+ | |||
+ | <cli> | ||
+ | lab46:~$ count=0; for ((i=2; i<152; i++)); do printf "[%3d] %2d " " | ||
+ | [ 2] 1 [ 3] 1 [ 4] 2 [ 5] 2 [ 6] 2 [ 7] 2 [ 8] 2 [ 9] 3 [ 10] 3 [ 11] 3 | ||
+ | [ 12] 3 [ 13] 3 [ 14] 3 [ 15] 3 [ 16] 4 [ 17] 4 [ 18] 4 [ 19] 4 [ 20] 4 [ 21] 4 | ||
+ | [ 22] 4 [ 23] 4 [ 24] 4 [ 25] 5 [ 26] 5 [ 27] 5 [ 28] 5 [ 29] 5 [ 30] 5 [ 31] 5 | ||
+ | [ 32] 5 [ 33] 5 [ 34] 5 [ 35] 5 [ 36] 6 [ 37] 6 [ 38] 6 [ 39] 6 [ 40] 6 [ 41] 6 | ||
+ | [ 42] 6 [ 43] 6 [ 44] 6 [ 45] 6 [ 46] 6 [ 47] 6 [ 48] 6 [ 49] 7 [ 50] 7 [ 51] 7 | ||
+ | [ 52] 7 [ 53] 7 [ 54] 7 [ 55] 7 [ 56] 7 [ 57] 7 [ 58] 7 [ 59] 7 [ 60] 7 [ 61] 7 | ||
+ | [ 62] 7 [ 63] 7 [ 64] 8 [ 65] 8 [ 66] 8 [ 67] 8 [ 68] 8 [ 69] 8 [ 70] 8 [ 71] 8 | ||
+ | [ 72] 8 [ 73] 8 [ 74] 8 [ 75] 8 [ 76] 8 [ 77] 8 [ 78] 8 [ 79] 8 [ 80] 8 [ 81] 9 | ||
+ | [ 82] 9 [ 83] 9 [ 84] 9 [ 85] 9 [ 86] 9 [ 87] 9 [ 88] 9 [ 89] 9 [ 90] 9 [ 91] 9 | ||
+ | [ 92] 9 [ 93] 9 [ 94] 9 [ 95] 9 [ 96] 9 [ 97] 9 [ 98] 9 [ 99] 9 [100] 10 [101] 10 | ||
+ | [102] 10 [103] 10 [104] 10 [105] 10 [106] 10 [107] 10 [108] 10 [109] 10 [110] 10 [111] 10 | ||
+ | [112] 10 [113] 10 [114] 10 [115] 10 [116] 10 [117] 10 [118] 10 [119] 10 [120] 10 [121] 11 | ||
+ | [122] 11 [123] 11 [124] 11 [125] 11 [126] 11 [127] 11 [128] 11 [129] 11 [130] 11 [131] 11 | ||
+ | [132] 11 [133] 11 [134] 11 [135] 11 [136] 11 [137] 11 [138] 11 [139] 11 [140] 11 [141] 11 | ||
+ | [142] 11 [143] 11 [144] 12 [145] 12 [146] 12 [147] 12 [148] 12 [149] 12 [150] 12 [151] 12 | ||
+ | </ | ||
+ | |||
+ | Or, if perhaps we instead order by square root value: | ||
+ | |||
+ | <cli> | ||
+ | lab46:~$ oldsqrt=$(echo " | ||
+ | [ 2] 1 [ 3] 1 | ||
+ | [ 4] 2 [ 5] 2 [ 6] 2 [ 7] 2 [ 8] 2 | ||
+ | [ 9] 3 [ 10] 3 [ 11] 3 [ 12] 3 [ 13] 3 [ 14] 3 [ 15] 3 | ||
+ | [ 16] 4 [ 17] 4 [ 18] 4 [ 19] 4 [ 20] 4 [ 21] 4 [ 22] 4 [ 23] 4 [ 24] 4 | ||
+ | [ 25] 5 [ 26] 5 [ 27] 5 [ 28] 5 [ 29] 5 [ 30] 5 [ 31] 5 [ 32] 5 [ 33] 5 [ 34] 5 [ 35] 5 | ||
+ | [ 36] 6 [ 37] 6 [ 38] 6 [ 39] 6 [ 40] 6 [ 41] 6 [ 42] 6 [ 43] 6 [ 44] 6 [ 45] 6 [ 46] 6 [ 47] 6 [ 48] 6 | ||
+ | </ | ||
+ | |||
+ | We see that the square root of 36 is 6, but so is the square root of 37, 38, 39... etc. up until we hit 49 (where the whole number square root increments to 7). | ||
+ | |||
+ | Therefore, if we were checking 42 to be prime, we'd only have to check up to 6. | ||
+ | |||
+ | We don't need a **sqrt()** function to tell us this, we can determine the approximate square root point ourselves- by squaring the current factor being tested, and so long as it hasn't exceeded the value we're checking, we know to continue. | ||
+ | |||
+ | There are some important lessons at play here: | ||
+ | |||
+ | * approximation can be powerful | ||
+ | * approximation can result in a simpler algorithm, improving runtime | ||
+ | * **sqrt()** is more complex than you may be aware, not to mention it is in a function. By avoiding that function call, we eliminate some overhead, and that can make a difference in runtime performance. | ||
+ | |||
+ | Depending on how you implement this and the original sqrt() algorithms, this version may have a noticeable performance difference. If, on the other hand, you were really optimal in both implementations, | ||
+ | |||
====primeregbm==== | ====primeregbm==== | ||
To get a taste for combining optimizations, | To get a taste for combining optimizations, | ||
Line 153: | Line 228: | ||
It is your task to write the following prime number variants: | It is your task to write the following prime number variants: | ||
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
====Program Specifications==== | ====Program Specifications==== | ||
Line 230: | Line 305: | ||
NOTE: You do NOT want to do this on a populated pnc0 project directory-- it will overwrite files. | NOTE: You do NOT want to do this on a populated pnc0 project directory-- it will overwrite files. | ||
- | And, of course, your basic compile and clean-up operations via the Makefile: | + | And, of course, your basic compile and clean-up operations via the Makefile. |
+ | |||
+ | =====Makefile operations===== | ||
+ | Makefiles provide a build automation system for our programs, instructing the computer on how to compile files, so we don't have to constantly type compiler command-lines ourselves. I've also integration some other useful, value-added features that will help you with overall administration of the project. | ||
+ | |||
+ | Basic operation of the Makefile is invoked by running the command " | ||
+ | |||
+ | Additional options are available, and they are provided as an argument to the make command. You can see the available options by running " | ||
+ | |||
+ | < | ||
+ | lab46: | ||
+ | ******************[ Discrete Structures pnc0 Project ]****************** | ||
+ | ** make - build everything | ||
+ | ** make showerrors | ||
+ | ** ** | ||
+ | ** make debug - build everything with debug symbols | ||
+ | ** make checkqty | ||
+ | ** make checkrange | ||
+ | ** ** | ||
+ | ** make verifyqty | ||
+ | ** make verifyrange | ||
+ | ** make verifyall | ||
+ | ** ** | ||
+ | ** make save - create a backup archive | ||
+ | ** make submit | ||
+ | ** ** | ||
+ | ** make update | ||
+ | ** make reupdate | ||
+ | ** make reupdate-all | ||
+ | ** ** | ||
+ | ** make clean - clean; remove all objects/ | ||
+ | ** make help - this information | ||
+ | ************************************************************************ | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | A description of some available commands include: | ||
* **make**: compile everything | * **make**: compile everything | ||
Line 240: | Line 351: | ||
* **make submit**: archive and submit your project | * **make submit**: archive and submit your project | ||
- | Just another | + | The various |
+ | The various " | ||
+ | |||
+ | Just another "nice thing" we deserve. | ||
=====Command-Line Arguments===== | =====Command-Line Arguments===== | ||
- | To automate our comparisons, | + | To automate our comparisons, |
====header files==== | ====header files==== | ||
Line 261: | Line 375: | ||
int main(int argc, char **argv) | int main(int argc, char **argv) | ||
</ | </ | ||
+ | |||
+ | There are two very important variables involved here (the types are actually what are important, the names given to the variables are actually quite, variable; you may see other references refer to them as things like " | ||
+ | |||
+ | * int argc: the count (an integer) of tokens given on the command line (program name + arguments) | ||
+ | * < | ||
The arguments are accessible via the argv array, in the order they were specified: | The arguments are accessible via the argv array, in the order they were specified: | ||
Line 269: | Line 388: | ||
* argv[3]: conditionally optional; represents lower bound | * argv[3]: conditionally optional; represents lower bound | ||
* argv[4]: conditionally optional; represents upper bound | * argv[4]: conditionally optional; represents upper bound | ||
+ | |||
+ | Additionally, | ||
+ | |||
+ | ===example=== | ||
+ | For example, if we were to execute the **primereg** program: | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | We'd have: | ||
+ | |||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | |||
+ | and let's not forget: | ||
+ | |||
+ | * argc: 5 | ||
+ | |||
+ | With the conditionally optional arguments as part of the program spec, for a valid execution of the program, argc could be a value anywhere from 3 to 5. | ||
====Simple argument checks==== | ====Simple argument checks==== | ||
Line 469: | Line 611: | ||
<cli> | <cli> | ||
- | lab46: | + | lab46: |
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 | 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 | ||
0.0001 | 0.0001 | ||
- | lab46: | + | lab46: |
</ | </ | ||
Line 481: | Line 623: | ||
<cli> | <cli> | ||
- | lab46: | + | lab46: |
- | ./primebrute: invalid lower bound | + | ./primerego: invalid lower bound |
- | lab46: | + | lab46: |
</ | </ | ||
Line 492: | Line 634: | ||
<cli> | <cli> | ||
- | lab46: | + | lab46: |
7 11 13 17 19 23 | 7 11 13 17 19 23 | ||
0.0001 | 0.0001 | ||
- | lab46: | + | lab46: |
</ | </ | ||
Line 501: | Line 643: | ||
=====Check Results===== | =====Check Results===== | ||
- | If you'd like to compare your implementations, | + | If you'd like to compare your implementations, |
In order to work, you **MUST** be in the directory where your pnc0 binaries reside, and must be named as such (which occurs if you ran **make** to compile them). | In order to work, you **MUST** be in the directory where your pnc0 binaries reside, and must be named as such (which occurs if you ran **make** to compile them). | ||
+ | ====check qty==== | ||
For instance (running on my implementation of the pnc0 programs, some output omitted to keep the surprise alive): | For instance (running on my implementation of the pnc0 programs, some output omitted to keep the surprise alive): | ||
<cli> | <cli> | ||
- | lab46: | + | lab46: |
========================================================================================= | ========================================================================================= | ||
qty | qty | ||
Line 520: | Line 663: | ||
... | ... | ||
| | ||
+ | ========================================================================================= | ||
+ | | ||
+ | ========================================================================================= | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | ====check range==== | ||
+ | Or check range runtimes: | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | ========================================================================================= | ||
+ | range | ||
+ | ========================================================================================= | ||
+ | | ||
+ | | ||
+ | 128 0.0002 | ||
+ | 256 0.0004 | ||
+ | 512 0.0015 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | ... | ||
+ | 2097152 | ||
========================================================================================= | ========================================================================================= | ||
| | ||
Line 530: | Line 698: | ||
If you don't feel like waiting, simply hit **CTRL-c** (maybe a couple of times) and the script will terminate. | If you don't feel like waiting, simply hit **CTRL-c** (maybe a couple of times) and the script will terminate. | ||
+ | ====Verification==== | ||
I also include a validation check- to ensure your prime programs are actually producing the correct list of prime numbers. If the check is successful, you will see " | I also include a validation check- to ensure your prime programs are actually producing the correct list of prime numbers. If the check is successful, you will see " | ||
+ | ====Full Verification Compliance==== | ||
+ | There' | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | ========================================================================================= | ||
+ | reg regm rego regb | ||
+ | ========================================================================================= | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | coop: OK OK OK OK OK OK OK OK OK OK | ||
+ | | ||
+ | | ||
+ | noargs: | ||
+ | | ||
+ | invqty: | ||
+ | | ||
+ | invlow: | ||
+ | | ||
+ | ========================================================================================= | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | ===verifyall tests=== | ||
+ | The " | ||
+ | * **qtynorm**: | ||
+ | * **./ | ||
+ | * **qtypart**: | ||
+ | * **./ | ||
+ | * **rngnorm**: | ||
+ | * **./ | ||
+ | * **rngpart**: | ||
+ | * **./ | ||
+ | * **coop**: both qty and upper bounds set (q: 2048, ub: 8192) | ||
+ | * **./ | ||
+ | * **coop2**: both qty and upper bounds set (q: 512, ub: 8192) | ||
+ | * **./ | ||
+ | * **coop3**: both qty and upper bounds set, offset start (24-max, q: 2048, ub: 8192) | ||
+ | * **./ | ||
+ | * **noargs**: | ||
+ | * **./ | ||
+ | * **invargs**: | ||
+ | * **./ | ||
+ | * **invqty**: invalid value for quantity argument given (invokes error) | ||
+ | * **./ | ||
+ | * **invnary**: | ||
+ | * **./ | ||
+ | * **invlow**: invalid value given for lower bound (invokes error) | ||
+ | * **./ | ||
+ | * **invhigh**: | ||
+ | * **./ | ||
+ | |||
+ | If you'd actually to see the output your program' | ||
+ | |||
+ | For example, if you wanted to see the intended output of the **invnary** test, that would be found in: | ||
+ | |||
+ | * **/ | ||
+ | |||
+ | You could easily run your program with the stated arguments for the test, then use **cat** to display the test results and do a visual comparison. | ||
+ | ====In general==== | ||
Analyze the times you see... do they make sense, especially when comparing the algorithm used and the quantity being processed? These are related to some very important core Computer Science considerations we need to be increasingly mindful of as we design our programs and implement our solutions. Algorithmic complexity and algorithmic efficiency will be common themes in all we do. | Analyze the times you see... do they make sense, especially when comparing the algorithm used and the quantity being processed? These are related to some very important core Computer Science considerations we need to be increasingly mindful of as we design our programs and implement our solutions. Algorithmic complexity and algorithmic efficiency will be common themes in all we do. | ||
Line 562: | Line 793: | ||
<cli> | <cli> | ||
- | lab46: | + | lab46: |
- | Submitting | + | removed ‘primerega’ |
- | -> primereg.c(OK) | + | removed ‘primeregba’ |
- | | + | removed ‘primeregb’ |
+ | removed ‘primeregbm’ | ||
+ | removed ‘primeregbo’ | ||
+ | removed ‘primeregbs’ | ||
+ | removed ‘primereg’ | ||
+ | removed ‘primeregm’ | ||
+ | removed ‘primerego’ | ||
+ | removed ‘primeregs’ | ||
+ | removed ‘errors’ | ||
+ | |||
+ | Project backup process commencing | ||
+ | |||
+ | Taking snapshot of current project (pnc0) | ||
+ | Compressing snapshot of pnc0 project archive | ||
+ | Setting secure permissions on pnc0 archive | ||
+ | |||
+ | Project backup process complete | ||
+ | |||
+ | Submitting | ||
+ | -> ../pnc0-20170712-16.tar.gz(OK) | ||
SUCCESSFULLY SUBMITTED | SUCCESSFULLY SUBMITTED | ||
</ | </ | ||
- | You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches. | + | You should get that final " |
+ | |||
+ | ====Evaluation Criteria==== | ||
+ | Grand total points: | ||
+ | |||
+ | < | ||
+ | 390: | ||
+ | </ | ||
- | What I will be looking for: | + | What I will be looking for (for each file): |
< | < | ||
- | 182: | + | *:pnc0:primeALGO.c compiles cleanly, |
- | *:pnc0:primereg.c performs proper argument checking [2/2] | + | *:pnc0:primeALGO.c implements only specified algorithm [6/6] |
- | *: | + | *:pnc0:primeALGO.c consistent indentation |
- | *:pnc0:primereg.c implements only specified algorithm [6/6] | + | *:pnc0:primeALGO.c relevant |
- | *:pnc0:primereg.c consistent indentation | + | *:pnc0:primeALGO.c code conforms |
- | *:pnc0:primereg.c data and output conform to specifications [6/6] | + | *:pnc0:primeALGO.c runtime output |
- | *: | + | *:pnc0:primeALGO.c make checkqty test times within reason |
- | *: | + | *:pnc0:primeALGO.c make checkrange test times within reason |
- | *: | + | *:pnc0:primeALGO.c make verifyall |
- | *: | + | |
- | *: | + | |
- | *:pnc0:primeregb.c data and output conform | + | |
- | *:pnc0:primeregb.c primerun | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *:pnc0:primerego.c data and output conform to specifications [6/6] | + | |
- | *: | + | |
- | *: | + | |
- | *:pnc0:primeregs.c no negative compiler messages | + | |
- | *:pnc0:primeregs.c implements only specified algorithm [6/6] | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
</ | </ | ||
+ | As the optimizations improve upon others, some evaluations will be based upon differences between a baseline (in some cases, primereg) and the optimization. |