Start Task 3

This commit is contained in:
AZEN-SGG 2025-03-26 15:17:02 +03:00
parent 4134eda893
commit 3eb349f81c
32 changed files with 475 additions and 16 deletions

5
2025.03.28/02Ex/a.txt Normal file
View file

@ -0,0 +1,5 @@
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

View file

@ -8,12 +8,9 @@ void init_vec_b(const double * restrict a, double * restrict b, int n)
{
double sum = 0;
// #pragma omp simd reduction(+:sum)
// for (int k = 1; k < n; k+=2)
// sum += a[i * n + k];
for (int k = 0; k < (n-1)/2; ++k)
sum += a[i*n + 2*k+1];
#pragma omp simd reduction(+:sum)
for (int k = 1; k < n; k+=2)
sum += a[i * n + k];
b[i] = sum;
}

View file

@ -5,7 +5,6 @@
void t2_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, int n, int m, double t)
{
#pragma omp parallel for
for (int k = 0; k < m; ++k)
{
double * swap_temp;
@ -20,7 +19,7 @@ void t2_solve(const double * restrict A, double * restrict x_0, const double * r
x_0 = swap_temp;
}
if (m % 2 == 0)
if (m % 2 == 0) // Проверил 100 раз
for (int i = 0; i < n; i++)
{
double temp = x[i];
@ -62,18 +61,18 @@ double t2_get_r1(const double * restrict A, const double * restrict x_k, const d
double t2_get_r2_value(const double * restrict x_k, int n)
{
double r2 = 0;
double abs_xi = 0;
double abs_i = 0;
double relative_error = 0;
double total_diff = 0;
double template_sum = 0;
#pragma omp parallel for reduction(+:abs_xi, abs_i)
#pragma omp parallel for reduction(+:total_diff, template_sum)
for (int i = 0; i < n; ++i)
{
short int modi = i ^ 1;
abs_xi += fabs(x_k[i] - modi);
abs_i += modi;
total_diff += fabs(x_k[i] - modi);
template_sum += modi;
}
r2 = abs_xi / abs_i;
return r2;
relative_error = total_diff / template_sum;
return relative_error;
}

5
2025.03.28/02Ex/t.txt Normal file
View file

@ -0,0 +1,5 @@
0
0
0
0
0

30
2025.03.28/03Ex/Makefile Normal file
View file

@ -0,0 +1,30 @@
CFLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \
-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \
-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \
-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \
-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \
-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \
-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \
-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): $(OBJ)
gcc $^ -o $@ -lm -fopenmp
main.o: main.c
gcc $(CFLAGS) -c $^
solve.o: solve.c
gcc $(CFLAGS) -c $^
array_io.o: array_io.c
gcc $(CFLAGS) -c $^
init_f.o: init_f.c
gcc $(CFLAGS) -c $^
clean:
rm *.o *.out

2
2025.03.28/03Ex/a.txt Normal file
View file

@ -0,0 +1,2 @@
1.0001 0
0 1

View file

@ -0,0 +1,40 @@
#include <stdio.h>
#include "array_io.h"
io_status read_matrix(double *a, int m, int n, const char *name)
{
int i, j;
FILE *fp;
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
if (fscanf(fp, "%lf", a + i * n + j) != 1)
{fclose(fp); return ERROR_READ;}
fclose(fp);
return SUCCESS;
}
void print_matrix(const double *a, int m, int n, int p)
{
int mp = (m > p ? p : m);
int np = (n > p ? p : n);
int i, j;
for (i = 0; i < mp; i++)
{
for (j = 0; j < np; j++)
printf(" %10.3e", a[i * n + j]);
printf("\n");
}
}
void init_matrix(double *a, int m, int n, int k)
{
double (*q)(int, int, int, int);
double (*f[])(int, int, int, int) = {f1, f2, f3, f4};
int i, j;
q = f[k-1];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
a[i * n + j] = q(m, n, i+1, j+1);
}

View file

@ -0,0 +1,11 @@
#ifndef ARRAY_IO_H
#define ARRAY_IO_H
#include "io_status.h"
#include "init_f.h"
io_status read_matrix(double *a, int m, int n, const char *name);
void print_matrix(const double *a, int m, int n, int p);
void init_matrix(double *a, int m, int n, int k);
#endif

30
2025.03.28/03Ex/init_f.c Normal file
View file

@ -0,0 +1,30 @@
#include "init_f.h"
#include <stdlib.h>
#define MAX(n, m) (n < m ? m : n)
double f1(int m, int n, int i, int j)
{
return MAX(n, m) - MAX(i, j) + 1;
}
double f2(int m, int n, int i, int j)
{
(void)n;
(void)m;
return MAX(i, j);
}
double f3(int m, int n, int i, int j)
{
(void)n;
(void)m;
return abs(i - j);
}
double f4(int m, int n, int i, int j)
{
(void)n;
(void)m;
return 1./(i+j-1);
}

9
2025.03.28/03Ex/init_f.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef INIT_F_H
#define INIT_F_H
double f1(int m, int n, int i, int j);
double f2(int m, int n, int i, int j);
double f3(int m, int n, int i, int j);
double f4(int m, int n, int i, int j);
#endif

View file

@ -0,0 +1,13 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define LEN 1234
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

141
2025.03.28/03Ex/main.c Normal file
View file

@ -0,0 +1,141 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.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;
int n, m, p, k_a, k_x, task = 3;
char *name_a = 0, *name_x = 0;
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", &k_a) == 1 &&
(k_a >= 0 && k_a <= 4) &&
(!(k_a == 0 && argc == 6)) &&
((k_a == 0 && sscanf(argv[6], "%d", &k_x) == 1) ||
(k_a != 0 && sscanf(argv[5], "%d", &k_x) == 1)) &&
(k_x >= 0 && k_x <= 4) &&
(!(k_x == 0 && argc == 6)) &&
(!((k_a == 0 && k_x == 0) && argc != 8))))
{
printf("Usage: %s m n p k_a [filename_a] k_x [filename_x]\n", argv[0]);
return 1;
}
if (argc != 6)
{
int i_x = 6;
if (k_a == 0) {name_a = argv[5];i_x++;}
if (k_x == 0) name_x = argv[i_x];
}
a = (double *)malloc((size_t)n * (size_t)n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
x_0 = (double *)malloc((size_t)n * sizeof(double));
if (!x_0)
{
free(a);
printf("Not enough memory\n");
return 2;
}
b = (double *)malloc((size_t)n * sizeof(double));
if (!b)
{
free(a);
free(x_0);
printf("Not enough memory\n");
return 2;
}
x = (double *)malloc((size_t)n * sizeof(double));
if (!x)
{
free(a);
free(x_0);
free(b);
printf("Not enough memory\n");
return 2;
}
if (name_a)
{ /* из файла */
io_status ret;
ret = read_matrix(a, n, n, name_a);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name_a);
break;
case ERROR_READ:
printf("Cannot read %s\n", name_a);
break;
}
free(a);
free(x_0);
free(b);
free(x);
return 3;
} while (0);
} else init_matrix(a, n, n, k_a);
if (name_x)
{
io_status ret;
ret = read_matrix(x_0, n, 1, name_x);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name_x);
break;
case ERROR_READ:
printf("Cannot read %s\n", name_x);
break;
}
free(a);
free(x_0);
free(b);
free(x);
return 3;
} while (0);
} else init_matrix(x_0, n, 1, k_x);
init_vec_b(a, b, n);
printf("Matrix A:\n");
print_matrix(a, n, n, p);
printf("Vector x_0:\n");
print_matrix(x_0, 1, n, p);
printf("Vector b:\n");
print_matrix(b, 1, n, p);
t = clock();
t3_solve(a, x_0, x, n, m);
t = (clock() - t) / CLOCKS_PER_SEC;
r1 = get_r1(a, x, b, n);
r2 = get_r2_value(x, n);
printf("Vector x_m:\n");
print_matrix(x, 1, n, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);
free(a);
free(x_0);
free(x);
return 0;
}

