tweak parallelism example

This commit is contained in:
NunoSempere 2023-11-27 16:46:57 +00:00
parent 66d33c0fb7
commit ffd6e5dcbb
16 changed files with 27 additions and 14 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -15,7 +15,7 @@ int main()
// Question being asked: what is the distribution of sampling 1000 times and taking the min? // Question being asked: what is the distribution of sampling 1000 times and taking the min?
double sample_min_of_n(uint64_t* seed, int n){ double sample_min_of_n(uint64_t* seed, int n){
double min = sample_normal(5, 2, seed); double min = sample_normal(5, 2, seed);
for(int i=0; i<(n-2); i++){ for(int i=0; i<(n-1); i++){
double sample = sample_normal(5, 2, seed); double sample = sample_normal(5, 2, seed);
if(sample < min){ if(sample < min){
min = sample; min = sample;
@ -27,7 +27,7 @@ int main()
return sample_min_of_n(seed, 1000); return sample_min_of_n(seed, 1000);
} }
int n_samples = 1000000, n_threads = 16; int n_samples = 10000, n_threads = 16;
double* results = malloc(n_samples * sizeof(double)); double* results = malloc(n_samples * sizeof(double));
parallel_sampler(sampler_min_of_1000, results, n_threads, n_samples); parallel_sampler(sampler_min_of_1000, results, n_threads, n_samples);
printf("Mean of the distribution of (taking the min of 1000 samples of a normal(5,2)): %f\n", array_mean(results, n_samples)); printf("Mean of the distribution of (taking the min of 1000 samples of a normal(5,2)): %f\n", array_mean(results, n_samples));
@ -41,27 +41,29 @@ int main()
int quotient = n / 16; int quotient = n / 16;
int remainder = n % 16; int remainder = n % 16;
uint64_t seed = 1000; uint64_t seed = 100;
double result_remainder = sample_min_of_n(&seed, remainder); double result_remainder = sample_min_of_n(&seed, remainder);
double sample_min_of_quotient(uint64_t* seed) { double sample_min_of_quotient(uint64_t* seed) {
return sample_min_of_n(seed, quotient); double result = sample_min_of_n(seed, quotient);
// printf("Result: %f\n", result);
return result;
} }
double* results_quotient = malloc(quotient * sizeof(double)); double* results = malloc(n_threads * sizeof(double));
parallel_sampler(sample_min_of_quotient, results_quotient, n_threads, quotient); parallel_sampler(sample_min_of_quotient, results, n_threads, n_threads);
double min = results_quotient[0]; double min = results[0];
for(int i=1; i<quotient; i++){ for(int i=1; i<n_threads; i++){
if(min > results_quotient[i]){ if(min > results[i]){
min = results_quotient[i]; min = results[i];
} }
} }
if(min > result_remainder){ if(min > result_remainder){
min = result_remainder; min = result_remainder;
} }
free(results_quotient); free(results);
return min; return min;
} }
printf("Minimum of 1M samples of normal(5,2): %f\n", sample_n_parallel(1000000)); printf("Minimum of 10M samples of normal(5,2): %f\n", sample_n_parallel(1000 * 1000));
} }

View File

@ -326,14 +326,25 @@ void parallel_sampler(double (*sampler)(uint64_t* seed), double* results, int n_
int divisor_multiple = quotient * n_threads; int divisor_multiple = quotient * n_threads;
uint64_t** seeds = malloc(n_threads * sizeof(uint64_t*)); uint64_t** seeds = malloc(n_threads * sizeof(uint64_t*));
// printf("UINT64_MAX: %lu\n", UINT64_MAX);
srand(1);
for (uint64_t i = 0; i < n_threads; i++) { for (uint64_t i = 0; i < n_threads; i++) {
seeds[i] = malloc(sizeof(uint64_t)); seeds[i] = malloc(sizeof(uint64_t));
*seeds[i] = i + 1; // xorshift can't start with 0 // Constraints:
// - xorshift can't start with 0
// - the seeds should be reasonably separated and not correlated
*seeds[i] = (uint64_t) rand() * (UINT64_MAX / RAND_MAX);
// printf("#%ld: %lu\n",i, *seeds[i]);
// Other initializations tried:
// *seeds[i] = 1 + i;
// *seeds[i] = (i + 0.5)*(UINT64_MAX/n_threads);
// *seeds[i] = (i + 0.5)*(UINT64_MAX/n_threads) + constant * i;
} }
int i; int i;
#pragma omp parallel private(i) #pragma omp parallel private(i)
{ {
#pragma omp for #pragma omp for
for (i = 0; i < n_threads; i++) { for (i = 0; i < n_threads; i++) {
int lower_bound_inclusive = i * quotient; int lower_bound_inclusive = i * quotient;