forked from DanQuenaz/MPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrizes.cpp
139 lines (118 loc) · 3.13 KB
/
matrizes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "mpi.h"
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <map>
using namespace std;
//Tamanho das matrizes -> A[l1][c1] X B[l2][c2] = C[l1][c2]
#define l1 2
#define c1 3
#define l2 3
#define c2 2
void preencheMatrizes(int a[][c1], int b[][c2], int c[][c2], int d[][c2]){
for(int i=0; i<l1; ++i){
for(int j=0; j<c1; ++j){
a[i][j] = rand()%10;
}
}
for(int i=0; i<l2; ++i){
for(int j=0; j<c2; ++j){
b[i][j] = rand()%10;
}
}
for(int i=0; i<l1; ++i){
for(int j=0; j<c2; ++j){
c[i][j] = 0;
d[i][j] = 0;
}
}
}
void imprimeResultado(int a[][c1], int b[][c2], int c[][c2], int d[][c2]){
cout<<"MATRIZ A: "<<endl;
for(int i=0; i<l1; ++i){
for(int j=0; j<c1; ++j){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
cout<<"MATRIZ B: "<<endl;
for(int i=0; i<l2; ++i){
for(int j=0; j<c2; ++j){
cout<<b[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
cout<<"MATRIZ D = A x B: "<<endl;
for(int i=0; i<l1; ++i){
for(int j=0; j<c2; ++j){
cout<<d[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
}
void pirntC(int c[][c2]){
cout<<"MATRIZ C: "<<endl;
for(int i=0; i<l1; ++i){
for(int j=0; j<c2; ++j){
cout<<c[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
}
int main( int argc, char **argv ){
int rank, size;
int intervalos[3] = {0, 0, 0};
//a x b = d
int a[l1][c1];
int b[l2][c2];
int c[l1][c2];
int d[l1][c2];
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
srand(816823);
preencheMatrizes(a, b, c, d);
if(rank==0){
int totalOp = l1*c2;
int line = 0;
int col = 0;
int node = 1;
for(int i=0; i<totalOp; ++i){
intervalos[0] = line;
intervalos[1] = col++;
intervalos[2] = node++; if(node >= size) node = 1;
if(col >= c2){
col = 0;
line++;
}
MPI_Bcast(intervalos, 3, MPI_INT, 0, MPI_COMM_WORLD);
}
intervalos[0] = -1;
MPI_Bcast(intervalos, 3, MPI_INT, 0, MPI_COMM_WORLD);
}else{
while(intervalos[0] != -1){
MPI_Bcast(intervalos, 3, MPI_INT, 0, MPI_COMM_WORLD);
if(intervalos[2] == rank && intervalos[0] != -1){
for(int i=0; i<c1; ++i){
c[intervalos[0]][intervalos[1]] += a[intervalos[0]][i] * b[i][intervalos[1]];
}
}
}
}
for(int i = 0; i<l1; ++i){
for(int j=0; j<c2; ++j){
MPI_Reduce(&(c[i][j]), &(d[i][j]), 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
}
}
if(rank==0){
imprimeResultado(a, b, c, d);
}
MPI_Finalize();
}