-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_sum.cpp
More file actions
70 lines (59 loc) · 1.58 KB
/
simple_sum.cpp
File metadata and controls
70 lines (59 loc) · 1.58 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
//import module
#include <iostream>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include "mpi.h"
using namespace std;
//main function
int main(int argc, char *argv[])
{//initialise mpi
int *InputArr = NULL;
int myid, numprocs;
MPI_Status status;
int MySum = 0;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
// Generate data and Print it too see (unsorted)
if (myid == 0)
{
InputArr = new int[numprocs * 1000];
srand(time(NULL));
for (int i = 0; i<numprocs * 1000; i++)
InputArr[i] = rand() % 1000;
for (int i = 0; i<numprocs * 1000; i++)
cout << InputArr[i] << "\t";
cout << endl;
//send data to anyprocess by process that id is 0
for (int i = 1; i < numprocs; i++)
MPI_Send(&InputArr[i * 1000], 1000, MPI_INT, i, 0, MPI_COMM_WORLD);//send data for any process
}
else
{
InputArr = new int[1000];
MPI_Recv(&InputArr[0], 1000, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);//recive that and put them on inputarr
}
//any process sum all of number that recieved
for (int i = 0; i < 1000; i++)
MySum += InputArr[i];
cout << myid << "\t MySUM = " << MySum << endl;
if (myid == 0)
{
for (int i = 1; i < numprocs; i++)
{
int HisSum;
//process that id is 0 sum all mysum that recive from any process
MPI_Recv(&HisSum, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &status);
MySum += HisSum;
}
cout << endl << "Sum = " << MySum << endl;
}
else
{
//other process without 0 send mysum to process 0
MPI_Send(&MySum, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
//finalize program
MPI_Finalize();
}