2

I am having a hard time grasping how to count FLOPs. One moment I think I get it, and the next it makes no sense to me. Some help explaining this would greatly be appreciated. I have looked at all other posts about this topic and none have completely explained in a programming language I am familiar with (I know some MATLAB and FORTRAN).

Here is an example, from one of my books, of what I am trying to do.

For the following piece of code, the total number of flops can be written as (n*(n-1)/2)+(n*(n+1)/2) which is equivalent to n^2 + O(n).

[m,n]=size(A)
nb=n+1;
Aug=[A b];
x=zeros(n,1);
x(n)=Aug(n,nb)/Aug(n,n);
for i=n-1:-1:1
    x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i);
end

I am trying to apply the same principle above to find the total number of FLOPs as a function of the number of equations n in the following code (MATLAB).

% e = subdiagonal vector
% f = diagonal vector
% g = superdiagonal vector
% r = right hand side vector
% x = solution vector

n=length(f);

% forward elimination
for k = 2:n
    factor = e(k)/f(k­‐1);
    f(k) = f(k) – factor*g(k‐1);
    r(k) = r(k) – factor*r(k‐1);
end

% back substitution
x(n) = r(n)/f(n);
for k = n‐1:­‐1:1
    x(k) = (r(k)‐g(k)*x(k+1))/f(k);
end
iled
  • 2,142
  • 3
  • 31
  • 43
user1757273
  • 71
  • 1
  • 2
  • 9
  • possible duplicate of [MATLAB/octave count number of operations (flops)](http://stackoverflow.com/questions/2868429/matlab-octave-count-number-of-operations-flops) – Eitan T Mar 27 '13 at 22:46
  • no its not. i need to understand how to determine the total number of flops as a function of the number of equations "n". i dont want a code or something to determine it for me. – user1757273 Mar 27 '13 at 22:50
  • First of all, you didn't provide that information in the question. Second, you have a problem in your first loop: the iteration variable should be `k`, not `x`. Did you run this code? – Eitan T Mar 27 '13 at 22:55
  • yes, it was an error when posting it. and i fixed my question. no need to get all defensive. – user1757273 Mar 27 '13 at 23:02
  • Your question is very vague and the answer that you're looking for seems unclear. Are you asking how to count FLOPs? Are you looking for an analytical expression which is a function of _n_? Would a graphical visualization suffice? – Eitan T Mar 27 '13 at 23:08
  • I am honestly not familiar with the entire concept but in general i am looking for a function of the number of flops with respect to n. does that help? – user1757273 Mar 27 '13 at 23:12

1 Answers1

2

I'm by no means expert at MATLAB but I'll have a go.

I notice that none of the lines of your code index ranges of your vectors. Good, that means that every operation I see before me is involving a single pair of numbers. So I think the first loop is 5 FLOPS per iteration, and the second is 3 per iteration. And then there's that single operation in the middle.

However, MATLAB stores everything by default as a double. So the loop variable k is itself being operated on once per loop and then every time an index is calculated from it. So that's an extra 4 for the first loop and 2 for the second.

But wait - the first loop has 'k-1' twice, so in theory one could optimise that a bit by calculating and storing that, reducing the number of FLOPs by one per iteration. The MATLAB interpreter is probably able to spot that sort of optimisation for itself. And for all I know it can work out that k could in fact be an integer and everything is still okay.

So the answer to your question is that it depends. Do you want to know the number of FLOPs the CPU does, or the minimum number expressed in your code (ie the number of operations on your vectors alone), or the strict number of FLOPs that MATLAB would perform if it did no optimisation at all? MATLAB used to have a flops() function to count this sort of thing, but it's not there anymore. I'm not an expert in MATLAB by any means, but I suspect that flops() has gone because the interpreter has gotten too clever and does a lot of optimisation.

I'm slightly curious to know why you wish to know. I used to use flops() to count how many operations a piece of maths did as a crude way of estimating how much computing grunt I'd need to make it work in real time written in C.

Nowadays I look at the primitives themselves (eg there's a 1k complex FFT, that'll be 7us on that CPU according to the library datasheet, there's a 2k vector multiply, that'll be 2.5us, etc). It gets a bit tricky because one has to consider cache speeds, data set sizes, etc. The maths libraries (eg fftw) themselves are effectively opaque so that's all one can do.

So if you're counting the FLOPs for that reason you'll probably not get a very good answer.

bazza
  • 7,580
  • 15
  • 22
  • so if my code had indexed ranges of the vectors inside the loop, it would be a much more complex problem right? since it no longer be a single set of numbers with an operation done on them, it would be the entire index of numbers with the operation and then the loop iterations on top of that? And i am supposed to learn this concept for a class i am taking, the professor isnt very helpful so i needed some extra advice. – user1757273 Mar 28 '13 at 13:59
  • 1
    Yes, that's about the size of it. Imagine doing it all by hand with a calculator. Every time you hit +, -, * or / you're doing a FLOP. So for example, y=8*x(1:10) would boil down to multiplying each of the 10 elements in x by 8. That'd be 10 FLOPs. But as I hinted about the number of operations apparent in the code doesn't mean that MATLAB actually performs that many; optimisation is all about reducing the work done but ending up with exactly the same results. MATLAB was/is veeeery slow, so I think Mathworks put some effort in to optimise code on the fly. – bazza Mar 28 '13 at 21:03