-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpiEx5a.c
64 lines (56 loc) · 1.3 KB
/
mpiEx5a.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
#include <mpi.h>
#include <stdio.h>
#define MAXARRAY 20
int iniarray(int n, int *array) {
int i;
for (i = 0; i < n; i++)
array[i] = i;
return i;
}
void printarray(int n, int *array) {
int i = 0;
printf("[ ");
for (; i < n; i++) {
if (i == n - 1)
printf("%d ", array[i]);
else
printf("%d, ", array[i]);
}
printf("]\n");
}
void myBcast(void * data, int numelem, MPI_Datatype datatype, int root, MPI_Comm communicator){
int size = 0, rank = 0;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Status Stat;
if(rank==root){
for(int i=0;i<size;++i){
if(i != root){
MPI_Send(data,numelem,datatype, i, 1, communicator);
}
}
}else{
MPI_Recv(data, numelem, datatype, root, 1, communicator, &Stat);
}
}
int main(int argc, char** argv) {
int arreglo[MAXARRAY];
int rank;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
iniarray(MAXARRAY, arreglo);
myBcast(arreglo, MAXARRAY, MPI_INT, 0, MPI_COMM_WORLD);
} else {
if (rank == 1) {
printf("Imprimiendo arreglo desde [%d]\n", rank);
printarray(MAXARRAY, arreglo);
}
myBcast(arreglo, MAXARRAY, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 1) {
printf("Imprimiendo arreglo desde [%d]\n", rank);
printarray(MAXARRAY, arreglo);
}
}
MPI_Finalize();
}