-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal_sum_rp.c
More file actions
103 lines (89 loc) · 2.88 KB
/
global_sum_rp.c
File metadata and controls
103 lines (89 loc) · 2.88 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
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
/* File: global_sum_rp.c
*
* Purpose: Driver for a program that uses MPI to implement a global
* sum
*
* Input: None.
* Output: Random values generated by processes and sum of random
* values on each process.
*
* Compile: mpicc -g -Wall -o gsrp global_sum_rp.c
* Run: mpiexec -n <number of processes> gsrp
*
* Notes:
* 1. The result returned by all the processes should be valid.
* 2. The Global_sum function uses a ``ring-pass''.
*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
const int MAX_CONTRIB = 20;
int Global_sum(int my_contrib, int my_rank, int p, MPI_Comm comm);
void Print_results(char title[], int value, int my_rank, int p,
MPI_Comm comm);
int main(void) {
int p, my_rank;
MPI_Comm comm;
int my_contrib;
int sum;
MPI_Init(NULL, NULL);
comm = MPI_COMM_WORLD;
MPI_Comm_size(comm, &p);
MPI_Comm_rank(comm, &my_rank);
/* Generate a random int */
srandom(my_rank);
my_contrib = random() % MAX_CONTRIB;
Print_results("Process Values", my_contrib, my_rank, p, comm);
sum = Global_sum(my_contrib, my_rank, p, comm);
Print_results("Process Totals", sum, my_rank, p, comm);
MPI_Finalize();
return 0;
}
/*---------------------------------------------------------------
* Function: Print_results
* Purpose: Gather an int from each process onto process 0 and
* print the values.
* In args:
* title: the title of the output
* value: the int contributed by each process
* my_rank, p, comm: the usual MPI values
*/
void Print_results(char title[], int value, int my_rank, int p,
MPI_Comm comm) {
int* vals = NULL, q;
if (my_rank == 0) {
vals = malloc(p*sizeof(int));
MPI_Gather(&value, 1, MPI_INT, vals, 1, MPI_INT, 0, comm);
printf("%s:\n", title);
for (q = 0; q < p; q++)
printf("Proc %d > %d\n", q, vals[q]);
printf("\n");
free(vals);
} else {
MPI_Gather(&value, 1, MPI_INT, vals, 1, MPI_INT, 0, comm);
}
} /* Print_results */
/*-----------------------------------------------------------------
* Function: Global_sum
* Purpose: Compute the global sum of ints stored on the processes
*
* Input args: my_contrib = process's contribution to the global sum
* my_rank = process's rank
* p = number of processes
* comm = communicator
* Return val: Sum of each process's my_contrib: valid on all
* processes
*
*/
int Global_sum(int my_contrib, int my_rank, int p, MPI_Comm comm) {
int sum, temp, i;
int dest = (my_rank + 1) % p;
int source = (my_rank - 1 + p) % p;
temp = sum = my_contrib;
for (i = 1; i < p; i++) {
MPI_Send(&temp, 1, MPI_INT, dest, 0, comm);
MPI_Recv(&temp, 1, MPI_INT, source, 0, comm, MPI_STATUS_IGNORE);
sum += temp;
}
return sum;
} /* Global_sum */