0% found this document useful (0 votes)
57 views2 pages

Spr22 CIS545 Sect52 Programming Assignment 1: N M N M M

This document describes two programming assignments that involve concurrently calculating the sum of quadruple roots from 1 to n using m computational threads. For the first program thread_atomic.c, each thread computes a partial sum that it atomically adds to a shared global variable and prints. For the second program thread_reduce.c, threads store partial sums in a shared array and perform a parallel reduction to combine the sums by having threads join and add array elements together over multiple steps. Students are instructed to submit their work by creating a project folder, adding source code and README, and using scp and turnin commands to upload it.

Uploaded by

Aniket
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views2 pages

Spr22 CIS545 Sect52 Programming Assignment 1: N M N M M

This document describes two programming assignments that involve concurrently calculating the sum of quadruple roots from 1 to n using m computational threads. For the first program thread_atomic.c, each thread computes a partial sum that it atomically adds to a shared global variable and prints. For the second program thread_reduce.c, threads store partial sums in a shared array and perform a parallel reduction to combine the sums by having threads join and add array elements together over multiple steps. Students are instructed to submit their work by creating a project folder, adding source code and README, and using scp and turnin commands to upload it.

Uploaded by

Aniket
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Spr22 CIS545 Sect52 Programming Assignment 1

In this project, you are asked to write two independent programs, thread_atomic.c and
thread_reduce.c, each of which uses m computational threads to concurrently calculate the sum of
quadruple roots from 1 to n, where m and n are powers of 2 and are specified in the command line.

For the program thread_atomic.c, it gets the values m and n from the command line arguments and
converts them to two integers, respectively. Next, it creates m threads using pthread_create() and each
n
thread computes the sum of quadruple roots. Namely, the first thread (i.e. thread 0) computes the
m
n
sum of quadruple roots from 1 to , the second thread (i.e. thread 1) computes the sum of the
m
2n
quadruple roots from m+1 to , etc.
m
When a thread finishes its computation, it should print its partial sum and atomically add it to a shared
global variable. Note that your program needs to use pthread_barrier_wait() to let the main
thread know that all of the m computational threads have done the atomic additions and hence it can
print the result. Below is an example of running your thread program:

bach> ./thread_atomic 2 65536


thr 0: 352703.926537
thr 1: 486164.553017
sum of quadruple roots: 838868.479554

The program thread_ reduce.c is similar to thread_atomic.c except that you need to use the parallel
reduction approach to combine the partial sums. That is, your program uses a shared global array and
each computational thread just stores, but not prints, its partial sum in an array element indexed on its
thread ID. Then, half of these threads call pthread_join() to wait for their corresponding partner
threads completion, and then each of these threads can add two numbers in the array together. This
reduction procedure will be performed log(m) times and each time the number of the active threads will
be reduced half. Finally, there will be only one active thread and this thread should print the whole
array.

For example, assume that there are 8 computational threads. In the first reduction step, in order to add
two elements in the array, thread 4 should wait for thread 0 done, thread 5 has to wait for thread 1
done, threads 6 needs to wait for thread 2 done, and thread 7 should wait for thread 3 done. In the
second reduction step, thread 6 waits for thread 4 finished, and thread 7 waits for thread 5 finished. In
the third step, thread 7 waits for thread 6 done and then prints the the whole array. Hint: to find its
partner thread ID during the ith reduction step, a thread can use its ID XORed with 2r-i, where r = log(m).
Note that the main thread just calls pthread_exit() after creating m threads. It does not need to wait for
these threads. Calling pthread_exit() in the main thread will allow other threads to continue execution.

Your report should include the description of your code, experiences in debugging and testing, etc. The
cover page should contain your picture(s), name(s) and the login id you used to turnin the project. Start
on time and good luck. If you have any questions, send e-mail to y.zheng65@csuohio.edu.

Below is an example of how students should submit an assignment:


1. Create a folder and name it as your [first name]_[last name]_proj1 ($ mkdir
john_smith_proj1) 
2. Copy all your source code to the above folder (clean your source code folder and
remove all binary files). 
3. Edit a README.txt file (in text format only) and provide the following information: 
o Project description 
o Compiling instruction and execution instruction 
o A sample test run 
o Existing bugs (things not finished)

4. scp the folder (e.g., john_smith_proj1) to your account in grail. 


5. SSH log in grail, go to the parent directory of the folder you created, and run (suppose
your folder is "john_smith_proj1") 
turnin -c cis545h -p proj1 john_smith_proj
6. If there is no error message, your submission is successful. 
7. You can submit more than one time. Your most recent submission will always
automatically overwrite the previous one. 

You might also like