M2_SETI/A4/TP_A4/exo3/exo3.c
2023-02-12 16:06:32 +01:00

66 lines
1.5 KiB
C

#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;
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 = 0;
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);
for(int t=0; t<boucle;t++){
start = omp_get_wtime();
for(int i=0; i<N; i++){
y += x[i];
}
end = omp_get_wtime();
mean_s += (end-start);
}
omp_set_num_threads(nb_thread);
for(int t=0; t<boucle;t++){
start = omp_get_wtime();
// #pragma omp parallel for reduction(+:y)
//#pragma omp parallel for schedule(guided,100)
#pragma omp parallel for schedule(static,1000) reduction(+:y)
for(int i=0; i<N; i++){
// #pragma omp atomic
// #pragma omp critical
y += x[i];
}
end = omp_get_wtime();
mean_omp += (end-start);
}
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;
}