Same program, different performance on mac and windows

Status
Not open for further replies.

741

Seniorius Lurkius
48
Dear Ars,

I have number-crunching program that processes data generated on our experimental setups. It's programmed in C++, and makes use of the GNU Scientific Library, FFTW, boost::threads, and libtiff. It's portable and compiles on Mac and Windows.

Oddly the performance on OS X and Windows differs strongly on the same hardware. For example, analyzing a particular piece of data takes 23 s on OS X (compiled with g++ 4.2.1) and 1 min 5 s on Windows (compiled with Visual Studio 2008). This is on a dual-booting Macbook Pro. Optimization is on, settings are -O3 on mac and 'fastest code' in Visual Studio. The code is absolutely identical in both cases.

The threefold performance difference annoys me. Since my code is the same on both platforms I'm assuming that the slowdown is in the libraries I'm calling. In particular, for the GSL and FFTW I'm using precompiled binaries as I'm too unfamiliar with Windows to compile them myself. Libtiff is not involved in this particular example, so I can probably eliminate that one (and I have managed to compile that one myself).

I'd love to hear your thoughts on how I can bring Windows up to par. Even if my reasoning is fine then I don't have a clue as to how I could demonstrate which library (or both) is causing the performance slowdown (yes, I do realize that I could write a few test programs to compare the performance of each library separately, but I'm hoping that there is some clever way I haven't discovered yet). Then again, I might be entirely wrong about this.


Edit for improved clarity:
the code in question is pure number crunching, and doesn't include any interface code or anything. It basically starts, crunches, and terminates.
 

741

Seniorius Lurkius
48
I can profile on mac using Shark (app bundled with the developer tools), but I don't have the visual studio version that includes the profiler, and am stuck on XP. A quick google search turns up a few profilers that are pretty expensive (for my needs), but I'll download a trial version tomorrow and give it a go.

Unfortunately, I don't even know what math library I'm using and how I would check if they're vectorized (on mac or windows).
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
I'd guess memory allocation issues, but it's just a guess. You need a profiler to isolate. One crude way to verify would be to run on Linux as well, and see what happens there. Another possibility is something with the threading, but I don't know what it could be.

I seriously doubt it's anything about compilation, save for maybe vectorization and possibly debug mode issues (but doubtful). If anything, GSL and FFTW would be better optimized by MS' compiler over GCC, properly configured. Anyway, a trivial way to isolate is may be to install mingw and compile with that.
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
741":1txuh05u said:
I can profile on mac using Shark (app bundled with the developer tools), but I don't have the visual studio version that includes the profiler, and am stuck on XP. A quick google search turns up a few profilers that are pretty expensive (for my needs), but I'll download a trial version tomorrow and give it a go.

Unfortunately, I don't even know what math library I'm using and how I would check if they're vectorized (on mac or windows).
The Visual Studio profiler is free. The UI for making pretty graphs from the output isn't.

I can't find the download link now (I posted it before in this forum, but search isn't finding it).

This might be of help though: http://msdn.microsoft.com/en-us/library/bb385771.aspx

You also could try Very Sleepy: http://www.codersnotes.com/sleepy
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
Here's a wild guess: The OS X versions of your math libs are vectorized, and the Windows versions aren't?
Or one is x86 and the other is x64...

VS profiler download link: http://www.microsoft.com/downloads/deta ... laylang=en

If it's easy to do, it wouldn't hurt to try compiling your Windows binaries with MingGW and see if that brings things closer in line with g++.
 

fitten

Ars Legatus Legionis
55,636
Subscriptor++
LordHunter317":36a2cm39 said:
If anything, GSL and FFTW would be better optimized by MS' compiler over GCC, properly configured.

Yeah, 'back in the day' I ran FFTW using gcc (Linux, PPro200) and MSVC++ (Windows, Pentium-133) and the Windows version blew away the gcc one. Of course, they've put a lot of work into gcc since then ;)
 

kuya

Ars Scholae Palatinae
881
wow 3x difference on the same hardware makes this interesting.

