-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram.c
67 lines (61 loc) · 1.74 KB
/
histogram.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>
int main (int argc, char **argv)
{
/*Part 1, open the file*/
int rank, totalProcesses, i = 0, totalBins = 0, totalInts;
MPI_Init (&argc, &argv);
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
MPI_Comm_size (MPI_COMM_WORLD, &totalProcesses);
FILE *fptr;
if(argc != 3 ){
printf("Program receives two inputs: number of bins and a filename.\n");
exit(1);
}
if((fptr = fopen(argv[2], "r")) == NULL )
{
printf("Cannot read the file %s\n", argv[2]);
exit(1);
}
totalBins = atoi(argv[1]);
fscanf(fptr, "%d,", &totalInts);
int* myArray = (int*)malloc(totalInts * sizeof(int));
while(!feof(fptr)){
fscanf(fptr, "%d,", &myArray[i] );
i++;
}
/*for (i = 0; i < totalBins; i++){
printf("Number is: %d\n\n", myArray[i] );
}*/
fclose(fptr);
int bin[totalBins], result[totalBins];
for(i = 0; i < totalBins; i++){
bin[i] = 0;
}
/*Part 1 ends*/
/*Part 2, create bin and assign*/
int element = totalInts / totalProcesses;
int bar = 100 / totalBins;
if(rank < totalProcesses - 1){
for(i = rank * element; i < (rank + 1) * element; i++){
bin[myArray[i] / bar] ++;
}
}else{
for(i = rank * element; i < totalInts; i++){
bin[myArray[i] / bar] ++;
}
}
/*Part 2 ends*/
/*Part 3, combine*/
MPI_Reduce(&bin, &result, totalBins, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if(rank == 0){
for(i = 0; i < totalBins; i++){
printf("bin[%d] = %d\n", i, result[i]);
}
}
MPI_Finalize ();
/*Part 3 ends*/
return 0;
}