Skip to content
Extraits de code Groupes Projets
Valider 0b693697 rédigé par Mickaël Boichot's avatar Mickaël Boichot
Parcourir les fichiers

correction pi omp

parent 18c04b9f
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
CC=gcc
CUDA_CC=nvcc
CFLAGS_OMP=-O3 -Wall -fopenmp -DUSE_OMP
CFLAGS=-O3 -Wall
EXE1=pi.exe
EXE2=pi_omp.exe
# EXE3=pi_task.exe
#EXE3=pi_cuda.exe
#EXE4=pi_cuda_shared.exe
#EXE5=pi_cuda_shared_2.exe
#EXE6=pi_cuda_shared_3.exe
#EXE7=pi_cuda_shared_4.exe
all : $(EXE1) $(EXE2)
#$(EXE3) $(EXE4) $(EXE5) $(EXE6) $(EXE7)
$(EXE1) : pi_sequentiel.o
$(CC) $(CFLAGS) -o $@ $<
$(EXE2) : pi_omp.o
$(CC) $(CFLAGS_OMP) -o $@ $<
# $(EXE3) : pi_task.o
# $(CC) $(CFLAGS_OMP) -o $@ $<
$(EXE3) : pi_cuda.cu
$(CUDA_CC) -O3 -o $@ $<
$(EXE4) : pi_cuda_shared.cu
$(CUDA_CC) -O3 -o $@ $<
$(EXE5) : pi_cuda_shared_2.cu
$(CUDA_CC) -O3 -o $@ $<
$(EXE6) : pi_cuda_shared_3.cu
$(CUDA_CC) -O3 -o $@ $<
$(EXE7) : pi_cuda_shared_4.cu
$(CUDA_CC) -O3 -o $@ $<
%_omp.o : %_omp.c
$(CC) $(CFLAGS_OMP) -c -o $@ $<
%_task.o : %_task.c
$(CC) $(CFLAGS_OMP) -c -o $@ $<
%.o : %.c
$(CC) $(CFLAGS) -c -o $@ $<
clean :
rm -f *.o $(EXE1) $(EXE2) $(EXE3) $(EXE4) $(EXE5) $(EXE6) $(EXE7)
proper :
rm -f *.o
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "omp.h"
#define TRIALS_PER_THREAD 10E7
#define gettime(t) clock_gettime(CLOCK_MONOTONIC_RAW, t)
#define get_sub_seconde(t) (1e-9 * (double)t.tv_nsec)
/** return time in second
*/
double get_elapsedtime(void) {
struct timespec st;
int err = gettime(&st);
if (err != 0) return 0;
return (double)st.tv_sec + get_sub_seconde(st);
}
int main(int argc, char** argv) {
uint64_t const n_test = TRIALS_PER_THREAD;
uint64_t i;
double x = 0., y = 0.;
double pi = 0.;
double t0 = 0., t1 = 0., duration = 0.;
int nb_threads = 0;
#pragma omp parallel shared(nb_threads)
#pragma omp master
nb_threads = omp_get_num_threads();
fprintf(stdout, "Nb threads: %d\n", nb_threads);
uint64_t* result = (uint64_t*)malloc(sizeof(uint64_t) * nb_threads);
for (i = 0; i < nb_threads; ++i) {
result[i] = 0;
}
srand(2023);
t0 = get_elapsedtime();
#pragma omp parallel shared(result) firstprivate(n_test, x, y)
{
uint64_t local_res = 0;
unsigned int seed = time(NULL) ^ omp_get_thread_num() ^ getpid();
#pragma omp for schedule(static)
for (i = 0; i < n_test; ++i) {
x = rand_r(&seed) / (double)RAND_MAX;
y = rand_r(&seed) / (double)RAND_MAX;
local_res += (((x * x) + (y * y)) <= 1);
}
int tid = omp_get_thread_num();
result[tid] = local_res;
}
for (i = 0; i < nb_threads; ++i) {
pi += result[i];
}
t1 = get_elapsedtime();
duration = (t1 - t0);
fprintf(stdout, "%ld of %ld throws are in the circle ! (Time: %lf s)\n",
(uint64_t)pi, n_test, duration);
pi *= 4;
pi /= (double)n_test;
fprintf(stdout, "Pi ~= %lf\n", pi);
return 0;
}
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
uint64_t n_test = 10E7;
uint64_t i;
uint64_t count = 0;
double x = 0., y = 0.;
double pi = 0.;
srand(2023);
for (i = 0; i < n_test; ++i) {
x = rand() / (double)RAND_MAX;
y = rand() / (double)RAND_MAX;
count += (((x * x) + (y * y)) <= 1);
}
fprintf(stdout, "%ld of %ld throws are in the circle !\n", count, n_test);
pi = (count * 4) / (double)n_test;
fprintf(stdout, "Pi ~= %lf\n", pi);
return 0;
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter