Task 6 was fixed and Task 8 was done

This commit is contained in:
AZEN-SGG 2025-03-27 20:44:47 +03:00
parent 863948011f
commit 0a8c75a0cf
45 changed files with 1294 additions and 241 deletions

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include "array_io.h"
#include "io_status.h"
#include "matrix.h"
@ -135,14 +136,14 @@ int main(int argc, char *argv[])
printf("Vector b:\n");
print_matrix(b, 1, n, p);
t = clock();
t = omp_get_wtime();
t4_solve(a, x_0, b, x, r, n, m);
t = (clock() - t) / CLOCKS_PER_SEC;
t = omp_get_wtime() - t;
r1 = get_r1(a, x, b, n);
r2 = get_r2_value(x, n);
printf("Vector x_m:\n");
printf("New vector:\n");
print_matrix(x, 1, n, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);

View file

@ -29,3 +29,45 @@ void matvec_mul(int n, const double * restrict A, const double * restrict x, dou
}
}
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;
}

View file

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

View file

@ -62,45 +62,3 @@ void t4_solve(const double * restrict A, double * restrict x_0, const double * r
}
}
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;
}

View file

@ -2,7 +2,5 @@
#define SOLVE_H
void t4_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m);
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