Docsity

Assignment 6 for Synchronization - Computer Architecture | ECE 3055, Assignments of Electrical and Electronics Engineering

Toggle sidebar
bg1
Summer 2007 ECE 3055
Assignment 6
Due Date: 11:59 pm, Friday July 27th, 2007.
Abstract:
A thread is an independent stream of instructions that is meant to execute in parallel with
other independent streams of instructions. Up until now, you have probably only written
single threaded programs. This means that within your program, there is only one code
segment executing (serially) at any given time. With multithreaded applications, there
can be multiple code segments executing.
The concept of multithreading should not be new to you. When ever you open a web
browser, type in www.google.com, and hit enter you are dealing with multiple threads.
One thread is contacting the web server for www.google.com while another thread is
waiting for you to press a button so that it can process your input. If your web browser
was not multithreaded, you would not be able to interact with it while it was trying to
load a web page.
1 Assignment
There are two components to this assignment – a warm-up component designed to help
you become familiar with multithreaded programming and a component where you will
introduce synchronization of threads to the program.
Use one of the Solaris or Linux machines in Klaus 1448. If you want to remote connect
and need help, see the TA at office hours.
1.1 Warm-up
The Black–Scholes model is a mathematical model of the market for an equity, in which
the equity's price is a stochastic process. The Black–Scholes PDE is an equation which
(in the model) must be satisfied by the price of a derivative on the equity. The course
website provides links to both the single threaded version and multi-threaded version of
Black-Scholes.
- Compile and run the single threaded version Black-Scholes (black_single.c).
o To compile type: gcc –o black black_ut.c –lm
o To run type: ./black
o To record the execution time type: time ./black
- Now Compile and run the multi-threaded version (black_multi.c)
o To compile type: gcc –o black black_ut.c –lm –pthreads
 This will overwrite the previous executable (change the name if
you don’t want it to overwrite)
 ā€œ-pthreadā€ is to include the pthread library while compiling
o To run and record the execution time type: time ./black
pf3

Partial preview of the text

Download Assignment 6 for Synchronization - Computer Architecture | ECE 3055 and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

Assignment 6

Due Date: 11:59 pm, Friday July 27th^ , 2007.

Abstract:

A thread is an independent stream of instructions that is meant to execute in parallel with other independent streams of instructions. Up until now, you have probably only written single threaded programs. This means that within your program, there is only one code segment executing (serially) at any given time. With multithreaded applications, there can be multiple code segments executing.

The concept of multithreading should not be new to you. When ever you open a web browser, type in www.google.com, and hit enter you are dealing with multiple threads. One thread is contacting the web server for www.google.com while another thread is waiting for you to press a button so that it can process your input. If your web browser was not multithreaded, you would not be able to interact with it while it was trying to load a web page.

1 Assignment

There are two components to this assignment – a warm-up component designed to help you become familiar with multithreaded programming and a component where you will introduce synchronization of threads to the program.

Use one of the Solaris or Linux machines in Klaus 1448. If you want to remote connect and need help, see the TA at office hours.

1.1 Warm-up

The Black–Scholes model is a mathematical model of the market for an equity, in which the equity's price is a stochastic process. The Black–Scholes PDE is an equation which (in the model) must be satisfied by the price of a derivative on the equity. The course website provides links to both the single threaded version and multi-threaded version of Black-Scholes.

  • Compile and run the single threaded version Black-Scholes (black_single.c). o To compile type: gcc –o black black_ut.c –lm o To run type: ./black o To record the execution time type: time ./black
  • Now Compile and run the multi-threaded version (black_multi.c) o To compile type: gcc –o black black_ut.c –lm –pthreads ƒ This will overwrite the previous executable (change the name if you don’t want it to overwrite) ƒ ā€œ-pthreadā€ is to include the pthread library while compiling o To run and record the execution time type: time ./black

Observe the performance difference between single and multi-threaded programs. Now increase the number of threads in the multi-threaded version (change the Num-Threads definition). Plot the execution time vs. number of threads. Briefly explain the behavior that is captured in the plots.

1.2 Synchronization

In this section you will extend the Black-Scholes multi-threaded program to include synchronization of data accesses. The goal is to have many threads executing at one time. This can only achieved by using locks to control the access to shared data.

In the current Black-Scholes program there are a set of threads created and each thread is given separate arguments to execute code. This should be modified so that there is one main that initializes the thread_data_array where each element can be thought of as ā€œa piece of workā€. Then an arbitrary amount of ā€œworkerā€ threads are created. These threads will check the thread_data_array for an available ā€œpiece of workā€ that hasn’t been allocated. The thread will mark this element as allocated, execute the Black-Scholes model on the data stored in this element, and write the result back to this element in the thread array. It is the accesses to the thread array that must be synchronized across threads since multiple threads may be trying to access the array concurrently.

Structure of the program:

  • First loop: to initialize the thread_data_array. This area should have two additional variables: ā€œAllocatedā€ (a flag that designates whether the work has been performed) and ā€œResultā€ which will hold the final results computed by the thread.
  • Second Loop: to spawn a number of threads for execution. Note that the arguments to pthread _create (and the Black-Scholes function) will change
  • Third loop: to wait for the threads to finish execution (pthread_join). Each Thread:
  • Once a thread enters the Black-Scholes function, it should try and acquire a lock on the thread_data_array.
  • Once a lock is acquired, check the array to see if an element requires computation. It so, copy the data into the local thread_data variables, unlock the array and start execution.
  • When values have been computed, acquire a lock on the array. Write back the results for that specific element and update the ā€œallocateā€ flag.
  • Release the lock. Generate Plot:
  • First try using two ā€œworkerā€ threads of execution in the program and record the time. Then try at least 5 more numbers of worker threads.
  • Create a plot of the execution time vs. number of worker threads (there should be at least 6 points). Explain the trends found in the plot. Was there a speed-up/slow- down? Explain

The tutorial links at the bottom of the page has examples of using functions from the pthread library, mutex and condition variables. They have many helpful examples that are worthwhile to go through before starting to code.