Search code examples
c++console-applicationflops

C++ calculate GFlops


I recently trying to make a simple program that calculates FLOPS. Since c++ is fast enough so I think that worth a try to have close result.
When I compile it with Notepad++ plugins, NppExec, it works fine, but i doesn't build it. When i build and run in CodeBlocks, it keep iterating and won't finish the process. So I go back to notepad++ and compile it again, then this time when I run it works fine, the iteration just elapsed a sec.

 #include<iostream>
 #include<conio.h>
 #include<ctime>
 #include<iomanip>

 using namespace std;

 int main(){

     float a=1.0,b=2.0,var,j;
     double flop;
     clock_t start,end;

     cout<<"\n Iterating...";

     start=clock();

     for(j=0;j<999999999;j++){    // Iterates 999999999 times
         var=a*b+a/b;             // <-- 5 Flops, or am I wrong?
     }

     end=clock();

     cout<<"\n\n Calculating...";

     double secs=((float)(end-start))/CLOCKS_PER_SEC;

     flop=999999;   // Actually is 999999999, but integer overflow in expression
     flop=5*(flop*1000+999);   // In the brackets I make the value to same as 999999999
                               // Multiply with 5 and basically get Flops here
     flop/=secs;    // To get the Flops in second, multiply with time elapsed

     string prefix,fstr;

     if(flop/1000000000>=1||flop/1000000000<1){
         flop/=1000000000;
         prefix="GFLOPS";
     }

     else if(flop/1000000000000>=1){
         flop/=1000000000000;
         prefix="TFLOPS";
     }

     cout<<"\n\n\n Floating-points Operations Per Second\n\n > "<<setprecision(3)<<flop<<" "<<prefix;
     getch();
     return 0;
  }

If you know how to make the result more precise, go ahead, any answer will be appreciated!


Solution

  • There are many issues with this code. First, you are using a float variable (j) to maintain the counter of the loop with strict termination condition j<999999999. This is probably the reason why the loop may run forever. The type of j should be an integral type such as int.

    Second, the number of flops in the loop depends on the compiler you are using, the compiler options you are passing to the compiler and the target architecture. The best way to figure this out is to see the generated assembly code.

    Third, the first call to clock and the second call to clock might be reordered due to compiler optimizations rendering the results invalid. You'll have to make sure that they have not been reordered by looking at the assembly code. The way to ensure this depends on the compiler.

    Fourth, what does this mean?

    flop=999999;   // Actually is 999999999, but integer overflow in expression
    

    Is the compiler telling you that 999999999 results in an overflow? If yes then how are you using it in the loop termination condition? What exactly is the error?

    Fifth, this

    if(flop/1000000000>=1||flop/1000000000<1){
    

    should probably be like this

    if(flop/1000000000>=1){
    

    Sixth, the whole loop might be optimized away by the compiler because you are not using var after the loop. You should print the value of var at the end so this does not happen.

    Seventh, the expression a*b+a/b has a constant value. So practically, the same value is assigned to var every iteration. The compiler might optimize this to a single constant assignment. In this case, zero flops will be emitted.

    Eighth, the comment here should say divide not multiply.

    flop/=secs;    // To get the Flops in second, multiply with time elapsed
    

    Ninth, this condition if(flop/1000000000000>=1) should come before this condition if(flop/1000000000>=1)

    Tenth, in this line of code, it's called Floating-point

    cout<<"\n\n\n Floating-points Operations Per Second\n\n
    

    Eleventh, this number 999999999 should be defined as a constant at the beginning of the function so that it's easier to change.

    Finally, the method you used to compute the running time is the simplest and is good enough in many cases. But there are far more complex methods that are far more accurate.