-
Notifications
You must be signed in to change notification settings - Fork 0
/
cholesky.c
248 lines (217 loc) · 8.06 KB
/
cholesky.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include "matrix.h"
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
struct arg_struct {
Matrix *m;
int i0;
int in;
int i;
int j;
int threadNb;
int pas;
float *s;
};
// Algorithme normal. Celui de wikipédia
Matrix *cholesky1(Matrix *mat){
int i,j,k;
int n = mat->rows;
Matrix *l = Matrix_create(n,n);
// Algorithme sans threads.
// - i le numéro de la colonne et j celui de la ligne
// - Note, il y a peut être moyen de réduire à seulement deux boucles?
for(i=0;i<n; i++){
for(j=i;j<n; j++){
double somme = 0;
if(i==j){
for(k=0; k<(i); k++){
somme += Matrix_get(l,i,k) * Matrix_get(l,i,k);
}
Matrix_set(l,i,j, sqrt(Matrix_get(mat,i,j)-somme));
}else{
for(k=0; k<j-1; k++){
somme += Matrix_get(l,i,k)*Matrix_get(l,j,k);
}
Matrix_set(l,j,i, (Matrix_get(mat,i,j)-somme)/Matrix_get(l,i,i));
}
}
}
return(l);
}
// Fonction qui executée par les threads pour le calcul de la diagonale.
void *calculSommeDiag(void *arguments)
{
struct arg_struct *args = arguments;
double sum=0;
double *totalsum = args -> s;
int ii = args -> i;
int pas = args -> pas;
int k0 = args -> threadNb;
int kk;
for(kk=k0; kk<ii; kk+=pas){
sum += Matrix_get(args -> m,ii,kk) * Matrix_get(args -> m,ii,kk);
}
pthread_mutex_lock(&mut);
*totalsum += sum;
pthread_mutex_unlock (&mut);
return NULL;
}
// Fonction qui executée par les threads pour le calcul des autres éléments.
void *calculSommeAutre(void *arguments)
{
struct arg_struct *args = arguments;
int k;
double sum=0;
double *totalsum = args -> s;
int ii = args -> i;
int jj = args -> j;
int pas = args -> pas;
int k0 = args -> threadNb;
for(k=k0; k<jj-1; k+=pas){
sum += Matrix_get(args -> m,ii,k) * Matrix_get(args -> m,jj,k);
}
pthread_mutex_lock(&mut);
*totalsum += sum;
pthread_mutex_unlock (&mut);
return NULL;
}
// Algorithme avec paralélisation, nombre de thread fixe.
Matrix *choleskyParaSafe(Matrix *matOrigin){
int threadNumber = 2;
int i,j,k;
Matrix *mat = Matrix_clone(matOrigin);
int n = mat->rows;
Matrix *l = Matrix_create(n,n);
// Algorithme avec threads.
// - i le numéro de la colonne et j celui de la ligne
// - Note, dans cette version on parallélise seulement la troisème boucle.
for(i=0;i<n; i++){
for(j=i;j<n; j++){
double somme = 0;
if(i==j){
// On lance plusieurs threads
pthread_t thread1, thread2;
int tn;
// On définit un tableau donc chaque element va être passé en param à un thread
struct arg_struct args[threadNumber];
for(tn=0;tn<threadNumber;tn++){
// On initialise les arguments.
args[tn].s = &somme;
args[tn].m = l;
args[tn].i = i;
args[tn].j = j;
args[tn].threadNb = tn;
args[tn].pas = threadNumber;
}
// On lance les threads.
pthread_create (&thread1, NULL, calculSommeDiag, (void *)&args[0]);
pthread_create (&thread2, NULL, calculSommeDiag, (void *)&args[1]);
// On attend la fin de tous les threads.
// On attend la fin de tous les threads.
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
// On actualise l'élément de la matrice.
Matrix_set(l,i,j, sqrt(Matrix_get(mat,i,j)-somme));
}else{
// On lance plusieurs threads
//pthread_t threads[threadNumber];
pthread_t thread1, thread2;
int tn;
// On définit un tableau donc chaque element va être passé en param à un thread
struct arg_struct args[2];
for(tn=0;tn<2;tn++){
// On initialise les arguments.
args[tn].s = &somme;
args[tn].m = l;
args[tn].i = i;
args[tn].j = j;
args[tn].threadNb = tn;
args[tn].pas = threadNumber;
}
pthread_create (&thread1, NULL, calculSommeAutre, (void *)&args[0]);
pthread_create (&thread2, NULL, calculSommeAutre, (void *)&args[1]);
// On attend la fin de tous les threads.
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
Matrix_set(l,j,i, (Matrix_get(mat,i,j)-somme)/Matrix_get(l,i,i));
}
}
}
return(l);
}
// Algorithme avec paralélisation, nombre de thread en fonction des processeurs.
Matrix *cholesky2Para(Matrix *matOrigin){
int threadNumber = sysconf(_SC_NPROCESSORS_ONLN);
int i,j,k;
Matrix *mat = Matrix_clone(matOrigin);
int n = mat->rows;
Matrix *l = Matrix_create(n,n);
// Algorithme avec threads.
// - i le numéro de la colonne et j celui de la ligne
// - Note, dans cette version on parallélise seulement la troisème boucle.
for(i=0;i<n; i++){
for(j=i;j<n; j++){
double somme = 0;
if(i==j){
// On lance plusieurs threads
pthread_t *threads;
threads = (pthread_t*) malloc((threadNumber+1) * sizeof (pthread_t));
int tn;
// On définit un tableau donc chaque element va être passé en param à un thread
struct arg_struct args[threadNumber];
for(tn=0;tn<threadNumber;tn++){
// On initialise les arguments.
args[tn].s = &somme;
args[tn].m = l;
args[tn].i = i;
args[tn].j = j;
args[tn].threadNb = tn;
args[tn].pas = threadNumber;
// On lance les threads.
if (pthread_create(&threads[i], NULL, calculSommeDiag, (void *)&args[tn]) != 0) {
printf("error");
return -1;
}
}
// On attend la fin de tous les threads.
for(tn=0;tn<threadNumber;tn++){
pthread_join(threads[i], NULL);
}
// On actualise l'élément de la matrice.
Matrix_set(l,i,j, sqrt(Matrix_get(mat,i,j)-somme));
}else{
// On lance plusieurs threads
//pthread_t threads[threadNumber];
pthread_t *threads;
threads = (pthread_t*) malloc((threadNumber+1) * sizeof (pthread_t));
int tn;
// On définit un tableau donc chaque element va être passé en param à un thread
struct arg_struct args[threadNumber];
for(tn=0;tn<threadNumber;tn++){
// On initialise les arguments.
args[tn].s = &somme;
args[tn].m = l;
args[tn].i = i;
args[tn].j = j;
args[tn].threadNb = tn;
args[tn].pas = threadNumber;
// On lance les threads.
if (pthread_create(&threads[i], NULL, calculSommeAutre, (void *)&args[tn]) != 0) {
printf("error");
return -1;
}
}
// On attend la fin de tous les threads.
for(tn=0;tn<threadNumber;tn++){
pthread_join(threads[i], NULL);
}
Matrix_set(l,j,i, (Matrix_get(mat,i,j)-somme)/Matrix_get(l,i,i));
}
}
}
return(l);
}