Автор работы: Пользователь скрыл имя, 28 Января 2013 в 16:45, курсовая работа
Розробити схему та описати процедуру перемноження матриці А (розмірністю N1*N2) на матрицю В (розмірністю N2*N3) на структурі з восьми процесорів. Для цієї структури визначити час виконання алгоритму, відсоток послідовної частини алгоритму та ефективність алгоритму.
Список використаної літератури
10. http://www.parallel.ru –
11. http://www.csa.ru –
12. http://www.hpc.nw.ru –
Додаток.Лістинг програми
Processor.java
package nulp.pro.tasks;
publicclass Processor implements Runnable {
privateint[][] matrix1;
privateint[][] matrix2;
privateint[][] newMatrix2;
privateintindexM1;
privateintindexM2;
privateintnewIndexM2;
privateint[][] currentResult;
privateint[][] globalResult = newint[60][67];
privateintmyNumber;
privateintnumberOfMatrix2;
privateintnewNumberOfMatrix2;
private Processor nextProc;
String actionSend = "send data to proc ";
String actionReceived = "received data Matrix2 B";
String actionMultiplyed = "multiplyed data to Matrix2 B";
privatebooleanstart;
privatebooleanreadyForRecived;
privatebooleanreceived;
privateintcycles = 7;
public Processor(int myNumber) {
this.myNumber = myNumber;
numberOfMatrix2 = myNumber;
start = true;
}
@Override
publicvoid run() {
// TODO Auto-generated method stub
currentResult = multiplication();
fillGlobalMatrix();
// //////////////////////////////
while (cycles != 0) {
readyForRecived = true;
sendData();
receivedData();
currentResult = multiplication();
showMyAction(actionMultiplyed, numberOfMatrix2);
fillGlobalMatrix();
cycles--;
}
start = false;
}
publicint[][] multiplication() {
int tmp = 0;
int[][] tmpMatrix = newint[matrix1.length][matrix2
for (int i = 0; i < tmpMatrix.length; i++) {
for (int j = 0; j < tmpMatrix[0].length; j++) {
for (int k = 0; k <matrix1[0].length; k++) {
tmp += matrix1[i][k] * matrix2[k][j];
}
tmpMatrix[i][j] = tmp;
tmp = 0;
}
}
return tmpMatrix;
}
publicvoid fillGlobalMatrix() {
int pseudoI = indexM1 - 1;
for (int i = 0; i <currentResult.length; i++) {
int pseudoJ = indexM2 - 1;
for (int j = 0; j <currentResult[0].length; j++) {
globalResult[pseudoI][pseudoJ] = currentResult[i][j];
pseudoJ++;
}
pseudoI++;
}
}
privatevoid showMyAction(String action, int proc) {
System.out.println(myNumber + " " + action + proc);
}
privatevoid sendData() {
while (start) {
if (nextProc.readyForRecived) {
nextProc.readyForRecived = false;
nextProc.setNewMatrix2(matrix2
nextProc.setNewIndexM2(indexM2
nextProc.
nextProc.setReceived(true);
showMyAction(actionSend, nextProc.getMyNumber());
break;
}
}
}
privatevoid receivedData() {
while (start) {
if (received) {
received = false;
matrix2 = newMatrix2;
indexM2 = newIndexM2;
numberOfMatrix2 = newNumberOfMatrix2;
showMyAction(actionReceived, numberOfMatrix2);
break;
}
}
}
publicvoid setIndexM1(int indexM1) {
this.indexM1 = indexM1;
}
publicvoid setIndexM2(int indexM2) {
this.indexM2 = indexM2;
}
publicvoid setNewMatrix2(int[][] newMatrix2) {
this.newMatrix2 = newMatrix2;
}
publicvoid setNewIndexM2(int newIndexM2) {
this.newIndexM2 = newIndexM2;
}
publicvoid setNewNumberOfMatrix2(int newNumberOfMatrix2) {
this.newNumberOfMatrix2 = newNumberOfMatrix2;
}
publicvoid setReceived(boolean received) {
this.received = received;
}
publicint getMyNumber() {
returnmyNumber;
}
publicvoid setMatrix1(int[][] matrix1) {
this.matrix1 = matrix1;
}
publicvoid setMatrix2(int[][] matrix2) {
this.matrix2 = matrix2;
}
publicint[][] getMatrix1() {
returnmatrix1;
}
publicint[][] getMatrix2() {
returnmatrix2;
}
publicvoid setNextProc(Processor nextProc) {
this.nextProc = nextProc;
}
publicboolean isStart() {
returnstart;
}
publicint[][] getGlobalResult() {
returnglobalResult;
}
}
LoadingData.java
package nulp.pro.tasks;
import java.util.Random;
import java.util.Scanner;
public class LoadingData {
private int[][] matrix1;
private int[][] matrix2;
private int[][] tmpMatrix;
private int n1 = 60;
private int n2 = 368;
private int n3 = 67;
private Input input = new Input();
private Output output = new Output();
Scanner scanner = new Scanner(System.in);
public void loadMatrix() {
System.out.println("Generate press 1 ");
System.out.println("Read from file press 2 ");
int choise = scanner.nextInt();
boolean tmp = true;
while (tmp) {
switch (choise) {
case 1:
matrix1 = fillMatrix(n1, n2);
output.writeFile(matrix1, "inputMatrix1.txt");
matrix2 = fillMatrix(n2, n3);
output.writeFile(matrix2, "inputMatrix2.txt");
tmp = false;
break;
case 2:
input.readFile("inputMatrix1.
matrix1 = input.parseToArray();
input.readFile("inputMatrix2.
matrix2 = input.parseToArray();
tmp = false;
break;
default:
System.out.println("Error comand, try again");
}
}
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.print(matrix1[i][j] + " ");
}
System.out.println();
}
}
public void dividedByProcessors(Processor proc1, Processor proc2,
Processor proc3, Processor proc4, Processor proc5, Processor proc6,
Processor proc7, Processor proc8) {
proc1.setMatrix1(
proc1.setMatrix2(
proc1.setIndexM1(1);
proc1.setIndexM2(1);
proc1.setNextProc(proc3);
proc2.setMatrix1(
proc2.setMatrix2(
proc2.setIndexM1(9);
proc2.setIndexM2(42);
proc2.setNextProc(proc1);
proc3.setMatrix1(
proc3.setMatrix2(
proc3.setIndexM1(17);
proc3.setIndexM2(83);
proc3.setNextProc(proc5);
proc4.setMatrix1(
proc4.setMatrix2(
proc4.setIndexM1(25);
proc4.setIndexM2(124);
proc4.setNextProc(proc7);
proc5.setMatrix1(
proc5.setMatrix2(
proc5.setIndexM1(33);
proc5.setIndexM2(165);
proc5.setNextProc(proc4);
proc6.setMatrix1(
proc6.setMatrix2(
proc6.setIndexM1(40);
proc6.setIndexM2(206);
proc6.setNextProc(proc8);
proc7.setMatrix1(
proc7.setMatrix2(
proc7.setIndexM1(47);
proc7.setIndexM2(246);
proc7.setNextProc(proc6);
proc8.setMatrix1(
proc8.setMatrix2(
proc8.setIndexM1(54);
proc8.setIndexM2(286);
proc8.setNextProc(proc2);
}
private int[][] fillMatrix(int m, int n) {
int[][] matrix = new int[m][n];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = getNewDigit();
// matrix[i][j] = 1;
}
}
return matrix;
}
private int getNewDigit() {
Random random = new Random();
int res = random.nextInt(25);
return res;
}
public int[][] getMatrix1() {
return matrix1;
}
public int[][] getMatrix2() {
return matrix2;
}
private int[][] getPartOfMatrix1(int from, int quantity) {
from--;
int k = 0;
tmpMatrix = new int[quantity][matrix1[0].lengt
for (int i = 0; i < matrix1.length; i++) {
if (i == from && quantity!=0) {
for (int j = 0; j < matrix1[0].length; j++) {
tmpMatrix[k][j] = matrix1[i][j];
}
from++;
k++;
quantity--;
}
}
return tmpMatrix;
}
private int[][] getPartOfMatrix2(int from, int quantity) {
from--;
int k = 0;
tmpMatrix = new int[matrix2.length][quantity];
for (int j = 0; j < matrix2[0].length; j++) {
if(j==from && quantity!=0){
for(int i = 0; i<matrix2.length;i++){
tmpMatrix[i][k] = matrix2[i][j];
}
k++;
from++;
quantity--;
}
}
return tmpMatrix;
}
}
ParalelSystem.java
package nulp.pro.tasks;
publicclass ParalelSystem {
privateint[][] matrixResult = newint[60][67];
Processor proc1 = new Processor(1);
Processor proc2 = new Processor(2);
Processor proc3 = new Processor(3);
Processor proc4 = new Processor(4);
Processor proc5 = new Processor(5);
Processor proc6 = new Processor(6);
Processor proc7 = new Processor(7);
Processor proc8 = new Processor(8);
private LoadingData load = new LoadingData();
private Output output = new Output();
public ParalelSystem(){
load.loadMatrix();
}
publicvoid run(){
load.dividedByProcessors(proc1
Thread t1 = new Thread(proc1);
Thread t2 = new Thread(proc2);
Thread t3 = new Thread(proc3);
Thread t4 = new Thread(proc4);
Thread t5 = new Thread(proc5);
Thread t6 = new Thread(proc6);
Thread t7 = new Thread(proc7);
Thread t8 = new Thread(proc8);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
t8.start();
while(true) {
if(!proc1.isStart() && !proc2.isStart() && !proc3.isStart() && !proc4.isStart()
&& !proc5.isStart() && !proc6.isStart() && !proc7.isStart() && !proc8.isStart()) {
for (int i = 0; i <matrixResult.length; i++) {
for (int j = 0; j <matrixResult[0].length; j++) {
matrixResult[i][j] = proc1.getGlobalResult()[i][j] + proc2.getGlobalResult()[i][j]
+ proc3.getGlobalResult()[i][j] + proc4.getGlobalResult()[i][j] + proc5.getGlobalResult()[i][j]
+ proc6.getGlobalResult()[i][j] + proc7.getGlobalResult()[i][j] + proc8.getGlobalResult()[i][j];
}
}
break;
}
}
output.writeFile(matrixResult, "outputMatrix.txt");
System.out.println("The end");
}
}
Информация о работе Паралельне виконання операцій множення матриць