Skip to content

Commit abfb65e

Browse files
authored
Merge pull request #32 from dheermrt/main
LU Factorisation #10 issue and Histogram #5 issue
2 parents 258f64f + c098452 commit abfb65e

File tree

3 files changed

+58
-39
lines changed

3 files changed

+58
-39
lines changed

LU_factorisation

17.2 KB
Binary file not shown.

src/Histogram/histo.cpp

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
#include <malloc.h>
44
#include <omp.h>
55
#include <mpi.h>
6+
#include <iostream>
7+
#include<algorithm>
68
using namespace std;
79

810
int NumberOfPoints = 0;
911
int max_value = -1;
1012
int *p;
11-
int arr[100];
12-
int *ReadFromFile()
13+
int *arr;
14+
15+
int* ReadFromFile()
1316
{
1417
int *points;
1518
FILE *file;
@@ -23,8 +26,8 @@ int *ReadFromFile()
2326
while (fgets(line, sizeof(line), file))
2427
NumberOfPoints++;
2528
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
2831
if (points == NULL) {
2932
perror("Error allocating memory");
3033
return NULL;
@@ -36,6 +39,7 @@ int *ReadFromFile()
3639
free(points);
3740
return NULL;
3841
}
42+
3943
while (fgets(line, sizeof(line), file)) {
4044
points[index++] = atoi(line);
4145
}
@@ -47,7 +51,7 @@ int main(int argc, char **argv)
4751
{
4852
int indexx = 0;
4953
int *points, Bars, np, Range, tmp_Range = 0,
50-
Points_per_process;
54+
Points_per_process;
5155
int size;
5256
int *irecv;
5357
int *AllCount, *count;
@@ -67,7 +71,6 @@ int main(int argc, char **argv)
6771
cout << "Enter the number of bars" << endl;
6872
cin >> Bars;
6973

70-
7174
points = ReadFromFile();
7275
Points_per_process = ((double)NumberOfPoints / (NumberOfprocess)) + 0.5;
7376

@@ -76,14 +79,15 @@ int main(int argc, char **argv)
7679
size = NumberOfPoints;
7780

7881
p = (int*)malloc(size * sizeof(int));
79-
8082
for (i = 0; i < size; i++)
8183
{
8284
if (i < NumberOfPoints)
8385
p[i] = points[i];
8486
else
8587
p[i] = -1;
8688
}
89+
90+
max_value = *std::max_element(p, p + NumberOfPoints); // Find max value for Range
8791
Range = max_value / Bars;
8892
if (max_value % Bars != 0)
8993
{
@@ -94,66 +98,70 @@ int main(int argc, char **argv)
9498

9599
MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD);
96100
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-
101101
MPI_Bcast(&Range, 1, MPI_INT, 0, MPI_COMM_WORLD);
102102
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);
104103

104+
irecv = (int*)malloc(Points_per_process * sizeof(int));
105+
count = (int*)malloc(Bars * sizeof(int));
105106
for (l = 0; l < Bars; l++)
106107
{
107108
count[l] = 0;
108109
}
109110

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)
111117
{
112118
#pragma omp for schedule(static)
113119
for (i = 0; i < Points_per_process; i++)
114120
{
115121
for (l = 0; l < Bars; l++)
116122
{
117-
if (irecv[i] <= l * Range + Range && irecv[i] != -1)
123+
if (irecv[i] <= (l + 1) * Range && irecv[i] != -1)
118124
{
119125
count[l]++;
120-
arr[indexx++] = l;
126+
arr[i] = l; // Store the bar index in arr
121127
break;
122128
}
123129
}
124130
}
125131
}
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);
129136

130137
if (rank == 0)
131138
{
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++)
134141
{
135-
count[l] = 0;
142+
count[i] = 0;
136143
}
137144

138-
for (i = 0; i < Bars; i++)
145+
for (i = 0; i < NumberOfPoints; i++)
139146
{
140-
for (j = 0; j < NumberOfPoints; j++)
147+
if (AllCount[i] != -1)
141148
{
142-
if (AllCount[j] == i && AllCount[j] != -1)
143-
{
144-
count[i]++;
145-
}
149+
count[AllCount[i]]++;
146150
}
147151
}
148152

149153
for (i = 0; i < Bars; i++)
150154
{
151155
cout << "Bar " << i << " has " << count[i] << " points" << endl;
152156
}
153-
157+
158+
free(AllCount);
154159
}
155-
MPI_Finalize();
156-
157160

161+
free(irecv);
162+
free(count);
163+
free(arr);
164+
165+
MPI_Finalize();
158166
return 0;
159-
}
167+
}

src/LU_factorisation.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ void l_u_d(float** a, float** l, float** u, int* p , int size) // Added pivot a
8888
omp_destroy_lock(&lock);
8989
}
9090
int main(int argc, char *argv[]) {
91-
int size = 2;
91+
int size;
92+
cout<<"Please Enter size"<<endl;
93+
cin>>size;//accept the size of the array
9294
float **a, **l, **u;
9395
int *p ; // Added permutation array
9496

@@ -104,16 +106,16 @@ int main(int argc, char *argv[]) {
104106
}
105107

106108
// Initialize the array 'a'
107-
float temp[2][2] = {
108-
{4, 3},
109-
{6, 3}
110-
};
109+
//Instead of manuall setting the value of an array we allow the user to input array values
110+
cout << "Enter the elements of the matrix A:" << endl;
111111
for (int i = 0; i < size; i++) {
112-
for (int j = 0; j < size; j++) {
113-
a[i][j] = temp[i][j];
114-
}
112+
for (int j = 0; j < size; j++) {
113+
cin >> a[i][j];
114+
}
115115
}
116116

117+
118+
117119
// Perform LU decomposition
118120
l_u_d(a , l , u , p , size) ; // Passed pivot array
119121

@@ -134,6 +136,15 @@ int main(int argc, char *argv[]) {
134136
}
135137
printf("\n");
136138
}
139+
//Free the dynamicall allocated memory
140+
for (int i = 0; i < size; i++) {
141+
free(a[i]);
142+
free(l[i]);
143+
free(u[i]);
144+
}
145+
free(a);
146+
free(l);
147+
free(u);
137148

138149
return 0;
139150
}

0 commit comments

Comments
 (0)