Compare commits

...

5 Commits

Author SHA1 Message Date
b6c3b42361 coorection td6 2025-10-09 15:58:39 +02:00
126b0890f1 oups 2025-10-08 14:09:51 +02:00
950c3bb003 typo tp6 2025-10-08 13:58:43 +02:00
3473b836b8 ds 2024 2025-10-08 13:52:09 +02:00
f83540153d utilisation de drand48_- 2025-10-08 12:32:29 +02:00
6 changed files with 99 additions and 4 deletions

BIN
cours/thread.pdf Normal file

Binary file not shown.

BIN
ds/ds.pdf Normal file

Binary file not shown.

View File

@@ -80,7 +80,7 @@ On comptabilise le nombre de lancers à l'intérieur du disque (`x^2+y^2 <= 1`).
Paralléliser le [code](src/pi.c) en créant pour la simulation plusieurs threads (passé à la ligne de commande).
Remarque : dans le code fournit, on utilise la fonction `rand_r`, et pas `rand`. Cette dernière n'est pas réentrante (état global caché).
Remarque : dans le code fournit, on utilise la fonction `drand48_r`, et pas `rand`. Cette dernière n'est pas réentrante (état global caché).
#### Ex3
On souhaite écrire une petite librairie implantant une pile d'entiers. Sa taille sera statique, et determinée au moment

30
tp/tp6/correc_gr34/ex1.c Normal file
View File

@@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#define NUM_THREADS 16
void *thread(void *thread_id) {
long id = ((long ) thread_id);
printf("Hello from thread %ld\n", id);
return (void*)(2*id);
}
int main() {
pthread_t threads[NUM_THREADS];
int t[NUM_THREADS];
long res;
for (long i = 0; i < NUM_THREADS; i++){
// t[i] = i;
assert( pthread_create(&threads[i], NULL, thread, (void*)i) == 0);
}
for (int i = 0; i < NUM_THREADS; i++){
assert( pthread_join(threads[i],(void *)&res) == 0);
printf("%ld\n",res);
}
return EXIT_SUCCESS;
}

60
tp/tp6/correc_gr34/sum.c Normal file
View File

@@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
static inline double tstamp(void)
{
struct timespec tv;
clock_gettime(CLOCK_MONOTONIC, &tv);
return tv.tv_sec + tv.tv_nsec * 1.0e-9;
}
typedef struct arg {
long start;
long end;
long step;
long S;
} arg;
void* slice(void* a) {
long start = ((arg*)a)->start;
long end = ((arg*)a)->end;
long step= ((arg*)a)->step;
long S = 0;
for(long i = start; i <= end; i += step)
S += i;
((arg*)a)->S = S;
return NULL;
//return (void*)S;
}
int main(int argc, char *argv[])
{
long N = strtol(argv[1], NULL,0);
long M = strtol(argv[2], NULL,0);
long S = 0, ret;
double t1,t2;
pthread_t* threads = (pthread_t*)calloc(M,sizeof(pthread_t));
arg* args = (arg*)calloc(M,sizeof(arg));
t1 = tstamp();
for (long i = 0; i < M; i++){
args[i].start = i + 1;
args[i].end = N;
args[i].step = M;
assert( pthread_create(&threads[i], NULL, slice, (void*)(args + i)) == 0);
}
for (int i = 0; i < M; i++){
// assert( pthread_join(threads[i], (void*)&ret) == 0);
assert( pthread_join(threads[i], NULL) == 0);
S += args[i].S;
}
t2 = tstamp();
assert( S == N*(N+1)/2 );
printf("S = %ld time = %lf\n",S,t2 - t1);
return EXIT_SUCCESS;
}

View File

@@ -6,10 +6,15 @@
void* nbDansCercleSeq( void* nbLancers )
{
long nb = 0;
unsigned int seed;
struct drand48_data bf;
double x,y;
srand48_r(pthread_self(),&bf); // initialisation de la graine
// avec le numero du thread
for( long i = 0; i < (long) nbLancers; i++ ) {
double x = (double) rand_r(&seed) / RAND_MAX;
double y = (double) rand_r(&seed) / RAND_MAX;
drand48_r(&bf,&x);
drand48_r(&bf,&y);
if ( x * x + y * y <= 1.0 ) {
nb += 1;
}