什么算作失败?
假设我有一个 C 程序,伪式是:
For i=0 to 10
x++
a=2+x*5
next
FLOP 数是 (1 [x++] + 1 [x*5] + 1 [2+(x+5))] * 10[loop], for 30失败?我很难理解什么是失败。
请注意,[...] 指示我从何处获取“操作”计数。
Say I have a C program that in pseudoish is:
For i=0 to 10
x++
a=2+x*5
next
Is the number of FLOPs for this (1 [x++] + 1 [x*5] + 1 [2+(x+5))] * 10[loop], for 30 FLOPS? I am having trouble understanding what a flop is.
Note the [...] are indicating where I am getting my counts for "operations" from.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
出于 FLOPS 测量的目的,通常仅包括加法和乘法。除法、倒数、平方根和超越函数之类的东西太昂贵而无法包含在单个操作中,而加载和存储之类的东西又太微不足道了。
换句话说,循环体包含 2 个加法和 1 个乘法,因此(假设 x 是浮点)每个循环迭代是 3 次操作;如果你运行循环 10 次,你就完成了 30 次操作。
请注意,测量 MIPS 时,您的循环将超过 3 条指令,因为它还包括 FLOPS 测量不计算在内的加载和存储。
For the purposes of FLOPS measurements, usually only additions and multiplications are included. Things like divisions, reciprocals, square roots, and transcendental functions are too expensive to include as a single operation, while things like loads and stores are too trivial.
In other words, your loop body contains 2 adds and 1 multiply, so (assuming
x
is floating point) each loop iteration is 3 ops; if you run the loop 10 times you've done 30 ops.Note that when measuring MIPS, your loop would be more than 3 instructions because it also includes loads and stores that the FLOPS measurement doesn't count.
FLOPS 代表每秒的浮动操作数。如果您正在处理整数,那么您的代码中没有任何浮点运算。
FLOPS stands for floating operations per second. If you are dealing with integers then you don't have any floating point operations in your code.
海报已经明确表示 FLOPS(详细信息此处)与浮点有关(而不是到整数)每秒操作,因此您不仅要计算正在执行的操作数量,还要计算在什么时间段内执行的操作。
如果“x”和“a”是浮点数,则您正在尝试计算代码中的操作数,但您必须检查目标代码以确保实际使用了多少浮点指令。例如,如果随后不使用“a”,则优化编译器可能不会费心去计算它。
此外,某些浮点运算(例如加法)可能比其他浮点运算(例如乘法)快得多,因此在同一台机器上,仅包含浮点加法的循环可以比仅包含浮点乘法的循环以更高的 FLOPS 运行。
The posters have made it clear that FLOPS (detailed here) are concerned with floating point (as opposed to integer) operations per second, so you not only have to count how many operations you're performing, but in what period of time.
If "x" and "a" are floats, you're making a good attempt at counting the number of operations in your code, but you'd have to check the object code to make sure what quantity of floating point instructions are actually used. Eg, if "a" is not subsequently used, an optimizing compiler might not be bothering to compute it.
Also, some floating operations (such as adding) might be much faster than others (such as multiplying), so a loop of only float adds could run at many more FLOPS than a loop of only float multiplies on the same machine.
FLOP(根据 Martinho Fernandes 的评论,小写 s 表示 FLOP 的复数)指的是机器语言浮点指令,因此这取决于您的代码编译为多少条指令。
首先,如果所有这些变量都是整数,那么这段代码中就不会出现 FLOP。但是,我们假设您的语言将所有这些常量和变量识别为单精度浮点变量(使用单精度可以更轻松地加载常量)。
该代码可以编译为(在 MIPS 上):
因此根据 Gabe 的定义,循环内有 4 个 FLOP(3x
add.s
和 1xmul.s
)。如果您还计算循环比较c.gt.s
,则有 5 次 FLOP。将该值乘以 10,得出程序总共使用 40(或 50)次 FLOP。更好的优化编译器可能会认识到
a
的值不在循环内使用,因此它只需要计算a
的最终值。它可以生成如下所示的代码:在这种情况下,循环内有 2 次加法和 1 次比较(乘以 10 即可获得 20 或 30 次 FLOP),再加上循环外 1 次乘法和 1 次加法。因此,您的程序现在需要 22 或 32 次 FLOP,具体取决于我们是否计算比较。
FLOPs (the lowercase s indicates the plural of FLOP, per Martinho Fernandes comment) are referring to machine language floating point instructions, so it depends how many instructions your code compiles down to.
First off, if all of these variables are integers, then there are no FLOPs in this code. Let's assume, however, that your language recognizes all of these constants and variables as single-precision floating point variables (using single-precision makes loading the constants easier).
This code could compile to (on MIPS):
So according to Gabe's definition, there are 4 FLOPs inside the loop (3x
add.s
and 1xmul.s
). There are 5 FLOPs if you also count the loop comparisionc.gt.s
. Multiply this by 10 for a total of 40 (or 50) FLOPs used by the program.A better optimizing compiler might recognize that the value of
a
isn't used inside the loop, so it only needs to compute the final value ofa
. It could generate code that looks likeIn this case, you have 2 adds and 1 comparision inside the loop (mutiplied by 10 gives you 20 or 30 FLOPs), plus 1 multiplication and 1 addition outside the loop. Thus, your program now takes 22 or 32 FLOPs depending whether we count comparisions.
x 是整数还是浮点变量?
如果它是一个整数,那么您的循环可能不包含任何触发器。
Is x an integer or a floating-point variable?
If it's an integer, then your loop may not contain any flops.