1
0

README 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // ~ Introduction ~ //
  2. So far we have only considered computer programs that try to do one
  3. thing at a time. In this programming assignment we will learn about
  4. computer programs that do muliple things at the same time. This is
  5. referred to as "threading". For example, a sufficiently smart computer
  6. program could optimize its time by watching important TV shows on HBO,
  7. while simultaneously doing the ironing. This example is not entirely
  8. glib. In this case we are talk about two different simultaneous tasks
  9. (TV and ironing), and each executing task is referred to as a thread.
  10. There are many reasons why threading is important. For this course we
  11. will consider speed. Although computers are still getting faster,
  12. today they are really only getting better at doing multiple things at
  13. the same time. (The execution of single threaded programs is not
  14. getting much faster.) To extend our analogy, if we have lots of
  15. ironing to do, we cannot build a faster robot to do the ironing. The
  16. speed of the robot is maxed out. But we can set up multiple robots on
  17. multiple ironing boards to do the work simultaneously. Ten robots (ten
  18. threads) would mean (roughly speaking) ten times as much ironing.
  19. In general, you rarely get ten times the speed for using ten times as
  20. many threads. The problem is that the threads will need to communicate
  21. with each other, and that slows everything down. To extend our
  22. analogy, imagine that our ironing robots are getting their tasks
  23. (particularly wrinkly shirts) from a big bin full of fresh laundry. If
  24. the robots do not communicate with each other, then we could imagine a
  25. situation where two robots attempt to pick up the same shirt at the
  26. same time -- probably tearing it in half. The chances of this
  27. happening may be small, but non-zero. That means, firstly, that the
  28. bug of a shirt being so-torn is very difficult to reproduce. If a bug
  29. like this happens in your computer program, it can be almost
  30. impossible to track down. Secondly, it means that all your robots need
  31. to communicate with each other to get the job done.
  32. In this assignment, we will create some threads (robots) to identify
  33. whether or not a large number is prime (do the laundry). The threads
  34. will each work on part of the task (break the laundry up into
  35. piles). Normally you would need to consider how to make threads work
  36. nicely together; however, this assignment has been carefully written
  37. so that you do not need to do that. (If done straight-forwardly,
  38. robots will never tear shirts in this assignment.) It is important to
  39. keep in mind that threading is difficult to get right, and to do so,
  40. you must learn how to synchronize the activities of your threads.
  41. // ~ Determining if a Number is Prime ~ //
  42. This is an ongoing area of research. For this assignment we will use a
  43. very simple approach. Please read the following webpage on factorizing
  44. prime numbers:
  45. http://cartera.me/2012/01/10/primality-testing-and-factorization-in-c/
  46. In this assignment, we will be using the
  47. "primalityTestWithOnlyOddsAndSqrtLimit" function from the above
  48. page. For your convenience, it is displayed below:
  49. int primalityTestWithOnlyOddsAndSqrtLimit(long int n)
  50. {
  51. if (n % 2 == 0)
  52. return 0;
  53. long int max = floor(sqrt(n));
  54. long int i;
  55. for (i = 2; i < max; i++)
  56. {
  57. if (n % ((2 * i) + 1) == 0)
  58. return 0;
  59. }
  60. return 1;
  61. }
  62. You will need to modify this function to make it
  63. parallel. Furthermore, this function deals with puny "long int"
  64. numbers. We'll want more juice than that. So you will also have to
  65. modify this function to use 128 bit integers.
  66. There are some other problems with this function too. (Lesson: do not
  67. believe everything you read on the interwebs.) Can you see the
  68. problems? For starters, this function says that 2 isn't a prime
  69. number! Secondly, (and more perniciously), Carter Allen does not
  70. handle the 'i' in the for loop correctly. For example, imagine that
  71. you are testing if 83 is prime. In this case max = floor(sqrt(83)) ==
  72. 9, which is correct. Now you want to test every odd number between 3
  73. and 9 inclusive, but that is not what Carter Allen has done! You will
  74. need to fix this bug.
  75. The webpage above (correctly) explains the very *very* simple
  76. mathematics behind this function.
  77. // ~ What you need to do :: Preliminaries ~ //
  78. We will be testing LARGE prime numbers. These will be 128 bit
  79. integers, which are far too large to fit in measly 32-bit or 64-bit
  80. int variables. A 32-bit int variable can only contain values from 0 to
  81. 4 294 967 295, but with unsigned 128 bit numbers, you can go all the
  82. way to 340 282 366 920 938 463 463 374 607 431 768 211 455. (If you
  83. need that extra push over the cliff, then you what we do? See:
  84. http://www.youtube.com/watch?v=EbVKWCpNFhY)
  85. GCC has a native 128-bit integer type called "__int128". In this
  86. assignment we are interested in /unsigned/ integers, and so we will be
  87. using the type "unsigned __int128". There is a typedef statement in
  88. pa06.h that defines a shorthand notation "uint128"
  89. typedef unsigned __int128 uint128;
  90. There is no built in functions to print these integers, or to convert
  91. strings into these integers. The following code converts a base-10
  92. character string into an unsigned 128 bit integer. It is provided for
  93. your convenience:
  94. uint128 alphaTou128(const char * str)
  95. {
  96. uint128 value = 0;
  97. while(*str >= '0' && *str <= '9') {
  98. value *= 10; // "left-shift" a base-ten number
  99. value += (*str - '0'); // add in the units
  100. ++str;
  101. }
  102. return value;
  103. }
  104. You must write your own function for printing unsigned 128-bit
  105. numbers. If you examine the code for alphaTou128(...), then it should
  106. become apparent how to do this.
  107. // Example: writing out a Biiigggg number
  108. int main(int argc, char * * argv)
  109. {
  110. uint128 w = alphaTou128("340282366920938463463374607431768211455");
  111. char * w_str = u128ToString(w); /* You need to write this!!! */
  112. printf("Biiigggg number: %s\n", w_str);
  113. free(w_str);
  114. return EXIT_SUCCESS;
  115. }
  116. Before continuing with the assignment, you need to complete the
  117. function u128ToString(w).
  118. // ~ What you need to do :: Testing primality ~ //
  119. As Carter Allen notes on his webpage, the function
  120. primalityTestWithOnlyOddsAndSqrtLimit lends itself to
  121. parallelism. Simple put, if you want to test if a number is divisible
  122. (exactly) by numbers in the range [0..n], then you can split this
  123. range into chunks, and test each chunk in a separate thread. If /any/
  124. chunk of the computation finds a factor, then the number is not prime.
  125. For his threaded version, Carter Allen used "Grand Central Dispatch",
  126. which is a relatively new Apple technology. Grand Central Dispatch
  127. cuts down on some of the work involved in writing threaded
  128. programs. For this assignment we will use the trusty pthreads library
  129. instead of Grand Central Dispatch. pthreads is the standard threading
  130. library available on all flavors of unix (including OS X and Linux),
  131. and is also available in some form on windows. One key reason for
  132. using pthreads is that, in my experience, it is not worth learning a
  133. shiny simplified technology until you understand a bit about the
  134. underlying machinery. So learning pthreads is a very good idea.
  135. Feel free to examine Carter Allen's code; however, you will not be
  136. able to compile it on the ece computers. Examining his code may help
  137. you understand how to create a threaded version using pthreads;
  138. however, many important details are missing.
  139. // ~ How to Compile, Run and Test ~ //
  140. In this assignment you are provided with a minimal make file, and a
  141. skeleton parallel-primes.c files. Use the make command to compile your
  142. program. You can run the program like so:
  143. ./pa06
  144. This will print a help message that should be self-explanatory. To
  145. test if the number '2' is prime, using '1' thread, you would execute
  146. the program as follows:
  147. ./pa06 2 1
  148. To run under valgrind, you would use the following command:
  149. valgrind --tool=memcheck --leak-check=full --verbose \
  150. --log-file=outputs/memoutput ./pa06 2 1
  151. You must then examine the file outputs/memoutput yourself to see if
  152. there are any memory leaks.
  153. For your convenience, the inputs folder contains two files. One file
  154. contains 437 prime numbers. The other file contains 2116 non-prime
  155. numbers. Your program should correctly detect all 437 primes as prime,
  156. and all 2116 non-primes as non-prime.
  157. NO SIMPLIFIED TESTING REGIME IS PROVIDED.
  158. You must figure out the best way to do this yourself.
  159. DO NOT LEAVE THIS TO THE LAST MINUTE.
  160. // ~ Why Prime Numbers ~ //
  161. You might know that modern cryptography relies on the fact that no-one
  162. knows how to efficiently determine whether or not a number is
  163. prime. If someone ever figures this out, then they may well be the
  164. subject of a real-life spy novel. This was the theme of the 90s hit
  165. film "Sneakers".
  166. But as far as prime numbers go, cryptography is the tip of the
  167. iceberg. If math has anything to do with the universe, then prime
  168. numbers have a special place in how it ticks. These special numbers
  169. have been the subject of extensive investigation by mathematicians for
  170. all of recorded history, yet they still present many mysteries. Their
  171. universality lead the great American scientist, Carl Sagan, to suggest
  172. that if aliens were to communicate with humans, then they could use
  173. prime numbers to capture our attention. This idea is the centre-piece
  174. of Sagan's only work of fiction, the best selling novel, "Contact",
  175. which has since been made into a film.
  176. Please watch this short video.
  177. http://www.youtube.com/watch?v=8qDjg8mdd8c
  178. So Jodie Foster thinks that primes are cool, okay?