From 0a8c75a0cfcdb999ba9265c87cc36e1fc66148e7 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Thu, 27 Mar 2025 20:44:47 +0300 Subject: [PATCH] Task 6 was fixed and Task 8 was done --- 2025.03.28/01Ex/main.c | 7 +- 2025.03.28/02Ex/main.c | 11 +- 2025.03.28/02Ex/matrix.c | 42 +++++++ 2025.03.28/02Ex/matrix.h | 2 + 2025.03.28/02Ex/solve.c | 43 ------- 2025.03.28/02Ex/solve.h | 2 - 2025.03.28/03Ex/main.c | 7 +- 2025.03.28/03Ex/matrix.c | 42 +++++++ 2025.03.28/03Ex/matrix.h | 2 + 2025.03.28/03Ex/solve.c | 42 ------- 2025.03.28/03Ex/solve.h | 2 - 2025.03.28/04Ex/main.c | 7 +- 2025.03.28/04Ex/matrix.c | 42 +++++++ 2025.03.28/04Ex/matrix.h | 2 + 2025.03.28/04Ex/solve.c | 42 ------- 2025.03.28/04Ex/solve.h | 2 - 2025.03.28/05Ex/main.c | 7 +- 2025.03.28/05Ex/matrix.c | 42 +++++++ 2025.03.28/05Ex/matrix.h | 2 + 2025.03.28/05Ex/solve.c | 44 +------ 2025.03.28/05Ex/solve.h | 2 - 2025.03.28/06Ex/main.c | 7 +- 2025.03.28/06Ex/matrix.c | 3 +- 2025.03.28/06Ex/solve.c | 6 +- 2025.03.28/07Ex/main.c | 7 +- 2025.03.28/07Ex/matrix.c | 2 +- 2025.03.28/07Ex/t.txt | 8 +- 2025.03.28/08Ex/Makefile | 2 +- 2025.03.28/08Ex/main.c | 27 +++- 2025.03.28/08Ex/matrix.c | 2 +- 2025.03.28/08Ex/solve.c | 34 +++-- 2025.03.28/08Ex/solve.h | 2 +- 2025.03.28/dist/Ulyanov/Makefile | 26 +++- 2025.03.28/dist/Ulyanov/input1.txt | 2 + 2025.03.28/dist/Ulyanov/input2.txt | 2 + 2025.03.28/dist/Ulyanov/matrix.c | 54 ++++++++ 2025.03.28/dist/Ulyanov/matrix.h | 6 + 2025.03.28/dist/Ulyanov/task01.c | 6 - 2025.03.28/dist/Ulyanov/task02.c | 4 +- 2025.03.28/dist/Ulyanov/task04.c | 4 +- 2025.03.28/dist/Ulyanov/task05.c | 194 ++++++++++++++++++++++++++++ 2025.03.28/dist/Ulyanov/task06.c | 195 +++++++++++++++++++++++++++++ 2025.03.28/dist/Ulyanov/task07.c | 172 +++++++++++++++++++++++++ 2025.03.28/dist/Ulyanov/task08.c | 188 +++++++++++++++++++++++++++ 2025.03.28/dist/Ulyanov/task09.c | 188 +++++++++++++++++++++++++++ 45 files changed, 1294 insertions(+), 241 deletions(-) create mode 100644 2025.03.28/dist/Ulyanov/input1.txt create mode 100644 2025.03.28/dist/Ulyanov/input2.txt create mode 100644 2025.03.28/dist/Ulyanov/task05.c create mode 100644 2025.03.28/dist/Ulyanov/task06.c create mode 100644 2025.03.28/dist/Ulyanov/task07.c create mode 100644 2025.03.28/dist/Ulyanov/task08.c create mode 100644 2025.03.28/dist/Ulyanov/task09.c diff --git a/2025.03.28/01Ex/main.c b/2025.03.28/01Ex/main.c index c0e0c9e..38a013b 100644 --- a/2025.03.28/01Ex/main.c +++ b/2025.03.28/01Ex/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "array_io.h" #include "io_status.h" #include "solve.h" @@ -107,13 +108,13 @@ int main(int argc, char *argv[]) printf("Vector x_0:\n"); print_matrix(x_0, 1, n, p); - t = clock(); + t = omp_get_wtime(); r1 = t1_solve(a, x_0, x, n, m); - t = (clock() - t) / CLOCKS_PER_SEC; + t = omp_get_wtime() - t; r2 = t1_get_residual_norm(x, x_0, n, r1); - printf("Vector x_m:\n"); + printf("New vector:\n"); print_matrix(x, 1, n, p); printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); diff --git a/2025.03.28/02Ex/main.c b/2025.03.28/02Ex/main.c index 467bca5..468e91d 100644 --- a/2025.03.28/02Ex/main.c +++ b/2025.03.28/02Ex/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "array_io.h" #include "io_status.h" #include "matrix.h" @@ -124,14 +125,14 @@ int main(int argc, char *argv[]) printf("Vector b:\n"); print_matrix(b, 1, n, p); - t = clock(); + t = omp_get_wtime(); t2_solve(a, x_0, b, x, n, m, tau); - t = (clock() - t) / CLOCKS_PER_SEC; + t = omp_get_wtime() - t; - r1 = t2_get_r1(a, x, b, n); - r2 = t2_get_r2_value(x, n); + r1 = get_r1(a, x, b, n); + r2 = get_r2_value(x, n); - printf("Vector x_m:\n"); + printf("New vector:\n"); print_matrix(x, 1, n, p); printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); diff --git a/2025.03.28/02Ex/matrix.c b/2025.03.28/02Ex/matrix.c index 74fea61..0cf5bbb 100644 --- a/2025.03.28/02Ex/matrix.c +++ b/2025.03.28/02Ex/matrix.c @@ -29,3 +29,45 @@ void matvec_mul(int n, const double * restrict A, const double * restrict x, dou } } +double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n) +{ + double norm_r1x_1 = 0; + double residual_norm_1 = 0; + double r1 = 0; + + #pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1) + for (int i = 0; i < n; ++i) + { + double bi = b[i]; + double sum = 0; + + #pragma omp simd reduction(+:sum) + for (int j = 0; j < n; ++j) + sum += A[i*n + j] * x_k[j]; + + residual_norm_1 += fabs(sum - bi); + norm_r1x_1 += fabs(bi); + } + + r1 = residual_norm_1 / norm_r1x_1; + + return r1; +} + +double get_r2_value(const double * restrict x_k, int n) +{ + double relative_error = 0; + double total_diff = 0; + double template_sum = 0; + + #pragma omp parallel for reduction(+:total_diff, template_sum) + for (int i = 0; i < n; ++i) + { + short int modi = !(i & 1); + total_diff += fabs(x_k[i] - modi); + template_sum += modi; + } + + relative_error = total_diff / template_sum; + return relative_error; +} diff --git a/2025.03.28/02Ex/matrix.h b/2025.03.28/02Ex/matrix.h index 66351d7..a170f22 100644 --- a/2025.03.28/02Ex/matrix.h +++ b/2025.03.28/02Ex/matrix.h @@ -3,5 +3,7 @@ void init_vec_b(const double * restrict a, double * restrict b, int n); void matvec_mul(int n, const double * restrict A, const double * restrict x, double * restrict x_k); +double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n); +double get_r2_value(const double * restrict x_k, int n); #endif diff --git a/2025.03.28/02Ex/solve.c b/2025.03.28/02Ex/solve.c index 8a55c05..4ff3fb9 100644 --- a/2025.03.28/02Ex/solve.c +++ b/2025.03.28/02Ex/solve.c @@ -33,46 +33,3 @@ void t2_solve(const double * restrict A, double * restrict x_0, const double * r x_0 = swap_temp; } } - -double t2_get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n) -{ - double norm_r1x_1 = 0; - double residual_norm_1 = 0; - double r1 = 0; - - #pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1) - for (int i = 0; i < n; ++i) - { - double bi = b[i]; - double sum = 0; - - #pragma omp simd reduction(+:sum) - for (int j = 0; j < n; ++j) - sum += A[i*n + j] * x_k[j]; - - residual_norm_1 += fabs(sum - bi); - norm_r1x_1 += fabs(bi); - } - - r1 = residual_norm_1 / norm_r1x_1; - - return r1; -} - -double t2_get_r2_value(const double * restrict x_k, int n) -{ - double relative_error = 0; - double total_diff = 0; - double template_sum = 0; - - #pragma omp parallel for reduction(+:total_diff, template_sum) - for (int i = 0; i < n; ++i) - { - short int modi = i ^ 1; - total_diff += fabs(x_k[i] - modi); - template_sum += modi; - } - - relative_error = total_diff / template_sum; - return relative_error; -} diff --git a/2025.03.28/02Ex/solve.h b/2025.03.28/02Ex/solve.h index 6149783..8293af7 100644 --- a/2025.03.28/02Ex/solve.h +++ b/2025.03.28/02Ex/solve.h @@ -2,7 +2,5 @@ #define SOLVE_H void t2_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, int n, int m, double t); -double t2_get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n); -double t2_get_r2_value(const double * restrict x_k, int n); #endif diff --git a/2025.03.28/03Ex/main.c b/2025.03.28/03Ex/main.c index 229c024..d76db2d 100644 --- a/2025.03.28/03Ex/main.c +++ b/2025.03.28/03Ex/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "array_io.h" #include "io_status.h" #include "matrix.h" @@ -135,14 +136,14 @@ int main(int argc, char *argv[]) printf("Vector b:\n"); print_matrix(b, 1, n, p); - t = clock(); + t = omp_get_wtime(); t3_solve(a, x_0, b, x, r, n, m); - t = (clock() - t) / CLOCKS_PER_SEC; + t = omp_get_wtime() - t; r1 = get_r1(a, x, b, n); r2 = get_r2_value(x, n); - printf("Vector x_m:\n"); + printf("New vector:\n"); print_matrix(x, 1, n, p); printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); diff --git a/2025.03.28/03Ex/matrix.c b/2025.03.28/03Ex/matrix.c index 74fea61..0cf5bbb 100644 --- a/2025.03.28/03Ex/matrix.c +++ b/2025.03.28/03Ex/matrix.c @@ -29,3 +29,45 @@ void matvec_mul(int n, const double * restrict A, const double * restrict x, dou } } +double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n) +{ + double norm_r1x_1 = 0; + double residual_norm_1 = 0; + double r1 = 0; + + #pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1) + for (int i = 0; i < n; ++i) + { + double bi = b[i]; + double sum = 0; + + #pragma omp simd reduction(+:sum) + for (int j = 0; j < n; ++j) + sum += A[i*n + j] * x_k[j]; + + residual_norm_1 += fabs(sum - bi); + norm_r1x_1 += fabs(bi); + } + + r1 = residual_norm_1 / norm_r1x_1; + + return r1; +} + +double get_r2_value(const double * restrict x_k, int n) +{ + double relative_error = 0; + double total_diff = 0; + double template_sum = 0; + + #pragma omp parallel for reduction(+:total_diff, template_sum) + for (int i = 0; i < n; ++i) + { + short int modi = !(i & 1); + total_diff += fabs(x_k[i] - modi); + template_sum += modi; + } + + relative_error = total_diff / template_sum; + return relative_error; +} diff --git a/2025.03.28/03Ex/matrix.h b/2025.03.28/03Ex/matrix.h index 66351d7..a170f22 100644 --- a/2025.03.28/03Ex/matrix.h +++ b/2025.03.28/03Ex/matrix.h @@ -3,5 +3,7 @@ void init_vec_b(const double * restrict a, double * restrict b, int n); void matvec_mul(int n, const double * restrict A, const double * restrict x, double * restrict x_k); +double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n); +double get_r2_value(const double * restrict x_k, int n); #endif diff --git a/2025.03.28/03Ex/solve.c b/2025.03.28/03Ex/solve.c index 2a77719..1515df3 100644 --- a/2025.03.28/03Ex/solve.c +++ b/2025.03.28/03Ex/solve.c @@ -62,45 +62,3 @@ void t3_solve(const double * restrict A, double * restrict x_0, const double * r } } -double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n) -{ - double norm_r1x_1 = 0; - double residual_norm_1 = 0; - double r1 = 0; - - #pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1) - for (int i = 0; i < n; ++i) - { - double bi = b[i]; - double sum = 0; - - #pragma omp simd reduction(+:sum) - for (int j = 0; j < n; ++j) - sum += A[i*n + j] * x_k[j]; - - residual_norm_1 += fabs(sum - bi); - norm_r1x_1 += fabs(bi); - } - - r1 = residual_norm_1 / norm_r1x_1; - - return r1; -} - -double get_r2_value(const double * restrict x_k, int n) -{ - double relative_error = 0; - double total_diff = 0; - double template_sum = 0; - - #pragma omp parallel for reduction(+:total_diff, template_sum) - for (int i = 0; i < n; ++i) - { - short int modi = i ^ 1; - total_diff += fabs(x_k[i] - modi); - template_sum += modi; - } - - relative_error = total_diff / template_sum; - return relative_error; -} diff --git a/2025.03.28/03Ex/solve.h b/2025.03.28/03Ex/solve.h index 41cdb4f..de26de5 100644 --- a/2025.03.28/03Ex/solve.h +++ b/2025.03.28/03Ex/solve.h @@ -2,7 +2,5 @@ #define SOLVE_H void t3_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m); -double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n); -double get_r2_value(const double * restrict x_k, int n); #endif diff --git a/2025.03.28/04Ex/main.c b/2025.03.28/04Ex/main.c index a3fe916..26dab52 100644 --- a/2025.03.28/04Ex/main.c +++ b/2025.03.28/04Ex/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "array_io.h" #include "io_status.h" #include "matrix.h" @@ -135,14 +136,14 @@ int main(int argc, char *argv[]) printf("Vector b:\n"); print_matrix(b, 1, n, p); - t = clock(); + t = omp_get_wtime(); t4_solve(a, x_0, b, x, r, n, m); - t = (clock() - t) / CLOCKS_PER_SEC; + t = omp_get_wtime() - t; r1 = get_r1(a, x, b, n); r2 = get_r2_value(x, n); - printf("Vector x_m:\n"); + printf("New vector:\n"); print_matrix(x, 1, n, p); printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); diff --git a/2025.03.28/04Ex/matrix.c b/2025.03.28/04Ex/matrix.c index 74fea61..0cf5bbb 100644 --- a/2025.03.28/04Ex/matrix.c +++ b/2025.03.28/04Ex/matrix.c @@ -29,3 +29,45 @@ void matvec_mul(int n, const double * restrict A, const double * restrict x, dou } } +double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n) +{ + double norm_r1x_1 = 0; + double residual_norm_1 = 0; + double r1 = 0; + + #pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1) + for (int i = 0; i < n; ++i) + { + double bi = b[i]; + double sum = 0; + + #pragma omp simd reduction(+:sum) + for (int j = 0; j < n; ++j) + sum += A[i*n + j] * x_k[j]; + + residual_norm_1 += fabs(sum - bi); + norm_r1x_1 += fabs(bi); + } + + r1 = residual_norm_1 / norm_r1x_1; + + return r1; +} + +double get_r2_value(const double * restrict x_k, int n) +{ + double relative_error = 0; + double total_diff = 0; + double template_sum = 0; + + #pragma omp parallel for reduction(+:total_diff, template_sum) + for (int i = 0; i < n; ++i) + { + short int modi = !(i & 1); + total_diff += fabs(x_k[i] - modi); + template_sum += modi; + } + + relative_error = total_diff / template_sum; + return relative_error; +} diff --git a/2025.03.28/04Ex/matrix.h b/2025.03.28/04Ex/matrix.h index 66351d7..a170f22 100644 --- a/2025.03.28/04Ex/matrix.h +++ b/2025.03.28/04Ex/matrix.h @@ -3,5 +3,7 @@ void init_vec_b(const double * restrict a, double * restrict b, int n); void matvec_mul(int n, const double * restrict A, const double * restrict x, double * restrict x_k); +double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n); +double get_r2_value(const double * restrict x_k, int n); #endif diff --git a/2025.03.28/04Ex/solve.c b/2025.03.28/04Ex/solve.c index 3859f58..56ca318 100644 --- a/2025.03.28/04Ex/solve.c +++ b/2025.03.28/04Ex/solve.c @@ -62,45 +62,3 @@ void t4_solve(const double * restrict A, double * restrict x_0, const double * r } } -double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n) -{ - double norm_r1x_1 = 0; - double residual_norm_1 = 0; - double r1 = 0; - - #pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1) - for (int i = 0; i < n; ++i) - { - double bi = b[i]; - double sum = 0; - - #pragma omp simd reduction(+:sum) - for (int j = 0; j < n; ++j) - sum += A[i*n + j] * x_k[j]; - - residual_norm_1 += fabs(sum - bi); - norm_r1x_1 += fabs(bi); - } - - r1 = residual_norm_1 / norm_r1x_1; - - return r1; -} - -double get_r2_value(const double * restrict x_k, int n) -{ - double relative_error = 0; - double total_diff = 0; - double template_sum = 0; - - #pragma omp parallel for reduction(+:total_diff, template_sum) - for (int i = 0; i < n; ++i) - { - short int modi = i ^ 1; - total_diff += fabs(x_k[i] - modi); - template_sum += modi; - } - - relative_error = total_diff / template_sum; - return relative_error; -} diff --git a/2025.03.28/04Ex/solve.h b/2025.03.28/04Ex/solve.h index 4ee8aa5..aea81eb 100644 --- a/2025.03.28/04Ex/solve.h +++ b/2025.03.28/04Ex/solve.h @@ -2,7 +2,5 @@ #define SOLVE_H void t4_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m); -double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n); -double get_r2_value(const double * restrict x_k, int n); #endif diff --git a/2025.03.28/05Ex/main.c b/2025.03.28/05Ex/main.c index 2e74bf1..8a9cbcc 100644 --- a/2025.03.28/05Ex/main.c +++ b/2025.03.28/05Ex/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "array_io.h" #include "io_status.h" #include "matrix.h" @@ -135,14 +136,14 @@ int main(int argc, char *argv[]) printf("Vector b:\n"); print_matrix(b, 1, n, p); - t = clock(); + t = omp_get_wtime(); t5_solve(a, x_0, b, x, r, n, m); - t = (clock() - t) / CLOCKS_PER_SEC; + t = omp_get_wtime() - t; r1 = get_r1(a, x, b, n); r2 = get_r2_value(x, n); - printf("Vector x_m:\n"); + printf("New vector:\n"); print_matrix(x, 1, n, p); printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); diff --git a/2025.03.28/05Ex/matrix.c b/2025.03.28/05Ex/matrix.c index 74fea61..0cf5bbb 100644 --- a/2025.03.28/05Ex/matrix.c +++ b/2025.03.28/05Ex/matrix.c @@ -29,3 +29,45 @@ void matvec_mul(int n, const double * restrict A, const double * restrict x, dou } } +double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n) +{ + double norm_r1x_1 = 0; + double residual_norm_1 = 0; + double r1 = 0; + + #pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1) + for (int i = 0; i < n; ++i) + { + double bi = b[i]; + double sum = 0; + + #pragma omp simd reduction(+:sum) + for (int j = 0; j < n; ++j) + sum += A[i*n + j] * x_k[j]; + + residual_norm_1 += fabs(sum - bi); + norm_r1x_1 += fabs(bi); + } + + r1 = residual_norm_1 / norm_r1x_1; + + return r1; +} + +double get_r2_value(const double * restrict x_k, int n) +{ + double relative_error = 0; + double total_diff = 0; + double template_sum = 0; + + #pragma omp parallel for reduction(+:total_diff, template_sum) + for (int i = 0; i < n; ++i) + { + short int modi = !(i & 1); + total_diff += fabs(x_k[i] - modi); + template_sum += modi; + } + + relative_error = total_diff / template_sum; + return relative_error; +} diff --git a/2025.03.28/05Ex/matrix.h b/2025.03.28/05Ex/matrix.h index 66351d7..a170f22 100644 --- a/2025.03.28/05Ex/matrix.h +++ b/2025.03.28/05Ex/matrix.h @@ -3,5 +3,7 @@ void init_vec_b(const double * restrict a, double * restrict b, int n); void matvec_mul(int n, const double * restrict A, const double * restrict x, double * restrict x_k); +double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n); +double get_r2_value(const double * restrict x_k, int n); #endif diff --git a/2025.03.28/05Ex/solve.c b/2025.03.28/05Ex/solve.c index 814e3db..2759c2a 100644 --- a/2025.03.28/05Ex/solve.c +++ b/2025.03.28/05Ex/solve.c @@ -42,7 +42,7 @@ void t5_solve(const double * restrict A, double * restrict x_0, const double * r } t = dot_Dr_r / dot_ADr_Dr; - + #pragma omp simd for (int i = 0; i < n; ++i) x[i] = x_0[i] - r[i]*t; @@ -68,45 +68,3 @@ void t5_solve(const double * restrict A, double * restrict x_0, const double * r } } -double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n) -{ - double norm_r1x_1 = 0; - double residual_norm_1 = 0; - double r1 = 0; - - #pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1) - for (int i = 0; i < n; ++i) - { - double bi = b[i]; - double sum = 0; - - #pragma omp simd reduction(+:sum) - for (int j = 0; j < n; ++j) - sum += A[i*n + j] * x_k[j]; - - residual_norm_1 += fabs(sum - bi); - norm_r1x_1 += fabs(bi); - } - - r1 = residual_norm_1 / norm_r1x_1; - - return r1; -} - -double get_r2_value(const double * restrict x_k, int n) -{ - double relative_error = 0; - double total_diff = 0; - double template_sum = 0; - - #pragma omp parallel for reduction(+:total_diff, template_sum) - for (int i = 0; i < n; ++i) - { - short int modi = i ^ 1; - total_diff += fabs(x_k[i] - modi); - template_sum += modi; - } - - relative_error = total_diff / template_sum; - return relative_error; -} diff --git a/2025.03.28/05Ex/solve.h b/2025.03.28/05Ex/solve.h index c4152b8..dd1b996 100644 --- a/2025.03.28/05Ex/solve.h +++ b/2025.03.28/05Ex/solve.h @@ -2,7 +2,5 @@ #define SOLVE_H void t5_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m); -double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n); -double get_r2_value(const double * restrict x_k, int n); #endif diff --git a/2025.03.28/06Ex/main.c b/2025.03.28/06Ex/main.c index 5df59a9..1a71b59 100644 --- a/2025.03.28/06Ex/main.c +++ b/2025.03.28/06Ex/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "array_io.h" #include "io_status.h" #include "matrix.h" @@ -135,14 +136,14 @@ int main(int argc, char *argv[]) printf("Vector b:\n"); print_matrix(b, 1, n, p); - t = clock(); + t = omp_get_wtime(); t6_solve(a, x_0, b, x, r, n, m); - t = (clock() - t) / CLOCKS_PER_SEC; + t = omp_get_wtime() - t; r1 = get_r1(a, x, b, n); r2 = get_r2_value(x, n); - printf("Vector x_m:\n"); + printf("New vector:\n"); print_matrix(x, 1, n, p); printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); diff --git a/2025.03.28/06Ex/matrix.c b/2025.03.28/06Ex/matrix.c index 7c6d930..0cf5bbb 100644 --- a/2025.03.28/06Ex/matrix.c +++ b/2025.03.28/06Ex/matrix.c @@ -63,7 +63,7 @@ double get_r2_value(const double * restrict x_k, int n) #pragma omp parallel for reduction(+:total_diff, template_sum) for (int i = 0; i < n; ++i) { - short int modi = i ^ 1; + short int modi = !(i & 1); total_diff += fabs(x_k[i] - modi); template_sum += modi; } @@ -71,4 +71,3 @@ double get_r2_value(const double * restrict x_k, int n) relative_error = total_diff / template_sum; return relative_error; } - diff --git a/2025.03.28/06Ex/solve.c b/2025.03.28/06Ex/solve.c index fc678d8..3c11b41 100644 --- a/2025.03.28/06Ex/solve.c +++ b/2025.03.28/06Ex/solve.c @@ -29,8 +29,8 @@ void t6_solve(const double * restrict A, double * restrict x_0, const double * r #pragma omp simd reduction(+:sum) for (int j = 0; j < n; ++j) sum += A[i*n + j] * r[j]; - - dot_ADr_r += sum * r[i]; + + dot_ADr_r += sum * r[i] * A[i*n + i]; dot_ADr_ADr += sum * sum; } @@ -38,7 +38,7 @@ void t6_solve(const double * restrict A, double * restrict x_0, const double * r #pragma omp simd for (int i = 0; i < n; ++i) - x[i] = x_0[i] - r[i]*t; + x[i] = x_0[i] - (r[i]*t); swap_temp = x; x = x_0; diff --git a/2025.03.28/07Ex/main.c b/2025.03.28/07Ex/main.c index ba9ec78..e9e1ea1 100644 --- a/2025.03.28/07Ex/main.c +++ b/2025.03.28/07Ex/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "array_io.h" #include "io_status.h" #include "matrix.h" @@ -136,14 +137,14 @@ int main(int argc, char *argv[]) printf("Vector b:\n"); print_matrix(b, 1, n, p); - t = clock(); + t = omp_get_wtime(); t7_solve(a, x_0, b, x, r, n, m, tau); - t = (clock() - t) / CLOCKS_PER_SEC; + t = omp_get_wtime() - t; r1 = get_r1(a, x, b, n); r2 = get_r2_value(x, n); - printf("Vector x_m:\n"); + printf("New vector:\n"); print_matrix(x, 1, n, p); printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); diff --git a/2025.03.28/07Ex/matrix.c b/2025.03.28/07Ex/matrix.c index e8e29d9..0cf5bbb 100644 --- a/2025.03.28/07Ex/matrix.c +++ b/2025.03.28/07Ex/matrix.c @@ -63,7 +63,7 @@ double get_r2_value(const double * restrict x_k, int n) #pragma omp parallel for reduction(+:total_diff, template_sum) for (int i = 0; i < n; ++i) { - short int modi = i ^ 1; + short int modi = !(i & 1); total_diff += fabs(x_k[i] - modi); template_sum += modi; } diff --git a/2025.03.28/07Ex/t.txt b/2025.03.28/07Ex/t.txt index 229972f..e8183f0 100644 --- a/2025.03.28/07Ex/t.txt +++ b/2025.03.28/07Ex/t.txt @@ -1,5 +1,3 @@ -0 -0 -0 -0 -0 +1 +1 +1 diff --git a/2025.03.28/08Ex/Makefile b/2025.03.28/08Ex/Makefile index 8274fa3..b98ac81 100644 --- a/2025.03.28/08Ex/Makefile +++ b/2025.03.28/08Ex/Makefile @@ -8,7 +8,7 @@ CFLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ -Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ -Wmissing-field-initializers -Wpointer-sign -fopenmp -O3 -TARGET = a07.out +TARGET = a08.out OBJ = main.o solve.o array_io.o init_f.o matrix.o %.o: %.c diff --git a/2025.03.28/08Ex/main.c b/2025.03.28/08Ex/main.c index ba9ec78..58d8e9a 100644 --- a/2025.03.28/08Ex/main.c +++ b/2025.03.28/08Ex/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "array_io.h" #include "io_status.h" #include "matrix.h" @@ -9,8 +10,8 @@ /* ./a.out t m n p k_a [filename_a] k_x [filename_x] */ int main(int argc, char *argv[]) { - double tau, t, r1, r2, *a, *x_0, *b, *x, *r; - int n, m, p, k_a, k_x, task = 7; + double tau, t, r1, r2, *a, *x_0, *b, *x, *r, *w; + int n, m, p, k_a, k_x, task = 8; char *name_a = 0, *name_x = 0; if (!((argc == 7 || argc == 8 || argc == 9) && sscanf(argv[1], "%lf", &tau) == 1 && @@ -76,6 +77,17 @@ int main(int argc, char *argv[]) printf("Not enough memory\n"); return 2; } + w = (double *)malloc((size_t)n * sizeof(double)); + if (!w) + { + free(a); + free(x_0); + free(b); + free(x); + free(r); + printf("Not enough memory\n"); + return 2; + } if (name_a) { /* из файла */ @@ -98,6 +110,7 @@ int main(int argc, char *argv[]) free(b); free(x); free(r); + free(w); return 3; } while (0); } else init_matrix(a, n, n, k_a); @@ -123,6 +136,7 @@ int main(int argc, char *argv[]) free(b); free(x); free(r); + free(w); return 3; } while (0); } else init_matrix(x_0, n, 1, k_x); @@ -136,14 +150,14 @@ int main(int argc, char *argv[]) printf("Vector b:\n"); print_matrix(b, 1, n, p); - t = clock(); - t7_solve(a, x_0, b, x, r, n, m, tau); - t = (clock() - t) / CLOCKS_PER_SEC; + t = omp_get_wtime(); + t8_solve(a, x_0, b, x, r, w, n, m, tau); + t = omp_get_wtime() - t; r1 = get_r1(a, x, b, n); r2 = get_r2_value(x, n); - printf("Vector x_m:\n"); + printf("New vector:\n"); print_matrix(x, 1, n, p); printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); @@ -152,6 +166,7 @@ int main(int argc, char *argv[]) free(b); free(x); free(r); + free(w); return 0; } diff --git a/2025.03.28/08Ex/matrix.c b/2025.03.28/08Ex/matrix.c index e8e29d9..0cf5bbb 100644 --- a/2025.03.28/08Ex/matrix.c +++ b/2025.03.28/08Ex/matrix.c @@ -63,7 +63,7 @@ double get_r2_value(const double * restrict x_k, int n) #pragma omp parallel for reduction(+:total_diff, template_sum) for (int i = 0; i < n; ++i) { - short int modi = i ^ 1; + short int modi = !(i & 1); total_diff += fabs(x_k[i] - modi); template_sum += modi; } diff --git a/2025.03.28/08Ex/solve.c b/2025.03.28/08Ex/solve.c index d2e5149..3a5c733 100644 --- a/2025.03.28/08Ex/solve.c +++ b/2025.03.28/08Ex/solve.c @@ -3,24 +3,40 @@ #include -void t7_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m, double t) +void t8_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, double * restrict w, int n, int m, double t) { (void)r; + (void)w; for (int k = 0; k < m; ++k) { double * swap_temp; - - #pragma omp parallel for + for (int i = 0; i < n; ++i) { - double sum = 0; - - #pragma omp simd reduction(+:sum) - for (int j = 0; j < n; ++j) - sum += A[i*n + j] * x_0[j]; + double sum_x = 0; + double sum_r = 0; + double temp = 0; + double aii = A[i*n + i]; - x[i] = x_0[i] + ((b[i] - sum) / A[i*n + i]) * t; + #pragma omp simd reduction(+:sum_x, sum_r) + for (int j = 0; j < i; ++j) + { + double aij = A[i*n + j]; + double rj = aij * x_0[j]; + sum_x += rj - aij * x[j]; + sum_r += rj; + } + + temp = aii * x_0[i]; + sum_x += temp; + sum_r += temp; + + #pragma omp simd reduction(+:sum_r) + for (int j = i+1; j < n; ++j) + sum_r += A[i*n + j] * x_0[j]; + + x[i] = (sum_x + (b[i] - sum_r) * t) / aii; } swap_temp = x; diff --git a/2025.03.28/08Ex/solve.h b/2025.03.28/08Ex/solve.h index 49b1583..75e1f0e 100644 --- a/2025.03.28/08Ex/solve.h +++ b/2025.03.28/08Ex/solve.h @@ -1,6 +1,6 @@ #ifndef SOLVE_H #define SOLVE_H -void t7_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m, double t); +void t8_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, double * restrict w, int n, int m, double t); #endif diff --git a/2025.03.28/dist/Ulyanov/Makefile b/2025.03.28/dist/Ulyanov/Makefile index a6677ef..a803d7a 100644 --- a/2025.03.28/dist/Ulyanov/Makefile +++ b/2025.03.28/dist/Ulyanov/Makefile @@ -1,11 +1,23 @@ FLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -Wmaybe-uninitialized -O3 -all: a01.out a02.out a03.out +all: a01.out a02.out a03.out a04.out a05.out a06.out a07.out a08.out a09.out a01.out: a01.o array.o matrix.o gcc a01.o array.o matrix.o -lm -o a01.out a02.out: a02.o array.o matrix.o gcc a02.o array.o matrix.o -lm -o a02.out a03.out: a03.o array.o matrix.o gcc a03.o array.o matrix.o -lm -o a03.out +a04.out: a04.o array.o matrix.o + gcc a04.o array.o matrix.o -lm -o a04.out +a05.out: a05.o array.o matrix.o + gcc a05.o array.o matrix.o -lm -o a05.out +a06.out: a06.o array.o matrix.o + gcc a06.o array.o matrix.o -lm -o a06.out +a07.out: a07.o array.o matrix.o + gcc a07.o array.o matrix.o -lm -o a07.out +a08.out: a08.o array.o matrix.o + gcc a08.o array.o matrix.o -lm -o a08.out +a09.out: a09.o array.o matrix.o + gcc a09.o array.o matrix.o -lm -o a09.out array.o: gcc -c $(FLAGS) -o array.o array.c matrix.o: @@ -16,5 +28,17 @@ a02.o: gcc -c $(FLAGS) -o a02.o task02.c a03.o: gcc -c $(FLAGS) -o a03.o task03.c +a04.o: + gcc -c $(FLAGS) -o a04.o task04.c +a05.o: + gcc -c $(FLAGS) -o a05.o task05.c +a06.o: + gcc -c $(FLAGS) -o a06.o task06.c +a07.o: + gcc -c $(FLAGS) -o a07.o task07.c +a08.o: + gcc -c $(FLAGS) -o a08.o task08.c +a09.o: + gcc -c $(FLAGS) -o a09.o task09.c clean: rm -f *.o *.out diff --git a/2025.03.28/dist/Ulyanov/input1.txt b/2025.03.28/dist/Ulyanov/input1.txt new file mode 100644 index 0000000..4d9f00d --- /dev/null +++ b/2025.03.28/dist/Ulyanov/input1.txt @@ -0,0 +1,2 @@ +1 2 +2 1 diff --git a/2025.03.28/dist/Ulyanov/input2.txt b/2025.03.28/dist/Ulyanov/input2.txt new file mode 100644 index 0000000..6ed281c --- /dev/null +++ b/2025.03.28/dist/Ulyanov/input2.txt @@ -0,0 +1,2 @@ +1 +1 diff --git a/2025.03.28/dist/Ulyanov/matrix.c b/2025.03.28/dist/Ulyanov/matrix.c index 5e22a00..78bcb3d 100644 --- a/2025.03.28/dist/Ulyanov/matrix.c +++ b/2025.03.28/dist/Ulyanov/matrix.c @@ -1,6 +1,7 @@ #include #include #include +#include "io_status.h" #include "matrix.h" void multmatvec(double* a, double* x1, double* x2, int n) @@ -97,3 +98,56 @@ int equal(double a, double b) if (fabs(a - b) <= EPS * fmax(fabs(a), fabs(b))) return 1; return 0; } + +void multdiagvec(double* a, double* x1, double* x2, int n) + { + int i; + for (i = 0; i < n; i++) x2[i] = x1[i] * a[i * n + i]; + } + +void deldiagvec(double* a, double* x1, double* x2, int n) + { + int i; + for (i = 0; i < n; i++) x2[i] = x1[i] / a[i * n + i]; + } + +void deldiagmultvec(double* a, double* x1, double* x2, double tau, int n) + { + int i; + for (i = 0; i < n; i++) x2[i] = tau * (x1[i] / a[i * n + i]); + } + +io_status checkdiag(double* a, int n) + { + int i; + for (i = 0; i < n; i++) + { + if (equal(a[i * n + i], 0)) return ERROR_READ; + } + return SUCCESS; + } + +void slaudown(double* a, double* x, double* r, int n) + { + int i, j; + double sum; + for (i = 0; i < n; i++) + { + sum = x[i]; + for (j = 0; j < i; j++) sum -= a[i * n + j] * r[j]; + r[i] = sum / a[i * n + i]; + } + } + +void slauup(double* a, double* x, double* r, int n) + { + int i, j; + double sum; + for (i = n - 1; i >= 0; i--) + { + sum = x[i]; + for (j = i + 1; j < n; j++) sum -= a[i * n + j] * r[j]; + r[i] = sum / a[i * n + i]; + } + } + diff --git a/2025.03.28/dist/Ulyanov/matrix.h b/2025.03.28/dist/Ulyanov/matrix.h index bc71ea1..4c56e5e 100644 --- a/2025.03.28/dist/Ulyanov/matrix.h +++ b/2025.03.28/dist/Ulyanov/matrix.h @@ -10,3 +10,9 @@ double count_task1(double*, double*, double, int); double count_r1(double*, double*, double*, int); double count_r2(double*, int); int equal(double, double); +void multdiagvec(double*, double*, double*, int); +void deldiagvec(double*, double*, double*, int); +void deldiagmultvec(double*, double*, double*, double, int); +io_status checkdiag(double*, int); +void slaudown(double*, double*, double*, int); +void slauup(double*, double*, double*, int); diff --git a/2025.03.28/dist/Ulyanov/task01.c b/2025.03.28/dist/Ulyanov/task01.c index 3aa68d8..0a99f60 100644 --- a/2025.03.28/dist/Ulyanov/task01.c +++ b/2025.03.28/dist/Ulyanov/task01.c @@ -123,12 +123,6 @@ int main(int argc, char* argv[]) r1 = solve1(a, x0, x, n, m); t = (clock() - t) / CLOCKS_PER_SEC; r2 = count_task1(a, x, r1, n); - - printf("Vector x:\n"); - if (m % 2) - print_matrix(x, 1, n, p); - else - print_matrix(x0, 1, n, p); printf ("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); free(a); free(x0); diff --git a/2025.03.28/dist/Ulyanov/task02.c b/2025.03.28/dist/Ulyanov/task02.c index 00364cd..a253adf 100644 --- a/2025.03.28/dist/Ulyanov/task02.c +++ b/2025.03.28/dist/Ulyanov/task02.c @@ -131,9 +131,9 @@ int main(int argc, char* argv[]) printf("Matrix:\n"); print_matrix(a, n, n, p); printf("Vector:\n"); - print_matrix(x0, 1, n, p); + print_matrix(x0, n, 1, p); printf("Vector b:\n"); - print_matrix(b, 1, n, p); + print_matrix(b, n, 1, p); t = clock(); solve2(a, x0, x, b, n, m, tau); t = (clock() - t) / CLOCKS_PER_SEC; diff --git a/2025.03.28/dist/Ulyanov/task04.c b/2025.03.28/dist/Ulyanov/task04.c index 7344dde..99e5774 100644 --- a/2025.03.28/dist/Ulyanov/task04.c +++ b/2025.03.28/dist/Ulyanov/task04.c @@ -171,8 +171,8 @@ void solve4(double* a, double* x0, double* x, double* b, double* r, int n, int m for (k = 1; k <= m; k++) { multmatvec(a, r, x0, n); - scalp2 = scalp(x0, r, n); - if (!equal(scalp2, 0)) tau = scalp(r, r, n) / scalp2; + scalp2 = scalp(x0, x0, n); + if (!equal(scalp2, 0)) tau = scalp(x0, r, n) / scalp2; else break; veccomb(1, x, -tau, r, n); veccomb(1, r, -tau, x0, n); diff --git a/2025.03.28/dist/Ulyanov/task05.c b/2025.03.28/dist/Ulyanov/task05.c new file mode 100644 index 0000000..bedb294 --- /dev/null +++ b/2025.03.28/dist/Ulyanov/task05.c @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include "io_status.h" +#include "array.h" +#include "matrix.h" + +void solve5(double*, double*, double*, double*, double*, int, int); + +int main(int argc, char* argv[]) + { + int task = 5; + double* a; + double* x0; + double* x; + double* b; + double* r; + int n, m, p, k1, k2; + char* name1 = 0; + char* name2 = 0; + double r1, r2; + double t; + if (!((argc == 6 || argc == 7 || argc == 8) && sscanf(argv[1], "%d", &m) == 1 && sscanf(argv[2], "%d", &n) == 1 && sscanf(argv[3], "%d", &p) == 1 && sscanf(argv[4], "%d", &k1) == 1 && k1 >= 0 && k1 <= 4)) + { + printf("Usage: %s n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + if (k1 == 0) + { + if (!(sscanf(argv[6], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4) + { + printf("Usage: %s n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + name1 = argv[5]; + } + else + { + if (!(sscanf(argv[5], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4) + { + printf("Usage: %s n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + } + if (k2 == 0) + { + if (k1 == 0) name2 = argv[7]; + else name2 = argv[6]; + } + a = (double*) malloc(n * n * sizeof(double)); + if (!a) + { + printf("Not enough memory\n"); + return 2; + } + if (name1) + { + io_status ret; + ret = read_matrix(a, n, n, name1); + do + { + switch(ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Can not open file %s\n", name1); + break; + case ERROR_READ: + printf("Can not read file %s\n", name1); + break; + } + free(a); + return 3; + } while (0); + } + else + init_matrix(a, n, n, k1); + x0 = (double*) malloc(n * sizeof(double)); + if (!x0) + { + printf("Not enough memory\n"); + free(a); + return 2; + } + x = (double*) malloc(n * sizeof(double)); + if (!x) + { + printf("Not enough memory\n"); + free(a); + free(x0); + return 2; + } + b = (double*) malloc(n * sizeof(double)); + if (!b) + { + printf("Not enough memory\n"); + free(a); + free(x0); + free(x); + return 2; + } + r = (double*) malloc(n * sizeof(double)); + if (!r) + { + printf("Not enough memory\n"); + free(a); + free(x0); + free(x); + free(b); + return 2; + } + if (name2) + { + io_status ret; + ret = read_matrix(x0, n, 1, name2); + do + { + switch(ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Can not open file %s\n", name2); + break; + case ERROR_READ: + printf("Can not read file %s\n", name2); + break; + } + free(a); + free(x0); + free(x); + free(b); + free(r); + return 3; + } while (0); + } + else + init_matrix(x0, n, 1, k2); + init_vector(b, a, n); + if (checkdiag(a, n) == ERROR_READ) + { + printf("Inverse matrix does not exist\n"); + free(a); + free(x0); + free(x); + free(b); + free(r); + return 4; + } + printf("Matrix:\n"); + print_matrix(a, n, n, p); + printf("Vector:\n"); + print_matrix(x0, n, 1, p); + printf("Vector b:\n"); + print_matrix(b, n, 1, p); + t = clock(); + solve5(a, x0, x, b, r, n, m); + t = (clock() - t) / CLOCKS_PER_SEC; + r1 = count_r1(a, b, x, n); + r2 = count_r2(x, n); + printf("New vector:\n"); + print_matrix(x, n, 1, p); + printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); + free(a); + free(x0); + free(x); + free(b); + free(r); + return 0; + } + +void solve5(double* a, double* x0, double* x, double* b, double* r, int n, int m) + { + int k; + double tau, scalp1, scalp2; + veccpy(x, x0, n); + multmatvec(a, x, r, n); + vecdiff(r, b, n); + for (k = 1; k <= m; k++) + { + deldiagvec(a, r, x0, n); + scalp1 = scalp(x0, r, n); + veccpy(r, x0, n); + multmatvec(a, r, x0, n); + scalp2 = scalp(x0, r, n); + if (!equal(scalp2, 0)) tau = scalp1 / scalp2; + else break; + veccomb(1, x, -tau, r, n); + multdiagvec(a, r, r, n); + veccomb(1, r, -tau, x0, n); + } + } diff --git a/2025.03.28/dist/Ulyanov/task06.c b/2025.03.28/dist/Ulyanov/task06.c new file mode 100644 index 0000000..a6187ab --- /dev/null +++ b/2025.03.28/dist/Ulyanov/task06.c @@ -0,0 +1,195 @@ +#include +#include +#include +#include +#include "io_status.h" +#include "array.h" +#include "matrix.h" + +void solve6(double*, double*, double*, double*, double*, int, int); + +int main(int argc, char* argv[]) + { + int task = 6; + double* a; + double* x0; + double* x; + double* b; + double* r; + int n, m, p, k1, k2; + char* name1 = 0; + char* name2 = 0; + double r1, r2; + double t; + if (!((argc == 6 || argc == 7 || argc == 8) && sscanf(argv[1], "%d", &m) == 1 && sscanf(argv[2], "%d", &n) == 1 && sscanf(argv[3], "%d", &p) == 1 && sscanf(argv[4], "%d", &k1) == 1 && k1 >= 0 && k1 <= 4)) + { + printf("Usage: %s n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + if (k1 == 0) + { + if (!(sscanf(argv[6], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4) + { + printf("Usage: %s n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + name1 = argv[5]; + } + else + { + if (!(sscanf(argv[5], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4) + { + printf("Usage: %s n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + } + if (k2 == 0) + { + if (k1 == 0) name2 = argv[7]; + else name2 = argv[6]; + } + a = (double*) malloc(n * n * sizeof(double)); + if (!a) + { + printf("Not enough memory\n"); + return 2; + } + if (name1) + { + io_status ret; + ret = read_matrix(a, n, n, name1); + do + { + switch(ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Can not open file %s\n", name1); + break; + case ERROR_READ: + printf("Can not read file %s\n", name1); + break; + } + free(a); + return 3; + } while (0); + } + else + init_matrix(a, n, n, k1); + x0 = (double*) malloc(n * sizeof(double)); + if (!x0) + { + printf("Not enough memory\n"); + free(a); + return 2; + } + x = (double*) malloc(n * sizeof(double)); + if (!x) + { + printf("Not enough memory\n"); + free(a); + free(x0); + return 2; + } + b = (double*) malloc(n * sizeof(double)); + if (!b) + { + printf("Not enough memory\n"); + free(a); + free(x0); + free(x); + return 2; + } + r = (double*) malloc(n * sizeof(double)); + if (!r) + { + printf("Not enough memory\n"); + free(a); + free(x0); + free(x); + free(b); + return 2; + } + if (name2) + { + io_status ret; + ret = read_matrix(x0, n, 1, name2); + do + { + switch(ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Can not open file %s\n", name2); + break; + case ERROR_READ: + printf("Can not read file %s\n", name2); + break; + } + free(a); + free(x0); + free(x); + free(b); + free(r); + return 3; + } while (0); + } + else + init_matrix(x0, n, 1, k2); + init_vector(b, a, n); + if (checkdiag(a, n) == ERROR_READ) + { + printf("Inverse matrix does not exist\n"); + free(a); + free(x0); + free(x); + free(b); + free(r); + return 4; + } + printf("Matrix:\n"); + print_matrix(a, n, n, p); + printf("Vector:\n"); + print_matrix(x0, n, 1, p); + printf("Vector b:\n"); + print_matrix(b, n, 1, p); + t = clock(); + solve6(a, x0, x, b, r, n, m); + t = (clock() - t) / CLOCKS_PER_SEC; + r1 = count_r1(a, b, x, n); + r2 = count_r2(x, n); + printf("New vector:\n"); + print_matrix(x, n, 1, p); + printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); + free(a); + free(x0); + free(x); + free(b); + free(r); + return 0; + } + +void solve6(double* a, double* x0, double* x, double* b, double* r, int n, int m) + { + int k; + double tau, scalp1, scalp2; + veccpy(x, x0, n); + multmatvec(a, x, r, n); + vecdiff(r, b, n); + for (k = 1; k <= m; k++) + { + deldiagvec(a, r, r, n); + multmatvec(a, r, x0, n); + multdiagvec(a, r, r, n); + scalp1 = scalp(x0, r, n); + scalp2 = scalp(x0, x0, n); + if (!equal(scalp2, 0)) tau = scalp1 / scalp2; + else break; + deldiagvec(a, r, r, n); + veccomb(1, x, -tau, r, n); + multdiagvec(a, r, r, n); + veccomb(1, r, -tau, x0, n); + } + } diff --git a/2025.03.28/dist/Ulyanov/task07.c b/2025.03.28/dist/Ulyanov/task07.c new file mode 100644 index 0000000..1ba64d7 --- /dev/null +++ b/2025.03.28/dist/Ulyanov/task07.c @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include "io_status.h" +#include "array.h" +#include "matrix.h" + +void solve7(double*, double*, double*, double*, int, int, double); + +int main(int argc, char* argv[]) + { + int task = 7; + double* a; + double* x0; + double* x; + double* b; + int n, m, p, k1, k2; + char* name1 = 0; + char* name2 = 0; + double tau; + double r1, r2; + double t; + if (!((argc == 7 || argc == 8 || argc == 9) && sscanf(argv[1], "%lf", &tau) == 1 && sscanf(argv[2], "%d", &m) == 1 && sscanf(argv[3], "%d", &n) == 1 && sscanf(argv[4], "%d", &p) == 1 && sscanf(argv[5], "%d", &k1) == 1 && k1 >= 0 && k1 <= 4)) + { + printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + if (k1 == 0) + { + if (!(sscanf(argv[7], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4) + { + printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + name1 = argv[6]; + } + else + { + if (!(sscanf(argv[6], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4) + { + printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + } + if (k2 == 0) + { + if (k1 == 0) name2 = argv[8]; + else name2 = argv[7]; + } + a = (double*) malloc(n * n * sizeof(double)); + if (!a) + { + printf("Not enough memory\n"); + return 2; + } + if (name1) + { + io_status ret; + ret = read_matrix(a, n, n, name1); + do + { + switch(ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Can not open file %s\n", name1); + break; + case ERROR_READ: + printf("Can not read file %s\n", name1); + break; + } + free(a); + return 3; + } while (0); + } + else + init_matrix(a, n, n, k1); + x0 = (double*) malloc(n * sizeof(double)); + if (!x0) + { + printf("Not enough memory\n"); + free(a); + return 2; + } + x = (double*) malloc(n * sizeof(double)); + if (!x) + { + printf("Not enough memory\n"); + free(a); + free(x0); + return 2; + } + b = (double*) malloc(n * sizeof(double)); + if (!b) + { + printf("Not enough memory\n"); + free(a); + free(x0); + free(x); + return 2; + } + if (name2) + { + io_status ret; + ret = read_matrix(x0, n, 1, name2); + do + { + switch(ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Can not open file %s\n", name2); + break; + case ERROR_READ: + printf("Can not read file %s\n", name2); + break; + } + free(a); + free(x0); + free(x); + free(b); + return 3; + } while (0); + } + else + init_matrix(x0, n, 1, k2); + init_vector(b, a, n); + if (checkdiag(a, n) == ERROR_READ) + { + printf("Inverse matrix does not exist\n"); + free(a); + free(x0); + free(x); + free(b); + return 4; + } + printf("Matrix:\n"); + print_matrix(a, n, n, p); + printf("Vector:\n"); + print_matrix(x0, n, 1, p); + printf("Vector b:\n"); + print_matrix(b, n, 1, p); + t = clock(); + solve7(a, x0, x, b, n, m, tau); + t = (clock() - t) / CLOCKS_PER_SEC; + r1 = count_r1(a, b, x, n); + r2 = count_r2(x, n); + printf("New vector:\n"); + print_matrix(x, n, 1, p); + printf ("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); + free(a); + free(x0); + free(x); + free(b); + return 0; + } + +void solve7(double* a, double* x0, double* x, double* b, int n, int m, double tau) + { + int k; + veccpy(x, x0, n); + for (k = 1; k <= m; k++) + { + multmatvec(a, x, x0, n); + vecdiff(x0, b, n); + deldiagmultvec(a, x0, x0, tau, n); + vecdiff(x, x0, n); + } + } diff --git a/2025.03.28/dist/Ulyanov/task08.c b/2025.03.28/dist/Ulyanov/task08.c new file mode 100644 index 0000000..2260cc2 --- /dev/null +++ b/2025.03.28/dist/Ulyanov/task08.c @@ -0,0 +1,188 @@ +#include +#include +#include +#include +#include "io_status.h" +#include "array.h" +#include "matrix.h" + +void solve8(double*, double*, double*, double*, double*, int, int, double); + +int main(int argc, char* argv[]) + { + int task = 8; + double* a; + double* x0; + double* x; + double* b; + double* r; + int n, m, p, k1, k2; + char* name1 = 0; + char* name2 = 0; + double tau; + double r1, r2; + double t; + if (!((argc == 7 || argc == 8 || argc == 9) && sscanf(argv[1], "%lf", &tau) == 1 && sscanf(argv[2], "%d", &m) == 1 && sscanf(argv[3], "%d", &n) == 1 && sscanf(argv[4], "%d", &p) == 1 && sscanf(argv[5], "%d", &k1) == 1 && k1 >= 0 && k1 <= 4)) + { + printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + if (k1 == 0) + { + if (!(sscanf(argv[7], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4) + { + printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + name1 = argv[6]; + } + else + { + if (!(sscanf(argv[6], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4) + { + printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + } + if (k2 == 0) + { + if (k1 == 0) name2 = argv[8]; + else name2 = argv[7]; + } + a = (double*) malloc(n * n * sizeof(double)); + if (!a) + { + printf("Not enough memory\n"); + return 2; + } + if (name1) + { + io_status ret; + ret = read_matrix(a, n, n, name1); + do + { + switch(ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Can not open file %s\n", name1); + break; + case ERROR_READ: + printf("Can not read file %s\n", name1); + break; + } + free(a); + return 3; + } while (0); + } + else + init_matrix(a, n, n, k1); + x0 = (double*) malloc(n * sizeof(double)); + if (!x0) + { + printf("Not enough memory\n"); + free(a); + return 2; + } + x = (double*) malloc(n * sizeof(double)); + if (!x) + { + printf("Not enough memory\n"); + free(a); + free(x0); + return 2; + } + b = (double*) malloc(n * sizeof(double)); + if (!b) + { + printf("Not enough memory\n"); + free(a); + free(x0); + free(x); + return 2; + } + r = (double*) malloc(n * sizeof(double)); + if (!b) + { + printf("Not enough memory\n"); + free(a); + free(x0); + free(x); + free(b); + return 2; + } + if (name2) + { + io_status ret; + ret = read_matrix(x0, n, 1, name2); + do + { + switch(ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Can not open file %s\n", name2); + break; + case ERROR_READ: + printf("Can not read file %s\n", name2); + break; + } + free(a); + free(x0); + free(x); + free(b); + free(r); + return 3; + } while (0); + } + else + init_matrix(x0, n, 1, k2); + init_vector(b, a, n); + if (checkdiag(a, n) == ERROR_READ) + { + printf("Inverse matrix does not exist\n"); + free(a); + free(x0); + free(x); + free(b); + return 4; + } + printf("Matrix:\n"); + print_matrix(a, n, n, p); + printf("Vector:\n"); + print_matrix(x0, n, 1, p); + printf("Vector b:\n"); + print_matrix(b, n, 1, p); + t = clock(); + solve8(a, x0, x, b, r, n, m, tau); + t = (clock() - t) / CLOCKS_PER_SEC; + r1 = count_r1(a, b, x, n); + r2 = count_r2(x, n); + printf("New vector:\n"); + print_matrix(x, n, 1, p); + printf ("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); + free(a); + free(x0); + free(x); + free(b); + free(r); + return 0; + } + +void solve8(double* a, double* x0, double* x, double* b, double* r, int n, int m, double tau) + { + int k; + veccpy(x, x0, n); + for (k = 1; k <= m; k++) + { + multmatvec(a, x, x0, n); + vecdiff(x0, b, n); + slaudown(a, x0, r, n); + vecmult(r, tau, n); + vecdiff(x, r, n); + } + } + + diff --git a/2025.03.28/dist/Ulyanov/task09.c b/2025.03.28/dist/Ulyanov/task09.c new file mode 100644 index 0000000..b5a5a84 --- /dev/null +++ b/2025.03.28/dist/Ulyanov/task09.c @@ -0,0 +1,188 @@ +#include +#include +#include +#include +#include "io_status.h" +#include "array.h" +#include "matrix.h" + +void solve9(double*, double*, double*, double*, double*, int, int, double); + +int main(int argc, char* argv[]) + { + int task = 9; + double* a; + double* x0; + double* x; + double* b; + double* r; + int n, m, p, k1, k2; + char* name1 = 0; + char* name2 = 0; + double tau; + double r1, r2; + double t; + if (!((argc == 7 || argc == 8 || argc == 9) && sscanf(argv[1], "%lf", &tau) == 1 && sscanf(argv[2], "%d", &m) == 1 && sscanf(argv[3], "%d", &n) == 1 && sscanf(argv[4], "%d", &p) == 1 && sscanf(argv[5], "%d", &k1) == 1 && k1 >= 0 && k1 <= 4)) + { + printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + if (k1 == 0) + { + if (!(sscanf(argv[7], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4) + { + printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + name1 = argv[6]; + } + else + { + if (!(sscanf(argv[6], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4) + { + printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]); + return 1; + } + } + if (k2 == 0) + { + if (k1 == 0) name2 = argv[8]; + else name2 = argv[7]; + } + a = (double*) malloc(n * n * sizeof(double)); + if (!a) + { + printf("Not enough memory\n"); + return 2; + } + if (name1) + { + io_status ret; + ret = read_matrix(a, n, n, name1); + do + { + switch(ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Can not open file %s\n", name1); + break; + case ERROR_READ: + printf("Can not read file %s\n", name1); + break; + } + free(a); + return 3; + } while (0); + } + else + init_matrix(a, n, n, k1); + x0 = (double*) malloc(n * sizeof(double)); + if (!x0) + { + printf("Not enough memory\n"); + free(a); + return 2; + } + x = (double*) malloc(n * sizeof(double)); + if (!x) + { + printf("Not enough memory\n"); + free(a); + free(x0); + return 2; + } + b = (double*) malloc(n * sizeof(double)); + if (!b) + { + printf("Not enough memory\n"); + free(a); + free(x0); + free(x); + return 2; + } + r = (double*) malloc(n * sizeof(double)); + if (!b) + { + printf("Not enough memory\n"); + free(a); + free(x0); + free(x); + free(b); + return 2; + } + if (name2) + { + io_status ret; + ret = read_matrix(x0, n, 1, name2); + do + { + switch(ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Can not open file %s\n", name2); + break; + case ERROR_READ: + printf("Can not read file %s\n", name2); + break; + } + free(a); + free(x0); + free(x); + free(b); + free(r); + return 3; + } while (0); + } + else + init_matrix(x0, n, 1, k2); + init_vector(b, a, n); + if (checkdiag(a, n) == ERROR_READ) + { + printf("Inverse matrix does not exist\n"); + free(a); + free(x0); + free(x); + free(b); + return 4; + } + printf("Matrix:\n"); + print_matrix(a, n, n, p); + printf("Vector:\n"); + print_matrix(x0, n, 1, p); + printf("Vector b:\n"); + print_matrix(b, n, 1, p); + t = clock(); + solve9(a, x0, x, b, r, n, m, tau); + t = (clock() - t) / CLOCKS_PER_SEC; + r1 = count_r1(a, b, x, n); + r2 = count_r2(x, n); + printf("New vector:\n"); + print_matrix(x, n, 1, p); + printf ("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); + free(a); + free(x0); + free(x); + free(b); + free(r); + return 0; + } + +void solve9(double* a, double* x0, double* x, double* b, double* r, int n, int m, double tau) + { + int k; + veccpy(x, x0, n); + for (k = 1; k <= m; k++) + { + multmatvec(a, x, x0, n); + vecdiff(x0, b, n); + slauup(a, x0, r, n); + vecmult(r, tau, n); + vecdiff(x, r, n); + } + } + +