parallel programming in java with ( Parallelisim in OpenMP is as easy as writing
ID: 3582737 • Letter: P
Question
parallel programming in java with ( Parallelisim in OpenMP is as easy as writing #pragma omp parallel { }, omp_get_thread_num() will give you the number of the thread (id))--------> MAKE A CLASS MATRIX IN JAVA with all matrices operations AND MAKE SURE ITS PARALLEL.
below are code examples: (adding)
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int id;
printf("We are going to add numbers in parallel " );
int n = 0;
printf("Every thread is going add 1 to a variable that starts out at zero and print out the result " );
#pragma omp parallel
{
n += 1;
printf("Thread number %d has added 1 and now the variable is %d ", omp_get_thread_num(), n);
}
printf("What a mess, we need to make this atomic " );
n = 0;
#pragma omp parallel
{
#pragma omp barrier
{
n += 1;
printf("Thread number %d has added 1 and now the variable is %d ", omp_get_thread_num(), n);
}
}
}
///////////////////
loop example in parallel:
int main ()
{
int i = 0;
int j = 0;
printf("Trying a loop " );
# pragma omp parallel
{
# pragma omp for
for (i = 0; i < 5; i += 1 )
{
printf("Thread %d at iteration %d ", omp_get_thread_num(),i);
}
}
printf("So far so good, lets try nested loop " );
# pragma omp parallel
{
# pragma omp for private (j)
for (i = 0; i < 5; i += 1 )
{
for (j = 0; j < 4; j += 1 )
{
printf("Thread %d at iteration %d,%d ", omp_get_thread_num(), i, j);
}
}
}
}
so make the program parallel ..... I already havew a code for matrix but its not parallel. maybe you can use this to make it parallel:
import java.util.Random;
final public class Matrix
{
private final int numberOfRows;
private final int numberOfColumns;
private final int [][] myElements;
//populate
void populate()
{
Random rand= new Random();
for (int i=0; i< numberOfRows; i+=1)
{
for (int j=0; j< numberOfColumns; j+=1)
{
myElements [i][j]= rand.nextInt(9)+1;
}
}
}
// Constructor numberOfRows, numberOfColumns matrix of 0
public Matrix(int numberOfRows, int numberOfColumns)
{
this.numberOfRows=numberOfRows;
this.numberOfColumns=numberOfColumns;
myElements= new int [numberOfRows][numberOfColumns];
}
//creating matrix based on double array
Matrix (int [][] myElements)
{
numberOfRows= myElements.length;
numberOfColumns=myElements[0].length;
this.myElements= myElements.clone();
}
private Matrix(Matrix One)
{
this(One.myElements);
}
//Extra since I populate it in the beginning first, but thought I should leave it for you to look at
public static Matrix random(int numberOfRows, int numberOfColumns)
{
Random rand = new Random();
Matrix Matrix(numberOfRows, numberOfColumns);
for (int i=0; i< numberOfRows;i+= 1)
for (int j=0; j< numberOfColumns; j+=1)
One.myElements[i][j]= rand.nextInt(9)+1 ;
return One;
}
//identity Matrix
public static Matrix theIdentity(int numberOfColumns)
{
Matrix Two= new Matrix(numberOfColumns,numberOfColumns);
for (int i=0; i Two.myElements[i][i] = 1;
return Two;
}
//swapping rows between i and j
private void nowSwap(int i, int j)
{
int[] tempory= myElements[i];
myElements[i]= myElements[j];
myElements[j]=tempory;
}
//make and retun
public Matrix trans()
{
Matrix Matrix(numberOfRows,numberOfColumns);
for (int i=0; i< numberOfRows; i+=1)
for(int j=0; j< numberOfColumns; j+=1)
One.myElements[j][i]=this.myElements[i][j];
return One;
}
//Matrix plus A+B=c
public Matrix Plus(Matrix B)
{
Matrix A= this;
if(B.numberOfRows != A.numberOfRows || B.numberOfColumns != A.numberOfColumns) throw new RuntimeException ("Illegal Matrix");
Matrix C= new Matrix (numberOfRows, numberOfColumns);
for (int i= 0; i for (int j=0; j< numberOfColumns; j+=1)
C.myElements[i][j]= A.myElements[i][j] + B.myElements[i][j];
return C;
}
// Matrix Subtraction A-B=C
public Matrix Minus(Matrix B)
{
Matrix A= this;
if (B.numberOfRows != A.numberOfColumns || B.numberOfColumns != A.numberOfRows) throw new RuntimeException ("Illegal matrix");
Matrix C= new Matrix (numberOfRows, numberOfColumns);
for (int i=0; i< numberOfRows; i+=1)
for (int j=0; j< numberOfColumns; j+=1)
C.myElements[i][j]= A.myElements[i][j] - B.myElements [i][j];
return C;
}
//Matrix multiplication C= A*B
public Matrix Multiplication(Matrix B)
{
Matrix A= this;
if (A.numberOfColumns != B.numberOfRows) throw new RuntimeException ("Illegal Matrix");
Matrix C= new Matrix (A.numberOfRows, B.numberOfColumns);
for(int i=0; i < C.numberOfRows; i+=1)
for (int j=0; j < C.numberOfColumns; j+=1)
for(int k=0; k < A.numberOfColumns; k+=1)
C.myElements [i][j] += (A.myElements [i][k] * B.myElements [k][j]);
return C;
}
//A=B !???
public boolean equalsOrNot(Matrix B)
{
Matrix A= this;
if (B.numberOfRows != A.numberOfRows || B.numberOfColumns != A.numberOfColumns) throw new RuntimeException ("Illegal Matrix");
for (int i=0; i < numberOfRows; i+=1)
for (int j=0; j < numberOfColumns; j+=1)
if (A.myElements [i][j] != B.myElements [i][j]) return false;
return true;
}
// When A is squared
public Matrix sqMhs (Matrix mhs)
{
if(numberOfRows != numberOfColumns || mhs.numberOfRows != mhs.numberOfColumns || mhs.numberOfColumns ==1)
throw new RuntimeException ("Illegal matrix");
// creating copies
Matrix A= new Matrix (this);
Matrix M= new Matrix(mhs);
//eliminating with partial pivotting
for (int i=0; i < numberOfColumns; i+=1)
{
int maximum= i; //will swap when pivot in row is found
for (int j= i + 1; j< numberOfColumns; j+=1)
{
if (Math.abs(A.myElements[j][i]) > Math.abs(A.myElements[maximum][i]))
maximum= j;
A.nowSwap(i, maximum);
M.nowSwap(i, maximum);
if (A.myElements[i][j] == 0) throw new RuntimeException ("Singular Matrix!");
}
for (int j= i + 1; j < numberOfColumns; j+=1)
M.myElements[j][0] -= M.myElements[i][0] * A.myElements[j][i] / A.myElements[i][i];
//pivot with A
for (int j= i + 1; j < numberOfColumns; j+=1)
{
int mn = A.myElements[j][i] / A.myElements[i][i];
for (int k= i+1; k < numberOfColumns; k+=1)
{
A.myElements[j][k] -= A.myElements[i][k] * mn;
}
A.myElements[j][i]= 0;
}
}
//substitute
Matrix r= new Matrix(numberOfColumns, 1);
for (int j= numberOfColumns - 1; j >= 0; j--)
{
int t= 0;
for (int k = j +1; k < numberOfColumns; k++)
t += A.myElements[j][k] * r.myElements[k][0];
r.myElements[j][0] = (M.myElements[j][0] - t) / A.myElements[j][j];
}
return r;
}
public void show(){
for(int i=0;i for(int j=0;j System.out.print(this.myElements[i][j]+" ");
}
System.out.println();
}
}
/**********************************************************************/
//Main and test cases
public static void main(String args[])
{
int [][] integer= { {1,2,3 }, {4,5,6 }, {9,1,3}};
Matrix L = new Matrix (integer);
L.show();
System.out.println();
Matrix A= Matrix.random(5, 5);
A.show();
System.out.println();
A.nowSwap (1, 2);
A.show();
System.out.println();
Matrix B= A.trans();
B.show();
System.out.println();
Matrix C= Matrix.theIdentity(5);
C.show();
System.out.println();
A.Plus(B).show();
System.out.println();
B.Multiplication(A).show();
System.out.println();
System.out.println(A.Multiplication(B).equals(B.Multiplication(A)));
System.out.println();
Matrix M= Matrix.random(5, 1);
M.show();
System.out.println();
A.Multiplication(C).show();
}
}// End Matrix
Explanation / Answer
//Parallel programming for Addition in Java//
public class Summation extends Thread {
private int[] arr1;
private int low1, high1, partial;
public Summation(int[] arr1, int low1, int high1)
{
this.arr1 = arr1;
this.low1 = low1;
this.high1 = Math.min(high1, arr1.length);
}
public int getPartialSum()
{
return partial;
}
public void run()
{
partial = sum(arr1, low1, high1);
}
public static int sum(int[] arr1)
{
return sum(arr1, 0, arr1.length);
}
public static int sum(int[] arr1, int low1, int high1)
{
int total = 0;
for (int i = low1; i < high1; i++) {
total += arr1[i];
}
return total;
}
public static int parallelSum(int[] arr1)
{
return parallelSum(arr1, Runtime.getRuntime().availableProcessors());
}
public static int parallelSum(int[] arr1, int threads)
{
int size = (int) Math.ceil(arr.length * 1.0 / threads);
Summation[] sums = new Summation[threads];
for (int i = 0; i < threads; i++) {
sums[i] = new Summation(arr1, i * size, (i + 1) * size);
sums[i].start();
}
try {
for (Summation sum : sums) {
sum.join();
}
} catch (InterruptedException e) { }
int total = 0;
for (Summation sum : sums) {
total += sum.getPartialSum();
}
return total;
}
}
Sample Output:
--------------
Rows & columns
2
2
First matrix :
1 2 3 4
Second matrix :
5 6 7 8
Total:
6 8
10 12
//Parallel programming for subtraction in Java//
public class Summation extends Thread {
private int[] arr1;
private int low1, high1, partial;
public Difference(int[] arr1, int low1, int high1)
{
this.arr1 = arr1;
this.high1 = Math.max(high1, arr1.length);
this.low1 = low1;
}
public int getPartialDifference()
{
return partial;
}
public void run()
{
partial = Difference(arr1, high1, low1);
}
public static int Difference(int[] arr1)
{
return Difference(arr1, 0, arr1.length);
}
public static int Difference(int[] arr1, int low1, int high1)
{
int total = 0;
for (int i = low1; i < high1; i++) {
total -= arr1[i];
}
return total;
}
public static int parallelDifference(int[] arr1)
{
return parallelDifference(arr1, Runtime.getRuntime().availableProcessors());
}
public static int parallelDifference(int[] arr1, int threads)
{
int size = (int) Math.ceil(arr.length * 1.0 / threads);
Diff[] sums = new Diff[threads];
for (int i = 0; i < threads; i++) {
Difference[i] = new Diff(arr1, i * size, (i + 1) * size);
Difference[i].start();
}
try {
for (Diff Difference : Differences) {
Difference.Sub();
}
} catch (InterruptedException e) { }
int total = 0;
for (Diff Differences : Differences) {
total -= Difference.getPartialDifference();
}
return total;
}
}
Sample Output:
-------
No of rows:
2
Columns:
2
First matrix :
9 8 7 6
Second matrix :
5 4 3 2
Total:
4 4
4 4
//Parallel programming for multiplication in Java//
public class MatrixParallel22 extends Thread{
final static int noThreads = 2 ;
public static void main(String args1[]) throws Exception{
MatrixParallel [] threads = new MatrixParallel [noThreads] ;
for(int me1 = 0 ; me1 < noThreads ; me1++) {
threads [me1] = new MatrixParallel(me1) ;
threads [me1].start() ;
}
for(int me1 = 0 ; me1 < noThreads ; me1++) {
threads [me1].join() ;
}
}
int me1 ;
public MatrixParallel(int me1) {
this.me1 = me1 ;
}
public void run() {
//Here we generate two matrices using random numbers
int mat1 [][] = matrixGenerator();
int mat2 [][] = matrixGenerator();
//to get the number of rows from the first matrix
int m1rows = mat1.length;
//to get the number of columns from the first matrix
int m1cols = mat1[0].length;
//to get the number of columns from the second matrix
int m2cols = mat2[0].length;
//to multiply the matrices and put the result in an array
int[][] result1 = new int[m1rows][m2cols];
for (int i=0; i< m1rows; i++){
for (int j=0; j< m2cols; j++){
for (int k=0; k< m1cols; k++){
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
public static int[][] matrixGenerator(){
//to create an array
int mat [][] = new int[550][550];
//we create a random generator and then fill it with random numbers
Random r = new Random( );
for(int i=0; i < mat.length; i++){
for(int j=0; j < mat[i].length; j++){
mat[i][j] = r.nextInt( 10000 );
}
}
return mat;
}
Output:
---------
Number of rows & columns
2
Data for first matrix :
1 2 3 4
Data for second matrix :
5 6 7 8
Mat //The matrix after multiplication would be
5 12
21 32
//Parallel programming for Division in Java//
public class MatrixParallel22 extends Thread{
final static int noThreads = 2 ;
public static void main(String args1[]) throws Exception{
MatrixParallel [] threads = new MatrixParallel [noThreads] ;
for(int me1 = 0 ; me1 < noThreads ; me1++) {
threads [me1] = new MatrixParallel(me1) ;
threads [me1].start() ;
}
for(int me1 = 0 ; me1 < noThreads ; me1++) {
threads [me1].join() ;
}
}
int me1 ;
public MatrixParallel(int me1) {
this.me1 = me1 ;
}
public void run() {
//Here we generate two matrices using random numbers
int mat1 [][] = matrixGenerator();
int mat2 [][] = matrixGenerator();
//to get the number of rows from the first matrix
int m1rows = mat1.length1;
//to get the number of columns from the first matrix
int m1cols = mat1[0].length1;
//to get the number of columns from the second matrix
int m2cols = mat2[0].length1;
//to divide the matrices and put the result in an array
int[][] result1 = new int[m1rows][m2cols];
for (int i=0; i< m1rows; i++){
for (int j=0; j< m2cols; j++){
for (int k=0; k< m1cols; k++){
result1[i][j] += mat1[i][k] / mat2[j][k];
}
}
}
}
public static int[][] matrixGenerator(){
//to create an array
int mat [][] = new int[550][550];
//we create a random generator and then fill it with random numbers
Random r = new Random( );
for(int i=0; i < mat.length1; i++){
for(int j=0; j < mat[i].length1; j++){
mat[i][j] = r.nextInt( 10000 );
}
}
return mat;
}
Sample output:
------------
Rows & columns
2
2
First matrix :
8 8 8 8
Second matrix :
4 4 4 4
Mat//The resultant matrix is :
2 2
2 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.