0% found this document useful (0 votes)
5 views

Correction-TD2-openMP-Ex1-Q1-2

The document contains a correction exercise for parallel programming using OpenMP, specifically focusing on a scalar product calculation. It includes two code examples: the first is a simple sequential implementation, and the second demonstrates a multithreaded approach using pthreads to compute the scalar product in parallel. Both examples initialize two arrays and compute their dot product, with the second example employing mutexes for thread safety.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Correction-TD2-openMP-Ex1-Q1-2

The document contains a correction exercise for parallel programming using OpenMP, specifically focusing on a scalar product calculation. It includes two code examples: the first is a simple sequential implementation, and the second demonstrates a multithreaded approach using pthreads to compute the scalar product in parallel. Both examples initialize two arrays and compute their dot product, with the second example employing mutexes for thread safety.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2ISEOC Institut Supérieur d’Informatique

Correction TD2 : Programmation des architectures parallèles à mémoire


partagée - OpenMP -

Exercice 1

Q1.

#include<stdio.h>
#define SIZE 256

int main()
{
double sum , a[SIZE], b[SIZE];

// Initialisation
sum = 0.;

for (int i = 0; i < SIZE ; i ++)


{
a[i] = i * 0.5;
b[i] = i * 2.0;
}

// Calcul
for (int i = 0; i < SIZE ; i ++) {
sum = sum + a[i ]* b[i ];
}
printf ("sum = %f\n", sum);
return 0;
}

Pour size=16

Q2.

#include<stdio.h>
#include<pthread.h>
#define SIZE 256
#define NUM_THREADS 4
#define intervalle SIZE/NUM_THREADS //256/4=64

struct threadargs{

1
2ISEOC Institut Supérieur d’Informatique

double aa[SIZE];
double bb[SIZE];
int debut;
int fin;
double *sum;
pthread_mutex_t *mutex;
};

void* scalaire(void* arg)


{
struct threadargs *tf=(struct threadargs*)arg;
double sum_local = 0.; // pr limiter la durée des mutex
// Calcul
for (int i = tf->debut; i <tf->fin; i++)
sum_local = sum_local + tf->aa[i]* tf->bb[i];
pthread_mutex_lock(tf->mutex);
*tf->sum = *tf->sum + sum_local ;
pthread_mutex_unlock(tf->mutex);
pthread_exit(NULL) ;
}

int main()
{

pthread_t th[NUM_THREADS];
pthread_mutex_t mux;
struct threadargs
args[NUM_THREADS];
pthread_mutex_init(&mux,NULL);
double a[SIZE];
double b[SIZE];

// Initialisation
double somme = 0.;
for (int i = 0; i<SIZE ; i++)
{ a[i] = i * 0.5;
b[i] = i * 2.0; }

for (int i = 0; i<NUM_THREADS ; i ++)


{
int d = i*intervalle;
// 0*64 puis 1*64 puis 2 *64 puis 3*64
int f = (i+1)*intervalle;
args[i].debut=d;
args[i].fin=f;
args[i].sum=&somme;
args[i].mutex=&mux;

2
2ISEOC Institut Supérieur d’Informatique

for (int k = 0; k<SIZE ; k++)


{
args[i].aa[k] = a[k];
args[i].bb[k] = b[k];
}

pthread_create(&th[i], NULL, scalaire, (void*)&args[i]);


}

for (int i = 0; i < NUM_THREADS ; i ++)


{
pthread_join (th[i], NULL );
}

printf ("sum = %f\n" , somme);

return 0;
}

You might also like