/
main.cpp
118 lines (111 loc) · 2.78 KB
/
main.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include "matrix.h"
#include "synchronize.h"
#include "get_time.h"
#include "processes.h"
#define MAX_N 6
int main(int argc, char * argv[])
{
pthread_t *tid;//number of thread
args *arg;
int total_processes;
double *a, *b, *x;
int res1, res2;
long int t;
int n;
int N;
int i;
char * filename = 0;
const char * name = "c.txt";
if( argc != 3 && argc != 4 )
{
printf("Usage : %s <n> <total_processes> <filename>\n", argv[0]);
return 0;
}
n = atoi(argv[1]);//from number to string
total_processes = atoi (argv[2]);
if(!n || !total_processes)
{
printf("Usage : %s <n> <total_processes> <filename>\n", argv[0]);
return 0;
}
a = new double[n*n];
b = new double[n];
x = new double[n];
tid = new pthread_t[total_processes];
arg = new args[total_processes];
if(argc > 3)
filename = argv[3];
if(filename)
{
res1 = read_matrix(a, n, "a.txt");
res2 = read_vector(b, n, "b.txt");
if(res1 || res2)
{
printf("cannot read from file\n");
delete [] tid;
delete [] arg;
delete [] a;
delete [] b;
delete [] x;
return 1;
}
}
else
{
init_matrix(a, n);
init_vector(b, a, n);
}
printf("matrix A:\n");
print_matrix(a, n);
printf("vector b:\n");
print_vector(b, n);
for (i = 0; i < total_processes; i++)
{
arg[i].a = a;
arg[i].b = b;
arg[i].n = n;
arg[i].total_processes = total_processes;
arg[i].num_process = i;
arg[i].error = 0;
}
t = get_full_time ();
for (i = 0; i < total_processes; i++)
{
if (pthread_create (tid + i, 0, &thread_method_of_reflections, arg + i))
{
printf ("Cannot create thread %d\n", i);
return 2;
}
}
for (i = 0; i < total_processes; i++)
pthread_join (tid[i], 0);
back_hod(a, b, x, n);
t = get_full_time () - t;
N = (n < MAX_N) ? n : MAX_N;
printf("result : ");
for(i = 0; i < N; i++)
printf("%lg ", x[i]);
printvectorfile(x,n,name);
if(filename)
{
read_matrix(a, n, "a.txt");
read_vector(b, n, "b.txt");
printf("\nResidual = %le\nElapsed time = %Lg\n",SolutionError(n,a,b,x),(long double)t/(CLOCKS_PER_SEC));
}
else
{
init_matrix(a, n);
init_vector(b, a, n);
printf("\nResidual = %le\nError = %le\nElapsed time = %Lg\n",SolutionError(n,a,b,x), SolutionAccuracy(n,x),(long double)t/(CLOCKS_PER_SEC));
}
delete [] tid;
delete [] arg;
delete [] a;
delete [] b;
delete [] x;
return 0;
}