Add replace between rows and strings, but this needs some work
This commit is contained in:
parent
f4c4a78fa0
commit
a7803c4e26
15 changed files with 153 additions and 50 deletions
|
@ -8,7 +8,16 @@ 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 = a08.out
|
OS := $(shell uname -s)
|
||||||
|
|
||||||
|
ifeq ($(filter $(OS), Linux Darwin), $(OS))
|
||||||
|
EXT = .out
|
||||||
|
|
||||||
|
else
|
||||||
|
EXT = .exe
|
||||||
|
endif
|
||||||
|
|
||||||
|
TARGET = a08$(EXT)
|
||||||
OBJ = main.o solve.o array_io.o init_f.o matrix.o
|
OBJ = main.o solve.o array_io.o init_f.o matrix.o
|
||||||
|
|
||||||
%.o: %.c
|
%.o: %.c
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
#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
|
|
|
@ -1,14 +0,0 @@
|
||||||
#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;
|
|
||||||
}
|
|
|
@ -1,6 +0,0 @@
|
||||||
#ifndef SOLVE_H
|
|
||||||
#define SOLVE_H
|
|
||||||
|
|
||||||
int t1_solve(double *a, int n);
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -8,7 +8,16 @@ 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
|
OS := $(shell uname -s)
|
||||||
|
|
||||||
|
ifeq ($(filter $(OS), Linux Darwin), $(OS))
|
||||||
|
EXT = .out
|
||||||
|
|
||||||
|
else
|
||||||
|
EXT = .exe
|
||||||
|
endif
|
||||||
|
|
||||||
|
TARGET = a14$(EXT)
|
||||||
OBJ = main.o solve.o array_io.o init_f.o matrix.o
|
OBJ = main.o solve.o array_io.o init_f.o matrix.o
|
||||||
|
|
||||||
%.o: %.c
|
%.o: %.c
|
|
@ -1,34 +1,34 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <omp.h>
|
||||||
#include "array_io.h"
|
#include "array_io.h"
|
||||||
|
|
||||||
io_status read_matrix(double *a, int n, int m, const char *name)
|
io_status read_matrix(double *a, int n, const char *name)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
for (j = 0; j < m; j++)
|
for (j = 0; j < n; j++)
|
||||||
if (fscanf(fp, "%lf", a + i * m + j) != 1)
|
if (fscanf(fp, "%lf", a + i * m + j) != 1)
|
||||||
{fclose(fp); return ERROR_READ;}
|
{fclose(fp); return ERROR_READ;}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_matrix(const double *a, int n, int m, int p)
|
void print_matrix(const double *a, int n, int p)
|
||||||
{
|
{
|
||||||
int np = (n > p ? p : n);
|
int np = (n > p ? p : n);
|
||||||
int mp = (m > p ? p : m);
|
|
||||||
int i, j;
|
int i, j;
|
||||||
|
|
||||||
for (i = 0; i < np; i++)
|
for (i = 0; i < np; i++)
|
||||||
{
|
{
|
||||||
for (j = 0; j < mp; j++)
|
for (j = 0; j < np; j++)
|
||||||
printf(" %10.3e", a[i * m + j]);
|
printf(" %10.3e", a[i * m + j]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_matrix(double *a, int n, int m, int k)
|
void init_matrix(double *a, int n, int k)
|
||||||
{
|
{
|
||||||
double (*q)(int, int, int, int);
|
double (*q)(int, int, int, int);
|
||||||
double (*f[])(int, int, int, int) = {f1, f2, f3, f4};
|
double (*f[])(int, int, int, int) = {f1, f2, f3, f4};
|
||||||
|
@ -38,3 +38,12 @@ void init_matrix(double *a, int n, int m, int k)
|
||||||
for (j = 0; j < m; j++)
|
for (j = 0; j < m; j++)
|
||||||
a[i * m + j] = q(n, m, i+1, j+1);
|
a[i * m + j] = q(n, m, i+1, j+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void init_identity_matrix(double *a, int n)
|
||||||
|
{
|
||||||
|
a = memset(a, 0, n * n * sizeof(double));
|
||||||
|
|
||||||
|
#pragma omp simd
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
a[i*n + i] = 1.0;
|
||||||
|
}
|
12
2025.04.04/14Ex/array_io.h
Normal file
12
2025.04.04/14Ex/array_io.h
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef ARRAY_IO_H
|
||||||
|
#define ARRAY_IO_H
|
||||||
|
|
||||||
|
#include "io_status.h"
|
||||||
|
#include "init_f.h"
|
||||||
|
|
||||||
|
io_status read_matrix(double *a, int n, const char *name);
|
||||||
|
void print_matrix(const double *a, int n, int p);
|
||||||
|
void init_matrix(double *a, int n, int k);
|
||||||
|
void init_identity_matrix(double *a, int n;)
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,7 +1,7 @@
|
||||||
#ifndef IO_STATUS_H
|
#ifndef IO_STATUS_H
|
||||||
#define IO_STATUS_H
|
#define IO_STATUS_H
|
||||||
|
|
||||||
#define LEN 1234
|
#define SINGULAR 2
|
||||||
|
|
||||||
typedef enum _io_status
|
typedef enum _io_status
|
||||||
{
|
{
|
|
@ -1,6 +1,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <omp.h>
|
||||||
#include "array_io.h"
|
#include "array_io.h"
|
||||||
#include "io_status.h"
|
#include "io_status.h"
|
||||||
#include "solve.h"
|
#include "solve.h"
|
||||||
|
@ -8,8 +9,8 @@
|
||||||
/* ./a.out n p k [filename] */
|
/* ./a.out n p k [filename] */
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
double t, *a;
|
double t, *a, *x;
|
||||||
int n, p, k, res, task = 1;
|
int n, p, k, res, *c, task = 14;
|
||||||
char *name = 0;
|
char *name = 0;
|
||||||
|
|
||||||
if (!((argc == 4 || argc == 5) &&
|
if (!((argc == 4 || argc == 5) &&
|
||||||
|
@ -29,11 +30,27 @@ int main(int argc, char *argv[])
|
||||||
printf("Not enough memory\n");
|
printf("Not enough memory\n");
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
x = (double *)malloc(n * n * sizeof(double));
|
||||||
|
if (!x)
|
||||||
|
{
|
||||||
|
free(a);
|
||||||
|
printf("Not enough memory\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
c = (int *)malloc(n * sizeof(int));
|
||||||
|
if (!c)
|
||||||
|
{
|
||||||
|
free(a);
|
||||||
|
free(b);
|
||||||
|
printf("Not enough memory\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
if (name)
|
if (name)
|
||||||
{ /* из файла */
|
{ /* из файла */
|
||||||
io_status ret;
|
io_status ret;
|
||||||
ret = read_sq_matrix(a, n, name);
|
ret = read_matrix(a, n, name);
|
||||||
do {
|
do {
|
||||||
switch (ret)
|
switch (ret)
|
||||||
{
|
{
|
||||||
|
@ -46,19 +63,32 @@ int main(int argc, char *argv[])
|
||||||
printf("Cannot read %s\n", name);
|
printf("Cannot read %s\n", name);
|
||||||
}
|
}
|
||||||
free(a);
|
free(a);
|
||||||
|
free(x);
|
||||||
|
free(c);
|
||||||
return 3;
|
return 3;
|
||||||
} while (0);
|
} while (0);
|
||||||
} else init_sq_matrix(a, n, k);
|
} else init_matrix(a, n, k);
|
||||||
|
|
||||||
|
init_identity_matrix(x, n);
|
||||||
|
|
||||||
|
#pragma omp simd
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
c[i] = i;
|
||||||
|
|
||||||
printf("Initial matrix:\n");
|
printf("Initial matrix:\n");
|
||||||
print_sq_matrix(a, n, p);
|
print_matrix(a, n, p);
|
||||||
|
|
||||||
t = clock();
|
t = omp_get_wtime();
|
||||||
res = t1_solve(a, n);
|
res = t14_solve(n, a, x, c);
|
||||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
t = omp_get_wtime() - t;
|
||||||
|
|
||||||
|
printf("Inverse matrix:\n");
|
||||||
|
print_matrix(x, n, p);
|
||||||
|
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f K = %d N = %d\n", argv[0], task, r1, r2, t, k, n);
|
||||||
|
|
||||||
printf("Result = %d\n", res);
|
|
||||||
printf("%s : Task = %d Elapsed = %.2f\n", argv[0], task, t);
|
|
||||||
free(a);
|
free(a);
|
||||||
|
free(x);
|
||||||
|
free(c);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
55
2025.04.04/14Ex/solve.c
Normal file
55
2025.04.04/14Ex/solve.c
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
#include "solve.h"
|
||||||
|
#include "io_status.h"
|
||||||
|
#include <float.h>
|
||||||
|
|
||||||
|
// c - changes in rows
|
||||||
|
int t14_solve(int n, double * restrict A, double * restrict X, int * restrict c)
|
||||||
|
{
|
||||||
|
for (int k = 0; k < n; ++k) {
|
||||||
|
int kn = 0, maxn = 0
|
||||||
|
max_t max = { .val = -1.0 };
|
||||||
|
for (int i = k; i < n; ++i)
|
||||||
|
for (int j = k; j < n; ++j)
|
||||||
|
{
|
||||||
|
double aij = fabs(A[i * n + j]);
|
||||||
|
if (aij > max.val) {
|
||||||
|
max.val = aij;
|
||||||
|
max.i = i;
|
||||||
|
max.j = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fabs(max.val) < DBL_EPSILON)
|
||||||
|
return SINGULAR;
|
||||||
|
|
||||||
|
#pragma omp parallel for
|
||||||
|
for (int j = 0; j < n; ++j)
|
||||||
|
{
|
||||||
|
double swap = X[k*n + j];
|
||||||
|
X[k*n + j] = X[max.i*n + j];
|
||||||
|
X[max.i*n + j] = swap;
|
||||||
|
|
||||||
|
if (j >= k)
|
||||||
|
{
|
||||||
|
swap = A[k*n + j];
|
||||||
|
A[k*n + j] = A[max.i*n + j];
|
||||||
|
A[max.i*n + j] = swap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma omp parallel for
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
double swap = X[i*n + k];
|
||||||
|
X[i*n + k] = X[i*n + max.j];
|
||||||
|
X[i*n + max.j] = swap;
|
||||||
|
|
||||||
|
if (i >= k)
|
||||||
|
{
|
||||||
|
swap = A[i*n + k];
|
||||||
|
A[i*n + k] = A[i*n + max.j];
|
||||||
|
A[i*n + max.j] = swap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
2025.04.04/14Ex/solve.h
Normal file
10
2025.04.04/14Ex/solve.h
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#ifndef SOLVE_H
|
||||||
|
#define SOLVE_H
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
double val;
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
} max_t;
|
||||||
|
|
||||||
|
#endif
|
Loading…
Add table
Add a link
Reference in a new issue