This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
haas:fall2017:cprog:projects:pnc1 [2017/03/01 18:43] – external edit 127.0.0.1 | haas:fall2017:cprog:projects:pnc1 [2017/10/15 21:27] (current) – [Full Verification Compliance] wedge | ||
---|---|---|---|
Line 4: | Line 4: | ||
</ | </ | ||
- | ~~TOC~~ | + | ======Project: |
- | ======Project: OPTIMIZING ALGORITHMS - PRIME NUMBER CALCULATION (pnc1)====== | + | =====Errata===== |
+ | With any increasingly complex piece of code or environment, | ||
- | =====Objective===== | + | Any typos, bugs, or other updates/ |
- | To apply your skills in algorithmic optimization through | + | |
- | =====Algorithmic Complexity===== | + | ====Revision List==== |
- | A concept in Computer Science curriculum is the notion of computational/ | + | |
- | Basically, a solution to a problem exists on a spectrum of efficiency | + | * revision #: < |
- | Additionally, | + | Some changes may involve updates being made available |
- | This project will endeavor to introduce you to the notion that the algorithms and constructs you use in coding your solution can and do make a difference to the overall runtime | + | =====Objective===== |
+ | To apply your skills | ||
- | =====Optimizing the prime number calculation===== | + | =====Background===== |
- | We should be fairly familiar with the process of computing primes by now, which is an essential beginning step to accomplish before pursuing optimization. Following will be some optimizations I'd like you to implement (as separate programs) so we can analyze the differences in approaches, and how they influence runtimes. | + | In mathematics, a **prime** number |
- | ====odds (primeodds)==== | + | The number **6** is a **composite** number, as in addition to 1 and 6, it also has the factors |
- | Some optimizations can be the result of sheer common sense observations. For instance, with the exception | + | |
- | So does it make sense to check an even number | + | The number **17**, however, is a **prime** |
- | And can we predict even numbers? Yes, we can: they occur every other number. | + | =====Calculating the primality of a number===== |
+ | As of yet, there is no quick and direct way of determining the primality of a given number. Instead, we must perform a series of tests to determine if it fails primality (typically by proving it is composite). | ||
- | Therefore, we can start our number | + | This process incurs a considerable amount of processing overhead on the task, so much so that increasingly large values take ever-expanding amounts of time. Often, approaches to prime number |
- | To make our output correct, we would simply display | + | Your task for this project is to implement a prime number program using the straightforward, |
- | This program should be an optimization based on your **primebruteopt** program from pnc0. | + | =====Main algorithm: brute force (primereg)===== |
+ | The brute force approach is the simplest to implement (although at some cost). | ||
- | ====square root (primesqrt)==== | + | As we will be looking |
- | An optimization | + | |
- | The C library has a **sqrt()** function available through including | + | To perform the process of computing the primality of a number, we simply attempt to evenly divide all the values between 2 and one less than the number in question. If any one of them divides evenly, the number is **NOT** prime, but instead composite. |
- | To use **sqrt()**, we pass in the value we wish to obtain the square root of, and assign the result to an **int**: | + | Checking the **remainder** of a division indicates whether or not a division was clean (having 0 remainder indicates such a state). |
- | <code c> | + | For example, the number 11: |
- | int x = 25; | + | |
- | int y = 0; | + | |
- | y = sqrt(x); | + | < |
- | + | 11 % 2 = 1 (2 is not a factor of 11) | |
- | // y should be 5 as a result | + | 11 % 3 = 2 (3 is not a factor of 11) |
+ | 11 % 4 = 3 (4 is not a factor of 11) | ||
+ | 11 % 5 = 1 (5 is not a factor of 11) | ||
+ | 11 % 6 = 5 (6 is not a factor of 11) | ||
+ | 11 % 7 = 4 (7 is not a factor of 11) | ||
+ | 11 % 8 = 3 (8 is not a factor of 11) | ||
+ | 11 % 9 = 2 (9 is not a factor of 11) | ||
+ | 11 % 10 = 1 (10 is not a factor of 11) | ||
</ | </ | ||
- | For instance, | + | Because none of the values 2-10 evenly divided into 11, we can say it passed |
+ | |||
+ | On the other hand, take 119: | ||
< | < | ||
- | 37 % 2 = 1 (2 is not a factor of 37) | + | 119 % 2 = 1 (2 is not a factor of 119) |
- | 37 % 3 = 1 (3 is not a factor of 37) | + | 119 % 3 = 2 (3 is not a factor of 119) |
- | 37 % 4 = 1 (4 is not a factor of 37) | + | 119 % 4 = 3 (4 is not a factor of 119) |
- | 37 % 5 = 2 (5 is not a factor of 37) | + | 119 % 5 = 4 (5 is not a factor of 119) |
- | 37 % 6 = 1 (6 is not a factor of 37) | + | 119 % 6 = 5 (6 is not a factor of 119) |
+ | 119 % 7 = 0 (7 is a factor of 119) | ||
+ | 119 % 8 = 7 | ||
+ | 119 % 9 = 2 | ||
+ | 119 % 10 = 9 | ||
+ | 119 % 11 = 9 | ||
+ | 119 % 12 = 11 | ||
+ | 119 % 13 = 2 | ||
+ | ... | ||
</ | </ | ||
- | Because | + | Because, during our range of testing every value from 2-118, we find that 7 evenly divides |
- | This will dramatically improve | + | Please NOTE: Even once a number is identified as composite, your **primereg** MUST **CONTINUE** evaluating |
- | NOTE: You will be reverting | + | ====algorithm==== |
+ | Some things | ||
- | This program | + | * you will want to use loops (no less than 2, no more than 2) for this program. |
+ | * a nested loop makes the most sense: | ||
+ | * an outer loop that drives the progression of each sequential number to be tested | ||
+ | * an inner loop that tests that current number to see if it has any factors | ||
+ | * you know the starting value and the terminating condition, so you have a clear starting and ending point to work with. | ||
+ | * I do **NOT** want to see ambiguous, one-letter variables used in your implementation(s). Please use // | ||
+ | | ||
+ | | ||
+ | * **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/ | ||
+ | * 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), | ||
- | ====sqrt() odds (primesqrtodds)==== | + | =====prime algorithm implementation===== |
- | In the previous program we used **sqrt()** against all the values, even or odd. | + | For simplicity, I have encoded important implementation information into the file name (and therefore resulting executable/ |
- | This program | + | To break it down, all prime programs |
+ | * primeALG[O...] | ||
+ | * where each and every program starts with " | ||
+ | * is immediately followed by a 3-letter (lowercase) abbreviation of the algorithm to be implemented (**reg**, for instance) | ||
+ | * and then is followed by 0 or more layered attributes describing | ||
- | This program should | + | The optimizations we will be implementing in this project (and their naming values) include: |
+ | * **break on composite (b)** - once a tested number is proven composite, there is no need to continue processing: break out of the factor loop and proceed to the next number | ||
+ | * **mapping factors of 6 (m)** - it turns out that, aside from the initial primes of **2** and **3**, that **all** prime numbers fall to a +1 or -1 off a factor of six (there is an algorithm for this: **6a+/ | ||
+ | | ||
+ | * **sqrt() trick (s)** - mathematically it has been shown that if a number has any evenly divisible factors, at least one half of that factor pair will occur by the square root point of the number being tested. | ||
+ | * **sqrt()-less square root approximation (a)** - **sqrt()**, a function in the math library, does an industrial strength square root calculation. We don't need that, merely a whole integer value corresponding to the approximate square root. Here we will implement our own logic to approximate square root, hopefully with a considerable performance impact. | ||
- | ====sqrt()-less square root (primesqrtopt)==== | + | Unless specified in the encoded name, your algorithm should only implement the algorithm and optimization(s) specified. |
+ | |||
+ | That is, if your program to implement is **primerego**, | ||
+ | |||
+ | Some of these optimizations can co-exist easily (break + map, odd + sqrt()), others are partially compatible (map + odd can coexist in a certain form), while others are mutually exclusive (sqrt() and approximated square root conflict). So there are definitely a few combinations that are possible using this scheme. | ||
+ | |||
+ | Here are the variants you'll be implementing for this project: | ||
+ | |||
+ | ====break on composite (primeregb)==== | ||
+ | This optimization to primereg will make but one algorithmic change, and that takes place at the moment of identifying a number as composite. So, if we had our 119 example above, and discovered that 7 was a factor: | ||
+ | |||
+ | There is no further need to check the remaining values, as once we have proven the non-primality of a number, the state is set: it is composite. So be sure to use a **break** statement to terminate the computation loop (how does this impact overall performance??? | ||
+ | |||
+ | Make no other optimizations- this first project is to set up some important base line values that we can use for algorithmic comparison later on. | ||
+ | |||
+ | ====mapping factors of 6 (primeregm)==== | ||
+ | This optimization will check only the numbers that fall on either side of a factor of 6 for primality. | ||
+ | |||
+ | NOTE: If applicable, just display the initial " | ||
+ | |||
+ | ====odds-only checking (primerego)==== | ||
+ | This optimization will check only the odd numbers for primality, skipping the evens entirely. | ||
+ | |||
+ | NOTE: If applicable, just display the initial " | ||
+ | |||
+ | ====sqrt() trick (primeregs)==== | ||
+ | This optimization employs the square root trick utilizing the C library' | ||
+ | |||
+ | ====sqrt()-less square root approximation | ||
+ | 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. | 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. | ||
Line 101: | Line 175: | ||
</ | </ | ||
- | Square root of 81 is 9, but so is the square root of 82, 83, 84... etc. up until we hit 100. | + | Or, if perhaps we instead order by square root value: |
- | If we were checking | + | < |
+ | 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 | ||
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. | 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. | ||
Line 111: | Line 197: | ||
* approximation can be powerful | * approximation can be powerful | ||
* approximation can result in a simpler algorithm, improving runtime | * 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 big difference in runtime performance. | + | * **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. |
- | NOTE: Again, for comparison sake, check ALL numbers | + | 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, |
+ | =====Programs===== | ||
+ | It is your task to write the following prime number variants: | ||
- | This program should be an optimization | + | * **primeregb.c**: |
+ | | ||
+ | * **primerego.c**: tests specifically the odd traversal | ||
+ | * **primeregs.c**: | ||
+ | * **primerega.c**: | ||
- | ====sqrt()-less odds (primesqrtoptodds)==== | ||
- | And, to round out our analysis, enhance the optimized sqrt variant to only check odd values. | ||
- | This program should be an optimization | + | ====Program Specifications==== |
+ | Your program | ||
+ | * obtain 2-4 parameters from the command-line (see **command-line arguments** section below). | ||
+ | * check to make sure the user indeed supplied enough parameters, and exit with an error message if not. | ||
+ | * argv[1]: maximum quantity of primes to calculate (your program should run until it discovers **that** many primes). | ||
+ | * this value should be an integer value, greater than or equal to 0. | ||
+ | * if argv[1] is 0, disable the quantity check, and rely on provided lower and upper bounds (up to argv[4] would be required in this case). | ||
+ | * argv[2]: reserved for future compatibility; | ||
+ | * argv[3]: **conditionally optional** lower bound (starting value). Most of the time, this will probably be **2**, but should be a positive integer greater than or equal to 2. This defines where your program will start its prime quantity check from. | ||
+ | | ||
+ | * if you desired to specify an upper bound (argv[4]), you obviously MUST provide the lower bound argument under this scheme. | ||
+ | * argv[4]: **conditionally optional** upper bound (ending value). If provided, this is the ending value you'd like to check to. | ||
+ | * If doing a quantity run (argv[1] is NOT 0), this value isn't necessary. | ||
+ | * If doing a quantity run AND you specify an upper bound, whichever condition is achieved first dictates | ||
+ | * for each argument: you should do a basic check to ensure the user complied with this specification, | ||
+ | * for insufficient quantity of arguments, display: **PROGRAM_NAME: | ||
+ | * for invalid argv[1], display: **PROGRAM_NAME: | ||
+ | * for invalid argv[2], display: **PROGRAM_NAME: | ||
+ | * for invalid argv[3], display: **PROGRAM_NAME: | ||
+ | * if argv[3] is not needed, ignore (no error displayed not forced exit, as it is acceptable defined behavior). | ||
+ | * for invalid argv[4], display: **PROGRAM_NAME: | ||
+ | * if argv[4] is not needed, ignore (no error displayed nor forced exit, as it is acceptable defined behavior). | ||
+ | * In these error messages, **PROGRAM_NAME** is the name of the program being run; this can be accessed as a string stored in **argv[0]**. | ||
+ | * implement ONLY the algorithm and optimization(s) specified in the program name. We are producing multiple data points for a broader performance comparison. | ||
+ | * please take note on differences in run-time, contemplating the impact the algorithm and optimization(s) have on performance (timing, specifically). | ||
+ | * immediately after argument processing: start your stopwatch (see **timing** section below). | ||
+ | * perform the correct algorithm and optimization(s) against the command-line input(s) given. | ||
+ | * each program is to have no fewer and no more than 2 loops in this prime processing section. | ||
+ | * display identified primes (space-separated) to **STDOUT** | ||
+ | * stop your stopwatch immediately following your prime processing loops (and terminating newline display to **STDOUT**). Calculate the time that has transpired (ending time minus starting time). | ||
+ | * output the processing run-time to **STDERR** | ||
+ | * your output **MUST** conform to the example output in the **execution** section below. This is also a test to see how well you can implement to specifications. Basically: | ||
+ | * as primes are being displayed, they are space-separated (first prime hugs the left margin), and when all said and done, a newline is issued (to **STDOUT**). | ||
+ | * the timing information will be displayed in accordance to code I will provide below (see the **timing** section). | ||
- | =====Program===== | + | =====Grabit Integration===== |
- | It is your task to write some optimized prime number calculating programs: | + | I have made some skeleton files and a custom **Makefile** available for this project. Since we've amassed considerable experience manually compiling our files, it is time to start experiencing |
- | - **primeodds.c**: checking only odd values | + | I have written a tool, known as **grabit**, which will let you obtain the files I have put together |
- | - **primesqrt.c**: | + | |
- | - **primesqrtodds.c**: **sqrt()**-based implementation only checking odds | + | |
- | - **primesqrtopt.c**: | + | |
- | - **primesqrtoptodds.c**: | + | |
- | Your program should: | + | < |
- | * obtain 1 parameter from the command-line (see **command-line arguments** section below): | + | lab46: |
- | * argv[1]: maximum value to calculate | + | make: Entering directory '/ |
- | * this value should be a positive integer value; you can make the assumption | + | Commencing copy process for SEMESTER cprog project pnc1: |
- | * do the specified algorithmic optimizations | + | -> Creating project pnc1 directory tree ... OK |
- | * please take note in differences in run-time, contemplating the impact | + | -> Copying pnc1 project files ... OK |
- | * start your stopwatch (see **timing** section below): | + | -> Synchronizing pnc1 project revision level ... OK |
- | * perform the correct algorithm against the input | + | -> Establishing sane file permissions for pnc1 ... OK |
- | * display (to STDOUT) the prime numbers found in the range | + | |
- | * output the processing run-time to STDERR | + | *** Copy COMPLETE! You may now go to the '/ |
- | * your output | + | |
- | * as primes are being displayed, they are space-separated (first prime hugs the left margin), and when all said and done, a newline is issued. | + | make: Leaving directory '/ |
- | * the timing information | + | lab46:~/ |
+ | </ | ||
+ | |||
+ | NOTE: You do NOT want to do this on a populated pnc1 project directory-- it will overwrite files. | ||
+ | |||
+ | And, of course, your basic compile and clean-up operations via the Makefile. | ||
+ | |||
+ | =====Makefile operations===== | ||
+ | Makefiles provide | ||
+ | |||
+ | Basic operation of the Makefile is invoked by running the command "**make**" | ||
+ | |||
+ | Additional options are available, and they are provided as an argument to the make command. You can see the available options by running "**make help**": | ||
+ | |||
+ | < | ||
+ | lab46: | ||
+ | *******************[ C/C++ Programming pnc1 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 | ||
+ | * any **warnings** or **errors** generated by the compiler will go into a file in the base directory of pnc1 in a file called | ||
+ | * **make debug**: compile everything with debug support | ||
+ | * any **warnings** or **errors** generated by the compiler | ||
+ | | ||
+ | * **make save**: make a backup of your current work | ||
+ | * **make submit**: archive and submit your project | ||
+ | |||
+ | 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 164: | Line 338: | ||
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 169: | Line 348: | ||
* argv[0]: program invocation (path + program name) | * argv[0]: program invocation (path + program name) | ||
* argv[1]: our maximum / upper bound | * argv[1]: our maximum / upper bound | ||
+ | * argv[2]: reserved value, should still be provided and be a 1 for this project | ||
+ | * argv[3]: conditionally optional; represents lower 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==== | ||
- | Although I'm not going to require extensive argument parsing or checking for this project, | + | While there are a number of checks |
<code c> | <code c> | ||
- | if (argc < 2) // if less than 2 arguments have been provided | + | if (argc < 3) // if less than 3 arguments |
{ | { | ||
- | fprintf(stderr, | + | fprintf(stderr, |
exit(1); | exit(1); | ||
} | } | ||
</ | </ | ||
+ | |||
+ | Since argv[3] (lower bound) and argv[4] (upper bound) are conditionally optional, it wouldn' | ||
====Grab and convert max==== | ====Grab and convert max==== | ||
- | Finally, we need to put the argument representing the maximum | + | Finally, we need to put the argument representing the maximum |
I'd recommend declaring a variable of type **int**. | I'd recommend declaring a variable of type **int**. | ||
Line 189: | Line 396: | ||
<code c> | <code c> | ||
- | max = atoi(argv[1]); | + | max = atoi (argv[1]); |
</ | </ | ||
Line 238: | Line 445: | ||
====Displaying the runtime==== | ====Displaying the runtime==== | ||
- | Once we having | + | Once we have the starting and ending times, we can display this to the **timing** file pointer. You'll want this line: |
<code c> | <code c> | ||
- | | + | fprintf(stderr, |
+ | time_end.tv_sec-time_start.tv_sec+((time_end.tv_usec-time_start.tv_usec)/ | ||
</ | </ | ||
- | For clarity sake, that format specifier is "%10.6lf", where the " | + | For clarity sake, that format specifier is "%8.4lf", where the " |
And with that, we can compute an approximate run-time of our programs. The timing won't necessarily be accurate down to that level of precision, but it will be informative enough for our purposes. | And with that, we can compute an approximate run-time of our programs. The timing won't necessarily be accurate down to that level of precision, but it will be informative enough for our purposes. | ||
=====Execution===== | =====Execution===== | ||
- | Your program output should be as follows (given the specified | + | |
+ | ====specified quantity==== | ||
+ | Your program output should be as follows (given the specified | ||
<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.000088 | + | 0.0001 |
lab46: | lab46: | ||
</ | </ | ||
The execution of the programs is short and simple- grab the parameters, do the processing, produce the output, and then terminate. | The execution of the programs is short and simple- grab the parameters, do the processing, produce the output, and then terminate. | ||
+ | |||
+ | ====invalid lower bound==== | ||
+ | Here's an example that should generate an error upon running (based on project specifications): | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | ./primereg: invalid lower bound | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | In this case, the program logic should have detected an invalid condition and bailed out before prime computations even began. No timing data is displayed, because exiting should occur even prior to that. | ||
+ | |||
+ | ====upper bound overriding quantity==== | ||
+ | As indicated above, there is potential interplay with an active quantity and upper bound values. Here is an example where upper bound overrides quantity, resulting in an early termination (ie upper bound is hit before quantity): | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | 7 11 13 17 19 23 | ||
+ | 0.0001 | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | Also for fun, I set the lower bound to 7, so you'll see computation starts at 7 (vs. the usual 2). | ||
=====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 **primesqrt**, | + | In order to work, you **MUST** be in the directory where your pnc1 binaries reside, and must be named as such (which occurs if you ran **make** to compile them). |
- | For instance (running on my implementations | + | ====check qty==== |
+ | For instance (running on my implementation | ||
<cli> | <cli> | ||
- | lab46: | + | lab46: |
- | ==================================================================================================== | + | ========================================================= |
- | | + | |
- | ==================================================================================================== | + | ========================================================= |
- | | + | 32 |
- | | + | 64 |
- | | + | |
- | 1024 0.005385 | + | |
- | 2048 0.019076 | + | |
- | 4096 0.070836 | + | 1024 |
- | 8192 0.270955 | + | 2048 |
- | 16384 1.059970 | + | |
- | | + | 8192 |
- | 65536 ---------- | + | |
- | | + | |
- | 262144 | + | |
- | 524288 | + | 131072 |
- | | + | ========================================================= |
- | | + | |
- | 4194304 | + | ========================================================= |
- | 8388608 | + | |
- | ==================================================================================================== | + | |
- | | + | |
- | ==================================================================================================== | + | |
lab46: | lab46: | ||
</ | </ | ||
- | For evaluation, each test is run 4 times, and the resulting time is averaged. During development, | + | ====check range==== |
+ | Or check range runtimes: | ||
+ | |||
+ | < | ||
+ | lab46: | ||
+ | ========================================================= | ||
+ | range | ||
+ | ========================================================= | ||
+ | | ||
+ | | ||
+ | 128 0.0001 | ||
+ | 256 0.0002 | ||
+ | 512 0.0006 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 16384 0.5402 | ||
+ | 32768 2.1530 | ||
+ | 65536 ------ | ||
+ | | ||
+ | | ||
+ | | ||
+ | 1048576 | ||
+ | 2097152 | ||
+ | ========================================================= | ||
+ | | ||
+ | ========================================================= | ||
+ | lab46: | ||
+ | </ | ||
- | If the runtime of a particular prime variant exceeds an upper threshold (likely to be set at 2 seconds), it will be omitted from further tests, and a series of dashes will instead appear in the output. | + | If the runtime of a particular prime variant exceeds an upper runtime |
- | If you don't feel like waiting, simply hit **CTRL-c** and the script will terminate. | + | If you don't feel like waiting, simply hit **CTRL-c** |
+ | ====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 regs rega | ||
+ | ========================================================= | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | coop: 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. | ||
=====Submission===== | =====Submission===== | ||
Line 309: | Line 633: | ||
* Output must be correct, and match the form given in the sample output above. | * Output must be correct, and match the form given in the sample output above. | ||
* Code must be nicely and consistently indented (you may use the **indent** tool) | * Code must be nicely and consistently indented (you may use the **indent** tool) | ||
- | * Code must utilize the algorithm(s) presented above. | + | * Code must utilize the algorithm(s) presented above: |
- | * **primeodds.c** | + | * **primeregb.c** adds in the break on composite optimization |
- | * **primesqrt.c** | + | * **primeregm.c** implements the map traversal |
- | * **primesqrtodds.c** | + | * **primerego.c** implements odds-only checking |
- | * **primesqrtopt.c** | + | * **primeregs.c** implements the sqrt() trick |
- | * **primesqrtoptodds.c** | + | * **primerega.c** implements square root trick by approximating square root |
* Code must be commented | * Code must be commented | ||
* have a properly filled-out comment banner at the top | * have a properly filled-out comment banner at the top | ||
Line 326: | Line 650: | ||
<cli> | <cli> | ||
- | $ submit | + | lab46: |
+ | removed ‘primeregb’ | ||
+ | removed ‘primeregm’ | ||
+ | removed ‘primerego’ | ||
+ | removed ‘primeregs’ | ||
+ | removed ‘primerega’ | ||
+ | removed ‘errors’ | ||
+ | |||
+ | Project backup process commencing | ||
+ | |||
+ | Taking snapshot of current project (pnc1) | ||
+ | Compressing snapshot of pnc1 project archive | ||
+ | Setting secure permissions on pnc1 archive | ||
+ | |||
+ | Project backup process complete | ||
Submitting cprog project " | Submitting cprog project " | ||
- | -> primeodds.c(OK) | + | -> ../pnc1-20171025-17.tar.gz(OK) |
- | -> primesqrt.c(OK) | + | |
- | | + | |
- | | + | |
- | -> primesqrtoptodds.c(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 " |
- | What I will be looking for: | + | ====Evaluation Criteria==== |
+ | Grand total points: | ||
< | < | ||
- | 78:pnc1:final tally of results (78/78) | + | 80:pnc1:final tally of results (80/80) |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
- | *: | + | |
</ | </ | ||
+ | |||
+ | What I will be looking for (for each file): | ||
+ | |||
+ | < | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | </ | ||
+ | |||
+ | As the optimizations improve upon others, some evaluations will be based upon differences between a baseline (in some cases, primereg) and the optimization. | ||
+ |