Homework 4

Computer Architecture I ShanghaiTech University
HW3 HW4 HW5

Prime Numbers

Your task is to write a C program (primes.c) to calculate the prime numbers. The test if a number is a prime number has to be done in a certain number of worker pthreads. The number of worker threads that your program should use will be given as the first command line argument.
The result has to be saved in our doubly linked list from HW1. Data will be of size sizeof(int) and represent an int - the found prime number. We would like the list to be ordered - biggest numbers first. Therefore you have to implement a special insert method that uses a pointer to a comparison function to compare the values and can thus find the right place to insert a new value into the list.
The doubly linked list from HW1 will be provided as a library - in 64bit binary for Ubuntu: libdoubll.a (same doubll.h as in HW 1). You have to implement said function insert_list_sorted as defined below. There is another new function called "testPrimeList". Once your calculations have all finished call this function with the sorted list to start the grading. In the library provided to you this function does nothing. In the library on the gradebot it will do the grading.
Your program calculates as many prime numbers as possible. It will only finish the calculation once it receives the signal SIGINT (Ctrl-C). So you have to write a signal handler that catches that signal and tells all worker threads to finish processing. The worker threads can finish the check of the current number. Your main thread should wait for all threads to finish and then run the test method mentioned above.

Tasks (all in primes.c)

Grading

Grading will be performed mostly by the gradebot. We will check by hand for (obvious) memory leaks, other obvious bugs and for proper comments.
Three days after the due date we will run all your programs on a high performance workstation for a fixed amount of time. We will record the number of primes you found. The fastest submission will get 200% grade for this homework. We will also calculate the median over all submissions of the number of primes found. Everybody with a number equal or below the median will get 100%. Anybody between the fastest and the median will get a lineary scaled score between 200% and 100%.

Notes