-
Notifications
You must be signed in to change notification settings - Fork 0
/
avg2.c
56 lines (45 loc) · 1.31 KB
/
avg2.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
/*
Parallel Programming
By : Abdallah Okasha
FCI-CU
*/
#include <stdio.h>
#include <string.h>
#include "mpi.h"
int main(int argc, char * argv[]) {
int my_rank, p, des, source = 0, tag = 0, size;
int i, j, k, subSize, subSum = 0, totalSum = 0, global = 0;
int *sumsArray, *inputArray, *subArray;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
if (my_rank == 0) {
printf("Enter your array size:\n");
scanf("%d", &size);
inputArray = malloc(sizeof(int) * size);
printf("ENter array numbers:\n");
for (i = 0; i < size; ++i) {
scanf("%d", &inputArray[i]);
}
subSize = size / p;
sumsArray = malloc(sizeof(int) * p);
}
MPI_Bcast(&subSize, 1, MPI_INT, 0, MPI_COMM_WORLD);
subArray = malloc(sizeof(int) * subSize);
MPI_Scatter(inputArray, subSize, MPI_INT, subArray, subSize, MPI_INT,
source, MPI_COMM_WORLD);
for (i = 0; i < subSize; ++i){
subSum += subArray[i];
printf("sub array %d ", subArray[i]);
}
printf("\n");
MPI_Reduce(&subSum, &totalSum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (my_rank == 0) {
for (i = p * subSize; i < size; ++i)
totalSum += inputArray[i];
printf("Summation of all numbers in array = %d \n", totalSum);
}
MPI_Finalize();
return 0;
}