Created
October 11, 2017 20:52
-
-
Save n-canter/7454433de46434a0dc1f4b68e13de0f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <omp.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char **argv) | |
{ | |
int num_threads = atoi(argv[1]); | |
int n = atoi(argv[2]); | |
struct drand48_data *seeds = malloc(n * sizeof(struct drand48_data)); | |
for (int i = 0; i < n; i++) { | |
srand48_r(i, seeds + i); | |
} | |
omp_set_num_threads(num_threads); | |
double begin, end; | |
double rnd; | |
begin = omp_get_wtime(); | |
#pragma omp parallel for | |
for (int i = 0; i < n; i++) { | |
drand48_r(seeds + i, &rnd); | |
} | |
end = omp_get_wtime(); | |
printf("elapsed %lf\n", end - begin); | |
begin = omp_get_wtime(); | |
#pragma omp parallel for | |
for (int i = 0; i < n; i++) { | |
drand48_r(seeds + omp_get_thread_num(), &rnd); | |
} | |
end = omp_get_wtime(); | |
printf("elapsed %lf\n", end - begin); | |
uint *srand_seeds = calloc(n, sizeof(uint)); | |
begin = omp_get_wtime(); | |
#pragma omp parallel for | |
for (int i = 0; i < n; i++) { | |
rand_r(srand_seeds + i); | |
} | |
end = omp_get_wtime(); | |
printf("elapsed %lf\n", end - begin); | |
begin = omp_get_wtime(); | |
#pragma omp parallel for | |
for (int i = 0; i < n; i++) { | |
rand_r(srand_seeds + omp_get_thread_num()); | |
} | |
end = omp_get_wtime(); | |
printf("elapsed %lf\n", end - begin); | |
free(seeds); | |
free(srand_seeds) | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment