-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPIsum.c
More file actions
38 lines (30 loc) · 767 Bytes
/
MPIsum.c
File metadata and controls
38 lines (30 loc) · 767 Bytes
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
#include<stdio.h>
#include<mpi.h>
static int rank, nodes;
int main()
{
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &nodes);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Status status;
int ans = 0;
int total = 0;
int start = rank * 1000;
int end = start + 999;
for(int i = start; i <= end; i++) {
ans = ans + i;
}
if(rank != 0) {
MPI_Ssend(&ans, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
} else {
total = ans;
for(int j = 1; j < 10; j++) {
MPI_Recv(&ans, 1, MPI_INT, j, 0, MPI_COMM_WORLD, &status);
total += ans;
}
printf("Total is %d\n", total);
printf("Total Nodes is %d\n", nodes);
}
MPI_Finalize();
return 0;
}