Question

I am doing parallel programming with MPI on Beowulf cluster. We wrote parallel algorithm for simulated annealing. It works fine. We expect 15 time faster execution than with serial code. But we did some execution of serial C code on different architectures and operating systems just so we could have different data sets for performance measurement. We have used this Random function in our code. We use GCC on both windows and ubuntu linux. We figured out that execution takes much longer on linuxes, and we don't know why. Can someone compile this code on linux and windows with gcc and try to explain me.

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

    int main (int argc, char** argv){
        double Random();

        int k,NUM_ITERATIONS = 10;
        clock_t start_time = clock();
        NUM_ITERATIONS=atoi(argv[1]);

        // iniciranje random generatora 
        srand(time(NULL));

        for(k=0; k<NUM_ITERATIONS; k++){
                double raa = Random();
        }
        clock_t end_time = clock();
    printf("Time of algorithm execution: %lf seconds\n",  ((double) (end_time - start_time)) / CLOCKS_PER_SEC);

    return 0;
    }

    // generate random number bettwen 0 and 1
    double Random(){
        srand(rand());
        double a = rand();
        return a/RAND_MAX; 
    }

If I execute it with 100 000 000 as argument for NUM_ITERATIONS, I get 20 times slower execution on linux than on windows. Tested on machine with same architecture with dual boot win + ubuntu linux. We need help as this Random function is bottleneck for what we want to show with our data.

Was it helpful?

Solution

I would investigate other random number generators available. Many exist that have been well tested and perform better than the standard library random functions, both in terms of speed of execution and in terms of pseudo-randomness. I have also implemented my own RNG for a graduate class, but I wouldn't use it in production code. Go with something that has been vetted by the community. Random.org is a good resource for testing whatever RNG you select.

OTHER TIPS

On Linux gcc, the call to srand(rand()); within the Random function accounts for more than 98 % of the time.

It is not needed for the generation of random numbers, at least not within the loop. You already call srand() once, it's enough.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top