-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathejercicio3.c
More file actions
55 lines (45 loc) · 1.25 KB
/
ejercicio3.c
File metadata and controls
55 lines (45 loc) · 1.25 KB
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
/*tree
*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
const int MAX_CONTRIB = 10;
int Global_sum(int my_contrib, int my_rank, int p, MPI_Comm comm);
int main(int argc, char* argv[]) {
int p, my_rank;
MPI_Comm comm;
int my_contrib;
int sum;
MPI_Init(&argc, &argv);
comm = MPI_COMM_WORLD;
MPI_Comm_size(comm, &p);
MPI_Comm_rank(comm, &my_rank);
srandom(my_rank+1);
my_contrib = random() % MAX_CONTRIB;
printf("Proc %d > my_contrib = %d\n", my_rank, my_contrib);
sum = Global_sum(my_contrib, my_rank, p, comm);
if (my_rank == 0)
printf("Proc %d > global sum = %d\n", my_rank, sum);
MPI_Finalize();
return 0;
}
int Global_sum(int my_contrib, int my_rank, int p, MPI_Comm comm) {
int sum = my_contrib;
int temp;
int partner;
int done = 0;
unsigned bitmask = 1;
MPI_Status status;
while (!done && bitmask < p) {
partner = my_rank ^ bitmask;
if (my_rank < partner) {
MPI_Recv(&temp, 1, MPI_INT, partner, 0, comm, &status);
sum += temp;
bitmask <<= 1;
} else {
MPI_Send(&sum, 1, MPI_INT, partner, 0, comm);
done = 1;
}
}
return sum;
}