Task 3 is ok!

This commit is contained in:
AZEN-SGG 2025-03-26 16:19:57 +03:00
parent 3eb349f81c
commit 335d164b10
7 changed files with 41 additions and 19 deletions

View file

@ -8,8 +8,8 @@ CFLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \
-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ -Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \
-Wmissing-field-initializers -Wpointer-sign -fopenmp -O3 -Wmissing-field-initializers -Wpointer-sign -fopenmp -O3
TARGET = a01.out TARGET = a03.out
OBJ = main.o solve.o array_io.o init_f.o OBJ = main.o solve.o array_io.o init_f.o matrix.o
$(TARGET): $(OBJ) $(TARGET): $(OBJ)
gcc $^ -o $@ -lm -fopenmp gcc $^ -o $@ -lm -fopenmp
@ -26,5 +26,8 @@ array_io.o: array_io.c
init_f.o: init_f.c init_f.o: init_f.c
gcc $(CFLAGS) -c $^ gcc $(CFLAGS) -c $^
matrix.o: matrix.c
gcc $(CFLAGS) -c $^
clean: clean:
rm *.o *.out rm *.o *.out

View file

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

View file

@ -3,12 +3,13 @@
#include <time.h> #include <time.h>
#include "array_io.h" #include "array_io.h"
#include "io_status.h" #include "io_status.h"
#include "matrix.h"
#include "solve.h" #include "solve.h"
/* ./a.out m n p k_a [filename_a] k_x [filename_x] */ /* ./a.out m n p k_a [filename_a] k_x [filename_x] */
int main(int argc, char *argv[]) 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; int n, m, p, k_a, k_x, task = 3;
char *name_a = 0, *name_x = 0; char *name_a = 0, *name_x = 0;
if (!((argc == 6 || argc == 7 || argc == 8) && if (!((argc == 6 || argc == 7 || argc == 8) &&
@ -64,6 +65,16 @@ int main(int argc, char *argv[])
printf("Not enough memory\n"); printf("Not enough memory\n");
return 2; 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) if (name_a)
{ /* из файла */ { /* из файла */
@ -123,7 +134,7 @@ int main(int argc, char *argv[])
print_matrix(b, 1, n, p); print_matrix(b, 1, n, p);
t = clock(); 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; t = (clock() - t) / CLOCKS_PER_SEC;
r1 = get_r1(a, x, b, n); r1 = get_r1(a, x, b, n);

View file

@ -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) void matvec_mul(int n, const double * restrict A, const double * restrict x, double * restrict x_k)
{ {
#pragma omp parallel for #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; 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);
}

View file

@ -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) 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) for (int k = 0; k < m; ++k)
{ {
double *swap_temp = 0; double *swap_temp = 0;
double t = 0; double t = 0;
double dot_r_r = 0, dot_Ar_r = 0; double dot_r_r = 0, dot_Ar_r = 0;
#pragma omp parallel for
for (int i = 0; i < n; ++i) for (int i = 0; i < n; ++i)
{ {
double sum = 0; double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; ++j) for (int j = 0; j < n; ++j)
sum += A[i*n + j] * x_0[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) for (int i = 0; i < n; ++i)
{ {
double ri = r[i]; double ri = r[i];
double sum = 0; double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; ++j) for (int j = 0; j < n; ++j)
sum += A[i*n + j] * r[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; t = dot_r_r / dot_Ar_r;
#pragma omp simd
for (int i = 0; i < n; ++i) for (int i = 0; i < n; ++i)
x[i] = x_0[i] - r[i]*t; 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 раз if (m % 2 == 0) // Проверил 100 раз
#pragma omp simd
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
double temp = x[i]; double temp = x[i];

View file

@ -1,8 +1,8 @@
#ifndef SOLVE_H #ifndef SOLVE_H
#define 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); 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 t2_get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n); double 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); double get_r2_value(const double * restrict x_k, int n);
#endif #endif

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

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