my random guess would be memory allocation/threading. one quick thing to try would be to use the intel tbb scalable memory allocator (should be a single line include and an extra line in your makefile), overriding the default malloc. if your threads are doing lots of allocations (maybe std::vector push_backs or something) and using the default allocator, then it could be a bottleneck.

see:
Automically Replacing malloc and Other C/C++ Functions for Dynamic Memory Allocation
in
http://www.threadingbuildingblocks.org/ ... torial.pdf
 

741

Seniorius Lurkius
48
Thanks for the replies, I really appreciate it.

I verfied the performance on Linux. The hardware is different, but the performance is essentially the same (Macbook Pro on OS X 23 s, Linux 28 s). Seems reasonable as the Linux PC is older.

Or one is x86 and the other is x64...
Mac and Windows are x86, Linux is x64.

unlikely, but make sure you're compiling in release, not debug mode in Windows.
I'm using release mode.


I'll try profiling on Windows using the Visual Studio or Very Sleepy profiler. I've got some other things to do today, but I hope to get back on this tonight (European time).

As stated before, I didn't compile the GSL and FFTW myself, as I'm not sure of how to do it on Windows. For FFTW I'm using the binary made available here, for the GSL it's the binary available here.
 
audioSE":1z9oahnu said:
The OS X versions of your math libs are vectorized, and the Windows versions aren't?

This is the most likely reason. GCC and VisualC++ have different vectorization hints and my guess is that the person porting it didn't bother to port the vectorization hints or did it poorly.

Try compiling under Cygwin with the gsl from there and see if there is a difference.
 

741

Seniorius Lurkius
48
An update: I succeeded in cross-compiling the GSL on Linux using this, which made it rather easy. Linking in the cross-compiled libraries, the running time on windows is approximately halved to 36 s. Still about 10 seconds slower than the mac version.

No profiling yet, didn't have time to investigate.

I wonder if the remaining difference might be due to the Apple-supplied BLAS library might be more efficient (vectorized?) compared to the gslcblas library included with the GSL, which I'm using on Windows (and was now cross-compiled using MinGW as well). Any recommendations for a fast BLAS library for Windows I could substitute?
 

job

Ars Tribunus Militum
1,569
741":1ppc78fu said:
I wonder if the remaining difference might be due to the Apple-supplied BLAS library might be more efficient (vectorized?) compared to the gslcblas library included with the GSL, which I'm using on Windows (and was now cross-compiled using MinGW as well).

Maybe. Profile and see. IIRC the gslcblas is more or less a C version of the basic netlib BLAS. I.e. no cache blocking, so performance for bigger arrays will be atrocious.

Any recommendations for a fast BLAS library for Windows I could substitute?

Maybe GOTO, MKL, or ATLAS.
 
Are you using STL containers?

In a decision of astonishing arrogance and bone-headedness, even in Release mode, VS 2008 will compile and link your code to use bounds-checking iterators and collections, unless you explicitly tell it not to, by (for example) using a #define _SECURE_SCL 0 before including any standard headers or STL-using code.

The bounds-checked STL containers are not link-compatible with the unchecked ones, and consequently this decision has the broader consequence of making linking with third-party STL-using libraries problematic, because for compatibility reasons, they are more or less obliged to provide versions with checked containers by default. Worse, given a binary library, there is no way to tell if it was compiled with _SECURE_SCL enabled or not, so in practice, you're pretty much obliged to compile them from source if you need to interact with them through STL containers or indeed, expect them to have a reasonable level of performance if they use the STL internally.

An alternative is to use a different compiler, such as gcc, even under Windows.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
In a decision of astonishing arrogance and bone-headedness

Interesting point of view.
They take a perf hit for a security improvement*
Given the flak they were taking on being unsecure is this such a bad decision?

* one assumes, if it does not significantly improve bugs from "Own you" -> "DOS" then obviously it was a waste of time
 