38
2025.03.28/03Ex/matrix.c Normal file
View file

@ -0,0 +1,38 @@
#include "matrix.h"
#include <math.h>
void init_vec_b(const double * restrict a, double * restrict b, int n)
{
#pragma omp parallel for
for (int i = 0; i < n; ++i)
{
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int k = 1; k < n; k+=2)
sum += a[i * n + k];
b[i] = sum;
}
}
/*
* OLD VERSION
void matvec_mul(int n, const double * restrict A, const double * restrict x, double * restrict x_k)
{
#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[j];
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);
}

7
2025.03.28/03Ex/matrix.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef MATRIX_H
#define MATRIX_H
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);
#endif

99
2025.03.28/03Ex/solve.c Normal file
View file

@ -0,0 +1,99 @@
#include "solve.h"
#include "matrix.h"
#include <math.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)
{
for (int k = 0; k < m; ++k)
{
double * swap_temp = 0;
double t = 0;
double dot_r_r = 0, dot_Ar_r = 0;
for (int i = 0; i < n; ++i)
{
double sum = 0;
for (int j = 0; j < n; ++j)
sum += A[i*n + j] * x_0[j];
r = sum - b[i];
}
for (int i = 0; i < n; ++i)
{
double ri = r[i];
double sum = 0;
for (int j = 0; j < n; ++j)
sum += A[i*n + j] * r[j];
dot_Ar_r += sum * ri;
dot_r_r += ri * ri;
}
t = dot_r_r / dot_Ar_r;
for (int i = 0; i < n; ++i)
x[i] = x_0[i] - r[i]*t;
swap_temp = x;
x = x_0;
x_0 = swap_temp;
}
if (m % 2 == 0) // Проверил 100 раз
for (int i = 0; i < n; i++)
{
double temp = x[i];
x[i] = x_0[i];
x_0[i] = temp;
}
else
{
double * swap_temp = x;
x = x_0;
x_0 = swap_temp;
}
}
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;
}

8
2025.03.28/03Ex/solve.h Normal file
View file

@ -0,0 +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);
#endif

2
2025.03.28/03Ex/v.txt Normal file
View file

@ -0,0 +1,2 @@
1
1

View file

@ -1,6 +1,7 @@
#include "solve.h"
#include <math.h>
#define eps 1e-17
int compare(const double a, const double b)
@ -38,3 +39,5 @@ void matvec_mul(int n, double * restrict A, double * restrict x, double * restri
x_k[i] = sum;
}
}

5
2025.03.28/dist/Sustavov/a.txt vendored Normal file
View file

@ -0,0 +1,5 @@
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

0
2025.03.28/dist/Sustavov/a01.out vendored Normal file → Executable file
View file

0
2025.03.28/dist/Sustavov/a02.out vendored Normal file → Executable file
View file

0
2025.03.28/dist/Sustavov/a03.out vendored Normal file → Executable file
View file

0
2025.03.28/dist/Sustavov/a04.out vendored Normal file → Executable file
View file

0
2025.03.28/dist/Sustavov/a05.out vendored Normal file → Executable file
View file

0
2025.03.28/dist/Sustavov/a06.out vendored Normal file → Executable file
View file

0
2025.03.28/dist/Sustavov/a07.out vendored Normal file → Executable file
View file

0
2025.03.28/dist/Sustavov/a08.out vendored Normal file → Executable file
View file

0
2025.03.28/dist/Sustavov/a09.out vendored Normal file → Executable file
View file

0
2025.03.28/dist/Sustavov/a10.out vendored Normal file → Executable file
View file

5
2025.03.28/dist/Sustavov/t.txt vendored Normal file
View file

@ -0,0 +1,5 @@
0
0
0
0
0

5
2025.03.28/dist/Ulyanov/a.txt vendored Normal file
View file

@ -0,0 +1,5 @@
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

5
2025.03.28/dist/Ulyanov/t.txt vendored Normal file
View file

@ -0,0 +1,5 @@
0
0
0
0
0