First task is done

This commit is contained in:
AZEN-SGG 2025-03-23 18:47:53 +03:00
parent 02b8132a94
commit 2cf18a1ff3
48 changed files with 857 additions and 1217 deletions

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.exe
OBJ = main.o solve.o array_io.o init_f.o
$(TARGET): $(OBJ)
gcc $^ -o $@ -lm -fopenmp -lssp
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:
del *.o *.exe

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

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);
}

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

View file

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "solve.h"
// Прототип вашей функции перемножения
void multiply_matrix_vector(int n, double** matrix, double* vector, double* result);
int main(int argc, char* argv[]) {
int n, i, j;
double* matrix;
double* vector;
double* result;
clock_t start, end;
double elapsed;
if (argc != 2) {
fprintf(stderr, "Usage: %s <n>\n", argv[0]);
return 1;
}
n = atoi(argv[1]);
if (n <= 0) {
fprintf(stderr, "n must be a positive integer\n");
return 1;
}
matrix = (double*)malloc(n * n * sizeof(double));
vector = (double*)malloc(n * sizeof(double));
result = (double*)malloc(n * sizeof(double));
srand((unsigned int)time(NULL));
for (i = 0; i < n; ++i) {
vector[i] = rand() % 10;
for (j = 0; j < n; ++j)
matrix[i*n + j] = rand() % 10;
}
start = clock();
matvec_mul(n, matrix, vector, result);
end = clock();
elapsed = (double)(end - start) / CLOCKS_PER_SEC;
printf("Result vector:\n");
for (i = 0; i < (n > 5 ? 5 : n); ++i)
printf("%.2f ", result[i]);
printf("\n");
printf("Elapsed time: %.6f seconds\n", elapsed);
free(matrix);
free(vector);
free(result);
return 0;
}

View file

@ -0,0 +1,40 @@
#include "solve.h"
#include <math.h>
#define eps 1e-17
int compare(const double a, const double b)
{
if (a - b > eps) return 1;
else if (b - a > eps) return -1;
return 0;
}
double t1_solve(double *a, int m, int n)
{
int i, j;
double cur = 0, maximum = 0;
for (i = 0; i < n; i++)
{
cur = 0;
for (j = 0; j < m; j++)
cur += fabs(a[i * m + j]);
if (compare(cur, maximum) == 1) maximum = cur;
}
return maximum;
}
void matvec_mul(int n, double * restrict A, 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;
}
}

View file

@ -0,0 +1,8 @@
#ifndef SOLVE_H
#define SOLVE_H
int compare(const double a, const double b);
double t1_solve(double *a, int m, int n);
void matvec_mul(int n, double * restrict A, double * restrict x, double * restrict x_k);
#endif