Page 1 of 1

Topic: Code implementation Introduction: You will create a pair of client and server programs to perform matrix multipli

Posted: Thu Jul 14, 2022 2:07 pm
by answerhappygod
Topic: Code implementation
Introduction:
You will create a pairof client and server programs to perform matrix multiplicationdescribed as follow:
In addition to theabove functional requirements, your program should also be object-oriented and follow the Java naming convention. Programming stylewill be considered in the marking.
You are given two program templates (Client andServer). Study the code and comments written in thetemplates to learn more about the implementation details.
Topic Code Implementation Introduction You Will Create A Pair Of Client And Server Programs To Perform Matrix Multipli 1
Topic Code Implementation Introduction You Will Create A Pair Of Client And Server Programs To Perform Matrix Multipli 1 (76.44 KiB) Viewed 24 times
Topic Code Implementation Introduction You Will Create A Pair Of Client And Server Programs To Perform Matrix Multipli 2
Topic Code Implementation Introduction You Will Create A Pair Of Client And Server Programs To Perform Matrix Multipli 2 (30.73 KiB) Viewed 24 times
Code Template and the position ofimplementation:
1. Client:
import java.io.*;
import java.net.*;
import java.util.Arrays;
import java.util.Formatter;
/**
* A client program that send a matrix to server to computeits inverse.
*
* @author vanting
*/
public class Client {
/**
* Driver function.
*/
public static void main(String[] args) {
// Create two matrices with randomvalues
Matrix matA = new Matrix(1000, 1200,100);
Matrix matB = new Matrix(1200, 1500,100);
// We request the server to computethe product first
long start =System.currentTimeMillis();
Matrix multiProduct =remoteMultiply("127.0.0.1", 33333, matA, matB);
double multiTime =System.currentTimeMillis() - start;
// Then we compute the productlocally using the built-in mutliply() method
start =System.currentTimeMillis();
Matrix singleProduct =matA.multiply(matB);
double singleTime =System.currentTimeMillis() - start;
// Finally, we check if they are thesame and their runtime ratio.
if(singleProduct.equals(multiProduct)) {
System.out.println("The computation is correct.");
System.out.println("Single-to-Multi Ratio: " +singleTime/multiTime);
} else {
System.out.println("The computation is NOT correct.");
}
}
/**
* Connect to the matrix server at thespecified host and port, and then
* request the server to compute the matrixproduct of AxB.
*
* @param host IP address of the server
* @param port port number used by theserver
* @param matA matrix A
* @param matB matrix B
* @return the matrix product of AxB
*/
public static Matrix remoteMultiply(String host,int port, Matrix matA, Matrix matB) {
Matrix product = null;
// your implementationhere
return product;
}
}
//------------------------------------------------------------------------------------------------------
/**
* Immutable Integer Matrix. Do not modify this class.
*
* @author vanting
*/
class Matrix implements Serializable {
private long[][] nums;
public Matrix() {
this(1, 1);
}
public Matrix(int row, int col) {
nums = new long[row][col];
}
// Initialize this matrix with random numbersbetween 0 to n (exclusive)
public Matrix(int row, int col, int n) {
this(row, col);
for (int i = 0; i < row; i++){
for (int j = 0; j <col; j++) {
nums[j] = (int) (Math.random() * n);
}
}
}
public Matrix(long[][] n) {
this(n.length, n[0].length);
for (int i = 0; i < nums.length;i++) {
for (int j = 0; j <nums[0].length; j++) {
nums[j] = n[j];
}
}
}
// Copy constructor
public Matrix(Matrix other) {
this(other.row(), other.col());
for (int i = 0; i < nums.length;i++) {
for (int j = 0; j <nums[0].length; j++) {
nums[j] = other.at(i, j);
}
}
}
//-----------------------------------------------------------------
public long at(int row, int col) {
return nums[row][col];
}
public int row() {
return nums.length;
}
public int col() {
return nums[0].length;
}
// Return the product of this multiplying other;return null on fail
public Matrix multiply(Matrix other) {
int row1 = this.nums.length;
int col1 = this.nums[0].length;
int row2 = other.nums.length;
int col2 = other.nums[0].length;
if (col1 == row2) {
long sum = 0;
long[][] product = newlong[row1][col2];
for (int i = 0; i <row1; i++) {
for (intj = 0; j < col2; j++) {
for (int k = 0; k < col1; k++) {
sum = sum + nums[k] * other.nums[k][j];
}
product[j] = sum;
sum = 0;
}
}
return newMatrix(product);
} else {
return null;
}
}
public void print() {
System.out.println(this);
}
// Return a string representation of thismatrix
@Override
public String toString() {
StringBuilder sb = newStringBuilder();
Formatter fmt = newFormatter(sb);
int row = this.nums.length;
int col = this.nums[0].length;
for (int i = 0; i < row; i++){
for (int j = 0; j <col; j++) {
fmt.format("[%4d]", this.nums[j]);
}
fmt.format("%n");
}
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (o != null && oinstanceof Matrix) {
Matrix other =(Matrix) o;
if (nums.length !=other.row()) {
returnfalse;
}
if (nums[0].length !=other.col()) {
returnfalse;
}
for (int i = 0; i <nums.length; i++) {
for (intj = 0; j < nums[0].length; j++) {
if (nums[j] != other.at(i, j)) {
return false;
}
}
}
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash +Arrays.deepHashCode(this.nums);
return hash;
}
}
2. Server:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
/**
* Multi-threaded server program that multiplies matricesusing fork-join
* framework.
*
* @author vanting
*/
public class Server implements Runnable {
private Socket socket;
public Server() {
}
public Server(Socket socket) {
this.socket = socket;
}
/**
* Driver function. Start this server at port33333.
*/
public static void main(String[] args) {
start(33333);
}
/**
* Start matrix server at the specified port.It should accept and handle
* multiple client requests concurrently.
*
* @param port port number listened by theserver
*/
public static void start(int port) {
// your implementationhere
}
/**
* Handle a matrix client request. It readstwo matrices from socket,
* compute their product, and then send theproduct matrix back to the
* client.
*/
@Override
public void run() {
// your implementationhere
}
/**
* Compute A x B using fork-joinframework.
* @param matA matrix A
* @param matB matrix B
* @return the matrixproduct of AxB
*/
public static Matrix multiThreadMultiply(MatrixmatA, Matrix matB) {
Matrix product = null;
// your implementationhere
return product;
}
}
/**
* Design a recursive and resultless ForkJoinTask. Itsplits the matrix multiplication
* into multiple tasks to be executed in parallel.
*
*/
class ParallelMultiply extends RecursiveAction {
// your implementation here
@Override
protected void compute() {
}
}
Figure 1 . A reference model on how to handle multiple client requests.
Figure 2. Transforming a single-thread multiplication task to a multi-thread multiplication task. A N×M matrix can be divided into N rows. Each row is then computed by a different thread independently.