M2_SETI/A4/TP_A4/exo2/exo2.c

75 lines
1.6 KiB
C
Raw Permalink Normal View History

2023-02-12 16:06:32 +01:00
#include "stdio.h"
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <omp.h>
#include <math.h>
int main(int argc, char **argv)
{
int N;
int nb_thread;
double start;
double end;
// Prise en compte des arguments
if (argc == 3){
N = atoi(argv[1]);
nb_thread = atoi(argv[2]);
printf("Nb thread max %d\n", nb_thread);
printf("N %d\n", N);
} else {
printf("Missing parameters\n\r");
return EXIT_FAILURE;
}
srand(time(NULL));
double x[N], y[N];
for(int i=0; i<N; i++)x[i]=rand();
double mean_s = 0, mean_omp = 0;
int boucle = 10000;
printf("Mean time on %d executions\n", boucle);
// Version séquentielle
for(int t=0; t<boucle;t++){
start = omp_get_wtime();
for(int i=0; i<N; i++){
//Calcul 1
y[i] = 2.27*x[i];
//Calcul 2
// y[i] = 2.27*log(x[i])*cos(x[i]);
}
end = omp_get_wtime();
mean_s += (end-start);
}
// Version parallélisée
omp_set_num_threads(nb_thread);
for(int t=0; t<boucle;t++){
start = omp_get_wtime();
#pragma omp parallel for
for(int i=0; i<N; i++){
//Calcul 1
y[i] = 2.27*x[i];
//Calcul 2
// y[i] = 2.27*log(x[i])*cos(x[i]);
}
end = omp_get_wtime();
mean_omp += (end-start);
}
// Moyenne sur B boucles
mean_s = mean_s/boucle;
mean_omp = mean_omp/boucle;
printf("Sequential work took %f seconds\n", mean_s);
printf("OMP work took %f seconds\n\r", mean_omp);
return 0;
}