Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LU Factorisation #10 issue and Histogram #5 issue #32

Merged
merged 2 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added LU_factorisation
Binary file not shown.
70 changes: 39 additions & 31 deletions src/Histogram/histo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
#include <malloc.h>
#include <omp.h>
#include <mpi.h>
#include <iostream>
#include<algorithm>
using namespace std;

int NumberOfPoints = 0;
int max_value = -1;
int *p;
int arr[100];
int *ReadFromFile()
int *arr;

int* ReadFromFile()
{
int *points;
FILE *file;
Expand All @@ -23,8 +26,8 @@ int *ReadFromFile()
while (fgets(line, sizeof(line), file))
NumberOfPoints++;
fclose(file);
int size = NumberOfPoints;
points = (int *)malloc(size* sizeof(int));

points = (int *)malloc(NumberOfPoints * sizeof(int)); // Allocate memory for points
if (points == NULL) {
perror("Error allocating memory");
return NULL;
Expand All @@ -36,6 +39,7 @@ int *ReadFromFile()
free(points);
return NULL;
}

while (fgets(line, sizeof(line), file)) {
points[index++] = atoi(line);
}
Expand All @@ -47,7 +51,7 @@ int main(int argc, char **argv)
{
int indexx = 0;
int *points, Bars, np, Range, tmp_Range = 0,
Points_per_process;
Points_per_process;
int size;
int *irecv;
int *AllCount, *count;
Expand All @@ -67,7 +71,6 @@ int main(int argc, char **argv)
cout << "Enter the number of bars" << endl;
cin >> Bars;


points = ReadFromFile();
Points_per_process = ((double)NumberOfPoints / (NumberOfprocess)) + 0.5;

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

p = (int*)malloc(size * sizeof(int));

for (i = 0; i < size; i++)
{
if (i < NumberOfPoints)
p[i] = points[i];
else
p[i] = -1;
}

max_value = *std::max_element(p, p + NumberOfPoints); // Find max value for Range
Range = max_value / Bars;
if (max_value % Bars != 0)
{
Expand All @@ -94,66 +98,70 @@ int main(int argc, char **argv)

MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&Bars, 1, MPI_INT, 0, MPI_COMM_WORLD);

irecv = (int*)malloc(size * sizeof(int *));
count = (int*)malloc(Bars * sizeof(int *));

MPI_Bcast(&Range, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&Points_per_process, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Scatter(p, Points_per_process, MPI_INT, irecv, Points_per_process, MPI_INT, 0, MPI_COMM_WORLD);

irecv = (int*)malloc(Points_per_process * sizeof(int));
count = (int*)malloc(Bars * sizeof(int));
for (l = 0; l < Bars; l++)
{
count[l] = 0;
}

#pragma omp parallel shared(AllCount)
MPI_Scatter(p, Points_per_process, MPI_INT, irecv, Points_per_process, MPI_INT, 0, MPI_COMM_WORLD);

// Dynamically allocate arr based on Points_per_process
arr = (int*)malloc(Points_per_process * sizeof(int));

#pragma omp parallel shared(count)
{
#pragma omp for schedule(static)
for (i = 0; i < Points_per_process; i++)
{
for (l = 0; l < Bars; l++)
{
if (irecv[i] <= l * Range + Range && irecv[i] != -1)
if (irecv[i] <= (l + 1) * Range && irecv[i] != -1)
{
count[l]++;
arr[indexx++] = l;
arr[i] = l; // Store the bar index in arr
break;
}
}
}
}
free(irecv);
AllCount = (int*)malloc(NumberOfPoints * sizeof(int));
MPI_Gather(arr, indexx, MPI_INT, AllCount, indexx, MPI_INT, 0, MPI_COMM_WORLD);

AllCount = (int*)malloc(NumberOfPoints * sizeof(int)); // Gather result

MPI_Gather(arr, Points_per_process, MPI_INT, AllCount, Points_per_process, MPI_INT, 0, MPI_COMM_WORLD);

if (rank == 0)
{
// count = malloc(Bars * sizeof(int));
for (l = 0; l < Bars; l++)
// Count the final distribution of points across bars
for (i = 0; i < Bars; i++)
{
count[l] = 0;
count[i] = 0;
}

for (i = 0; i < Bars; i++)
for (i = 0; i < NumberOfPoints; i++)
{
for (j = 0; j < NumberOfPoints; j++)
if (AllCount[i] != -1)
{
if (AllCount[j] == i && AllCount[j] != -1)
{
count[i]++;
}
count[AllCount[i]]++;
}
}

for (i = 0; i < Bars; i++)
{
cout << "Bar " << i << " has " << count[i] << " points" << endl;
}


free(AllCount);
}
MPI_Finalize();


free(irecv);
free(count);
free(arr);

MPI_Finalize();
return 0;
}
}
27 changes: 19 additions & 8 deletions src/LU_factorisation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ void l_u_d(float** a, float** l, float** u, int size)
omp_destroy_lock(&lock);
}
int main(int argc, char *argv[]) {
int size = 2;
int size;
cout<<"Please Enter size"<<endl;
cin>>size;//accept the size of the array
float **a, **l, **u;

// Allocate memory for the 2D arrays
Expand All @@ -74,16 +76,16 @@ int main(int argc, char *argv[]) {
}

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



// Perform LU decomposition
l_u_d(a, l, u, size);

Expand All @@ -104,6 +106,15 @@ int main(int argc, char *argv[]) {
}
printf("\n");
}
//Free the dynamicall allocated memory
for (int i = 0; i < size; i++) {
free(a[i]);
free(l[i]);
free(u[i]);
}
free(a);
free(l);
free(u);

return 0;
}