{"id":23382,"date":"2018-02-20T23:50:30","date_gmt":"2018-02-20T23:50:30","guid":{"rendered":"https:\/\/writemyessayfree.com\/sieve-of-eratosthenes-with-pthreads-c"},"modified":"2017-08-03T16:10:34","modified_gmt":"2017-08-03T16:10:34","slug":"sieve-of-eratosthenes-with-pthreads-c","status":"publish","type":"post","link":"https:\/\/www.benedictsol.com\/blogs\/sieve-of-eratosthenes-with-pthreads-c\/","title":{"rendered":"Sieve of Eratosthenes with pthreads, C"},"content":{"rendered":"<div class=\"the_content_wrapper\">\n<p>Sieve of Eratosthenes with pthreads, C<\/p>\n<p>In this assignment, you will write a multi-threaded C program to find all prime numbers up to N using Sieve Eratosthenes Algorithm (where N is an integer greater than<br \/> 1). To find the prime numbers up to N, your program will use Pthreads library to implement a pipelined parallelism model. Your program will read N as a command line<br \/> argument (Usage: .\/minor4 [N]). See references [1-5] and sample outputs for examples. The pipeline model is based directly on the same work model that is used in CPUs<br \/> and on factory floors. Each processing element will do a certain amount of the job, and then pass the partially completed task on to the next element. Here the<br \/> processing elements are threads, and each thread is going to do a portion of the task, then pass the partial results on to the next thread. A pipelined model of<br \/> parallelism, is where: \u2022 A given thread T0 performs some computation and sends results to thread T1 \u2022 Then, T1 computes its computation and passes results to T2, and<br \/> so on. The Sieve Eratosthenes algorithm is expressed as follow: PROGRAM PARTIAL PSEUDOCODE : Using a pipelined parallelism approach: \u2022 Each thread keeps the next prime<br \/> number \u2022 And if a new number is not divisible by the prime at Ti send it to Ti+1 Thread T0 starts reading numbers, and checks if the number is divisible by 2 (the<br \/> first prime number). If the number is not divisible, it is passed to T1. T1 will have the second prime number. T1 passes only the numbers that are not divisible to T2<br \/> and so on. 1. Create list of unmarked natural numbers 2, 3, \u2026, n 2. k = 2 is the first in the list 3. Repeat (a) Mark all multiples of k between k2 and n (b) set k to<br \/> the smallest unmarked number &gt; k until k2 &gt; n 4. The unmarked numbers are primes 2 An example of finding prime numbers up to N=61 using Sieve algorithm graphically is<br \/> shown as follow: REQUIREMENTS: \u2022 Your code should be well documented in terms of comments. For example, good comments in general consist of a header (with your name,<br \/> course section, date, and brief description), comments for each variable, and commented blocks of code. \u2022 Your program should correctly implement the Sieve algorithm<br \/> (35 pts), along with proper implementation of the Pthreads library functions (35 pts), and Pipeline model (20 pts). \u2022 Your program should be named \u201cminor4.c\u201d, without<br \/> the quotes. \u2022 Your program will be graded based largely on whether it works correctly on the CSE machines (e.g., cse01, cse02, \u2026, cse06), so you should make sure that<br \/> your program compiles and runs on a CSE machine. \u2022 This is an individual programming assignment that must be the sole work of the individual student. SUBMISSION: \u2022 You<br \/> will electronically submit your program to the Minor Assignment 4 dropbox in Blackboard by the due date<\/p>\n<p>1<br \/> CSCE 3600: Systems Programming<br \/> Minor Assignment 4 \u2013 Sieve of Eratosthenes (100 pts)<br \/> Due: 11:59 PM on Sunday, April 16, 2017<br \/> PROGRAM DESCRIPTION:<br \/> In this assignment, you will write a multi-threaded C program to find all prime numbers up<br \/> to N using Sieve Eratosthenes Algorithm (where N is an integer greater than 1). To find<br \/> the prime numbers up to N, your program will use Pthreads library to implement a<br \/> pipelined parallelism model. Your program will read N as a command line argument<br \/> (Usage: .\/minor4 [N]). See references [1-5] and sample outputs for examples.<br \/> The pipeline model is based directly on the same work model that is used in CPUs and<br \/> on factory floors. Each processing element will do a certain amount of the job, and then<br \/> pass the partially completed task on to the next element. Here the processing elements<br \/> are threads, and each thread is going to do a portion of the task, then pass the partial<br \/> results on to the next thread.<br \/> A pipelined model of parallelism, is where:<br \/> \u2022 A given thread T0 performs some computation and sends results to thread T1<br \/> \u2022 Then, T1 computes its computation and passes results to T2, and so on.<br \/> The Sieve Eratosthenes algorithm is expressed as follow:<br \/> PROGRAM PARTIAL PSEUDOCODE :<br \/> Using a pipelined parallelism approach:<br \/> \u2022 Each thread keeps the next prime number<br \/> \u2022 And if a new number is not divisible by the prime at Ti send it to Ti+1<br \/> Thread T0 starts reading numbers, and checks if the number is divisible by 2 (the first<br \/> prime number).<br \/> If the number is not divisible, it is passed to T1.<br \/> T1 will have the second prime number. T1 passes only the numbers that are not<br \/> divisible to T2 and so on.<br \/> 1. Create list of unmarked natural numbers 2, 3, \u2026, n<br \/> 2. k = 2 is the first in the list<br \/> 3. Repeat<br \/> (a) Mark all multiples of k between k2<br \/> and n<br \/> (b) set k to the smallest unmarked number &gt; k<br \/> until k2<br \/> &gt; n<br \/> 4. The unmarked numbers are primes<br \/> 2<br \/> An example of finding prime numbers up to N=61 using Sieve algorithm graphically is<br \/> shown as follow:<br \/> REQUIREMENTS:<br \/> \u2022 Your code should be well documented in terms of comments. For example, good<br \/> comments in general consist of a header (with your name, course section, date, and<br \/> brief description), comments for each variable, and commented blocks of code.<br \/> \u2022 Your program should correctly implement the Sieve algorithm (35 pts), along with<br \/> proper implementation of the Pthreads library functions (35 pts), and Pipeline<br \/> model (20 pts).<br \/> \u2022 Your program should be named \u201cminor4.c\u201d, without the quotes.<br \/> \u2022 Your program will be graded based largely on whether it works correctly on the CSE<br \/> machines (e.g., cse01, cse02, \u2026, cse06), so you should make sure that your<br \/> program compiles and runs on a CSE machine.<br \/> \u2022 This is an individual programming assignment that must be the sole work of the<br \/> individual student.<br \/> SUBMISSION:<br \/> \u2022 You will electronically submit your program to the Minor Assignment 4 dropbox in<br \/> Blackboard by the due date.<br \/> REFERENCES:<br \/> 1. Pthreads, https:\/\/computing.llnl.gov\/tutorials\/pthreads\/<br \/> 2. Prime numbers, https:\/\/www.khanacademy.org\/math\/pre-algebra\/pre-algebrafactors-multiples\/pre-algebra-prime-numbers\/v\/prime-numbers<br \/> 3. Sieve of Eratosthenes, https:\/\/en.wikipedia.org\/wiki\/Sieve_of_Eratosthenes<br \/> 4. Sieve Eratosthenes, https:\/\/www.khanacademy.org\/computing\/computerscience\/cryptography\/comp-number-theory\/v\/sieve-of-eratosthenes-primeadventure-part-4<br \/> 5. Instruction pipelining, https:\/\/en.wikipedia.org\/wiki\/Instruction_pipelining<br \/> 6. Pipeline, Multithreaded Programming with Pthreads Book by Bil Lewis &amp; Daniel<br \/> J. Berg, ISBN 0-13-680729-1, page 227-228.<br \/> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16<br \/> 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31<br \/> 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46<br \/> 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61<br \/> 2 4 6 8 10 12 14 16<br \/> 18 20 22 24 26 28 30<br \/> 32 34 36 38 40 42 44 46<br \/> 48 50 52 54 56 58 60<br \/> 3 9 15<br \/> 21 27<br \/> 33 39 45<br \/> 51 57<br \/> 5<br \/> 25<br \/> 35<br \/> 55<br \/> 7<br \/> 49<br \/> 3<br \/> SAMPLE OUTPUTS:<br \/> Case1: (5 pts)<br \/> \u2022 $ .\/minor4<br \/> \u2022 $ Usage: .\/minor4 [N]<br \/> Case2: (5 pts)<br \/> \u2022 $ .\/minor4 1<br \/> \u2022 $ [N=1] needs to be greater than 1<br \/> Case3:<br \/> \u2022 $ .\/minor4 2<br \/> \u2022 $ Prime Set of 2 = { 2 }<br \/> Case4:<br \/> \u2022 $ .\/minor4 10<br \/> \u2022 $ Prime Set of 10 = { 2 , 7 , 5 , 3 }<br \/> Case5:<br \/> \u2022 $ .\/minor4 100<br \/> \u2022 $ Prime Set of 100 = { 2 , 97 , 89 , 83 , 79 , 73 , 71 , 67 , 61 , 59 , 53 , 47 , 43 ,<br \/> 41 , 37 , 31 , 29 , 23 , 19 , 17 , 13 , 11 , 7 , 5 , 3 }<br \/> Optional \u2013 Bonus: (10 pts)<br \/> \u2022 Output all found prime numbers sorted in an ascending order<br \/> HINTS:<br \/> Recommended Pthreads functions:<br \/> \u2022 pthread_create, pthread_mutex_lock, pthread_mutex_init,<br \/> pthread_mutex_unlock, pthread_cond_wait, pthread_cond_init,<br \/> pthread_cond_signal, pthread_join, pthread_exit<br \/> Recommended C constructs\/techniques:<br \/> \u2022 structs &amp; pointers to keep tracks of thread information and program information<br \/> sharing, including mutex and conditional variables needed to synchronize the<br \/> pipeline processing.<br \/> \u2022 Helper functions to organize your program codes, and of course a thread<br \/> function.<br \/> \u2022 Recursion (hint: on your defined thread function).<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sieve of Eratosthenes with pthreads, C In this assignment, you will write a multi-threaded C program to find all prime numbers up to N using Sieve Eratosthenes Algorithm (where N is an integer greater than 1). To find the prime <a href=\"https:\/\/www.benedictsol.com\/blogs\/sieve-of-eratosthenes-with-pthreads-c\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-23382","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/23382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/comments?post=23382"}],"version-history":[{"count":0,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/23382\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/media?parent=23382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/categories?post=23382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/tags?post=23382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}