From 335d164b10c0f506aedd0e76109c521e9dbeff21 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Wed, 26 Mar 2025 16:19:57 +0300 Subject: [PATCH] Task 3 is ok! --- 2025.03.28/03Ex/Makefile | 7 +++++-- 2025.03.28/03Ex/a.txt | 7 +++++-- 2025.03.28/03Ex/main.c | 15 +++++++++++++-- 2025.03.28/03Ex/matrix.c | 7 ------- 2025.03.28/03Ex/solve.c | 13 ++++++++++--- 2025.03.28/03Ex/solve.h | 6 +++--- 2025.03.28/03Ex/t.txt | 5 +++++ 7 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 2025.03.28/03Ex/t.txt diff --git a/2025.03.28/03Ex/Makefile b/2025.03.28/03Ex/Makefile index 0cc03d0..52d1463 100644 --- a/2025.03.28/03Ex/Makefile +++ b/2025.03.28/03Ex/Makefile @@ -8,8 +8,8 @@ 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 = a01.out -OBJ = main.o solve.o array_io.o init_f.o +TARGET = a03.out +OBJ = main.o solve.o array_io.o init_f.o matrix.o $(TARGET): $(OBJ) gcc $^ -o $@ -lm -fopenmp @@ -26,5 +26,8 @@ array_io.o: array_io.c init_f.o: init_f.c gcc $(CFLAGS) -c $^ +matrix.o: matrix.c + gcc $(CFLAGS) -c $^ + clean: rm *.o *.out diff --git a/2025.03.28/03Ex/a.txt b/2025.03.28/03Ex/a.txt index 4c8e387..171f0cf 100644 --- a/2025.03.28/03Ex/a.txt +++ b/2025.03.28/03Ex/a.txt @@ -1,2 +1,5 @@ -1.0001 0 -0 1 +4 1 0 0 0 +1 4 1 0 0 +0 1 4 1 0 +0 0 1 4 1 +0 0 0 1 4 diff --git a/2025.03.28/03Ex/main.c b/2025.03.28/03Ex/main.c index 7b25841..da817ae 100644 --- a/2025.03.28/03Ex/main.c +++ b/2025.03.28/03Ex/main.c @@ -3,12 +3,13 @@ #include #include "array_io.h" #include "io_status.h" +#include "matrix.h" #include "solve.h" /* ./a.out m n p k_a [filename_a] k_x [filename_x] */ int main(int argc, char *argv[]) { - double t, r1, r2, *a, *x_0, *b, *x; + double t, r1, r2, *a, *x_0, *b, *x, *r; int n, m, p, k_a, k_x, task = 3; char *name_a = 0, *name_x = 0; if (!((argc == 6 || argc == 7 || argc == 8) && @@ -64,6 +65,16 @@ int main(int argc, char *argv[]) printf("Not enough memory\n"); return 2; } + r = (double *)malloc((size_t)n * sizeof(double)); + if (!r) + { + free(a); + free(x_0); + free(b); + free(x); + printf("Not enough memory\n"); + return 2; + } if (name_a) { /* из файла */ @@ -123,7 +134,7 @@ int main(int argc, char *argv[]) print_matrix(b, 1, n, p); t = clock(); - t3_solve(a, x_0, x, n, m); + t3_solve(a, x_0, b, x, r, n, m); t = (clock() - t) / CLOCKS_PER_SEC; r1 = get_r1(a, x, b, n); diff --git a/2025.03.28/03Ex/matrix.c b/2025.03.28/03Ex/matrix.c index d73646a..f52cc14 100644 --- a/2025.03.28/03Ex/matrix.c +++ b/2025.03.28/03Ex/matrix.c @@ -16,8 +16,6 @@ void init_vec_b(const double * restrict a, double * restrict b, int n) } } -/* - * OLD VERSION void matvec_mul(int n, const double * restrict A, const double * restrict x, double * restrict x_k) { #pragma omp parallel for @@ -30,9 +28,4 @@ void matvec_mul(int n, const double * restrict A, const double * restrict x, dou x_k[i] = sum; } } -*/ -void matvec_mul(int n, const double * restrict A, const double * restrict x, double * restrict x_k) -{ - cblas_dgemv(CblasRowMajor, CblasdNoTrans, n, n, 1.0, A, n, x, 1, 0.0, x_k, 1); -} diff --git a/2025.03.28/03Ex/solve.c b/2025.03.28/03Ex/solve.c index 3074fdf..2a77719 100644 --- a/2025.03.28/03Ex/solve.c +++ b/2025.03.28/03Ex/solve.c @@ -5,24 +5,29 @@ 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) { + for (int k = 0; k < m; ++k) { - double * swap_temp = 0; + double *swap_temp = 0; double t = 0; double dot_r_r = 0, dot_Ar_r = 0; - + + #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]; - r = sum - b[i]; + r[i] = sum - b[i]; } + #pragma omp parallel for reduction(+:dot_r_r, dot_Ar_r) for (int i = 0; i < n; ++i) { double ri = r[i]; double sum = 0; + #pragma omp simd reduction(+:sum) for (int j = 0; j < n; ++j) sum += A[i*n + j] * r[j]; @@ -32,6 +37,7 @@ void t3_solve(const double * restrict A, double * restrict x_0, const double * r t = dot_r_r / dot_Ar_r; + #pragma omp simd for (int i = 0; i < n; ++i) x[i] = x_0[i] - r[i]*t; @@ -41,6 +47,7 @@ void t3_solve(const double * restrict A, double * restrict x_0, const double * r } if (m % 2 == 0) // Проверил 100 раз + #pragma omp simd for (int i = 0; i < n; i++) { double temp = x[i]; diff --git a/2025.03.28/03Ex/solve.h b/2025.03.28/03Ex/solve.h index 6149783..41cdb4f 100644 --- a/2025.03.28/03Ex/solve.h +++ b/2025.03.28/03Ex/solve.h @@ -1,8 +1,8 @@ #ifndef SOLVE_H #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); +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/03Ex/t.txt b/2025.03.28/03Ex/t.txt new file mode 100644 index 0000000..229972f --- /dev/null +++ b/2025.03.28/03Ex/t.txt @@ -0,0 +1,5 @@ +0 +0 +0 +0 +0