0
double itime = omp_get_wtime();
    for (int i=0; i < x; i++) {
        cellx[i]=i*i;
    }

    for (int i = 0; i < y; i++) {
        celly[i]=i*i;
    }
    std::cout<<"execution time:"<<omp_get_wtime() - itime<<std::endl;

If x and y values are 40000, then the execution time is zero. Increasing it to 46000 is throwing Error - terminate called after throwing an instance of 'std::bad_array_new_length' what(): std::bad_array_new_length. Also, at times execution time is around 0.000099 for the same x and y values(40000). How do I measure the execution time for this sequential code?

swe
  • 1
  • 1
  • 2
    If you are not using the values of `cellx` or `celly` after the `std::cout` statement, the compiler is free to optimise the loops out and simply not emit any code for them. Even if that is not the case, incrementing and squaring an integer 80000 times takes on the order of 100 us on a modern CPU. – Hristo Iliev Feb 22 '21 at 19:24
  • Yes, but if you check omp_get_wtick(), it's likely to be something around 1 ns, so microseconds are massive in comparison to the resolution. – Mark H Feb 22 '21 at 20:19

1 Answers1

1

If, for some reason, the code has adjusted the default iostream configuration, you can get a value of 0. The code at the very bottom demonstrates this. The code that follows next will give you valid results.

Running the code that works, I get the following. Like Hristo said, a lot gets optimized out if you add the optimization flag (-O3), but it doesn't give you a perfect 0.

$ g++ test.cc -o test.exe -fopenmp
$ ./test.exe
execution time:0.00124655
$ g++ test.cc -o test.exe -fopenmp -O3
./test.exe
execution time:2.16998e-06

Here's the code I used to get meaningful results:

    #include <iostream>
    #include <omp.h>
    
    int main() {
        const int x = 40000;
        const int y = 40000;
        double cellx[x];
        double celly[y];
        
        double itime = omp_get_wtime();
        for (int i=0; i < x; i++) {
            cellx[i]=i*i;
        }
    
        for (int i = 0; i < y; i++) {
            celly[i]=i*i;
        }
        std::cout<<"execution time:"<<omp_get_wtime() - itime<<std::endl;
        return 0;
    }

The code below will give you the value 0:

$ ./test.exe
execution time:0
    #include <iostream>
    #include <iomanip>
    #include <omp.h>
    
    
    int main() {
        const int x = 40000;
        const int y = 40000;
        double cellx[x];
        double celly[y];
        
        double itime = omp_get_wtime();
        for (int i=0; i < x; i++) {
            cellx[i]=i*i;
        }
    
        for (int i = 0; i < y; i++) {
            celly[i]=i*i;
        }
        
        std::cout << std::fixed << std::setprecision(0);
        std::cout<<"execution time:"<<omp_get_wtime() - itime<<std::endl;
        return 0;
    }

You can show more precision by calling something like the following before outputting the value:

std::cout << std::scientific << std::setprecision(6);
Mark H
  • 4,246
  • 3
  • 12
  • There was no problem by default. I had to introduce a potential reason it would happen – Mark H Feb 22 '21 at 20:30
  • After adding std::cout << std::scientific << std::setprecision(6);, I am getting execution time as 0.000000e+00. Don't know where the problem is. – swe Feb 23 '21 at 05:57
  • The only way this is possible is if omp_get_wtime() is returning 0.0 Here's a post mentioning that: https://stackoverflow.com/questions/16777810/c-omp-omp-get-wtime-returning-time-0-00 – Mark H Feb 23 '21 at 07:02
  • I added header also. But still, it is not working. – swe Feb 23 '21 at 07:23
  • Have you tried to find the execution time using and the clock() function or chrono::steady_clock::now()? https://stackoverflow.com/questions/876901/calculating-execution-time-in-c – Mark H Feb 23 '21 at 15:52
  • Can you create a minimum, reproducible example? https://stackoverflow.com/help/minimal-reproducible-example – Mark H Feb 24 '21 at 00:11