Create 1 Task, but not worked

This commit is contained in:
AZEN-SGG 2025-03-30 16:28:50 +03:00
parent babeb62ae9
commit f4c4a78fa0
12 changed files with 290 additions and 0 deletions

21
2025.04.04/01Ex/Makefile Normal file
View file

@ -0,0 +1,21 @@
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 matrix.o
%.o: %.c
gcc $(CFLAGS) -c $< -o $@
$(TARGET): $(OBJ)
gcc $^ -o $@ -lm -fopenmp
clean:
rm *.o *.out

View file

@ -0,0 +1,40 @@
#include <stdio.h>
#include "array_io.h"
io_status read_matrix(double *a, int n, int m, 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++)
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)
{
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++)
printf(" %10.3e", a[i * m + j]);
printf("\n");
}
}
void init_matrix(double *a, int n, int m, 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 < n; i++)
for (j = 0; j < m; j++)
a[i * m + j] = q(n, m, 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 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

30
2025.04.04/01Ex/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 n, int m, int i, int j)
{
return MAX(n, m) - MAX(i, j) + 1;
}
double f2(int n, int m, int i, int j)
{
(void)n;
(void)m;
return MAX(i, j);
}
double f3(int n, int m, int i, int j)
{
(void)n;
(void)m;
return abs(i - j);
}
double f4(int n, int m, int i, int j)
{
(void)n;
(void)m;
return 1./(i+j-1);
}

9
2025.04.04/01Ex/init_f.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef INIT_F_H
#define INIT_F_H
double f1(int n, int m, int i, int j);
double f2(int n, int m, int i, int j);
double f3(int n, int m, int i, int j);
double f4(int n, int m, 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

64
2025.04.04/01Ex/main.c Normal file
View file

@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out n p k [filename] */
int main(int argc, char *argv[])
{
double t, *a;
int n, p, k, res, task = 1;
char *name = 0;
if (!((argc == 4 || argc == 5) &&
sscanf(argv[1], "%d", &n) == 1 &&
sscanf(argv[2], "%d", &p) == 1 &&
sscanf(argv[3], "%d", &k) == 1 &&
k >= 0 && k <= 4 && (!(k == 0 && argc != 5))))
{
printf("Usage: %s n p k [filename]\n", argv[0]);
return 1;
}
if (argc == 5) name = argv[4];
a = (double *)malloc(n * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name)
{ /* из файла */
io_status ret;
ret = read_sq_matrix(a, n, name);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name);
break;
case ERROR_READ:
printf("Cannot read %s\n", name);
}
free(a);
return 3;
} while (0);
} else init_sq_matrix(a, n, k);
printf("Initial matrix:\n");
print_sq_matrix(a, n, p);
t = clock();
res = t1_solve(a, n);
t = (clock() - t) / CLOCKS_PER_SEC;
printf("Result = %d\n", res);
printf("%s : Task = %d Elapsed = %.2f\n", argv[0], task, t);
free(a);
return 0;
}

73
2025.04.04/01Ex/matrix.c Normal file
View file

@ -0,0 +1,73 @@
#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 = 0; k < n; k+=2)
sum += a[i * n + k];
b[i] = sum;
}
}
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;
}
}
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;
}

9
2025.04.04/01Ex/matrix.h Normal file
View file

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

14
2025.04.04/01Ex/solve.c Normal file
View file

@ -0,0 +1,14 @@
#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;
}

6
2025.04.04/01Ex/solve.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef SOLVE_H
#define SOLVE_H
int t1_solve(double *a, int n);
#endif

BIN
2025.04.04/Tasks08.pdf Normal file

Binary file not shown.