1

I am on a Windows 10 machine with a processor Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz, 1800 Mhz, 4 Core(s), 8 Logical Processor(s) and 8 GB RAM. I have been running this small openmp code to compare the performance of a normal sequential program and an omp program.

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

void normal(unsigned int num_steps){
    double step = 1.0/(double)(num_steps);
    double sum = 0.0;
    double start=omp_get_wtime();
    for (long i = 0; i < num_steps;i++){
        double x = i * step;
        sum += (4.0 / (1.0 + x * x));
    }
    double pi = step * sum;
    double end=omp_get_wtime();
    printf("Time taken : %0.9lf\n",end-start);
    printf("The value of pi is : %0.9lf\n",pi);
}

void parallel(unsigned int num_steps,unsigned int thread_cnt){
    double pi=0.0;
    double sum[thread_cnt];
    for(unsigned int i=0;i<thread_cnt;i++)
        sum[i]=0.0;
    omp_set_num_threads(thread_cnt);
    double start=omp_get_wtime();
    #pragma omp parallel
    {
        double x;
        double sum_temp=0.0;
        double step = 1.0 / (double)(num_steps);
        int num_threads = omp_get_num_threads();
        int thread_no = omp_get_thread_num();
        if(thread_no==0){
            thread_cnt = num_threads;
            printf("Number of threads assigned is : %d\n",num_threads);
        }
        for (unsigned int i = thread_no; i < num_steps;i+=thread_cnt){
            x=(i*step);
            sum_temp+=(4.0/(1+x*x))*step;
        }
        #pragma omp critical
        {
            sum[thread_no]=sum_temp; 
        }
    }
    double end=omp_get_wtime();
    printf("Time taken : %0.9lf\n",end-start);
    for(unsigned int i=0;i<thread_cnt;i++){
        pi+=sum[i];
    }
    printf("The value of pi is : %0.9lf\n",pi);
}

int main(){
    unsigned int num_steps=1000000;
    unsigned int thread_cnt=4;
    scanf("%d",&thread_cnt);
    normal(num_steps);
    parallel(num_steps,thread_cnt);
    return 0;
}

enter image description here

I am using mingw's GCC compiler and to run openmp programs which require pthread library i had downloaded the mingw32-pthreads-w32 library. So is it not working, because I don't seem to be able to beat the normal sequential execution despite using so many threads and also handling race conditions and false sharing using the critical pragma.

Reference : I have been following the OPENMP playlist on youtube by Intel.

  • https://stackoverflow.com/questions/40088101/can-gcc-make-my-code-parallel – Hans Passant Nov 20 '21 at 09:46
  • Hey could you point me out to the exact answer you want me to refer to, I couldn't find something related to the library's functionality in windows. Thanks in advance – Abhishek Mittal Nov 20 '21 at 10:44
  • 2
    Your test case is much too small to even check this. The cost of launching threads far outweighs the benefits. I've tested it on my system where I know openmp works and all I can see is statistical noise caused by the scheduler. Revise your test case so that the single threaded case runs at least a few seconds. For example compute pi 1000 times – Homer512 Nov 20 '21 at 11:16
  • Did you compile your program with `-fopenmp` (otherwise OpenMP directives are simply ignored)? What is the compilation flags used and the version of GCC? Note that windows is *very* slow to print things in the console, so it is better not to have the time of printf calls included in the timings. – Jérôme Richard Nov 20 '21 at 11:39
  • On my machine, with mingw, the main problem comes from the precision of the timer which is about to few milliseconds (so I get null durations sometimes). With a 100 time bigger `num_steps` (and `-fopenmp`) everything is fine. Note that there are ways to speed up the program even with 1 threads. – Jérôme Richard Nov 20 '21 at 11:55
  • @Homer512 I ran for 1e9 steps and the results are almost in the same proportion 6.6 seconds for normal sequential and 9.8 seconds for 4 threaded. Also i ran on my linux machine and even with the same million steps I was able to achieve 4 times speedup using the 4 parallel threads – Abhishek Mittal Nov 20 '21 at 11:59

0 Answers0