Significance of -Pthread Flag When Compiling

Meaning of g++ flags -Wall -W -Werror

-Wall: enable a set of warning, actually not all.

-W: enable extra warning, it's advised to use -Wextra instead which has the same meaning

-Werror: every warning is treated as an error.

See GCC documentation:
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options

what does the -p and -g flag in compiler

-p provides information for prof, and -pg provides information for gprof.

Let's look at the latter.
Here's an explanation of how gprof works,
but let me condense it here.

When a routine B is compiled with -pg, some code is inserted at the routine's entry point that looks up which routine is calling it, say A.
Then it increments a counter saying that A called B.

Then when the code is executed, two things are happening.
The first is that those counters are being incremented.
The second is that timer interrupts are occurring, and there is a counter for each routine, saying how many of those interrupts happened when the PC was in the routine.

The timer interrupts happen at a certain rate, like 100 times per second.
Then if, for example, 676 interrupts occurred in a routine, you can tell that its "self time" was about 6.76 seconds, spread over all the calls to it.

What the call counts allow you to do is add them up to tell how many times a routine was called, so you can divide that into its total self time to estimate how much self time per call.
Then from that you can start to estimate "cumulative time".
That's the time spent in a routine, plus time spent in the routines that it calls, and so on down to the bottom of the call tree.

This is all interesting technology, from 1982, but if your goal is to find ways to speed up your program, it has a lot of issues.

Why do you have to use both a compiler flag and a run-time flag to get multicore-support in Haskell?

While you're developing the program the extra +RTS ... shouldn't be a big deal (though I admit it struck me as odd when I first picked up Haskell). For the final (shipped) binary you can link it with static RTS options (GHC manual) by providing a C file containing char *ghc_rts_opts = "-N";.

EDIT: Updating this question for GHC 7.x, there is now a way to specify RTS options at compile time:

ghc -threaded -rtsopts -with-rtsopts=-N

This 1) uses the threaded runtime system 2) Enables the RTS options 3) Sets the RTS option to use as many threads as there are cores available (use -Nx where x is a number to manually control the number of OS threads).

What does the fpermissive flag do?

Right from the docs:

-fpermissive

Downgrade some diagnostics about nonconformant code from errors to warnings.
Thus, using -fpermissive will allow some nonconforming code to compile.

Bottom line: don't use it unless you know what you are doing!

what '-DLOCAL' flag does in C++ compilation

one use that I found is compiled with -DLOCAL flag allows us to run a statement like cerr, which is for testing

cerr << "some variable for testing";

if we remove -DLOCAL flag in the compilation, cerr will not be showing any output on the console.

this is helpful even we forget to remove all those testing statements it will not affect the program. because we are not compiling with -DLOCAL in general.

thanks

What exactly is the -xhost flag?

The -xhost flag generates the most optimal code possible, based on the capabilities of your current CPU (that is, the one in the computer you're using to do the compilation).

By "highest instruction set", it means that the compiler will automatically turn on the code-generation flags corresponding to the highest instruction set supported by your CPU. So, if your CPU only supports SSE2, then that's all that will be turned on. If it supports AVX2, then that option will be turned on. Whatever the highest instruction set extension that your CPU supports, the compiler will generate code targeting that instruction set extension.

This option is generally used when you want to build code to run on the same computer where you're building it. For example, when building a scientific algorithm that you'll run on the same computer, or when compiling your own Linux kernel.

Technically speaking, the generated binaries will run on any computer that supports at least the same instruction set extensions as the build computer, which is why the documentation talks about "the highest instruction set available on the compilation host processor".

As Peter Cordes already noted in a comment, ICC's -xhost flag is essentially equivalent to GCC and Clang's -march=native flag. Both of them tell the compiler to automatically turn on all options that match what the host CPU is capable of, generating the most optimal binary possible for the host CPU, but which will run on other CPUs, as long as they have equal or higher capabilities.

You can do exactly the same thing that -xhost is going to do by looking up the specifications for your computer's CPU and adding the corresponding code-gen options to the compiler command line. -xhost just does it for you, looking up what your host CPU supports and enabling those flags automatically, without you having to do the legwork. So, it is a convenience feature; nothing more, nothing less.

The -xhost flag can, indeed, speed up your code by taking advantage of certain instruction set extensions, but it can also result in a binary that won't work at all (on a different computer that doesn't support the same instruction set extensions as your build computer). Maybe that's not a problem for you; in that case, you'd definitely turn on the -host flag. But, in many cases, we software developers are building binaries for other people to run, and in that case, we have to be a bit more careful about exactly which CPUs we want to exclude.

It is also worth noting that Intel's compiler can actually generate a single executable with dynamic dispatching support that allows you to support two different architectures. See Sergey L.'s answer to a related question for more details.

What are the useful GCC flags for C?

Several of the -f code generation options are interesting:

  • -fverbose-asm is useful if you're compiling with -S to examine the assembly output - it adds some informative comments.

  • -finstrument-functions adds code to call user-supplied profiling functions at every function entry and exit point.

  • --coverage instruments the branches and calls in the program and creates a coverage notes file, so that when the program is run coverage data is produced that can be formatted by the gcov program to help analysing test coverage.

  • -fsanitize={address,thread,undefined} enables the AddressSanitizer, ThreadSanitizer and UndefinedBehaviorSanitizer code sanitizers respectively. These instrument the program to check for various sorts of errors at runtime.

Previously this answer also mentioned -ftrapv, however this functionality has been superseded by -fsanitize=signed-integer-overflow which is one of the sanitizers enabled by -fsanitize=undefined.



Related Topics



Leave a reply



Submit