CPSC 2800 – Introduction to Operating Systems – Spring 2011

Project 2 – Matrix Multiplication

Submission due on March 31, and demonstration on March 31in-class.

For this project, we will complete Project 2: Matrix multiplication project as described in the textbook from pages 188-191.

  1. Read the textbook project description thoroughly before you do anything else.
  2. Create a Matrix class in Java. The Matrix class should have four instance variables. The first is a two-dimensional matrix that you must call ‘matrix’ and the second is a Random object generator. You can find the Random class in java.util package. You will also need to store variables for the number of rows and the number of columns. Your Matrix class should specify the following methods first (we will get to multiplication later).

public Matrix(int m, int n)//create mxn Matrix with random entries

public Matrix(String s)//create a matrix from a file

public Matrix(int m, int n, intval) //create mxn Matrix with val in every cell

publicintgetNumRows()

publicintgetNumCols()

public void setValue(int i, int j, intval)

public String toString()//return a readable String representing the object

you do not need to implement all these methods immediately – just create headers and fake method bodies for each one so your class will compile.

  1. Begin by creating your random entry constructor and your toString method. These will be the easiest. Next create a ddriver program called MatrixTester that tests your random constructor and your toString method. Note that the toString method should return a String that is a nice representation of your object. For example, your initial code might look like this:

public class MatrixTester{

Public static void main(String[] args){

Matrix m = new Matrix(2, 3);

System.out.println(m.toString());

}

}

Your program might print out something like this:

524

331

  1. Fill in the getNumRows(), getNumCols(), and setValue() methods and add code to test them to your MatrixTester program.
  2. Work on the constructor that involves creating a Matrix from a file. The file will be formatted similar to the output of your toString() method. In other words, one row per line, and each entry will be separated by (possibly more than one) space. Read the file a like at a time (using BufferedReader object) and use the String object’s split() method to get one entry at a time. You will also need to figure out how to convert a String to an Integer, buy you already know how to do that! Create some test files, and add code to your MatrixTester program to test the method.
  3. Once that is working, we will go ahead and implement the multiplication method. Use the following method header:

public Matrix normalMultiply(Matrix input)

in this method, you will perform the standard matrix multiplication algorithm, which traditionally requires 3 nested for-loops. You method should create a new Matrix object which will be returned at the end, once the computation is completed. Add code to your MatrixTester program that tests this method.

  1. Now we will add a multi-threaded version.

public Matrix mutithreadedMultiply(Matrix input)

this will require you to create a new class (preferable in a separate file) that I called “DotProductThread.java” My DotProductThread had 5 instances variables, including references to the first matrix, the second matrix and the resulting matrix (all of which are allocated on the head). It also had two integers representing the row of the first matrix and the column of the second matrix that is being computed.

  1. Last, but not least, we will add some timing code to test both version of our method implementation. The easiest way to time code in Java is to use the System.currentTimeMillis() method. Here’s some sample code to get you started:

long start, elapse;

start = System.currentTimeMillis();

//insert the code you want to test here

elapse = System.currentTimeMillis()-start;

System.out.println("It took " + elapse + " milliseconds to complete the task");

Time your MultiThread Program and your single threaded program on several different size matrices. Make sure that all of your test code is included in your submission. The extent of your testing and your response to the question about it below will figure into your grade substantially.

What did you learn?

  1. How far did you get in this project? What grade are you expecting?
  2. Describe 3 problems (relating to this assignment) that arose while you were working on this project and explain how you solved them.
  3. How long did you spend on this assignment? Give me specifics.

Date / Time / Activities / Outcomes
  1. Describe the results of your timing tests. In particular, which version of multiplication method was faster? Did you expect this? Explain your results as best you can.

Turn in

  1. Your source code.
  2. The answers to above 4 questions in a plain text document called README.

Note

-Make frequent backups of your program so that you can revert to a working copy if you mess something up.

-Do NOT turn a program that will no compile. We will not be able to test it, and we can’t spend time trying to get it working, so you will not get any credit.

-Keep sharing and posting in the discussion board.

Grading

Code does not compile, but other documentation submitted / 30
Code compiles, and other documentation submitted / 20
Code compiles but cannot run only either single thread or multi-thread / 10
Code compiles and both single thread and multi-thread methods are working / With random matrices / 10
With given matrices / 10
With reading from a file / 10
Code compiles, both version of multiplication is working under above three scenarios, timing info is printed correctly. / 10

1