3

I have a c++ code which calculates factorial of int data type, addition of float data type and execution time of each function as follows:

long Sample_C:: factorial(int n)
{
    int counter;
    long fact = 1;
    for (int counter = 1; counter <= n; counter++)
    {
        fact = fact * counter;
    }
    Sleep(100);
    return fact;
}

float Sample_C::add(float a, float b)
{

    return a+b;
}

int main(){
    Sample_C object;
    clock_t start = clock();
    object.factorial(6);
    clock_t end = clock();
    double time =(double)(end - start);// finding execution time of factorial()
    cout<< time;
    clock_t starts = clock();
    object.add(1.1,5.5);
    clock_t ends = clock();
    double total_time = (double)(ends -starts);// finding execution time of add()
    cout<< total_time;
    return 0;
}

Now , i want to have the mesure GFLOPs for "add " function. So, kindly suggest how will i caculate it. As, i am completly new to GFLOPs so kindly tell me wether we can have GFLOPs calculated for functions having only foat data types? and also GFLOPs value vary with different functions?

anamika email
  • 327
  • 9
  • 21
  • If you run the `add` operation a million times while checking the time it takes, you should be able to figure out how many operations can be done in one second. However be warned that it's not a FLOPS value you will get, as you will include the time for the function call as well, and not only the `+` operation. – Some programmer dude Jun 18 '15 at 11:57
  • If you are only interested in the `+` operation I would be tempted to write assembly and be very specific about what you are actually calculating. When your C++ compiler comes along it is probably going to optimise out the function call unless you tell it otherwise, but it might also optimise out the add operation too. – ilent2 Jun 18 '15 at 13:27
  • Are you certain you want to estimate GFLOPs for the function add or are you just interested in measuring/estimating the execution time? – ilent2 Jun 18 '15 at 13:37
  • @ilent2 i dont want execution time i simply want to have estimate of GFLops related to add function. – anamika email Jun 18 '15 at 15:20

1 Answers1

0

If I was interested in estimating the execution time of the addition operation I might start with the following program. However, I would still only trust the number this program produced to within a factor of 10 to 100 at best (i.e. I don't really trust the output of this program).

#include <iostream>
#include <ctime>

int main (int argc, char** argv)
{
  // Declare these as volatile so the compiler (hopefully) doesn't
  // optimise them away.
  volatile float a = 1.0;
  volatile float b = 2.0;
  volatile float c;

  // Preform the calculation multiple times to account for a clock()
  // implementation that doesn't have a sufficient timing resolution to
  // measure the execution time of a single addition.
  const int iter = 1000;

  // Estimate the execution time of adding a and b and storing the
  // result in the variable c.
  // Depending on the compiler we might need to count this as 2 additions
  // if we count the loop variable.
  clock_t start = clock();
  for (unsigned i = 0; i < iter; ++i)
  {
    c = a + b;
  }
  clock_t end = clock();

  // Write the time for the user
  std::cout << (end - start) / ((double) CLOCKS_PER_SEC * iter)
      << " seconds" << std::endl;

  return 0;
}

If you knew how your particular architecture was executing this code you could then try and estimate FLOPS from the execution time but the estimate for FLOPS (on this type of operation) probably wouldn't be very accurate.

An improvement to this program might be to replace the for loop with a macro implementation or ensure your compiler expands for loops inline. Otherwise you may also be including the addition operation for the loop index in your measurement.

I think it is likely that the error wouldn't scale linearly with problem size. For example if the operation you were trying to time took 1e9 to 1e15 times longer you might be able to get a decent estimate for GFLOPS. But, unless you know exactly what your compiler and architecture are doing with your code I wouldn't feel confident trying to estimate GFLOPS in a high level language like C++, perhaps assembly might work better (just a hunch).

I'm not saying it can't be done, but for accurate estimates there are a lot of things you might need to consider.

ilent2
  • 5,171
  • 3
  • 21
  • 30