-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallelSum.c
190 lines (135 loc) · 4.02 KB
/
parallelSum.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mpi.h>
#include <sys/time.h>
#include <time.h>
#define MIN 0
#define MAX 5
//Citing Sources: https://www.geeksforgeeks.org/generating-random-number-range-c/
// http://homepages.math.uic.edu/~jan/mcs572/using_mpi.pdf
// http://homepages.math.uic.edu/~jan/mcs572/mcs572notes/lec05.html
int* generateRands(int lower, int upper, int count){
int i;
int *num_array = (int *)malloc(sizeof(int)*count);
if(num_array == NULL){
printf("malloc failed");
exit(1);
}
srand(53);
for(i = 0; i < count; i++){
num_array[i] = (rand() % (upper-lower+1))+lower;
}
return num_array;
}
long long serialSum(long randNums, int *numArray){
int i;
long sum = 0;
for(i=0; i<randNums; i++){
sum += numArray[i];
}
return sum;
}
long long pTopSum(long randNums, int *numArray, int np, int pid, MPI_Status status){
int load = randNums/np;
int excess = randNums % np;
int sum = 0;
int i;
int ptp_sum = 0;
if (pid == 0)
{
for (i = 0; i < load; i++)
{
ptp_sum += numArray[pid*load+i];
}
for(i = 1; i < np; i++){
MPI_Recv(&sum, 1, MPI_INT, i, 1, MPI_COMM_WORLD, &status);
ptp_sum += sum;
}
}else{
if(pid == (np-1)){
for(i = 0; i < (load + excess); i++){
sum += numArray[pid*load+i];
}
}else{
for(i = 0; i < load; i++){
sum += numArray[pid*load+i];
}
}
MPI_Send(&sum, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
}
if(pid == 0){
return ptp_sum;
}
return 0;
}
long long collectiveSum(long randNums, int *numArray, int np, int pid){
int load = randNums/np;
long long coll_sum = 0;
int i;
int *output = (int*) malloc(sizeof(int)* randNums);
int excess = randNums % np;
if(pid == (np-1)){
for(i = 0; i < (load+excess); i++){
coll_sum += numArray[pid*load+i];
}
}else{
for(i =0; i < load; i++){
coll_sum += numArray[pid*load+i];
}
}
MPI_Reduce(&coll_sum, output, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if(pid == 0){
return output[0];
}
return 0;
}
int main(int argc, char **argv){
int np, pid;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &pid);
MPI_Comm_size(MPI_COMM_WORLD, &np);
long randNums;
int *numArray;
if(argc != 2){
printf("Invalid number of argument\n");
return 1;
}
randNums = atoi(argv[1]);
randNums *= 100000;
numArray = generateRands(MIN, MAX, randNums);
// SERIAL SUMMATION!
clock_t begin = clock();
if(pid==0){
long long serial = serialSum(randNums, numArray);
printf("The serial sum is %d\n", serial);
clock_t end = clock();
double time_spent = (double)(end - begin);
printf("Time spent in serial: %f\n", (float)time_spent/CLOCKS_PER_SEC);
printf("\n");
}
MPI_Barrier(MPI_COMM_WORLD);
// POINT TO POINT SUMMATION!
clock_t begin1 = clock();
long long ptop = pTopSum(randNums, numArray, np, pid, status);
if(pid==0){
printf("The point to point sum is %d\n", ptop);
clock_t end1 = clock();
double time_spent1 = (double)(end1 - begin1);
printf("Time spent in point to point: %f\n", (float)time_spent1/CLOCKS_PER_SEC);
printf("\n");
}
MPI_Barrier(MPI_COMM_WORLD);
// COLLECTIVE COMMUNICATION SUMMATION!
clock_t begin2 = clock();
long long collSum = collectiveSum(randNums, numArray, np, pid);
if(pid==0){
printf("The collective sum is %d\n", collSum);
clock_t end2 = clock();
double time_spent2 = (double)(end2 - begin2);
printf("Time spent in collective communication: %f\n", (float)time_spent2/CLOCKS_PER_SEC);
}
MPI_Finalize();
return 0;
}