lundi 21 octobre 2019

Using both rand() and rand_r() : is this simple example correct?

I am trying to understand the correct usage of parallel random number generation. There are multiple resources available online, as well as on this website. As a result, I wrote a simple code that seems to work, but it would be nice if someone could confirm my understanding.

For the sake of pointing out the difference and relationship between rand() and rand_r(), let's solve:

Produce a random integer N, then extract N random numbers in parallel and compute their average.

This is my proposal (checking and free omitted), small integers on purpose:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>

int main(){
        /* Initialize and extract an integer via rand() */
        srand(time(NULL));
        int N = rand() % 100;

        /* Storage array */ 
        int *extracted = malloc(sizeof(int) * N);

        /* Initialize N seeds for rand_r, which is completely
         * independent on rand and srand().
         * Setting the first as time(NULL), and the others
         * via successive increasing is a good idea */
        unsigned int *my_seeds = malloc(sizeof(unsigned int) * N);
        my_seeds[0] = time(NULL);
        for (int i = 1; i < N; ++i) {
                my_seeds[i] = my_seeds[i - 1] + 1;
        }

        /* The seeds for rand_r are ready:
         * extract N random numbers in parallel */
        #pragma omp for
        for(int i = 0; i < N; ++i){
                extracted[i] = rand_r(my_seeds + i) % 10;
        }

        /* Compute the average: must be done sequentially,
         * because of time-sincronization in reading/writing avg */
        double avg = 0;
        for(int i = 0; i < N; ++i){
                avg += extracted[i];
        }
        avg /= N;
        printf("%d samples, %.2f in average.\n", N, avg);
        return 0;
}

Comments in the code highlight how I would be interested in understanding if my my_seeds initialization was correct, and if I can safely use rand and rand_r as described. Thank you so much!




Aucun commentaire:

Enregistrer un commentaire