1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <time.h>
double mul_vector_transpose_vector(double* v1, double* v2, int n) {
int i;
double result = 0;
#pragma omp parallel for reduction(+:result)
for (i = 0; i < n; i++) {
result += v1[i] * v2[i];
}
return result;
}
double* matrix_mul_vector(double** A, double* x, int n) {
int i, j;
double* result = (double*)malloc(n * sizeof(double));
#pragma omp parallel for
for (i = 0; i < n; i++) {
result[i] = mul_vector_transpose_vector(A[i], x, n);
}
return result;
}
double* negate_vector(double* g, int n) {
int i;
double* s = (double*)malloc(n * sizeof(double));
#pragma omp parallel for
for (i = 0; i < n; i++) {
s[i] = (-1) * g[i];
}
return s;
}
void copy_vector(double* v1, double* v2, int n) {
int i;
#pragma omp parallel for
for (i = 0; i < n; i++) {
v1[i] = v2[i];
}
}
double* scalar_mul_vector(double* s, double c, int n) {
int i;
double* result = (double*)malloc(n * sizeof(double));
#pragma omp parallel for
for (i = 0; i < n; i++) {
result[i] = c * s[i];
}
return result;
}
double* vector_add_vector(double* v1, double* v2, int n) {
int i;
double* result = (double*)malloc(n * sizeof(double));
#pragma omp parallel for
for (i = 0; i < n; i++) {
result[i] = v1[i] + v2[i];
}
return result;
}
double* vector_sub_vector(double* v1, double* v2, int n) {
int i;
double* result = (double*)malloc(n * sizeof(double));
#pragma omp parallel for
for (i = 0; i < n; i++) {
result[i] = v1[i] - v2[i];
}
return result;
}
int main() {
int n, i, j, t = 20;
printf("Enter the size of equation \n");
scanf_s("%d", &n);
double** A = (double**)malloc(n * sizeof(double*));
double* b = (double*)malloc(n * sizeof(double));
double* x = (double*)malloc(n * sizeof(double));
for (i = 0; i < n; i++) {
A[i] = (double*)malloc(n * sizeof(double));
}
printf("Enter the elements A \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf_s("%lf", &A[i][j]);
}
}
printf("Enter the elements of b \n");
for (i = 0; i < n; i++) {
scanf_s("%lf", &b[i]);
}
printf("Enter initial guess x \n");
for (i = 0; i < n; i++) {
scanf_s("%lf", &x[i]);
}
clock_t start, end;
start = clock();
double* Ax = matrix_mul_vector(A, x, n);
double* g = vector_sub_vector(Ax, b, n);
double* s = negate_vector(g, n);
double numerator = (-1) * mul_vector_transpose_vector(s, g, n);
double* tmp = matrix_mul_vector(A, s, n);
double denominator = mul_vector_transpose_vector(s, tmp, n);
double gamma = (numerator) / (denominator);
double* s1 = scalar_mul_vector(s, gamma, n);
x = vector_add_vector(x, s1, n);
while (t--) {
Ax = matrix_mul_vector(A, x, n);
double* gnew = vector_sub_vector(Ax, b, n);
numerator = mul_vector_transpose_vector(gnew, gnew, n);
denominator = mul_vector_transpose_vector(g, g, n);
double beta = (numerator) / (denominator);
s1 = scalar_mul_vector(s, beta, n);
double* neggnew = negate_vector(gnew, n);
s = vector_add_vector(s1, neggnew, n);
numerator = mul_vector_transpose_vector(s, neggnew, n);
tmp = matrix_mul_vector(A, s, n);
denominator = mul_vector_transpose_vector(s, tmp, n);
gamma = (numerator) / (denominator);
s1 = scalar_mul_vector(s, gamma, n);
x = vector_add_vector(x, s1, n);
copy_vector(g, gnew, n);
}
end = clock();
printf("Solution x = [");
for (i = 0; i < n - 1; i++) {
printf(" %lf,", x[i]);
}
printf(" %lf ]\n", x[n - 1]);
printf("Parallel execution time %f \n", (double)(end - start) / CLOCKS_PER_SEC);
return 0;
}
| |