4

I'm trying to optimize a fairly complex C++ project (multiple source files, linked to Boost libraries, GSL and OpenCV) using profiling. Using CMake, I first compile with

set(CMAKE_CXX_FLAGS " -O3 -ffast-math -fprofile-generate=profiling -pg -fopenmp ")

After running the resulting executable with typical inputs, I compile with

set(CMAKE_CXX_FLAGS " -O3 -ffast-math -fprofile-use=profiling -fopenmp ")

Compilation fails with a large number of errors like this:

/n/user/projects/project_name/src/foo.cpp: In member function ‘double TLinearInterp::operator()(double) const’:
/n/user/projects/project_name/src/foo.cpp:86:1: error: corrupted profile info: profile data is not flow-consistent
 }
 ^
/n/user/projects/project_name/src/foo.cpp:86:1: error: corrupted profile info: number of executions for edge 2-7 thought to be -7232
/n/user/projects/project_name/src/foo.cpp:86:1: error: corrupted profile info: number of executions for edge 2-3 thought to be 20996551
/n/user/projects/project_name/src/foo.cpp:86:1: error: corrupted profile info: number of executions for edge 3-7 thought to be -28135
/n/user/projects/project_name/src/foo.cpp:86:1: error: corrupted profile info: number of executions for edge 3-4 thought to be 21024686

I'm using version 4.8.0 of the GNU compilers. As one can see from the compiler flags, my project uses OpenMP.

What could be causing the corruption of the profile info?

apdnu
  • 6,717
  • 5
  • 24
  • 24
  • http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38292 – kfsone Jun 01 '13 at 01:46
  • Should this bug affect every project I try to compile with profiling and -O2/3? I've found that I can successfully profile an extremely simple C++ project (solving Project Euler problem 16) with -03 enabled. – apdnu Jun 01 '13 at 02:11
  • Usually what people mean by successfully profile is they managed to make the profiler generate some output, and since they find nothing in it to improve, they declare success. I'm sure you can do better than that. [*Check here.*](http://stackoverflow.com/a/927773/23771) BTW, I don't turn on -O3 until *after* I've squeezed it dry manually. – Mike Dunlavey Jun 01 '13 at 12:59
  • I'm not talking about generating profiling information to look at. I know how to do that. I'm talking about using automated profile-driven optimization. Basically, if the compiler has profiling information, it can do better than if you just give it the -O3 flag. – apdnu Jun 01 '13 at 18:41

1 Answers1

6

I suspect that multithreading is causing the problem, since you are using -fopenmp.

At a high level, -fprofile-generate causes the compiler to instrument your program with counter increments. These increments are not thread-safe, so your test runs may yield corrupted profiling data.

You can solve this problem by passing -fprofile-correction to the compiler [1]. Alternatively, you could disable -fopenmp/multithreading when attempting PGO. I've had success with the first approach.

[1] https://bugs.launchpad.net/ubuntu/+source/gcc-4.4/+bug/598462

vedantk
  • 356
  • 3
  • 5