-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi-multiplicador-worker-b.c
65 lines (48 loc) · 1.27 KB
/
mpi-multiplicador-worker-b.c
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
#include <mpi.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#define LINHA 500
#define COLUNA 500
#define PROCS 4
void init_matriz(double matriz[LINHA][COLUNA]){
int i,j;
#pragma omp parallel for num_threads(PROCS) shared(matriz)
for(i = 0; i < LINHA; i++){
for(j = 0; j < COLUNA; j++){
matriz[i][j] = i + j;
}
}
}
void multiplica(double mat1[LINHA][COLUNA], double mat2[LINHA][COLUNA], MPI_Comm parent){
int i,j,k;
double result[LINHA][COLUNA];
#pragma omp parallel for num_threads(PROCS) shared(mat1, mat2, result)
for(i = 0; i < LINHA; i++){
for(j = 0; j < COLUNA; j++){
for(k = 0; k < LINHA; k++){
result[i][j] = result[i][j] + (mat1[i][k] * mat2[k][j]);
}
}
//envia a linha para o no principal para adicao da linha da matriz
MPI_Send(result[i], LINHA, MPI_DOUBLE, 0, i, parent);
}
}
void main(int argc, char** argv){
int nodes, rank;
double c[LINHA][COLUNA];
double d[LINHA][COLUNA];
MPI_Status status;
MPI_Comm parent;
MPI_Init(&argc, &argv);
MPI_Comm_get_parent(&parent);
MPI_Comm_size(MPI_COMM_WORLD, &nodes);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
//inicializa as matrizes C e D
init_matriz(c);
init_matriz(d);
//multiplica as matrizes C e D
multiplica(c, d, parent);
MPI_Finalize();
}