Skip to content

Commit 047070c

Browse files
MPI
1 parent d7e832d commit 047070c

File tree

10 files changed

+137
-0
lines changed

10 files changed

+137
-0
lines changed

MPI/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MPI/.idea/libraries/lib.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MPI/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MPI/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MPI/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MPI/MPI.iml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="lib" level="project" />
11+
</component>
12+
</module>

MPI/matrix.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 2 3
2+
4 5 6
3+
7 8 9
3.27 KB
Binary file not shown.

MPI/src/InvertibleMatrix.java

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import mpi.MPI;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.Scanner;
11+
12+
public class InvertibleMatrix {
13+
private static int[] matrix;
14+
15+
public static void main(String[] args) throws IOException {
16+
MPI.Init(args);
17+
int my_rank = MPI.COMM_WORLD.Rank();
18+
int root = 0;
19+
matrix = new int[9];
20+
int[] sendBuffer = new int[1];
21+
int[] receiveBuffer = new int[4];
22+
23+
// TODO: Read the file by the root
24+
if (my_rank == root) {
25+
readFile();
26+
System.out.println("process " + my_rank + " matrix values after reading file " + Arrays.toString(matrix));
27+
} else {
28+
System.out.println("process " + my_rank + " matrix values before Bcast " + Arrays.toString(matrix));
29+
}
30+
31+
// TODO: broadcast the matrix
32+
MPI.COMM_WORLD.Bcast(matrix, 0, 9, MPI.INT, root);
33+
System.out.println("process " + my_rank + " matrix values after Bcast " + Arrays.toString(matrix));
34+
35+
// TODO: calculate the values
36+
if (my_rank != root) {
37+
int[][] mat2d = new int[3][3];
38+
// 0 1 2 [0][r-1] [1][] [2] [2] [1]
39+
//0*3+0 0*3+1 0*3+2 0 0 1 2 mat2d[0][0]*(mat2d[1][1]*mat2d[2][2]-mat2d[2][1]*mat2d[1][2])
40+
//1*3+0 1*3+1 1*3+2 1 3 4 5 - mat2d[0][1]*(mat2d[1][0]*mat2d[2][2]-mat2d[2][0]*mat2d[1][2])
41+
//2*3+0 2*3+1 2*3+2 2 6 7 8 mat2d[0][2]*(mat2d[1][0]*mat2d[2][1]-mat2d[2][0]*mat2d[1][1])
42+
// TODO: Convert the 1d array into 2d array
43+
for (int i = 0; i < 3; i++) {
44+
for (int j = 0; j < 3; j++) {
45+
mat2d[i][j] = matrix[(i * 3) + j];
46+
}
47+
}
48+
System.out.println("process " + my_rank + " matrix values after converting 1d into 2d " + Arrays.deepToString(mat2d));
49+
sendBuffer[0] = 0;
50+
51+
// TODO: calculate the values
52+
if (my_rank == 1) {
53+
sendBuffer[0] = mat2d[0][0] * (mat2d[1][1] * mat2d[2][2] - mat2d[2][1] * mat2d[1][2]);
54+
} else if (my_rank == 2) {
55+
sendBuffer[0] = -mat2d[0][1] * (mat2d[1][0] * mat2d[2][2] - mat2d[2][0] * mat2d[1][2]);
56+
} else {
57+
sendBuffer[0] = mat2d[0][2] * (mat2d[1][0] * mat2d[2][1] - mat2d[2][0] * mat2d[1][1]);
58+
}
59+
System.out.println("process " + my_rank + " my value is " + sendBuffer[0]);
60+
}
61+
// TODO: Gather the values
62+
MPI.COMM_WORLD.Gather(sendBuffer, 0, 1, MPI.INT, receiveBuffer, 0, 1, MPI.INT, root);
63+
if (my_rank == root) {
64+
System.out.println("I am the root & I have received: " + Arrays.toString(receiveBuffer));
65+
66+
// TODO: determine whether the matrix is singular or not
67+
int sum = Arrays.stream(receiveBuffer).sum();
68+
if (sum == 0) {
69+
System.out.println("The determinant is " + sum + " so the matrix is singular");
70+
} else {
71+
System.out.println("The determinant is " + sum + " so the matrix is not singular");
72+
}
73+
}
74+
MPI.Finalize();
75+
}
76+
77+
private static void readFile() throws IOException {
78+
Scanner scanner = new Scanner(new File("matrix.txt"));
79+
int i = 0;
80+
while (scanner.hasNextInt()) {
81+
matrix[i++] = scanner.nextInt();
82+
}
83+
}
84+
}
Binary file not shown.

0 commit comments

Comments
 (0)