From a7803c4e2618488287d6b755552b5355848582e1 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Sun, 30 Mar 2025 19:44:32 +0300 Subject: [PATCH] Add replace between rows and strings, but this needs some work --- 2025.03.28/08Ex/Makefile | 11 +++++- 2025.04.04/01Ex/array_io.h | 11 ------ 2025.04.04/01Ex/solve.c | 14 ------- 2025.04.04/01Ex/solve.h | 6 --- 2025.04.04/{01Ex => 14Ex}/Makefile | 11 +++++- 2025.04.04/{01Ex => 14Ex}/array_io.c | 21 +++++++--- 2025.04.04/14Ex/array_io.h | 12 ++++++ 2025.04.04/{01Ex => 14Ex}/init_f.c | 0 2025.04.04/{01Ex => 14Ex}/init_f.h | 0 2025.04.04/{01Ex => 14Ex}/io_status.h | 2 +- 2025.04.04/{01Ex => 14Ex}/main.c | 50 +++++++++++++++++++----- 2025.04.04/{01Ex => 14Ex}/matrix.c | 0 2025.04.04/{01Ex => 14Ex}/matrix.h | 0 2025.04.04/14Ex/solve.c | 55 +++++++++++++++++++++++++++ 2025.04.04/14Ex/solve.h | 10 +++++ 15 files changed, 153 insertions(+), 50 deletions(-) delete mode 100644 2025.04.04/01Ex/array_io.h delete mode 100644 2025.04.04/01Ex/solve.c delete mode 100644 2025.04.04/01Ex/solve.h rename 2025.04.04/{01Ex => 14Ex}/Makefile (85%) rename 2025.04.04/{01Ex => 14Ex}/array_io.c (60%) create mode 100644 2025.04.04/14Ex/array_io.h rename 2025.04.04/{01Ex => 14Ex}/init_f.c (100%) rename 2025.04.04/{01Ex => 14Ex}/init_f.h (100%) rename 2025.04.04/{01Ex => 14Ex}/io_status.h (86%) rename 2025.04.04/{01Ex => 14Ex}/main.c (55%) rename 2025.04.04/{01Ex => 14Ex}/matrix.c (100%) rename 2025.04.04/{01Ex => 14Ex}/matrix.h (100%) create mode 100644 2025.04.04/14Ex/solve.c create mode 100644 2025.04.04/14Ex/solve.h diff --git a/2025.03.28/08Ex/Makefile b/2025.03.28/08Ex/Makefile index b98ac81..f0a30dd 100644 --- a/2025.03.28/08Ex/Makefile +++ b/2025.03.28/08Ex/Makefile @@ -8,7 +8,16 @@ 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 = a08.out +OS := $(shell uname -s) + +ifeq ($(filter $(OS), Linux Darwin), $(OS)) + EXT = .out + +else + EXT = .exe +endif + +TARGET = a08$(EXT) OBJ = main.o solve.o array_io.o init_f.o matrix.o %.o: %.c diff --git a/2025.04.04/01Ex/array_io.h b/2025.04.04/01Ex/array_io.h deleted file mode 100644 index e312a84..0000000 --- a/2025.04.04/01Ex/array_io.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef ARRAY_IO_H -#define ARRAY_IO_H - -#include "io_status.h" -#include "init_f.h" - -io_status read_matrix(double *a, int n, int m, const char *name); -void print_matrix(const double *a, int n, int m, int p); -void init_matrix(double *a, int n, int m, int k); - -#endif diff --git a/2025.04.04/01Ex/solve.c b/2025.04.04/01Ex/solve.c deleted file mode 100644 index bf5b598..0000000 --- a/2025.04.04/01Ex/solve.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "solve.h" -#include "math.h" - -#define eps 1e-6 - -int t1_solve(double *a, int n) -{ - int i, j; - for (i = 0; i < n; i++) - for (j = i; j < n; j++) - if (i != j) - if (fabs(a[i * n + j] - a[j * n + i]) > eps) return 0; - return 1; -} diff --git a/2025.04.04/01Ex/solve.h b/2025.04.04/01Ex/solve.h deleted file mode 100644 index c6b0387..0000000 --- a/2025.04.04/01Ex/solve.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef SOLVE_H -#define SOLVE_H - -int t1_solve(double *a, int n); - -#endif diff --git a/2025.04.04/01Ex/Makefile b/2025.04.04/14Ex/Makefile similarity index 85% rename from 2025.04.04/01Ex/Makefile rename to 2025.04.04/14Ex/Makefile index b7b00e3..5a4f64c 100644 --- a/2025.04.04/01Ex/Makefile +++ b/2025.04.04/14Ex/Makefile @@ -8,7 +8,16 @@ 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 +OS := $(shell uname -s) + +ifeq ($(filter $(OS), Linux Darwin), $(OS)) + EXT = .out + +else + EXT = .exe +endif + +TARGET = a14$(EXT) OBJ = main.o solve.o array_io.o init_f.o matrix.o %.o: %.c diff --git a/2025.04.04/01Ex/array_io.c b/2025.04.04/14Ex/array_io.c similarity index 60% rename from 2025.04.04/01Ex/array_io.c rename to 2025.04.04/14Ex/array_io.c index 6e56ecf..2011499 100644 --- a/2025.04.04/01Ex/array_io.c +++ b/2025.04.04/14Ex/array_io.c @@ -1,34 +1,34 @@ #include +#include #include "array_io.h" -io_status read_matrix(double *a, int n, int m, const char *name) +io_status read_matrix(double *a, int n, const char *name) { int i, j; FILE *fp; if (!(fp = fopen(name, "r"))) return ERROR_OPEN; for (i = 0; i < n; i++) - for (j = 0; j < m; j++) + for (j = 0; j < n; j++) if (fscanf(fp, "%lf", a + i * m + j) != 1) {fclose(fp); return ERROR_READ;} fclose(fp); return SUCCESS; } -void print_matrix(const double *a, int n, int m, int p) +void print_matrix(const double *a, int n, int p) { int np = (n > p ? p : n); - int mp = (m > p ? p : m); int i, j; for (i = 0; i < np; i++) { - for (j = 0; j < mp; j++) + for (j = 0; j < np; j++) printf(" %10.3e", a[i * m + j]); printf("\n"); } } -void init_matrix(double *a, int n, int m, int k) +void init_matrix(double *a, int n, int k) { double (*q)(int, int, int, int); double (*f[])(int, int, int, int) = {f1, f2, f3, f4}; @@ -38,3 +38,12 @@ void init_matrix(double *a, int n, int m, int k) for (j = 0; j < m; j++) a[i * m + j] = q(n, m, i+1, j+1); } + +void init_identity_matrix(double *a, int n) +{ + a = memset(a, 0, n * n * sizeof(double)); + + #pragma omp simd + for (int i = 0; i < n; ++i) + a[i*n + i] = 1.0; +} diff --git a/2025.04.04/14Ex/array_io.h b/2025.04.04/14Ex/array_io.h new file mode 100644 index 0000000..51a8ac8 --- /dev/null +++ b/2025.04.04/14Ex/array_io.h @@ -0,0 +1,12 @@ +#ifndef ARRAY_IO_H +#define ARRAY_IO_H + +#include "io_status.h" +#include "init_f.h" + +io_status read_matrix(double *a, int n, const char *name); +void print_matrix(const double *a, int n, int p); +void init_matrix(double *a, int n, int k); +void init_identity_matrix(double *a, int n;) + +#endif diff --git a/2025.04.04/01Ex/init_f.c b/2025.04.04/14Ex/init_f.c similarity index 100% rename from 2025.04.04/01Ex/init_f.c rename to 2025.04.04/14Ex/init_f.c diff --git a/2025.04.04/01Ex/init_f.h b/2025.04.04/14Ex/init_f.h similarity index 100% rename from 2025.04.04/01Ex/init_f.h rename to 2025.04.04/14Ex/init_f.h diff --git a/2025.04.04/01Ex/io_status.h b/2025.04.04/14Ex/io_status.h similarity index 86% rename from 2025.04.04/01Ex/io_status.h rename to 2025.04.04/14Ex/io_status.h index 420a981..4ec83a5 100644 --- a/2025.04.04/01Ex/io_status.h +++ b/2025.04.04/14Ex/io_status.h @@ -1,7 +1,7 @@ #ifndef IO_STATUS_H #define IO_STATUS_H -#define LEN 1234 +#define SINGULAR 2 typedef enum _io_status { diff --git a/2025.04.04/01Ex/main.c b/2025.04.04/14Ex/main.c similarity index 55% rename from 2025.04.04/01Ex/main.c rename to 2025.04.04/14Ex/main.c index e75409c..44ab36b 100644 --- a/2025.04.04/01Ex/main.c +++ b/2025.04.04/14Ex/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "array_io.h" #include "io_status.h" #include "solve.h" @@ -8,8 +9,8 @@ /* ./a.out n p k [filename] */ int main(int argc, char *argv[]) { - double t, *a; - int n, p, k, res, task = 1; + double t, *a, *x; + int n, p, k, res, *c, task = 14; char *name = 0; if (!((argc == 4 || argc == 5) && @@ -29,11 +30,27 @@ int main(int argc, char *argv[]) printf("Not enough memory\n"); return 2; } + + x = (double *)malloc(n * n * sizeof(double)); + if (!x) + { + free(a); + printf("Not enough memory\n"); + return 2; + } + c = (int *)malloc(n * sizeof(int)); + if (!c) + { + free(a); + free(b); + printf("Not enough memory\n"); + return 2; + } if (name) { /* из файла */ io_status ret; - ret = read_sq_matrix(a, n, name); + ret = read_matrix(a, n, name); do { switch (ret) { @@ -46,19 +63,32 @@ int main(int argc, char *argv[]) printf("Cannot read %s\n", name); } free(a); + free(x); + free(c); return 3; } while (0); - } else init_sq_matrix(a, n, k); + } else init_matrix(a, n, k); + + init_identity_matrix(x, n); + + #pragma omp simd + for (int i = 0; i < n; ++i) + c[i] = i; printf("Initial matrix:\n"); - print_sq_matrix(a, n, p); + print_matrix(a, n, p); - t = clock(); - res = t1_solve(a, n); - t = (clock() - t) / CLOCKS_PER_SEC; + t = omp_get_wtime(); + res = t14_solve(n, a, x, c); + t = omp_get_wtime() - t; + + printf("Inverse matrix:\n"); + print_matrix(x, n, p); + printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f K = %d N = %d\n", argv[0], task, r1, r2, t, k, n); - printf("Result = %d\n", res); - printf("%s : Task = %d Elapsed = %.2f\n", argv[0], task, t); free(a); + free(x); + free(c); + return 0; } diff --git a/2025.04.04/01Ex/matrix.c b/2025.04.04/14Ex/matrix.c similarity index 100% rename from 2025.04.04/01Ex/matrix.c rename to 2025.04.04/14Ex/matrix.c diff --git a/2025.04.04/01Ex/matrix.h b/2025.04.04/14Ex/matrix.h similarity index 100% rename from 2025.04.04/01Ex/matrix.h rename to 2025.04.04/14Ex/matrix.h diff --git a/2025.04.04/14Ex/solve.c b/2025.04.04/14Ex/solve.c new file mode 100644 index 0000000..b9f2f83 --- /dev/null +++ b/2025.04.04/14Ex/solve.c @@ -0,0 +1,55 @@ +#include "solve.h" +#include "io_status.h" +#include + +// c - changes in rows +int t14_solve(int n, double * restrict A, double * restrict X, int * restrict c) +{ + for (int k = 0; k < n; ++k) { + int kn = 0, maxn = 0 + max_t max = { .val = -1.0 }; + for (int i = k; i < n; ++i) + for (int j = k; j < n; ++j) + { + double aij = fabs(A[i * n + j]); + if (aij > max.val) { + max.val = aij; + max.i = i; + max.j = j; + } + } + + if (fabs(max.val) < DBL_EPSILON) + return SINGULAR; + + #pragma omp parallel for + for (int j = 0; j < n; ++j) + { + double swap = X[k*n + j]; + X[k*n + j] = X[max.i*n + j]; + X[max.i*n + j] = swap; + + if (j >= k) + { + swap = A[k*n + j]; + A[k*n + j] = A[max.i*n + j]; + A[max.i*n + j] = swap; + } + } + + #pragma omp parallel for + for (int i = 0; i < n; ++i) + { + double swap = X[i*n + k]; + X[i*n + k] = X[i*n + max.j]; + X[i*n + max.j] = swap; + + if (i >= k) + { + swap = A[i*n + k]; + A[i*n + k] = A[i*n + max.j]; + A[i*n + max.j] = swap; + } + } + } +} diff --git a/2025.04.04/14Ex/solve.h b/2025.04.04/14Ex/solve.h new file mode 100644 index 0000000..d098c47 --- /dev/null +++ b/2025.04.04/14Ex/solve.h @@ -0,0 +1,10 @@ +#ifndef SOLVE_H +#define SOLVE_H + +typedef struct { + double val; + int i; + int j; +} max_t; + +#endif