-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpi1.c
More file actions
45 lines (37 loc) · 849 Bytes
/
mpi1.c
File metadata and controls
45 lines (37 loc) · 849 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
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.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;
printf("no is %d\n", ans);
}
if(rank != 0)
{
MPI_Send(&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, MPI_STATUS_IGNORE);
total += ans;
}
printf("Total is %d\n", total);
printf("Total Nodes is %d\n", nodes);
}
MPI_Finalize();
return 0;
}