Synchronization, Thread Parallelism and Performance - Homework 2 | CSE 160, Assignments of Computer Science

Material Type: Assignment; Class: Intro to Parallel Computing; Subject: Computer Science & Engineering; University: University of California - San Diego; Term: Spring 2005;

Typology: Assignments

Pre 2010

Uploaded on 03/28/2010

koofers-user-0tr
koofers-user-0tr 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE 160 Chien Homework #2
Page 1 of 4 April 3, 2005
Synchronization, Thread Parallelism, and Performance
Distributed: April 12, 2005
Due: April 21, 2005 in Lecture
For the CSE160 Homework Collaboration Policy: (See HW1 and Course Web site)
All students are expected to comment their code and give a brief description of their
adopted approach. For problem 1 you are not expected to provide running code but
your answers should have enough code (or pseudo code) to give us an idea of how
the problem can be solved in Java. Failing to comply to this can lead to deduction of
points.
Homework Problem 1.a. Look at the basic code for the class given below.
class ProblemA extends Thread
{
Random r;
.
.
public void run()
{
synchronized(sharedVariable)
{
sharedVariable++;
if(r.nextInt()==0)
Thread.currentthread().suspend();
}
}
}
Now imagine a program is running two instances of the ProblemA. Is there a possibility
of a deadlock? If so, describe a sequence of events which leads to deadlock.
Suggest a way to fix this code, preserving its function while removing the deadlock.
Explain in detail why the improved code will not deadlock.
1.b. Look at the following code where a Thread updates the value of a shared variable
until it reaches a given maximum value.
class ProblemB extends Thread
{
.
.
public void run()
{
synchronized(sharedVariable)
{
while(sharedVariable < MAX_VALUE)
{
sharedVariable++;
}
pf3
pf4

Partial preview of the text

Download Synchronization, Thread Parallelism and Performance - Homework 2 | CSE 160 and more Assignments Computer Science in PDF only on Docsity!

Synchronization, Thread Parallelism, and Performance Distributed: April 12, 2005 Due: April 21, 2005 in Lecture For the CSE160 Homework Collaboration Policy : (See HW1 and Course Web site)

All students are expected to comment their code and give a brief description of their adopted approach. For problem 1 you are not expected to provide running code but your answers should have enough code (or pseudo code) to give us an idea of how the problem can be solved in Java. Failing to comply to this can lead to deduction of points.

Homework Problem 1.a. Look at the basic code for the class given below.

class ProblemA extends Thread { Random r; . . public void run() { synchronized(sharedVariable) { sharedVariable++;

if(r.nextInt()==0) Thread.currentthread().suspend(); } } }

Now imagine a program is running two instances of the ProblemA. Is there a possibility of a deadlock? If so, describe a sequence of events which leads to deadlock.

Suggest a way to fix this code, preserving its function while removing the deadlock. Explain in detail why the improved code will not deadlock.

1.b. Look at the following code where a Thread updates the value of a shared variable until it reaches a given maximum value.

class ProblemB extends Thread { . . public void run() { synchronized(sharedVariable) { while(sharedVariable < MAX_VALUE) { sharedVariable++; }

} } }

Now imagine that there are two ProblemB objects running, with both trying to update the shared object. Explain briefly what will happen to the sharedVariable?

If we ran 100 ProblemB objects, what amount thread parallelism would be achieved? Explain.

Will all of the ProblemB objects get to update the sharedVariable? (if one of them does not, we call that effect, “starvation”? If starvation is a possibility, suggest a way to avoid such a situation. Explain in detail why our approach would ensure fairness.

(Note: The behavior of this program is independent of the underlying hardware specifications.)

1.c. Consider the following code

class ProblemC {

boolean isProducing; boolean isConsuming; Buffer buffer;

class Producer extends Thread { public void run() { . . produce(n); }

produce(int n) { parent.isProducing = true; synchronized(parent.buffer) { while(parent.isConsuming) { notifyall(); wait(); } buffer += n; } parent.isProducing = false; } }

class Consumer extends Thread { public void run()

Plot the performance of the word counting program (1/run time) as a function of the number of threads used. Did adding threads increase performance? Did it decrease performance? Explain why.

-*-

Homework 3.a. In this problem, we will consider thread parallelism and performance. The code for a basic non-threaded version of the Jacobi Iteration Solver is given in (/home/cse160/hw2/BasicJacobiIterator/BasicJacobi.java).

Discuss the scope of parallelism in the code and the need for synchronization. Suggest a partition strategy for the data and the associated synchronization issues involved. Implement your discussed model of parallelism and test it for 1-8 threads for N= (where N is the dimension of the matrix). Plot the performance the same way as you did for Question 2.b.

3.b. Now run your program for different sizes of N (N = 2K^ where K = 8, 9, …, 14), varying the number of threads used from 1-8 in each case. What is the optimal number of threads for each input size?

4.a. Write a program that performs bucket sort on a set of numbers read from a file (a sample file can be found in /home/cse160/hw2/BucketSort/sample.txt where the first line is the number of values followed by the actual inputs). Assume all inputs to be between 0-1000. Use a given number B (to be given by the user as command line argument) buckets.

4.b. Implement a multi-threaded version of the above program where each bucket is sorted by a separate thread. Vary the number of threads used from 1-20 (in steps of 5) and plot the effect on performance.

4.c. What if the numbers are not uniformly distributed? What will be the effect on parallelization and performance and scalability? Can you suggest something to deal with this situation?