ShuggyCoUk":2ev3g1li said:
In a decision of astonishing arrogance and bone-headedness
Interesting point of view.
They take a perf hit for a security improvement*
Given the flak they were taking on being unsecure is this such a bad decision?
There's nothing wrong with allowing more secure code. On the contrary, this is a good thing! But the choice of implementation is horrible. C++ is all about opt-in features — if you don't use something, it shouldn't cause a performance hit. So while the choice to make it on by default in Debug builds might be justifiable, in Release builds it really is counter to the philosophy. They could have put the checked collections in a separate namespace, or made it link incompatible with the unchecked versions (thereby avoiding the third party library issues), but they chose to make the security/speed trade off for developers unilaterally and in a way that makes the efficient version hard.

That's why I think it is boneheaded and arrogant.
 
audioSE":2qlmy891 said:
HalfFlat: Did not know that. Could you link me to some documentation on it? I imagine it's fascinating to read the reasoning behind it.
Some links from the VC++ Team blog:
http://blogs.msdn.com/vcblog/archive/2007/08/10/the-future-of-the-c-language.aspx
http://blogs.msdn.com/vcblog/archive/2007/02/26/stl-destructor-of-bugs.aspx
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
, but they chose to make the security/speed trade off for developers unilaterally
How else do you make things more secure?
Serious question. The people for whom this was a problem likely didn't realise so simply saying, 'hey you can flip this toggle to get it' is unlikely to yield significant results.

How much code is still out there using things like scanf?

The idea that it can be secure in Debug but not Release seems silly, surely you want it secure in Release!

and in a way that makes the efficient version hard.
The pain there I can understand, could they have achieved something similar (bounds checked by default) without the complexities associated with disabling it?
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
HalfFlat":f3ii753d said:
C++ is all about opt-in features — if you don't use something, it shouldn't cause a performance hit. So while the choice to make it on by default in Debug builds might be justifiable, in Release builds it really is counter to the philosophy.
No, this doesn't follow, and C++ isn't about opt-in features in this sense anyway. If it were, then there'd be a hard requirement for vector iterators to be pointers; the lack of such a requirement should tell that your thinking on this matter is misguided.

The linking issues are vastly more problematic, but only to a certain degree, C++ frequently requires the source anyway (e.g., templates) so it's less of a huge deal, though definitely a pain. It's a bigger problem on Linux and UNIX, though this is little comfort.

but they chose to make the security/speed trade off for developers unilaterally and in a way that makes the efficient version hard.
Developers have shown time and time again they cannot make the security/speed trade off correctly, and that they cannot write correct, secure code without these aids. As such, I think making disabling the bounds checking very difficult is not a bad thing in the least. Besides, for vector you can still get at the underlying storage, so the security is only skin deep anyway (not that this detracts from its utility).
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
ShuggyCoUk":2c9y6nr9 said:
The pain there I can understand, could they have achieved something similar (bounds checked by default) without the complexities associated with disabling it?
Keeping link-compatibility between the checked and unchecked collections/iterators is certainly possible. Doing it without incurring a performance hit in the unchecked case is a bit harder, since the most straightforward way to do it is pImpl. Plus, that means you could end up with some code that runs unchecked and some code that runs checked iterators, which isn't exactly what you want if you're going after performance. As such, you'd end up having to recompile anyway. While the current situation is definitely frustrating, I'm not sure the alternatives are dramatically less so.
 

741

Seniorius Lurkius
48
Are you using STL containers?
I'm using the STL containers, but in the performance-sensitive areas the data is stored in Boost::ublas matrices. Bounds checking etc. should be disabled for the matrices (as far as Visual Studio allows, I presume) because NDEBUG is defined.

I seem to have had some success in getting profiles using the Visual Studio commandline tool and Very Sleepy. I'm attaching those below. The GSL is linked in statically, FFTW is a dll. I don't know how to interpret them.

However, some more background information: the program is not standalone command line tool, but rather is an extension for the Igor Pro analysis software. However, as far as I can tell Igor simply passes control off to my code and remains blocked until it returns, which is why I didn't mention it before. I'm not aware of any interactions between Igor and my code between start and return. However, because of this there are some references to Igor below. I apologize if the fact that this code runs as an extension is important.

