Task 6 was fixed and Task 8 was done
This commit is contained in:
parent
863948011f
commit
0a8c75a0cf
45 changed files with 1294 additions and 241 deletions
|
@ -8,7 +8,7 @@ CFLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \
|
|||
-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \
|
||||
-Wmissing-field-initializers -Wpointer-sign -fopenmp -O3
|
||||
|
||||
TARGET = a07.out
|
||||
TARGET = a08.out
|
||||
OBJ = main.o solve.o array_io.o init_f.o matrix.o
|
||||
|
||||
%.o: %.c
|
||||
|
|
|
@ -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"
|
||||
|
@ -9,8 +10,8 @@
|
|||
/* ./a.out t m n p k_a [filename_a] k_x [filename_x] */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
double tau, t, r1, r2, *a, *x_0, *b, *x, *r;
|
||||
int n, m, p, k_a, k_x, task = 7;
|
||||
double tau, t, r1, r2, *a, *x_0, *b, *x, *r, *w;
|
||||
int n, m, p, k_a, k_x, task = 8;
|
||||
char *name_a = 0, *name_x = 0;
|
||||
if (!((argc == 7 || argc == 8 || argc == 9) &&
|
||||
sscanf(argv[1], "%lf", &tau) == 1 &&
|
||||
|
@ -76,6 +77,17 @@ int main(int argc, char *argv[])
|
|||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
w = (double *)malloc((size_t)n * sizeof(double));
|
||||
if (!w)
|
||||
{
|
||||
free(a);
|
||||
free(x_0);
|
||||
free(b);
|
||||
free(x);
|
||||
free(r);
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (name_a)
|
||||
{ /* из файла */
|
||||
|
@ -98,6 +110,7 @@ int main(int argc, char *argv[])
|
|||
free(b);
|
||||
free(x);
|
||||
free(r);
|
||||
free(w);
|
||||
return 3;
|
||||
} while (0);
|
||||
} else init_matrix(a, n, n, k_a);
|
||||
|
@ -123,6 +136,7 @@ int main(int argc, char *argv[])
|
|||
free(b);
|
||||
free(x);
|
||||
free(r);
|
||||
free(w);
|
||||
return 3;
|
||||
} while (0);
|
||||
} else init_matrix(x_0, n, 1, k_x);
|
||||
|
@ -136,14 +150,14 @@ int main(int argc, char *argv[])
|
|||
printf("Vector b:\n");
|
||||
print_matrix(b, 1, n, p);
|
||||
|
||||
t = clock();
|
||||
t7_solve(a, x_0, b, x, r, n, m, tau);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
t = omp_get_wtime();
|
||||
t8_solve(a, x_0, b, x, r, w, n, m, tau);
|
||||
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);
|
||||
|
||||
|
@ -152,6 +166,7 @@ int main(int argc, char *argv[])
|
|||
free(b);
|
||||
free(x);
|
||||
free(r);
|
||||
free(w);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ double get_r2_value(const double * restrict x_k, int n)
|
|||
#pragma omp parallel for reduction(+:total_diff, template_sum)
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
short int modi = i ^ 1;
|
||||
short int modi = !(i & 1);
|
||||
total_diff += fabs(x_k[i] - modi);
|
||||
template_sum += modi;
|
||||
}
|
||||
|
|
|
@ -3,24 +3,40 @@
|
|||
#include <math.h>
|
||||
|
||||
|
||||
void t7_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m, double t)
|
||||
void t8_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, double * restrict w, int n, int m, double t)
|
||||
{
|
||||
(void)r;
|
||||
(void)w;
|
||||
|
||||
for (int k = 0; k < m; ++k)
|
||||
{
|
||||
double * swap_temp;
|
||||
|
||||
#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_0[j];
|
||||
double sum_x = 0;
|
||||
double sum_r = 0;
|
||||
double temp = 0;
|
||||
double aii = A[i*n + i];
|
||||
|
||||
x[i] = x_0[i] + ((b[i] - sum) / A[i*n + i]) * t;
|
||||
#pragma omp simd reduction(+:sum_x, sum_r)
|
||||
for (int j = 0; j < i; ++j)
|
||||
{
|
||||
double aij = A[i*n + j];
|
||||
double rj = aij * x_0[j];
|
||||
sum_x += rj - aij * x[j];
|
||||
sum_r += rj;
|
||||
}
|
||||
|
||||
temp = aii * x_0[i];
|
||||
sum_x += temp;
|
||||
sum_r += temp;
|
||||
|
||||
#pragma omp simd reduction(+:sum_r)
|
||||
for (int j = i+1; j < n; ++j)
|
||||
sum_r += A[i*n + j] * x_0[j];
|
||||
|
||||
x[i] = (sum_x + (b[i] - sum_r) * t) / aii;
|
||||
}
|
||||
|
||||
swap_temp = x;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
void t7_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m, double t);
|
||||
void t8_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, double * restrict w, int n, int m, double t);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue