Question

HELP PLZ: USE JAVA: Make a matrix in java so that every operation uses as many...

HELP PLZ:

USE JAVA:

Make a matrix in java so that every operation uses as many thread as there are elements in the output matrix. The point of this project is parallel programming.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


package parallelmatrix;
import java.util.*;
import java.util.concurrent.*;


public class PMatrix
{
public static void main (String arg[]) throws Exception
{
int dataA[][] = new int[][]
{
{1, 2,5},
{3, 4,6},
{8, 9,10}
};
  
int dataB[][] = new int[][]
{
{1, 2, 3},
{4, 5, 6},
{5, 8, 7}
};
  
ExecutorService executor = Executors.newFixedThreadPool(3);
  
Matrix Amat = new Matrix (dataA);
Matrix Bmat = new Matrix (dataB);
System.out.println(Amat.multiply(Bmat));
  
executor.shutdown();
}

static class Matrix
{
private static final int THREAD_COUNT = 3;
  
private int rCount, cCount, data[][];
  
public Matrix (int data[][])
{
this.data = data;
this.rCount = data.length;
this.cCount = data[0].length;
}
  
public Matrix multiply (Matrix m)
{
ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
  
try
{
if (this.cCount != m.rCount)
throw new IllegalArgumentException ("Matrix col count does not match argument's row count. cCount=" + this.cCount + " rCount=" + m.rCount);

List<Task> listFrag = Matrix.getTasks(this, m);
List<Future<Integer>> listResult = executor.invokeAll(listFrag);

int data[][] = new int [this.rCount][m.cCount];
for (int i = 0; i < this.rCount; ++i)
for (int j = 0; j < m.cCount; ++j)
data[i][j] = listResult.get(i*m.cCount + j).get();

return new Matrix (data);
}
catch (Exception e)
{
executor.shutdown();
}
return null;
}
  
public static List<Task> getTasks (Matrix Amat, Matrix Bmat)
{
List<Task> listFrag = new ArrayList<> ();
for (int i = 0; i < Amat.rCount; ++i)
for (int j = 0; j < Bmat.cCount; ++j)
listFrag.add(new Task (Amat, Bmat, i, j));
return listFrag;
}
  
public String toString ()
{
StringBuilder builder = new StringBuilder ();
for (int i = 0; i < rCount; ++i)
{
for (int j = 0; j < cCount; ++j)
builder.append(String.format(" %3d ", data[i][j]));
builder.append("\n");
};
return builder.toString();
}
}
  
static class Task implements Callable<Integer>
{
int rowId, colId;
  
Matrix Amat, Bmat;
  
public Task (Matrix Amat, Matrix Bmat, int rowId, int colId)
{
this.rowId = rowId;
this.colId = colId;
this.Amat = Amat;
this.Bmat = Bmat;
}

@Override
public Integer call() throws Exception
{
String mesg = String.format("A[%d] * B[%d]", rowId, colId);
System.out.println("Started Task "+mesg);
int product = 0;
for (int i = 0; i < Amat.cCount; ++i)
product = product + Amat.data[rowId][i] * Bmat.data[i][colId];
System.out.println("Finished Task. " + mesg + " = " + product);
return product;
}
  
}
}

*************OUTPUT

Started Task A[0] * B[2]
Started Task A[0] * B[0]
Started Task A[0] * B[1]
Finished Task. A[0] * B[1] = 52
Finished Task. A[0] * B[0] = 34
Started Task A[1] * B[0]
Finished Task. A[1] * B[0] = 49
Started Task A[1] * B[2]
Finished Task. A[1] * B[2] = 75
Started Task A[1] * B[1]
Finished Task. A[0] * B[2] = 50
Finished Task. A[1] * B[1] = 74
Started Task A[2] * B[1]
Finished Task. A[2] * B[1] = 141
Started Task A[2] * B[0]
Finished Task. A[2] * B[0] = 94
Started Task A[2] * B[2]
Finished Task. A[2] * B[2] = 148
34 52 50
49 74 75
94 141 148