In the profiles below I've replaced the name of the extension with <my extension>

Very Sleepy
Name %Exclusive %Inclusive Module
RtlFreeHeap 0.000 93.678 ntdll
main 63.793 92.529 <my extension>
endthreadex 0.000 26.628 MSVCR90
GetModuleFileNameA 0.000 26.628 kernel32
[07710df9] 0.000 26.628 <my extension>
RtlpNtMakeTemporaryKey 6.705 26.533 ntdll
LdrAlternateResourcesEnabled 0.766 26.533 ntdll
RtlDosSearchPath_Ustr 0.000 21.169 ntdll
[0779478] 0.000 20.690 <my extension>
[07702e2e] 0.000 20.690 <my extension>
KiFastSystemCallRet 17.912 17.912 ntdll
malloc 0.096 17.816 MSVCR90
free 0.000 13.218 MSVCR90
operator new 0.000 13.218 MSVCR90
RtlEnterCriticalSection 0.096 7.375 ntdll
fftw_plan_guru64_split_dft 5.747 5.747 libfftw3-3
RtlOemStringToUnicodeString 0.000 13.218 MSVCR90

Visual Studio FunctionSummary
Function Name Inclusive Samples Exclusive Samples Inclusive Samples % Exclusive Samples %
_threadstartex 11222 0 99.99 0
[<my extension>] 11008 9120 98.08 81.26
Unknown Frame(s) 8449 0 75.28 0
_callthreadstartex 2773 0 24.71 0
[libfftw3-3.dll] 1377 1109 12.27 9.88
[ntdll.dll] 899 899 8.01 8.01
_malloc 560 0 4.99 0
operator new(unsigned int) 356 1 3.17 0.01
[msvcrt.dll] 268 0 2.39 0
__hypot 82 2 0.73 0.02
_hypothlp 80 15 0.71 0.13
_free 62 0 0.55 0
__ctrlfp 19 19 0.17 0.17
__decomp 19 7 0.17 0.06
__set_exp 19 19 0.17 0.17
__startOneArgErrorHandling 13 7 0.12 0.06
__87except 12 2 0.11 0.02
__math_exit 12 0 0.11 0
__CIsqrt 10 5 0.09 0.04
__handle_exc 6 0 0.05 0
__set_statfp 6 6 0.05 0.05
__load_CW 4 4 0.04 0.04
__get_exp 2 2 0.02 0.02
[Igor.exe] 1 0 0.01 0
[kernel32.dll] 1 0 0.01 0
__ceil_pentium4 1 1 0.01 0.01
__fload_withFB 1 1 0.01 0.01
__SEH_epilog4 1 1 0.01 0.01
__SEH_prolog4 1 1 0.01 0.01
__VEC_memzero 1 0 0.01 0
_floor 1 1 0.01 0.01
fastzero_I 1 1 0.01 0.01


Visual Studio Module Summary
Name Inclusive Samples Exclusive Samples Inclusive Samples % Exclusive Samples % Module Name
MSVCR90.dll 11222 95 99.99 0.85 MSVCR90.dll
<my extension> 11008 9120 98.08 81.26 <my extension>
UNKNOWN 8449 0 75.28 0 UNKNOWN
libfftw3-3.dll 1377 1109 12.27 9.88 libfftw3-3.dll
ntdll.dll 899 899 8.01 8.01 ntdll.dll
msvcrt.dll 268 0 2.39 0 msvcrt.dll
Igor.exe 1 0 0.01 0 Igor.exe
kernel32.dll 1 0 0.01 0 kernel32.dll
 
