Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC performance

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.

like image 1000
azec-pdx Avatar asked Dec 08 '09 13:12

azec-pdx


1 Answers

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.

like image 197
Didier Trosset Avatar answered Sep 22 '22 18:09

Didier Trosset