3
3
#include < malloc.h>
4
4
#include < omp.h>
5
5
#include < mpi.h>
6
+ #include < iostream>
7
+ #include < algorithm>
6
8
using namespace std ;
7
9
8
10
int NumberOfPoints = 0 ;
9
11
int max_value = -1 ;
10
12
int *p;
11
- int arr[100 ];
12
- int *ReadFromFile ()
13
+ int *arr;
14
+
15
+ int * ReadFromFile ()
13
16
{
14
17
int *points;
15
18
FILE *file;
@@ -23,8 +26,8 @@ int *ReadFromFile()
23
26
while (fgets (line, sizeof (line), file))
24
27
NumberOfPoints++;
25
28
fclose (file);
26
- int size = NumberOfPoints;
27
- points = (int *)malloc (size * sizeof (int ));
29
+
30
+ points = (int *)malloc (NumberOfPoints * sizeof (int )); // Allocate memory for points
28
31
if (points == NULL ) {
29
32
perror (" Error allocating memory" );
30
33
return NULL ;
@@ -36,6 +39,7 @@ int *ReadFromFile()
36
39
free (points);
37
40
return NULL ;
38
41
}
42
+
39
43
while (fgets (line, sizeof (line), file)) {
40
44
points[index++] = atoi (line);
41
45
}
@@ -47,7 +51,7 @@ int main(int argc, char **argv)
47
51
{
48
52
int indexx = 0 ;
49
53
int *points, Bars, np, Range, tmp_Range = 0 ,
50
- Points_per_process;
54
+ Points_per_process;
51
55
int size;
52
56
int *irecv;
53
57
int *AllCount, *count;
@@ -67,7 +71,6 @@ int main(int argc, char **argv)
67
71
cout << " Enter the number of bars" << endl;
68
72
cin >> Bars;
69
73
70
-
71
74
points = ReadFromFile ();
72
75
Points_per_process = ((double )NumberOfPoints / (NumberOfprocess)) + 0.5 ;
73
76
@@ -76,14 +79,15 @@ int main(int argc, char **argv)
76
79
size = NumberOfPoints;
77
80
78
81
p = (int *)malloc (size * sizeof (int ));
79
-
80
82
for (i = 0 ; i < size; i++)
81
83
{
82
84
if (i < NumberOfPoints)
83
85
p[i] = points[i];
84
86
else
85
87
p[i] = -1 ;
86
88
}
89
+
90
+ max_value = *std::max_element (p, p + NumberOfPoints); // Find max value for Range
87
91
Range = max_value / Bars;
88
92
if (max_value % Bars != 0 )
89
93
{
@@ -94,66 +98,70 @@ int main(int argc, char **argv)
94
98
95
99
MPI_Bcast (&size, 1 , MPI_INT, 0 , MPI_COMM_WORLD);
96
100
MPI_Bcast (&Bars, 1 , MPI_INT, 0 , MPI_COMM_WORLD);
97
-
98
- irecv = (int *)malloc (size * sizeof (int *));
99
- count = (int *)malloc (Bars * sizeof (int *));
100
-
101
101
MPI_Bcast (&Range, 1 , MPI_INT, 0 , MPI_COMM_WORLD);
102
102
MPI_Bcast (&Points_per_process, 1 , MPI_INT, 0 , MPI_COMM_WORLD);
103
- MPI_Scatter (p, Points_per_process, MPI_INT, irecv, Points_per_process, MPI_INT, 0 , MPI_COMM_WORLD);
104
103
104
+ irecv = (int *)malloc (Points_per_process * sizeof (int ));
105
+ count = (int *)malloc (Bars * sizeof (int ));
105
106
for (l = 0 ; l < Bars; l++)
106
107
{
107
108
count[l] = 0 ;
108
109
}
109
110
110
- #pragma omp parallel shared(AllCount)
111
+ MPI_Scatter (p, Points_per_process, MPI_INT, irecv, Points_per_process, MPI_INT, 0 , MPI_COMM_WORLD);
112
+
113
+ // Dynamically allocate arr based on Points_per_process
114
+ arr = (int *)malloc (Points_per_process * sizeof (int ));
115
+
116
+ #pragma omp parallel shared(count)
111
117
{
112
118
#pragma omp for schedule(static)
113
119
for (i = 0 ; i < Points_per_process; i++)
114
120
{
115
121
for (l = 0 ; l < Bars; l++)
116
122
{
117
- if (irecv[i] <= l * Range + Range && irecv[i] != -1 )
123
+ if (irecv[i] <= (l + 1 ) * Range && irecv[i] != -1 )
118
124
{
119
125
count[l]++;
120
- arr[indexx++ ] = l;
126
+ arr[i ] = l; // Store the bar index in arr
121
127
break ;
122
128
}
123
129
}
124
130
}
125
131
}
126
- free (irecv);
127
- AllCount = (int *)malloc (NumberOfPoints * sizeof (int ));
128
- MPI_Gather (arr, indexx, MPI_INT, AllCount, indexx, MPI_INT, 0 , MPI_COMM_WORLD);
132
+
133
+ AllCount = (int *)malloc (NumberOfPoints * sizeof (int )); // Gather result
134
+
135
+ MPI_Gather (arr, Points_per_process, MPI_INT, AllCount, Points_per_process, MPI_INT, 0 , MPI_COMM_WORLD);
129
136
130
137
if (rank == 0 )
131
138
{
132
- // count = malloc(Bars * sizeof(int));
133
- for (l = 0 ; l < Bars; l ++)
139
+ // Count the final distribution of points across bars
140
+ for (i = 0 ; i < Bars; i ++)
134
141
{
135
- count[l ] = 0 ;
142
+ count[i ] = 0 ;
136
143
}
137
144
138
- for (i = 0 ; i < Bars ; i++)
145
+ for (i = 0 ; i < NumberOfPoints ; i++)
139
146
{
140
- for (j = 0 ; j < NumberOfPoints; j++ )
147
+ if (AllCount[i] != - 1 )
141
148
{
142
- if (AllCount[j] == i && AllCount[j] != -1 )
143
- {
144
- count[i]++;
145
- }
149
+ count[AllCount[i]]++;
146
150
}
147
151
}
148
152
149
153
for (i = 0 ; i < Bars; i++)
150
154
{
151
155
cout << " Bar " << i << " has " << count[i] << " points" << endl;
152
156
}
153
-
157
+
158
+ free (AllCount);
154
159
}
155
- MPI_Finalize ();
156
-
157
160
161
+ free (irecv);
162
+ free (count);
163
+ free (arr);
164
+
165
+ MPI_Finalize ();
158
166
return 0 ;
159
- }
167
+ }
0 commit comments