-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTp_multiThread.c
423 lines (370 loc) · 9.34 KB
/
Tp_multiThread.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//=============================================================
// MAGNAN Samuel, SIVANANDAN Bruno
// B3154
// Tp MultiThread 2014
//=============================================================
//--------------------------------------------- includes système
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <pthread.h>
#include <string.h>
//---------------------------------------- macros et definitions
#define FALSE 0
#define TRUE 1
#define MAX_FACTORS 64
#define MODE_PRIME TWO_THREADS_JOIN
//-------------------------------------------------------- enums
enum MODE {NO_THREAD,ONE_THREAD,TWO_THREADS_JOIN};
//------------------------------------------ structures et types
typedef struct primeFactors_s // Maillon d'une chaine
{
uint64_t n;
int size;
uint64_t factors[MAX_FACTORS];
struct primeFactors_s * next;
} primeFactors_t;
//------------------------------------ déclarations de fonctions
void print_prime_factors(uint64_t n);
int is_prime(uint64_t p);
void print_factors(uint64_t);
int get_prime_factors(uint64_t n, uint64_t * dest);
//-------------------------------------------- variables privées
static pthread_mutex_t mutex; // Mutex de lecture de fichier
static pthread_mutex_t mutex_array; // Mutex de chaine
static FILE * file; // Fichier de données
static primeFactors_t first_factors; // Chaine
//-------------------------------------------- fonctions privées
//static void *task_a (void *p_data)
//{
// puts ("Hello world A");
//
// (void) p_data;
// pthread_exit( NULL);
//}
//
//static void *task_b (void *p_data)
//{
// puts ("Hello world B");
//
// (void) p_data;
// pthread_exit( NULL);
//}
static void *task_prime_factors (void *p_data)
{
uint64_t value = *((uint64_t*)p_data);
printf("%llu: ",(value));
print_prime_factors(value);
printf("\n");
(void) p_data;
pthread_exit( NULL);
}
static void *task_worker(void *p_data)
{
file = fopen("numbers.txt","r");
uint64_t value;
pthread_mutex_lock(&mutex);
while(fscanf(file,"%llu",&value)!= EOF)
{
pthread_mutex_unlock(&mutex);
printf("%llu: ",value);
print_prime_factors(value);
printf("\n");
pthread_mutex_lock(&mutex);
}
pthread_mutex_unlock(&mutex);
}
static void *task_worker_array(void *p_data)
{
file = fopen("numbers.txt","r");
uint64_t value;
pthread_mutex_lock(&mutex);
while(fscanf(file,"%llu",&value)!= EOF)
{
pthread_mutex_unlock(&mutex);
//printf("%llu: ",value);
print_factors(value);
printf("\n");
pthread_mutex_lock(&mutex);
}
pthread_mutex_unlock(&mutex);
}
/*void addFactors(primeFactors_t * pf)
{
factorsMap=((factorsMap==NULL)?pf:);
//TODO
}*/
//-------------------------------------------------- main
int main(int argc, char *argv[])
{
int mode = MODE_PRIME;
uint64_t value;
uint64_t value2;
int th1;
int th2;
pthread_t ta;
pthread_t tb;
pthread_t tc;
pthread_t td;
pthread_mutex_init(&mutex,NULL);
pthread_mutex_init(&mutex_array,NULL);
first_factors.next = NULL; // initialisation de first -> next
first_factors.n = 0; // permet de savoir si on dispose d'un 1er élément ou non (-1)
//printf("test\n");
/*primeFactors_t begin;
begin.n = n;
begin.size = 0
begin.next = NULL;
factorsMap = begin;*/
file = fopen("numbers.txt","r");
//printf("%p",file);
//scanf("%llu",&value);
//printf("%llu\n",value);
/*while(fscanf(file,"%llu",&value)!= EOF)
{
int break_val = 0;
switch(mode)
{
case NO_THREAD:
printf("%llu: ",value);
print_prime_factors(value);
printf("\n");
break;
case ONE_THREAD:
pthread_create (&tc, NULL, task_prime_factors, &value);
pthread_join (tc,NULL);
brea0 case TWO_THREADS_JOIN:
pthread_create (&tc, NULL, task_prime_factors, &value);
if(fscanf(file,"%llu",&value2)!= EOF)
{
pthread_create (&td, NULL, task_prime_factors, &value2);
}
else
{
break_val = 1;
break;
}
pthread_join (tc,NULL);
pthread_join (td,NULL);
break;
}
if(break_val == 1)
{
break;
}
}*/
//print_factors(100000000);
//print_factors(100000000);
//print_factors(100000000);
//print_factors(100000000);
//print_factors(100000000);
//print_factors(1024);
//print_factors(1024);
//print_factors(1024);
//print_factors(100000000);
//print_factors(5555555555);
//print_factors(5555555555);
//print_factors(5555555555);
//TODO free les maillons
/*pthread_create (&ta, NULL, task_worker, (void*)1);
pthread_create (&tb, NULL, task_worker, (void*)2);
pthread_join (ta,NULL);
pthread_join (tb,NULL);
*/
pthread_create (&ta, NULL, task_worker_array, (void*)1);
pthread_create (&tb, NULL, task_worker_array, (void*)2);
pthread_join (ta,NULL);
pthread_join (tb,NULL);
pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&mutex_array);
return 0;
}
primeFactors_t compFactors(uint64_t n){
uint64_t i;
uint64_t n_initVal = n;//TODO calculs à part
primeFactors_t result;
result.n = n;
result.size = 0;
//uint64_t tot = 1;
while(n%2==0)
{
result.factors[result.size] = 2;
result.size++;
n /=2;
//tot *= 2;
}
for(i = 3; i<n ; i+=2)// remplissage du tableau de factors
{
while(n%i==0)
{
result.factors[result.size] = i;
result.size++;
n /=i;
//tot *=i;
}
if (n == 1 || i>=(uint64_t)sqrt(n_initVal))
{
break;
}
}
if(n !=1)
{
result.factors[result.size] = n;
result.size++;
}
return result;
}
void print_factors(uint64_t n)
{
uint64_t factors[MAX_FACTORS];
int j,k;
//TODO: mutex dans le coin?
k = get_prime_factors(n,factors);
printf("%llu :", n);
for(j=0; j<k ; j++)
{
printf(" %llu",factors[j]);
}
printf("\n");
}
int get_prime_factors(uint64_t n, uint64_t * dest)
{
primeFactors_t * current = &first_factors;
primeFactors_t * temp = &first_factors;
//printf("---- START ----\n");
int j = 0;
do
{
current = temp;
//printf("curr n - iter %d: %llu\n",j++,current->n);
if(current->n == n)
{
//printf("-> FOUND!");
memcpy(dest,current->factors,sizeof(current->factors));//FIXME ??? ok ou non?
return current->size; //size: taille du tabeau et donc nombre de facteurs
}
if(current->next!=NULL)
{
//printf("(Next not NULL)");
temp = current->next; // on travaille sur le suivant
}
}while(current->next!=NULL); // On recherche dans ce que l'on a déjà trouvé
// Si on arrive ici, c'est que l'on a parcouru toute la chaine sans trouver n
//printf("[n not found in previous values]\n");
primeFactors_t valComp = compFactors(n);
//memcpy(next,&valComp,sizeof(primeFactors_t));
/*for(i = 2; i<n ; i++)// remplissage du tableau de factors
{
while(n%i==0)
{
next->factors[next->size] = i;
next->size++;
n /=i;
}
if (n == 1 || i>=(uint64_t)sqrt(n_initVal))
{
break;
}
}*/
pthread_mutex_lock(&mutex_array);
primeFactors_t * next;
current->next = (primeFactors_t *) malloc(sizeof(primeFactors_t));
next = current->next;
next->n = n;
next->size = 0;
uint64_t i = 0;
//uint64_t n_initVal = n;//TODO calculs à part
next->n = valComp.n;
next->size = valComp.size;
for(i=0;i<valComp.size;i++)
{
//printf("%d\n",i);
next->factors[i] = valComp.factors[i];
}
next->next = NULL; // MAJ des pointeurs de maillons
memcpy(dest,next->factors,sizeof(next->factors));
pthread_mutex_unlock(&mutex_array);
return next->size;
/*int i,j;
for(i = 0; i<factorsMap[] ;i++)
{
for(j=0;j<tabPrimeFactors[i].length;j++)
{
if(valuesToFactorize[i]==n)
{
dest = tabPrimeFactors[i];
return tabPrimeFactors[i].length;
}
}
}*/
// TODO
/*
uint64_t i = 0; // XXX à enlever
uint64_t k;
int pos = 0;
uint64_t n_initVal = n;
primeFactors_t primeFact;
primeFact.n = n;
primeFact.size = 0
primeFact.next = NULL;
for(k = 2; i<n ; k++)
{
while(n%k==0)
{
tabPrimeFactors[i][pos] = k;
n /=k;
pos++;
}
if (n == 1 || k>=(uint64_t)sqrt(n_initVal))
{
break;
}
}
dest = tabPrimeFactors[i];
return tabPrimeFactors[i].length;*/
return 0; //XXX à virer!!!
}
void print_prime_factors(uint64_t n)
{
uint64_t i = 0;
uint64_t n_initVal = n;
// XXX NOTE: l'ajout de i<sqrt(n_initVal) permet d'obtenir un temps
// d'exécution beaucoup plus court pour les cas où le nombre possède
// un diviseur de très grande taille (>sqrt(n_initVal)
// Cela évite l'appel à is_prime(params) -> calculs redondants
while(n%2==0)
{
}
for(i = 2; i<n /*&& i<(uint64_t)sqrt(n_initVal)*/ /*i<4294967296*/; i++) // FIXME ?
{
while(n%i==0)
{
printf("%llu ",i);
n /=i;
}
if (n == 1 || i>=(uint64_t)sqrt(n_initVal))
{
//printf("--BREAK--[%llu]",i);
break;
}
}
//printf("OK");
printf("%llu ",n);
}
int is_prime(uint64_t p)
{
int i = 0;
if(!(p%2))
{
return FALSE;
}
for(i=3;i<sqrt(p);i+=2)
{
if(!(p%i))
{
return FALSE;
}
}
return TRUE;
}