GCC Optimization Options

06/09/2013 § Leave a comment


I’ve been really curious about optimization options with GCC and how different options affected my code so I spent the better part of the weekend working with a simple mandelbrot projection displayer written in C. It projects the classic graphic representation of the set in an X window. Its a great test case as it uses floating point math, calculations with complex numbers, and OpenGL, so it sucks up some pretty heavy duty cpu cycles to draw the projection. The test case is here if you’re interested in benchmarking your own system similarly. Start it from a shell and after you close the display window the execution time will be displayed in seconds.

First, as a starting point I compiled the program without any options; the projection appeared on my desktop in about 15 seconds. Then I added an option I knew would make a difference, the “arch” option. The current form of the flag is “-march=”, this tells the compiler to target the specified cpu architecture. I knew I had a corei5 cpu, but to make sure of this I issued an “lscpu” in a shell and got this:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 42
Stepping: 7
CPU MHz: 800.000
BogoMIPS: 3192.71
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 3072K
NUMA node0 CPU(s): 0-3

Ok, good to know, but not what I was looking for- this is all stuff I know. Fortunately in my bag of tricks I have this: gcc -c -Q -march=native --help=target | grep march. On my machine the grep returns this: -march= corei7-avx
Thoughtful of gcc to provide exactly what I need to pass to it. So, plugging in my compile command: gcc -Wall -lGL -lglut -lGLU -march=corei7-avx mand.c -o mand I got the execution time down to 13 seconds. Better, but… can we do better? Absolutely. We haven’t even told the compiler to optimize the code in its own way yet. The “-O” flag is the flag I’m talking about. According to the docs this flag takes a numeric parameter and a few alpha ones. The numeric params tell the compiler what “level” to optimize to, with the levels being 0 through 3, with 3 being the most.

So plugging “-O3” into my command line I see the execution speed up to a little over 9 seconds. Pretty good. But is that the best? I see an alphabetic option to “-O”, “fast”, for fast math. That looks promising. Plugging it in I get an execution time of 10+ seconds. What? Not even helpful. Ok, I see one option left; “-Os”, or “optimize for size.” Ok, let’s check it out. I plug it in and notice a full 1 second performance boost over the command that only employed the arch option. Wow, quite a difference. It seems the less code this particular application uses means less for the compiler to do, and hence less execution time. Makes sense, but I really didn’t think it would make that much a difference. Wrong!

Ok, I have one last collection of options I’d like to try; everything. Or at least everything suggested by a gcc grep I found the other day: echo "" | gcc -march=native -v -E - 2>&1 | grep cc1. This appears to actually be all the options supported by the compiler on the current platform, I really don’t think its a suggestion of what I should be using as appeared to be worded by the blogger who wrote it. And I’m right, using the string returned by this grep in my compile invocation the program runs as slowly as it did without any optimizations.

I played with many more options and variations on options than I have written about here, including the “mtune” option, which should be the same as whatever you’re passing to “march”, and it made no difference. Another option that didn’t seem to make a difference surprisingly was “-funroll-loops”, surprisingly to me because there are a number of loops in the program, especially in the init phase. Ultimately you need to use timing in your code and common sense in your brain to get the most out of gcc, like anything else.


Yeah, that’s right.

PS: I shaved an entire second off the runtime by simple declaring my constants.

Russell Brand is a Skinny Little Idiot

06/08/2013 § Leave a comment

Brand in a police lineup.

Brand in a police lineup.

I liked Russell Brand, I liked is humor and his comedic timing is impeccable. He was also married to one of the hottest pop divas to come along since Patsy Cline (what the hell happened to that situation Russell?) But like so many pop icons before him he had to open his mouth and completely destroy his very marketable image.

No, we shouldn’t blame a group of people even though they continually kill our own. Wait- let’s analyze that statement: Using a simple anology; If Sally slugs you everytime you offer to give her a kiss, you need to stop offering Sally a kiss, yes? Ok. What if Sally slugs you everytime she walks by your house, though- you have a problem. Ok, say, Sally slugs you several times while your waiting around for the bus, and everytime she does Billy, your brother, says she did it becuase Spike, your friend, went over to HER house to steal her bike? You may or may not have sent Spike over there to do that. In fact, your parents may have, and they may have been wrong. Never the less, Sally swears to kill you for past wrongs YOU’VE done to her. You, however, have done nothing more than go to school every day. Spike, when confronted regarding past aggression against Sally says he did it for you. You’ve not asked for his support in anyway. Further, Spike delivers your family’s news paper, and your family pays him accordingly. Sally now uses that fact against you. In fact, Sally engages in a covert campaign to discredit you in every way possible to all your friends, teachers, and people your parents do business with.

Then, one dark day, Sally gets hold of a gun and shoots your mother dead, then goes into hiding. And occasionally, from time to time, people from Sally’s family do things to you, poison your dog, flatten your tires, generally fuck with you. Then Russell Brand says “Don’t blame Sally’s family.”

Really, Russell? You have nothing better to do than get on the media and say something controversial? That is entirely possible. Its easy for some one whose been in the media eye before but currently sort of on the “back warmer” to do just such a thing to get back in the public eye. Was Despical Me II the last thing on your plate last month?

Just like Viet Nam before it, America’s position in the middle east is like a ham-fisted puddin’ head stumbling about in a quagmire without end surrounded by quick-footed little stinging wasps with a mastery of PR. Its as if Hanoi’s PR playbook was given to Al Queda and they are following it verbatim. What if, and I’m just musing here; but WHAT IF the cultural bias toward Islam in the popular western media was orchestrated by them? As already mentioned, its not like its never been accomplished before. What if popular idiots like Brand are compliciate and willing tools to Al Queda’s program?

Religion is a powerful tool for human conditioning. Islam is also a dynamic religion; it asks its adherents to strongly commit to its precepts and moires. Christianty certainly has done as much as well, but Islam is a newer religion, and its militant philosophy with regard to the Shia/Sunni split is much more apprent than the orthadox schism of Christianity in 1054 A.D. Just like Jesus Muhammad is attributed to many miracles and wonders. Of course proof of these accounts is only found in the Quran, just as Jesus’ miracles are really only recorded in the Bible, once again, as many times before and in the future, making religious claims both proofless and irrefutable. Christianity is certainly not without its attrocities, to be sure. But its been a while since the last crusade. Who will now paint Islam in the light in which it deserves lately? Certainly not Russell Brand.

Brand is an Islamic appologist becuase our culture demands it, not due to any objective or critical look at the current state of Islam or Western Pop Culture. The former becuase its in vogue, and the latter bcuase it pays his bills. What if both were WRONG? Would make Brand and his ilk look pretty damn foolish, wouldn’t it? To be imperically correct (and that means absolutely correct, not correct as far as we’re concerned) we need to apply the same standard to all measures. Has Brand or his buddies every been apologists for Christianity? Or Hinduism? or Buddhism? Not as far as I know.

Where Am I?

You are currently viewing the archives for June, 2013 at Twittech Conditional Behavior Modification.