From 0da0af49dceedd19643f4044d9a85ed4aaa1c629 Mon Sep 17 00:00:00 2001 From: dheer Date: Wed, 4 Dec 2024 01:24:07 +0530 Subject: [PATCH 1/2] fixed memory alloc bug --- src/Histogram/histo.cpp | 70 +++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/src/Histogram/histo.cpp b/src/Histogram/histo.cpp index 7f94cbe..0cb2f30 100644 --- a/src/Histogram/histo.cpp +++ b/src/Histogram/histo.cpp @@ -3,13 +3,16 @@ #include #include #include +#include +#include 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; @@ -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; @@ -36,6 +39,7 @@ int *ReadFromFile() free(points); return NULL; } + while (fgets(line, sizeof(line), file)) { points[index++] = atoi(line); } @@ -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; @@ -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; @@ -76,7 +79,6 @@ int main(int argc, char **argv) size = NumberOfPoints; p = (int*)malloc(size * sizeof(int)); - for (i = 0; i < size; i++) { if (i < NumberOfPoints) @@ -84,6 +86,8 @@ int main(int argc, char **argv) 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) { @@ -94,55 +98,55 @@ 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]]++; } } @@ -150,10 +154,14 @@ int main(int argc, char **argv) { cout << "Bar " << i << " has " << count[i] << " points" << endl; } - + + free(AllCount); } - MPI_Finalize(); - + free(irecv); + free(count); + free(arr); + + MPI_Finalize(); return 0; -} \ No newline at end of file +} From c098452037a11a9ed84ee3ce86a990ab7fed5e03 Mon Sep 17 00:00:00 2001 From: dheer Date: Wed, 4 Dec 2024 11:40:17 +0530 Subject: [PATCH 2/2] Change LU_factorisation to accept array size from user and then perform a LU_decomposition --- LU_factorisation | Bin 0 -> 17616 bytes src/LU_factorisation.cpp | 27 +++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) create mode 100755 LU_factorisation diff --git a/LU_factorisation b/LU_factorisation new file mode 100755 index 0000000000000000000000000000000000000000..89a757dd233bcb00cdbf494b7d475d5daccbf259 GIT binary patch literal 17616 zcmeHPeQ;dWb-ycFwhV@q!43*v-Vn^-fEURY9)FDMm91x^+2ChMOqE9zL8(fUHI=kpoIN6@np*Jq?piWrMBy(O(>(p)T`I4^}MuAsR^c32c@pp?D(gtuT$&AL(+`! zQm%(7*Y`!}QCu!GaW`+(_4)O~IUP@?x;w=!spPH|Ew`lNOH;}8z}}^MSFTvPq9vTm zgqKMd$gdDS)P1*Y?iMgJN|+9#s5z*3wnt?PbUDeN`Hw}xpjIVL(6P z6{Q=&Rm1P}k?-)qxBJ-nq7VK#&=&n>kl~pgfb8}ZRn=`id z2#d2xWV`H^L^_^ok9FD2&9UBS*0QtFq@8Pz8OBy%%XdX{$(WVN+1W(2uYH@ft;sUF zR=^FYE{`SC(p)B$GdeeJ?aFjnhAdguoXq4b=q4;o6WM5XpOs7}?LD$Em21DS+G5*K z*R59PAH<|yODr>Bi%eg?wL4*3>484W?uA$JoVaaEM<;5_W|N7mV)~=mXeyORNpY&$ zN@ZgANG=CuhZ#s$uw+;nCHqo5o=ngg_C+ zPxPVQ3>>D0$8@jtMJZ5LY;V-+Nv5Nzlx6(MP!TdQ3|7!8C zPJYh@Rs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~uR{9don^6z}+ zKaUK(OSAq;t64CE|1n?`6SnXC9@oFidEW{mGI{&|QJ`-9kk(@={}!)x ziRfa7e%GVlsrNOQvOJq$p6Rehf2Y=G8uD~F>oNV2N1xw=n6BWBH0=QEu|GfY=<|CQ zQ{ES3d2jrl(fZu~BBUiXjcb+(_aF2BiVQW8_2VH`!n94X&bPPz|3ryf|5!+gGCk*L zs*Jb(*8tP?2|aB&`z6l(dmnwa^A5^`E`8pgX;`WhS&q#x&-8aFps~yK^L_;X{}lND zn?TPWCD!A9{5~*hKkM^;=#96KH10hr;(W6{Q@ZEu))x^i+N3Dg!v>size;//accept the size of the array float **a, **l, **u; // Allocate memory for the 2D arrays @@ -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); @@ -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; } \ No newline at end of file