ShuggyCoUk":hqb94f2o said:
The idea that it can be secure in Debug but not Release seems silly, surely you want it secure in Release!
It's more of a case of different behaviour on bugs. Compiling with checked containers doesn't make the code secure, or not buggy ­(I know this sounds trite, but I don't mean to be patronizing) — it just means that a certain class of programmer error, when exercised, throws an exception instead of some other, underdefined form of behaviour. For many classes of application, I imagine it is better to throw this exception than to muddle along, even at the cost of performance. For some, it really isn't. The choice should be in the hands of the developer.

There is an unintended deleterious consequence of the secure-but-slower default, and that is in performance-critical code, a programmer may choose a completely unchecked, more complex and more error-prone approach than the more easily reasoned approach because the latter is observably slower. It's not hypothetical; I have seen this first hand. Because 'vectors are slow', this particular code was changed to use dynamically allocated chunks of memory explicitly, and there were concomitant memory leaks and unsafe (post-deallocation) access. I hope this is not a common response, but I fear it is not.
 

mert

Ars Tribunus Militum
2,088
Subscriptor
LordHunter317":12k52o96 said:
...
Developers have shown time and time again they cannot make the security/speed trade off correctly, and that they cannot write correct, secure code without these aids. As such, I think making disabling the bounds checking very difficult is not a bad thing in the least.
...
Another argument for more use of Ada(2005) ;)
(Of course, Ada also makes it easy to disable bounds checking, but the gain in performance, at least in my experience, has been very little with modern Ada compilers since they try really hard to only insert very minimal amount of checks to get the job done.)
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
It's more of a case of different behaviour on bugs. Compiling with checked containers doesn't make the code secure, or not buggy ­(I know this sounds trite, but I don't mean to be patronizing)
You may not mean to be but you are I'm afraid.

Checked containers which convert out of bounds read or writes to terminal exceptions are indeed _more_ secure than those which allow them to simply occur. Both are buggy it's just that one has well defined semantics and 'payload' which is a crash, the other is not and has the potential to be exploitable to either leak information (worse) or to allow unintended code execution (much worse) as well as the ability to crash.

You argument would be analogous to saying Memory Protection doesn't make an OS more stable because you can still have code try to do it...

— it just means that a certain class of programmer error, when exercised, throws an exception instead of some other, underdefined form of behaviour. For many classes of application, I imagine it is better to throw this exception than to muddle along, even at the cost of performance. For some, it really isn't. The choice should be in the hands of the developer.

I don't understand why you keep saying it's out of your control. You do have the choice, you can switch it off, it's just a pain to do.
If your complaint is that they changed the _default_ then I'm afraid that major upgrades always have possible issues like this to contend with and need to be looked at for performance regressions.

Plus if you think it's okay for any app to "just muddle along" if it reads/writes to memory it did not intend to then I'm afraid arguments based on that position will be largely ignored (if not outright denounced as wrong by others). An example of such an application would be instructive as to where you're coming from might help (and neither nuclear reactor nor pacemaker is going to cut it).

There is an unintended deleterious consequence of the secure-but-slower default, and that is in performance-critical code, a programmer may choose a completely unchecked, more complex and more error-prone approach than the more easily reasoned approach because the latter is observably slower. It's not hypothetical; I have seen this first hand.
If someone took something correct, and makes it faster by making it wrong I don't have a lot of sympathy for their position. Especially when they could instead have either: used the compiler directive that disabled it and fixed accordingly, downloaded and used an alternative STL without the checking.

If they failed to understand this, and are in a position where they make critical design decisions for performance without understanding the tradeoffs involved then they're being given too much rope and are hanging themselves. A shame but ultimately I believe this is supportive of the argument for the change rather than against: "Most people can't get it right themselves so we should guard against it. If you think you can get it right feel free to jump out of the box and on your own head be it".
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
HalfFlat":2g5uh8gs said:
ShuggyCoUk":2g5uh8gs said:
The idea that it can be secure in Debug but not Release seems silly, surely you want it secure in Release!
It's more of a case of different behaviour on bugs. Compiling with checked containers doesn't make the code secure, or not buggy ­(I know this sounds trite, but I don't mean to be patronizing) — it just means that a certain class of programmer error, when exercised, throws an exception instead of some other, underdefined form of behaviour.
Throwing an exception is infinitely better than allowing a buffer overflow or invalid deference and unquestionably makes the code more secure.

For many classes of application, I imagine it is better to throw this exception than to muddle along, even at the cost of performance. For some, it really isn't. The choice should be in the hands of the developer.
The cases where it isn't are few and far between. It's a minority case, making minority cases difficult at the expense of doing the right thing in the common case is not a bad thing in least.

There is an unintended deleterious consequence of the secure-but-slower default, and that is in performance-critical code, a programmer may choose a completely unchecked, more complex and more error-prone approach than the more easily reasoned approach because the latter is observably slower. It's not hypothetical; I have seen this first hand. Because 'vectors are slow', this particular code was changed to use dynamically allocated chunks of memory explicitly, and there were concomitant memory leaks and unsafe (post-deallocation) access. I hope this is not a common response, but I fear it is not.
I have no clue whether it is or isn't, but it's much easier to audit a codebase for, "Using C-pointers and arrays" then it is for "using iterators correctly".
 
ShuggyCoUk":1dx7kwht said:
I don't understand why you keep saying it's out of your control. You do have the choice, you can switch it off, it's just a pain to do.
If your complaint is that they changed the _default_ then I'm afraid that major upgrades always have possible issues like this to contend with and need to be looked at for performance regressions.
Well, I can and do switch it off in performance-critical code. But it's not at all obvious that the option is even there (see for example other posts in this thread.) If it were an upfront choice presented to the programmer, I'd have no issue with it (except for the library-linking incompatibilities.)

Plus if you think it's okay for any app to "just muddle along" if it reads/writes to memory it did not intend to then I'm afraid arguments based on that position will be largely ignored (if not outright denounced as wrong by others).
I didn't make my point clear. What I mean is: there is a trade-off between the sort of behaviour that results from a certain class of bug should it be present, and execution speed. If the chance of that bug being present is very low (ideally, one can ensure that there are no such bugs), and the cost of slower execution is high, then it is not a good trade-off. Examples of this sort of code are some scientific simulation codes, which is the sort of thing I've been involved with the last few years. Again, ideally, I could make the choice that the performance-critical parts used unchecked containers, and the scaffolding used checked ones. This is not easily manageable with Microsoft's implementation.

If someone took something correct, and makes it faster by making it wrong I don't have a lot of sympathy for their position. Especially when they could instead have either: used the compiler directive that disabled it and fixed accordingly, downloaded and used an alternative STL without the checking.
I don't have sympathy for their position either! (In fact, I was quite irked.) I mention this only to point out that this sort of technical approach to a programmer-education problem can backfire.
 
LordHunter317":omf3catx said:
The idea that it can be secure in The cases where it isn't are few and far between. It's a minority case, making minority cases difficult at the expense of doing the right thing in the common case is not a bad thing in least.
Why not make it easy to do it either way? As I've said before, I have nothing against checked containers. I just find it frustrating that they have been included in a way which makes avoiding them very awkward. There were alternative approaches.

[...] but it's much easier to audit a codebase for, "Using C-pointers and arrays" then it is for "using iterators correctly".
But much harder to check for safe, correct use of C-pointers and explicit dynamic allocation, than the safe, correct use of iterators and stack-managed collections.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
I could make the choice that the performance-critical parts used unchecked containers, and the scaffolding used checked ones. This is not easily manageable with Microsoft's implementation.

My C++ knowledge isn't up to this. Can one mix two different STL implementations (with namespaces to disambiguate?) within the same codebase easily. I would assume that the results would not play nice together, but if their paths never crossed within the code as it were how feasible is it?

I mention this only to point out that this sort of technical approach to a programmer-education problem can backfire.

Ah right, I actually think this wasn't an attempt to educate, it was an attempt to make the uneducated less dangerous. If you see a problem with vectors, fail to realise the performance issue is the bounds checking and drop STL entirely rather than use a non bounds checked implementation and do your own you're beyond the sort of re-education the MS could deliver en-mass.
 
Status
Not open for further replies.