Add a comment
Know the answer?
Add Answer to:
HELP PLZ: USE JAVA: Make a matrix in java so that every operation uses as many...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • *********************Java recursion********************** Plz help me write a method in java that traverse Through a integer linked...

    *********************Java recursion********************** Plz help me write a method in java that traverse Through a integer linked list recursively . The method returns a string with every element in the list and a space in between each element in reverse order. THE METHOD DOES NOT REVERSE THE LINKED LIST. IT JUST RETURNS A STRING CONTAINING THE ELEMENTS OF THE LIST IN REVERSE ORDER. ASSUME ALL THE ELEMENTS IN THE LINKED LIST ARE int... method signature is something like : public String...

  • *USE JAVA In this project you will create program that calculate the number of times every...

    *USE JAVA In this project you will create program that calculate the number of times every word appears in a given text file. Your program will take in the name of the file as well as the number of threads to create to speed up the process. The numbers of threads must be greater than 0. The output will be saved into a file in alphabetical order. Besides creating the program itself, you are to run experiments to show how...

  • Please help me the JAVA program - Choose Your Operation Write a program that uses a...

    Please help me the JAVA program - Choose Your Operation Write a program that uses a WidgetView object to do the following: Generate two random integers between 1 and 9 (inclusive). Name one of them x, the other y. Display them to the user using JLabel objects. Create a JLabel object displaying the text "Enter an operation number." Create a JTextField for the user's input. Create a JButton displaying the text "Press here when you've entered your operation." Use addAndWait...

  • The matrix operations that you should include are Addition, Subtraction and Multiplication. The driver can be...

    The matrix operations that you should include are Addition, Subtraction and Multiplication. The driver can be as simple as asking the user to enter two matrices and then presenting a simple menu that allows the user to select the operation they want to test. Have the menu in a loop so that the user can test any other operation unless they choose to exit the menu. Also, provide the user an option to select two new matrices. Make sure that...

  • plz write if it is in another class or package Question 1: 1. Create a new...

    plz write if it is in another class or package Question 1: 1. Create a new project in Eclipse (File > New > Java Project.) Name it Homework2Q1 2. Create a package for your classes 3. Create an abstract superclass with only non default constructor(s) 4. Create two different subclasses of that superclass 5. Create another class that is not related (you need a total of four classes up to this point) 6. Create an interface 7. Make the class...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Need help creating a customer order form using Netbeans (programming language is Java). I can design...

    Need help creating a customer order form using Netbeans (programming language is Java). I can design the form but need help with writing the codes. Use the below areas to input on the form Customer name Customer address Drink (tea, water etc) Sandwich (white, parmesan) Type (Italian, Turkey) Size (12" for $6) Provide screenshots to show output of the above elements. The output should prompt to enter customer name, address then the drink, sandwich and so on. The output should...

  • ** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B....

    ** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B. Divide-and-conquer matrix multiplication In order to obtain more accurate results, the algorithms should be tested with the same matrices of different sizes many times. The total time spent is then divided by the number of times the algorithm is performed to obtain the time taken to solve the given instance. For example, you randomly generate 1000 sets of input data for size_of_n=16. For each...

  • Please help with program this. Thank you so much in advance! Create a Java program which...

    Please help with program this. Thank you so much in advance! Create a Java program which implements a simple stack machine. The machine has 6 instructions Push operand Puts a value on the stack. The operand is either a floating point literal or one of 10 memory locations designated MO M9 Pop operand Pops the value on the top of the stack and moves it to the memory location MO-M9 Add Pops the top two values off the stack, performs...

  • I need help with my assignment. It is a java program. Please make it as simple...

    I need help with my assignment. It is a java program. Please make it as simple as you can. Create an ArrayList with elements the objects of previous class. Using a simple loop populate the ArrayList with data from some arrays with data, or from console. Use a loop with iterator and write the information in a File using PrintWriter. Use this address for the file (c:\\result\\MyData.txt). Use the Scanner class to display the content of the file on console.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT