finalized gaussian elimination

This commit is contained in:
ziadarafat committed 2023-10-22 18:36:12 -06:00
1 parent ffc8f374f1
commit b3674f6752
8 files changed
+3785 -134

No files matched your search

File diff suppressed because it is too large. Load diff
+23 -134
View File
@@ -1,138 +1,27 @@
# Program 1.5
# Gaussian Elimination in Parallel
## Overview
- This program demonstrates that certain tasks do not translate well into parallel.
- We developed a C program that will do gaussian elimination in parallel
- The forward elimination and the backsolving are implemented and measured seperately.
- We run the function on input sizes of random matrices of sizes 10, 100, 1000 to see how the program scales with input.
## Usage
This is a program to see if forked processes will respect a shared memory semaphore. The results and explanation is in the header comments.
1. run `make` in the terminal to compile the program
2. run `./gaussian_elimination` to execute the program
3. there are no command line args everything is hard coded.
```c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <semaphore.h>
// Ziad Arafat
// Sep 25 2021
/**
* Changelog:
* Sep 25 2021
* Used my old OS class code to create and manage shared memory
* Added another part of my old OS class code to create an IPC semaphore
*
* Sep 26 2021
* BUGFIX: Semaphore was not being respected by both processes because the
* sem_init() call was not enabling shared memory semaphore by setting the
* second parameter to 1.
*
* Sep 27 2021
* Cleaned up and documented code
*
*/
/**
* @brief This program will demonstrate if a forked process will respect a
* shared memory semaphore created in the parent code. We will achieve this by
* forcing the child to enter a critical segment protected by a mutex first and
* then forcing the parent to wait 10 seconds on the child. Print statements
* will help us know the order in which code was executed. If we succeed then
* the parent will never enter the critical section until the child has exited
* it. Otherwise the mutex is not being respected.
*/
/**
* Results:
* This is the output from running the code
* We can know that the semaphore behaves correctly because the parent
* does not enter the mutex until after the child has left.
*
* parent making child and waiting 2 seconds
* child in mutex
* child leaving mutex
* child has left the mutex
* parent in mutex
* parent is out of mutex
*/
// create key for shared memory
#define SHMKEY ((key_t)1497)
// define struct to store value in
// This might not be necessary but this is how I did it in OS class.
typedef struct {
sem_t mut;
} shared_mem;
// initialize memory
static shared_mem *mutex;
int main()
{
int shmid; // The shared memory id
int pid1; // The pid returned by the forked process
char *shmadd_ptr; // The address of the shared memory
shmadd_ptr = (char *)0;
// If shmget doesn't return a valid memory address then barf
if ((shmid = shmget(SHMKEY, sizeof(int), IPC_CREAT | 0666)) < 0) {
perror("uh oh, shmget failed");
exit(1);
}
// check if the shared mem attached correctly.
if ((mutex = (shared_mem *)shmat(shmid, shmadd_ptr, 0)) ==
(shared_mem *)-1) {
perror("shmat failed");
exit(1);
}
sem_init(&mutex->mut, 1, 1); // initialize the semaphore to 1
printf("parent making child and waiting 2 seconds\n");
if ((pid1 = fork()) == 0) { // parent process
sleep(2); // Wait 2 seconds so the child will enter first
sem_wait(&mutex->mut); // try to enter critical segment.
// This must not run before child leaves the mutex
printf("parent in mutex\n");
sem_post(&mutex->mut);
printf("parent is out of mutex\n");
waitpid(pid1, NULL, WEXITED); // wait for child to close
sem_destroy(&mutex->mut); // clean up the mutex.
// if we can't delete the memory, barf
if (shmdt(mutex) == -1) { // clean up the shared memory
// barf
perror("Cant delete the shared memory");
exit(-1);
}
exit(0); // exit parent
} else { // child process
sem_wait(&mutex->mut); // enter the critical segment
printf("child in mutex\n");
sleep(10); // sleep for 10 seconds so the parent has to wait.
printf("child leaving mutex\n");
sem_post(&mutex->mut);
printf("child has left the mutex\n");
exit(0); // exit child
}
return 0;
}
### Example execution
```bash
ziadarafat@dogearchvm ~/D/s/parallel-programming (main)> cd Gaussian_Elimination/
ziadarafat@dogearchvm ~/D/s/p/Gaussian_Elimination (main)> make
gcc -O0 -Wall -Werror -Wextra -pedantic -pthread -lpthread -o gaussian_elimination gaussian_elimination.c
ziadarafat@dogearchvm ~/D/s/p/Gaussian_Elimination (main)> ./gaussian_elimination
Time for forward elimination N=10: 0.001633 seconds
Time taken for backsolve with N=10: 0.000352 seconds
Time for forward elimination N=100: 0.095059 seconds
Time taken for backsolve with N=100: 0.003582 seconds
Time for forward elimination N=1000: 12.931657 seconds
Time taken for backsolve with N=1000: 0.034218 seconds
```
+388
View File
@@ -0,0 +1,388 @@
# Gaussian Elimination in Parallel
## Overview
- This program demonstrates that certain tasks do not translate well into parallel.
- We developed a C program that will do gaussian elimination in parallel
- The forward elimination and the backsolving are implemented and measured seperately.
- We run the function on input sizes of random matrices of sizes 10, 100, 1000 to see how the program scales with input.
## Results
- The results show that the program does not scale well when it comes to the forward elimination but it doesn't suffer as such with the backsolving.
- This is likely because the forward elimination requires thread synchronization and communication whereas the backsolving does not and is much less complex.
![forward elimination](./forwardelimination.png)
![backsolve](./backsolve.png)
## Code
```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <math.h>
// Macro for calculating the min value
#define MIN(a, b) ((a) < (b) ? (a) : (b))
// This will help us pass all the required data to the forward_elimination
// function which will be run by each thread
typedef struct {
float **matrix; // 2D array to store the matrix
int num_rows; // number of rows
int num_cols; // number of columns
int thread_id; // Current thread id to calculate which rows to process
// total number of threads we are using.
int num_threads; // Used with thread_id to divide the work
// mutex to protext the sync_counter
pthread_mutex_t *mutex;
pthread_cond_t *cond; // condition used to broadcast to other threads
// that we are ready to go to the next column
// keeps track of how many threads reached the critical instructions
int *sync_counter;
} thread_data_t;
// This serves a similar function as thread_data_t but for the backsolving
typedef struct {
float **matrix;
int num_rows;
int num_cols;
float *solution; // stores the solution array.
int variable_index; // the index that the thread will work on
} backsolve_thread_data_t;
// Because we don't want to nest another for loop
void update_row(
float **matrix,
int row,
int start_col,
int num_cols,
float factor,
float *pivot_row
)
{
int j;
for (j = start_col; j < num_cols; j++) {
matrix[row][j] -= factor * pivot_row[j];
}
}
// Configured to be used by pthreads
// each thread will perform forward elimination on it's own section.
void *forward_elimination(void *arg) {
// convert the argument back to struct thread_data_t
thread_data_t *data = (thread_data_t *)arg;
float factor;
for (int k = 0; k < data->num_cols - 1; k++) {
if (k % data->num_threads == data->thread_id) {
for (int i = k + 1; i < data->num_rows; i++) {
factor =
data->matrix[i][k] / data->matrix[k][k];
update_row(
data->matrix,
i, k,
data->num_cols,
factor,
data->matrix[k]
);
}
}
// lock the mutex before incrementing thread counter
pthread_mutex_lock(data->mutex);
(*data->sync_counter)++; // increment the thread counter
// if not all threads finished on this column
if (*data->sync_counter < data->num_threads) {
// wait for them to be done then proceed to next column
pthread_cond_wait(data->cond, data->mutex);
} else { // else
// start over and proceed to next column
*data->sync_counter = 0;
// let all the other threads know we are done
pthread_cond_broadcast(data->cond);
}
// unlock the mutex
pthread_mutex_unlock(data->mutex);
}
pthread_exit(NULL);
}
// Configured to be used by pthreads
// This will be run by each thread in the backsolve function
void *backsolve_thread(void *arg) {
backsolve_thread_data_t *data = (backsolve_thread_data_t *)arg;
int i = data->variable_index;
float sum = 0;
for (int j = i + 1; j < data->num_cols - 1; j++) {
sum += data->matrix[i][j] * data->solution[j];
}
data->solution[i] = (data->matrix[i][data->num_cols - 1] - sum) / data->matrix[i][i];
pthread_exit(NULL);
}
// Function to do the backsolving
float *backsolve(float **matrix, int num_rows, int num_cols) {
// intialize the array to store the solution
float *solution = malloc(num_rows * sizeof(float));
// intitialize an array of threads equal to the number of unknowns
pthread_t *threads = malloc(num_rows * sizeof(pthread_t));
// Array of the arguments for each thread function call
backsolve_thread_data_t *thread_data =
malloc(num_rows * sizeof(backsolve_thread_data_t));
// configure the arguments for each thread and create them
for (int thread_id = num_rows - 1; thread_id >= 0; thread_id--) {
thread_data[thread_id].matrix = matrix;
thread_data[thread_id].num_rows = num_rows;
thread_data[thread_id].num_cols = num_cols;
thread_data[thread_id].solution = solution;
thread_data[thread_id].variable_index = thread_id;
pthread_create(
&threads[thread_id],
NULL,
backsolve_thread,
&thread_data[thread_id]
);
}
// Wait for all the threads to complete before ending and returning
// the solution
for (int thread_id = num_rows - 1; thread_id >= 0; thread_id--) {
pthread_join(threads[thread_id], NULL);
}
// Free the allocated memory
free(threads);
free(thread_data);
return solution;
}
// Create a num_rows by num_cols matrix with random floats or predefined matrix
float **create_matrix(int num_rows, int num_cols, int predefined)
{
float **matrix = malloc(num_rows * sizeof(float *));
if (predefined) {
// predefined matrix that we know the answer to for testing
float predefined_matrix[5][6] = {
{2, -1, 1, 3, 1, 10},
{1, 3, 2, -1, 2, 5},
{1, -1, 2, 4, 1, 7},
{3, 2, -1, 1, 2, 12},
{2, 1, 3, -2, 1, 3}
};
for (int row = 0; row < num_rows; row++) {
matrix[row] = malloc(num_cols * sizeof(float));
for (int col = 0; col < num_cols; col++) {
matrix[row][col] = predefined_matrix[row][col];
}
}
} else {
for (int row = 0; row < num_rows; row++) {
matrix[row] = malloc(num_cols * sizeof(float));
for (int col = 0; col < num_cols; col++) {
matrix[row][col] = (float)rand() / RAND_MAX * 2000 - 1000;
}
}
}
return matrix;
}
// Frees all the memory created for a matrix
void free_matrix(float **matrix, int num_rows)
{
// free inner rows of the matrix
int row;
for (row = 0; row < num_rows; row++) {
free(matrix[row]);
}
// Free the outter memory of the matrix
free(matrix);
}
// Function to verify the solution
void verify_solution(float **matrix, float *solution, int num_rows, int num_cols)
{
float sum;
for (int i = 0; i < num_rows; i++) {
sum = 0;
for (int j = 0; j < num_cols - 1; j++) {
sum += matrix[i][j] * solution[j];
}
if (fabs(sum - matrix[i][num_cols - 1]) > 1e-5) {
printf("Solution is incorrect!\n");
return;
}
}
printf("Solution is correct!\n");
}
int main()
{
// initialize our thread sync variables
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int sync_counter = 0;
// The input matrix sizes to test
int input_matrix_sizes[] = { 10, 100, 1000 };
for (int size_index = 0; size_index < 3; size_index++) {
int num_rows = input_matrix_sizes[size_index];
int num_cols = num_rows + 1;
float **matrix = create_matrix(num_rows, num_cols, 0);
// We store the threads and their respective inputs in arrays
// So that we can iterate through them and execute them.
pthread_t *threads = malloc(num_rows * sizeof(pthread_t));
thread_data_t *thread_data =
malloc(num_rows * sizeof(thread_data_t));
clock_t start, end; // setup our timer
double cpu_time_used;
start = clock(); // record start time of forward elimination
for (int thread_id = 0; thread_id < num_rows; thread_id++) {
// Initialize all the inputs for this thread
thread_data[thread_id].matrix = matrix;
thread_data[thread_id].num_rows = num_rows;
thread_data[thread_id].num_cols = num_cols;
thread_data[thread_id].thread_id = thread_id;
thread_data[thread_id].num_threads = num_rows;
thread_data[thread_id].mutex = &mutex;
thread_data[thread_id].cond = &cond;
thread_data[thread_id].sync_counter = &sync_counter;
// start the thread and pass the appropriate inputs
// forward_elimination() is the function the threads run
pthread_create(
&threads[thread_id],
NULL,
forward_elimination,
&thread_data[thread_id]
);
}
// Wait for all threads to complete before proceeding
for (int thread_id = 0; thread_id < num_rows; thread_id++) {
pthread_join(threads[thread_id], NULL);
}
end = clock(); // record end time of forward elimination
// calculate time elapsed and convert to CPU time
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
// print results for forward elimination
printf(
"Time for forward elimination N=%d: %f seconds\n",
num_rows,
cpu_time_used
);
start = clock(); // record start time of backsolving
backsolve(matrix, num_rows, num_cols);
end = clock(); // record end time of backsolving
// calculate time elapsed and convert to CPU time
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
// print results for backsolving
printf(
"Time taken for backsolve with N=%d: %f seconds\n",
num_rows,
cpu_time_used
);
// Used to ensure the gaussian elimination is correct
// Capture the solution array
// float* solution = backsolve(matrix, num_rows, num_cols);
// Print the solution
// printf("Solution:\n");
// for (int i = 0; i < num_rows; i++) {
// printf("%f ", solution[i]);
// }
// printf("\n");
// Verify the solution
// verify_solution(matrix, solution, num_rows, num_cols);
// Free the solution array
// free(solution);
// No memory left behind.
free_matrix(matrix, num_rows);
free(threads);
free(thread_data);
}
return 0;
}
```
## Raw outputs
```bash
ziadarafat@dogearchvm ~/D/s/parallel-programming (main)> cd Gaussian_Elimination/
ziadarafat@dogearchvm ~/D/s/p/Gaussian_Elimination (main)> make
gcc -O0 -Wall -Werror -Wextra -pedantic -pthread -lpthread -o gaussian_elimination gaussian_elimination.c
ziadarafat@dogearchvm ~/D/s/p/Gaussian_Elimination (main)> ./gaussian_elimination
Time for forward elimination N=10: 0.001633 seconds
Time taken for backsolve with N=10: 0.000352 seconds
Time for forward elimination N=100: 0.095059 seconds
Time taken for backsolve with N=100: 0.003582 seconds
Time for forward elimination N=1000: 12.931657 seconds
Time taken for backsolve with N=1000: 0.034218 seconds
ziadarafat@dogearchvm ~/D/s/p/Gaussian_Elimination (main)> ./gaussian_elimination
Time for forward elimination N=10: 0.001544 seconds
Time taken for backsolve with N=10: 0.000303 seconds
Time for forward elimination N=100: 0.090941 seconds
Time taken for backsolve with N=100: 0.003233 seconds
Time for forward elimination N=1000: 13.024221 seconds
Time taken for backsolve with N=1000: 0.032342 seconds
ziadarafat@dogearchvm ~/D/s/p/Gaussian_Elimination (main)> ./gaussian_elimination
Time for forward elimination N=10: 0.001593 seconds
Time taken for backsolve with N=10: 0.000289 seconds
Time for forward elimination N=100: 0.087520 seconds
Time taken for backsolve with N=100: 0.003795 seconds
Time for forward elimination N=1000: 12.957925 seconds
Time taken for backsolve with N=1000: 0.032653 seconds
ziadarafat@dogearchvm ~/D/s/p/Gaussian_Elimination (main)> ./gaussian_elimination
Time for forward elimination N=10: 0.001633 seconds
Time taken for backsolve with N=10: 0.000289 seconds
Time for forward elimination N=100: 0.092816 seconds
Time taken for backsolve with N=100: 0.003512 seconds
Time for forward elimination N=1000: 12.753179 seconds
Time taken for backsolve with N=1000: 0.032123 seconds
ziadarafat@dogearchvm ~/D/s/p/Gaussian_Elimination (main)> ./gaussian_elimination
Time for forward elimination N=10: 0.001430 seconds
Time taken for backsolve with N=10: 0.000427 seconds
Time for forward elimination N=100: 0.099994 seconds
Time taken for backsolve with N=100: 0.003431 seconds
Time for forward elimination N=1000: 13.095672 seconds
Time taken for backsolve with N=1000: 0.034295 seconds
```
Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.
+102
View File
@@ -0,0 +1,102 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA0kAAAHWCAYAAACi1sL/AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAABxE0lEQVR4nO3dd3wUdeLG8c+mF5JAEkiBEAJpgBQFRHqoIfbTw4aiolgBAUXgPAU872iKKHhi+Qnq2c5ezgRCLyJVUIQ0CKGXUFIIabvz+4MjlxhKCElmkzzv14u72+/OzjyzySXzZGa+azEMw0BEREREREQAcDA7gIiIiIiIiD1RSRIRERERESlFJUlERERERKQUlSQREREREZFSVJJERERERERKUUkSEREREREpRSVJRERERESkFJUkERERERGRUlSSRERERERESlFJEhGR81qxYgUWi4UVK1ZU2TotFgtTpkwpebxw4UIsFgt79uypsm1MmTIFi8VSZeu7XDExMcTExJi2/Zp2vve7RYsWPPDAA+YEEhGpAipJIiKXcO5A/nz/Jk6caHY8013s/bFYLPz8889mR6xyO3bsYMqUKVVa7qrCnj17ePDBB2nVqhVubm4EBgbSu3dvJk+ebHY0EZFaxcnsACIitcWLL75IWFhYmbGrrrrKpDT253zvD0B4ePgFX3Pfffdx11134erqWmU5/vrXv1Z7ed2xYwdTp04lJiaGFi1alHlu8eLF1brtC0lLS6NLly64u7szfPhwWrRowaFDh9iyZQszZsxg6tSpNZYlOTkZBwf9HVZEai+VJBGRCoqLi6Nz585Vvt7Tp0/j6elZ5eu9FMMwyM/Px93dvUrWV5n3x9HREUdHxyrZ/jlOTk44OZn3683FxcWU7b766qvk5uaydetWQkNDyzx39OjRGs1SlaVXRMQM+jOPiEgVWbZsGb169cLT05OGDRtyyy23sHPnzjLLnLt/Y8eOHdxzzz00atSInj178t1332GxWPj1119Llv3yyy+xWCzcdtttZdbRunVr7rzzzpLHCxYsoF+/fjRp0gRXV1fatGnDm2++WS5fixYtuPHGG1m0aBGdO3fG3d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biI2NJT8/v1LbFBGpFENERC5qwYIFBnDef+d07NjRaNKkiXH8+PGSsW3bthkODg7GsGHDSsYmT55sAMbdd99dbjtt27Y17rjjjpLH11xzjTFkyBADMHbu3GkYhmF89dVXBmBs27atZLm8vLxy64qNjTVatmxZZiw0NNQAjISEhDLjc+bMMQDj3//+d8nY6dOnjfDwcAMwli9fXun3x9XVtcyygDF58uRyr01PTy+X86effioZW7RokQEY7u7uRkZGRsn4W2+9VS7juff4j9t1cXEx0tLSSsa2bdtmAMbcuXNLxs73Xq5bt84AjA8++KBk7PPPP7/ge9OnTx+jT58+JY/Pvb//+te/SsYKCwuNbt26GQ0aNDCys7MNwzCM9PR0AzD8/PyMEydOlCz77bffGoDx/fffl9tWadu3bzfc3d0NwOjYsaPx1FNPGd98841x+vTpMsudOnXK8PLyMrp27WqcOXOmzHM2m+2i78W0adMMi8VS5mtwvvc7NDTUuP/++0sen/s6DxgwoMw2xo4dazg6OhqnTp0yDMMwDh8+bDg5ORm33nprmfVNmTLFAMqsU0SkOulyOxGRCnrjjTdITEws8w/g0KFDbN26lQceeABfX9+S5du3b8/AgQP58ccfy63rscceKzfWq1cvVq9eDUBOTg7btm3jkUcewd/fv2R89erVNGzYsMyEEaXvKTp3NqdPnz7s3r2brKysMtsICwsjNja2zNiPP/5IUFAQf/7zn0vGPDw8eOSRRyr83sD535/4+PjLWsc5bdq0oVu3biWPu3btCkC/fv1o3rx5ufHdu3dfcp0DBgygVatWJY/bt2+Pt7d3mdeWfi+Lioo4fvw44eHhNGzYkC1btlRqX3788UcCAwO5++67S8acnZ0ZPXo0ubm5rFy5sszyd955J40aNSp53KtXL+DS+9i2bVu2bt3Kvffey549e3jttde49dZbCQgI4J133ilZLjExkZycHCZOnIibm1uZdZS+TLH0e3H69GkyMzPp3r07hmGUu8Sxoh555JEy2+jVqxdWq5WMjAwAli5dSnFxccmZwnNGjRpVqe2JiFSWLrcTEamga6+99rwTE5w7wIuKiir3XOvWrVm0aFG5yRnONwtcr169mD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iMgf6UySiEgVmDVrFnFxcXTr1o2HHnqoZApwHx8fpkyZUuH19OrVi48++giLxVJy+Z2joyPdu3dn0aJFxMTElPmw0kGDBuHi4sJNN93Eo48+Sm5uLu+88w5NmjQpOXNwKSNGjGDevHkMGzaMzZs3ExQUxIcffoiHh8dlvQfx8fElZ0dK6969Oy1btrysdZnlxhtv5MMPP8THx4c2bdqwbt06lixZUm4a844dO+Lo6MiMGTPIysrC1dW15LOq/uiRRx7hrbfe4oEHHmDz5s20aNGCL774grVr1zJnzhy8vLyqJPuMGTPYvHkzt912W8k08lu2bOGDDz7A19eXMWPGAODt7c2rr77Kww8/TJcuXUo+r2vbtm3k5eXx/vvvEx0dTatWrXjmmWc4cOAA3t7efPnll9V+NicgIICnnnqKV155hZtvvpnBgwezbds24uPj8ff3v6wzeCIiV0IlSUSkCgwYMICEhAQmT57MCy+8gLOzM3369GHGjBnnnSzhQs6Line truncated
"text/plain": [
"<Figure size 1000x500 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"# Data\n",
"n_values = [10, 100, 1000]\n",
"forward_elimination_times = [\n",
" [0.001633, 0.001544, 0.001593, 0.001633, 0.001430],\n",
" [0.095059, 0.090941, 0.087520, 0.092816, 0.099994],\n",
" [12.931657, 13.024221, 12.957925, 12.753179, 13.095672]\n",
"]\n",
"backsolve_times = [\n",
" [0.000352, 0.000303, 0.000289, 0.000289, 0.000427],\n",
" [0.003582, 0.003233, 0.003795, 0.003512, 0.003431],\n",
" [0.034218, 0.032342, 0.032653, 0.032123, 0.034295]\n",
"]\n",
"\n",
"# Calculate average times\n",
"average_forward_elimination_times = [sum(times) / len(times) for times in forward_elimination_times]\n",
"average_backsolve_times = [sum(times) / len(times) for times in backsolve_times]\n",
"\n",
"# Plot Forward Elimination\n",
"plt.figure(figsize=(10, 5))\n",
"plt.plot(n_values, average_forward_elimination_times, marker='o')\n",
"plt.xlabel('Number of Unknown Variables (N)')\n",
"plt.ylabel('Time (seconds)')\n",
"plt.title('Forward Elimination Scaling')\n",
"plt.xticks(n_values)\n",
"plt.grid(True)\n",
"plt.show()\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA18AAAHWCAYAAACIZjNQAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAAB+8klEQVR4nOzdeVxVdf7H8de9rKIsgrIpCgpuuVCaiLmg4JK22GJqlmZmq0vZlNpmVjNWM/VT09Fptc10bMqZzEzCXQn3zFxiU9zABQEFWe/5/YHeJFBBkYvwfj4ePqb7Pd9zzud7Yey8O+d8vybDMAxERERERETkmjLbugAREREREZHaQOFLRERERESkCih8iYiIiIiIVAGFLxERERERkSqg8CUiIiIiIlIFFL5ERERERESqgMKXiIiIiIhIFVD4EhERERERqQIKXyIiIiIiIlVA4UtERKqlwMBAbrvttio73/79+zGZTMyfP7/KzmlL8+fPx2QysX//fmtbREQEERERNqtJRKSmU/gSEZGLOn+BfuEfb29vevXqxQ8//GDr8mqM48ePM2HCBFq1akWdOnXw9vamc+fOTJo0iTNnzti6PBERqST2ti5ARESqv9dee42goCAMwyAtLY358+czYMAAvvvuuyq9O1UTpaen06lTJ7Kysnj44Ydp1aoVJ0+eZOfOncydO5cnnniCevXqVUktK1asqJLziIjUVgpfIiJyWbfeeiudOnWyfh49ejQ+Pj589dVXCl9X6aOPPiIlJYUNGzbQtWvXEtuysrJwdHSsslqq8lwiIrWRHjsUEZEK8/DwoE6dOtjbl/xveP/4xz/o2rUrXl5e1KlTh44dO/L111+XeYwvvviCzp074+LiQv369enRo8dl77x8+umn2Nvb89xzz1nbFi5cSMeOHXF1dcXNzY127doxc+bMEvslJSUxePBgPD09cXFxoUuXLnz//feXPNc//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H0dGR22+/ndtvvx2LxcKTTz7Jv/71L15++WWCg4Np2rQp+/btK3XsvXv3AsUTXlzKkCFDePLJJ9m3bx+LFi3CxcWF22+/vcR4oHhmwqioqEseqyKaNWtG/fr1OXr0aInz7Nq166LnOXXqFDExMUybNo1XXnnF2n4+IF8r57/DhIQEevXqZW0vLCxk//79tG/f/pqeX0TkeqE7XyIiUmEFBQWsWLECR0dH6+N7dnZ2mEwmioqKrP3279/PkiVLSuw7aNAgzGYzr732Wqk7MWXdSWncuDE//fQTZ8+epU+fPiWmTP/z9Olms9l6oZ+XlwfAgAED2LRpE7GxsdZ+2dnZvP/++wQGBl728cd77rkHOzs7vvrqKxYvXsxtt91G3bp1rds7duxI8+bN+cc//mENohc6fvz4JY8fFxdHdnZ2qfZNmzZx8uRJ6yOTN910E0FBQcyYMYOMjIwSfc9/b+fvUP35e5wxY8Yla7hanTp1wsvLiw8++MAaygG+/PLLEtPxi4jUdrrzJSIil/XDDz9Y7xQdO3aMBQsWEB8fz+TJk63vPg0cOJB3332X/v37c//993Ps2DHmzJlDcHAwO3futB4rODiYF198kddff53u3btz99134+TkxObNm/H392f69Omlzh8cHMyKFSuIiIigX79+rFy5Ejc3Nx555BHS09Pp3bs3jRs35sCBA7z33nuEhoZaQ+HkyZP56quvuPXWWxk/fjyenp58+umnJCcn85///KfUpB9/5u3tTa9evXj33Xc5ffp0iUcOoTjwffjhh9x6663ccMMNjBo1ikaNGnH48GFWrVqFm5sb33333UWP//nnn/Pll19y11130bFjRxwdHdmzZw8ff/wxzs7OvPDCC9bzzJ07l9tvv53Q0FBGjRqFn58fe/fu5bfffuPHH3/Ezc2NHj168Pbbb1NQUECjRo1YsWJFmY9sViZHR0deffVVxo0bR+/evbnvvvvYv38/8+fPp3nz5mVOpCIiUivZcKZFERGp5sqaat7Z2dkIDQ015s6dW2KKc8MwjI8++sgICQkxnJycjFatWhmffPKJMXXqVKOsf918/PHHxo033mg4OTkZ9evXN3r27GlER0dbt1841fx5cXFxhqurq9GjRw8jJyfH+Prrr42+ffsa3t7ehqOjo9GkSRPjscceM44ePVpiv8TEROPee+81PDw8DGdnZ6Nz587G0qVLS/Qpa6r58z744AMDMFxdXY2zZ8+W+V1t377duPvuuw0vLy/DycnJaNq0qXHfffcZMTExl/yOd+7caTz33HPGTTfdZHh6ehr29vaGn5+fMXjwYGPbtm2l+q9fv97o06eP4erqatStW9do37698d5771m3Hzp0yLjrrrsMDw8Pw93d3Rg8eLBx5MgRAzCmTp1q7VeRqeYXL15cru9q1qxZRtOmTQ0nJyejc+fOxoYNG4yOHTsa/fv3v+R3ICJSW5gMQ2/LioiISOWzWCw0bNiQu+++mw8++MDW5YiI2Jze+RIREZGrlpubW+pds88++4z09HQiIiJsU5SISDWjO18iIiJy1VavXs0zzzzD4MGD8fLyYtu2bXz00Ue0bt2arVu3llikWUSkttKEGyIiInLVAgMDCQgIYNasWaSnp+Pp6cmIESN48803FbxERM7RnS8REREREZEqoHe+REREREREqoDCl4iIiIiISBXQO19XyGKxcOTIEVxdXbV4pIiIiIhILWYYBqdPn8bf3x+z+eL3txS+rtCRI0cICAiwdRkiIiILine truncated
"text/plain": [
"<Figure size 1000x500 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Plot Backsolve\n",
"plt.figure(figsize=(10, 5))\n",
"plt.plot(n_values, average_backsolve_times, marker='o')\n",
"plt.xlabel('Number of Unknown Variables (N)')\n",
"plt.ylabel('Time (seconds)')\n",
"plt.title('Backsolve Scaling')\n",
"plt.xticks(n_values)\n",
"plt.grid(True)\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}