-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgather_sum1.cpp
More file actions
71 lines (53 loc) · 1.55 KB
/
gather_sum1.cpp
File metadata and controls
71 lines (53 loc) · 1.55 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
//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;
}
//array to save data that recive from scatter
int* receve_data=new int[1000];
//send data for any process with scatter c
MPI_Scatter(InputArr,1000, MPI_INT,receve_data,1000,MPI_INT, 0, MPI_COMM_WORLD);//1000=count of data that send and 0 is the admin id
//sum all number that recive by any process
for (int i = 0; i < 1000; i++)
MySum += receve_data[i];
cout << myid << "\t MySUM = " << MySum << endl;
//array that to save any MYsum that recive from
int AllSum[numprocs];
//gather all mysum taht make by any process and save on Allsum arry
MPI_Gather(&MySum,1,MPI_INT,AllSum,1,MPI_INT,0,MPI_COMM_WORLD);// 1=count of data that want to save
//process that id is 0 sum all number on Allsum
if (myid == 0)
{
int HisSum=0;
for (int i = 0; i < numprocs; i++)
{
HisSum+=AllSum[i];
}
cout << endl << "Sum = " << HisSum << endl;
}
//finalize program
MPI_Finalize();
}