Skip to content

Commit 7a25c57

Browse files
AnaghaSasikumariluwatar
authored andcommitted
Master worker pattern iluwatar#799 (iluwatar#831)
* master worker pattern * Update App.java * Adding new line to README.md * Adding new line to pom.xml * Adding new line to ArrayEquality.java * Adding new line to Input.java * Adding new line to Result.java * Adding new line to ArrayTransposeMasterWorker.java * Adding new line to ArrayTransposeMaster.java * Adding new line to ArrayTransposeWorker.java * Adding new line to Worker.java * Adding new line to ArrayInputTest.java * Adding new line ArrayTransposeMasterWorkerTest.java * Adding new line to ArrayResult.java * Review changes * Update README.md
1 parent 55c7579 commit 7a25c57

File tree

19 files changed

+1016
-0
lines changed

19 files changed

+1016
-0
lines changed

master-worker-pattern/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
layout: pattern
3+
title: Master-Worker
4+
folder: master-worker-pattern
5+
permalink: /patterns/master-worker-pattern/
6+
categories: Centralised Parallel Processing
7+
tags:
8+
- Java
9+
- Difficulty-Intermediate
10+
---
11+
12+
## Also known as
13+
14+
> Master-slave or Map-reduce
15+
16+
## Intent
17+
18+
> Used for centralised parallel processing.
19+
20+
## Applicability
21+
This pattern can be used when data can be divided into multiple parts, all of which need to go through the same computation to give a result, which need to be aggregated to get the final result.
22+
23+
## Explanation
24+
In this pattern, parallel processing is performed using a system consisting of a master and some number of workers, where a master divides the work among the workers, gets the result back from them and assimilates all the results to give final result. The only communication is between the master and the worker - none of the workers communicate among one another and the user only communicates with the master to get the required job done. The master has to maintain a record of how the divided data has been distributed, how many workers have finished their work and returned a result, and the results themselves to be able to aggregate the data correctly.
25+
26+
## Credits
27+
* [https://docs.gigaspaces.com/sbp/master-worker-pattern.html]
28+
* [http://www.cs.sjsu.edu/~pearce/oom/patterns/behavioral/masterslave.htm]

master-worker-pattern/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>com.iluwatar</groupId>
5+
<artifactId>java-design-patterns</artifactId>
6+
<version>1.21.0-SNAPSHOT</version>
7+
</parent>
8+
<artifactId>master-worker-pattern</artifactId>
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.junit.jupiter</groupId>
12+
<artifactId>junit-jupiter-api</artifactId>
13+
<scope>test</scope>
14+
</dependency>
15+
<dependency>
16+
<groupId>org.junit.jupiter</groupId>
17+
<artifactId>junit-jupiter-engine</artifactId>
18+
<scope>test</scope>
19+
</dependency>
20+
</dependencies>
21+
</project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.masterworker;
25+
26+
import com.iluwatar.masterworker.system.ArrayTransposeMasterWorker;
27+
28+
/**
29+
* <p>The <b><em>Master-Worker</em></b> pattern is used when the problem at hand can be solved by dividing into
30+
* multiple parts which need to go through the same computation and may need to be aggregated to get final result.
31+
* Parallel processing is performed using a system consisting of a master and some number of workers, where a
32+
* master divides the work among the workers, gets the result back from them and assimilates all the results to
33+
* give final result. The only communication is between the master and the worker - none of the workers communicate
34+
* among one another and the user only communicates with the master to get required job done.</p>
35+
* <p>In our example, we have generic abstract classes {@link MasterWorker}, {@link Master} and {@link Worker} which
36+
* have to be extended by the classes which will perform the specific job at hand (in this case finding transpose of
37+
* matrix, done by {@link ArrayTransposeMasterWorker}, {@link ArrayTransposeMaster} and {@link ArrayTransposeWorker}).
38+
* The Master class divides the work into parts to be given to the workers, collects the results from the workers and
39+
* aggregates it when all workers have responded before returning the solution. The Worker class extends the Thread
40+
* class to enable parallel processing, and does the work once the data has been received from the Master. The
41+
* MasterWorker contains a reference to the Master class, gets the input from the App and passes it on to the Master.
42+
* These 3 classes define the system which computes the result. We also have 2 abstract classes {@link Input} and
43+
* {@link Result}, which contain the input data and result data respectively. The Input class also has an abstract
44+
* method divideData which defines how the data is to be divided into segments. These classes are extended by
45+
* {@link ArrayInput} and {@link ArrayResult}.</p>
46+
*/
47+
48+
public class App {
49+
50+
/**
51+
* Program entry point.
52+
* @param args command line args
53+
*/
54+
55+
public static void main(String[] args) {
56+
ArrayTransposeMasterWorker mw = new ArrayTransposeMasterWorker();
57+
int rows = 10;
58+
int columns = 20;
59+
int[][] inputMatrix = ArrayUtilityMethods.createRandomIntMatrix(rows,columns);
60+
ArrayInput input = new ArrayInput(inputMatrix);
61+
ArrayResult result = (ArrayResult) mw.getResult(input);
62+
if (result != null) {
63+
ArrayUtilityMethods.printMatrix(inputMatrix);
64+
System.out.println("");
65+
ArrayUtilityMethods.printMatrix(result.data);
66+
} else {
67+
System.out.println("Please enter non-zero input");
68+
}
69+
}
70+
71+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.masterworker;
25+
26+
import java.util.ArrayList;
27+
import java.util.Arrays;
28+
29+
/**
30+
*Class ArrayInput extends abstract class {@link Input} and contains data
31+
*of type int[][].
32+
*/
33+
34+
public class ArrayInput extends Input<int[][]> {
35+
36+
public ArrayInput(int[][] data) {
37+
super(data);
38+
}
39+
40+
static int[] makeDivisions(int[][] data, int num) {
41+
int initialDivision = data.length / num; //equally dividing
42+
int[] divisions = new int[num];
43+
Arrays.fill(divisions, initialDivision);
44+
if (initialDivision * num != data.length) {
45+
int extra = data.length - initialDivision * num;
46+
int l = 0;
47+
//equally dividing extra among all parts
48+
while (extra > 0) {
49+
divisions[l] = divisions[l] + 1;
50+
extra--;
51+
if (l == num - 1) {
52+
l = 0;
53+
} else {
54+
l++;
55+
}
56+
}
57+
}
58+
return divisions;
59+
}
60+
61+
@Override
62+
public ArrayList<Input> divideData(int num) {
63+
if (this.data == null) {
64+
return null;
65+
} else {
66+
int[] divisions = makeDivisions(this.data, num);
67+
ArrayList<Input> result = new ArrayList<Input>(num);
68+
int rowsDone = 0; //number of rows divided so far
69+
for (int i = 0; i < num; i++) {
70+
int rows = divisions[i];
71+
if (rows != 0) {
72+
int[][] divided = new int[rows][this.data[0].length];
73+
for (int j = 0; j < rows; j++) {
74+
divided[j] = this.data[rowsDone + j];
75+
}
76+
rowsDone += rows;
77+
ArrayInput dividedInput = new ArrayInput(divided);
78+
result.add(dividedInput);
79+
} else {
80+
break; //rest of divisions will also be 0
81+
}
82+
}
83+
return result;
84+
}
85+
}
86+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.masterworker;
25+
26+
/**
27+
*Class ArrayResult extends abstract class {@link Result} and contains data
28+
*of type int[][].
29+
*/
30+
31+
public class ArrayResult extends Result<int[][]> {
32+
33+
public ArrayResult(int[][] data) {
34+
super(data);
35+
}
36+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.masterworker;
25+
26+
import java.util.Random;
27+
28+
/**
29+
*Class ArrayUtilityMethods has some utility methods for matrices and arrays.
30+
*/
31+
32+
public class ArrayUtilityMethods {
33+
34+
/**
35+
* Method arraysSame compares 2 arrays @param a1 and @param a2
36+
* and @return whether their values are equal (boolean).
37+
*/
38+
39+
public static boolean arraysSame(int[] a1, int[] a2) {
40+
//compares if 2 arrays have the same value
41+
if (a1.length != a2.length) {
42+
return false;
43+
} else {
44+
boolean answer = false;
45+
for (int i = 0; i < a1.length; i++) {
46+
if (a1[i] == a2[i]) {
47+
answer = true;
48+
} else {
49+
answer = false;
50+
break;
51+
}
52+
}
53+
return answer;
54+
}
55+
}
56+
57+
/**
58+
* Method matricesSame compares 2 matrices @param m1 and @param m2
59+
* and @return whether their values are equal (boolean).
60+
*/
61+
62+
public static boolean matricesSame(int[][] m1, int[][] m2) {
63+
if (m1.length != m2.length) {
64+
return false;
65+
} else {
66+
boolean answer = false;
67+
for (int i = 0; i < m1.length; i++) {
68+
if (arraysSame(m1[i], m2[i])) {
69+
answer = true;
70+
} else {
71+
answer = false;
72+
break;
73+
}
74+
}
75+
return answer;
76+
}
77+
}
78+
79+
/**
80+
* Method createRandomIntMatrix creates a random matrix of size @param rows
81+
* and @param columns @return it (int[][]).
82+
*/
83+
84+
public static int[][] createRandomIntMatrix(int rows, int columns) {
85+
int[][] matrix = new int[rows][columns];
86+
Random rand = new Random();
87+
for (int i = 0; i < rows; i++) {
88+
for (int j = 0; j < columns; j++) {
89+
//filling cells in matrix
90+
matrix[i][j] = rand.nextInt(10);
91+
}
92+
}
93+
return matrix;
94+
}
95+
96+
/**
97+
* Method printMatrix prints input matrix @param matrix.
98+
*/
99+
100+
public static void printMatrix(int[][] matrix) {
101+
//prints out int[][]
102+
for (int i = 0; i < matrix.length; i++) {
103+
for (int j = 0; j < matrix[0].length; j++) {
104+
System.out.print(matrix[i][j] + " ");
105+
}
106+
System.out.println("");
107+
}
108+
}
109+
110+
}

0 commit comments

Comments
 (0)