[PATCH 2/2] configure: Use intel math.h header.
icl when run under msvc12 currently generates incorrect code. This is due to an issue with msvc12's C99 additions to math.h. Several defines in msvc's math.h are not compatible with icl and will compile but generate erroneous results during runtime. An obvious example is that NAN from math.h evaluates to 0.0 under icl which is clearly not a correct value for NaN. This cause several errors that completly break FFmpeg. There are several ways to fix this. 1) Force icl's floating point compliance but negatively impact performance in doing so. 2) Add compiler specific code into FFmpeg or 3) Have icl use its own native math header (which can also improve performance). Using 1) introduces performance degradation and will break again should the user specify additional conflicting configure flags. Using 2) adds icl specific code handling into FFmpeg which is rather ugly and should probably be avoided. Using 3) fixes all errors and has the potential to increase performance through Intels optimized math routines. Since 3) seems to work best attached is a patch to force in the inclusion of icl's math.h (named mathimf.h) which will override msvc's math.h and avoid any issues. Note in order for this to work the previous patch entitled "Fix clashes with 'I' and 'complex' defined in complex.h" must have been previously applied.
On 7 May 2014 23:52, Matt Oliver <protogonoi@gmail.com> wrote:
icl when run under msvc12 currently generates incorrect code. This is due to an issue with msvc12's C99 additions to math.h. Several defines in msvc's math.h are not compatible with icl and will compile but generate erroneous results during runtime. An obvious example is that NAN from math.h evaluates to 0.0 under icl which is clearly not a correct value for NaN. This cause several errors that completly break FFmpeg.
There are several ways to fix this. 1) Force icl's floating point compliance but negatively impact performance in doing so. 2) Add compiler specific code into FFmpeg or 3) Have icl use its own native math header (which can also improve performance).
Using 1) introduces performance degradation and will break again should the user specify additional conflicting configure flags. Using 2) adds icl specific code handling into FFmpeg which is rather ugly and should probably be avoided. Using 3) fixes all errors and has the potential to increase performance through Intels optimized math routines.
Since 3) seems to work best attached is a patch to force in the inclusion of icl's math.h (named mathimf.h) which will override msvc's math.h and avoid any issues.
Note in order for this to work the previous patch entitled "Fix clashes with 'I' and 'complex' defined in complex.h" must have been previously applied.
Due to some issues raised about this patches sister patch [1/2] I have come up with a cleaner alternative. Its still based on 3) from previous email but it does it by creating a wrapper math.h that catches all header includes and then routes those to the icl header where appropriate. It also can then remove the clashes with 'I' and 'complex' that now makes the previous sister patch redundant. This makes the patch alot cleaner and less obtrusive (only affects icl) and fixes a fatal bug with icl and msvc12.
On May 7, 2014 10:41 AM, "Matt Oliver" <protogonoi@gmail.com> wrote: > > On 7 May 2014 23:52, Matt Oliver <protogonoi@gmail.com> wrote: > Due to some issues raised about this patches sister patch [1/2] I have come > up with a cleaner alternative. Its still based on 3) from previous email > but it does it by creating a wrapper math.h that catches all header > includes and then routes those to the icl header where appropriate. It also > can then remove the clashes with 'I' and 'complex' that now makes the > previous sister patch redundant. This makes the patch alot cleaner and less > obtrusive (only affects icl) and fixes a fatal bug with icl and msvc12. Sounds reasonable. Just curious, with this patch, how much faster is ICL compared to MSVC? > +#ifdef __INTEL_COMPILER > + // Use the Intel Math library. This provide Intel processor > optimized functions. + // This also avoids an error with the msvc math.h definition of NAN > found in msvc12. > +# include <mathimf.h> How about Linux? Intel Math Library is available for Linux too. > + // The Intel header automatically includes defines used with complex.h > + // that interfere with normal code and need to be removed. > +# undef I > +# undef complex > +#elif defined(_MSC_VER) > +# include <../include/math.h> What is this ../include/math.h? Also from the configure settings this header is not included by MSVC so what is the point of this? > +#else > +# error This header should only be used with native Windows > compilers > +#endif Timothy
Sounds reasonable. Just curious, with this patch, how much faster is ICL compared to MSVC?
General msvc/icl benchmarks show a significant speedup but as for FFmpeg specifically I havnt done a full performance check yet. Also at this point im not sure what the best way to benchmark FFmpeg for this is. If there are some specific benchmarks you want me to run let me know and ill compare the difference.
How about Linux? Intel Math Library is available for Linux too.
In theory this would work perfectly fine with icc but as I dont have an icc install to test it on I didnt want to submit a untested patch. Some one with icc should have a look at that one and confirm (also whether having the header in the msvcrt folder is a little too confusing if also being used for icc). There should be absolutely no problem to doing this with icc and I can extend the patch to cover that aswell if wanted (if so feel free to suggest a better location for the header file). What is this ../include/math.h? Also from the configure settings this
header is not included by MSVC so what is the point of this?
../include/math.h is there because msvc doesnt support include_next so this is the equivalent to pass on to the internal math.h file. This code is there as fallback to prevent issues arising with msvc picking up the compat/msvcrt path (which can happen if compiling any other file in that directory). Also I left it there so that it can be used to add some cleaner msvc compat in a future patch i.e. add in the missing defines not found in older versions of msvc so it is seamlessly C99 compat without changing any other FFmpeg code (currently mathematics.h tries to do this but anyone writing code with gcc will normally just include math.h and not realise there is a problem with msvc - this would fix that).
On 07.05.2014, at 15:52, Matt Oliver <protogonoi@gmail.com> wrote:
icl when run under msvc12 currently generates incorrect code. This is due to an issue with msvc12's C99 additions to math.h. Several defines in msvc's math.h are not compatible with icl and will compile but generate erroneous results during runtime. An obvious example is that NAN from math.h evaluates to 0.0 under icl which is clearly not a correct value for NaN. This cause several errors that completly break FFmpeg.
There are several ways to fix this. 1) Force icl's floating point compliance but negatively impact performance in doing so. 2) Add compiler specific code into FFmpeg or 3) Have icl use its own native math header (which can also improve performance).
To be honest I find this quite ridiculous. Almost two years should be enough for Intel to get the standard system headers to work correctly. Or to put it differently: Using ICL starts to sound like a horribly bad idea if it already miscompiles basic system headers, which in turn IMHO raises some questions how many hacks we should accept for it.
On 11 May 2014 03:03, Reimar Döffinger <Reimar.Doeffinger@gmx.de> wrote:
On 07.05.2014, at 15:52, Matt Oliver <protogonoi@gmail.com> wrote:
icl when run under msvc12 currently generates incorrect code. This is due to an issue with msvc12's C99 additions to math.h. Several defines in msvc's math.h are not compatible with icl and will compile but generate erroneous results during runtime. An obvious example is that NAN from math.h evaluates to 0.0 under icl which is clearly not a correct value for NaN. This cause several errors that completly break FFmpeg.
There are several ways to fix this. 1) Force icl's floating point compliance but negatively impact performance in doing so. 2) Add compiler specific code into FFmpeg or 3) Have icl use its own native math header (which can also improve performance).
To be honest I find this quite ridiculous. Almost two years should be enough for Intel to get the standard system headers to work correctly. Or to put it differently: Using ICL starts to sound like a horribly bad idea if it already miscompiles basic system headers, which in turn IMHO raises some questions how many hacks we should accept for it.
True, Im amazed this hasnt been an issue more often. Im guessing most people with icl will use the icl header and the rest just havnt noticed the problem. The exact issue is that the msvc headers define NAN as ( INFINITY * 0.0 ) which the optimizers in icl just replace with 0. So it will work fine if you disable optimization and force floating point compliance but this defeats the purpose of an optimizing compiler like icl and whats the point of a release build if it cant even be compiled using -O2. Optimizing out multiplies by zero is standard procedure by all compilers not just icl so im not sure whos to blame here intel or microsoft. Msvc also optimizes out multiplies by zero but they must have an exception for their own header whereas intel does not (im guessing because they have their own header and just recommend using that). Given how many hacks are already in FFmpeg to deal with various non-conformance issues with msvc headers/libs its not to surprising and does make me lean more towards questioning microsoft rather than intel. Either way there are some performance advantages to using the intel math library with various functions benchmarking 1.5x to 2x faster (many even faster). Of course in the scheme of an entire program like ffmpeg the actual performance difference benchmarks as being rather minimal (especially with so much hand tuned asm being used). But any free performance increase no matter how small probably shouldn't be scoffed at. So with that in mind I have a slightly modified patch that also can be applied to icc that ill submit once Ive run it through fate a few more times.
On Sun, May 11, 2014 at 03:44:23AM +1000, Matt Oliver wrote:
On 11 May 2014 03:03, Reimar Döffinger <Reimar.Doeffinger@gmx.de> wrote:
On 07.05.2014, at 15:52, Matt Oliver <protogonoi@gmail.com> wrote:
icl when run under msvc12 currently generates incorrect code. This is due to an issue with msvc12's C99 additions to math.h. Several defines in msvc's math.h are not compatible with icl and will compile but generate erroneous results during runtime. An obvious example is that NAN from math.h evaluates to 0.0 under icl which is clearly not a correct value for NaN. This cause several errors that completly break FFmpeg.
There are several ways to fix this. 1) Force icl's floating point compliance but negatively impact performance in doing so. 2) Add compiler specific code into FFmpeg or 3) Have icl use its own native math header (which can also improve performance).
To be honest I find this quite ridiculous. Almost two years should be enough for Intel to get the standard system headers to work correctly. Or to put it differently: Using ICL starts to sound like a horribly bad idea if it already miscompiles basic system headers, which in turn IMHO raises some questions how many hacks we should accept for it.
True, Im amazed this hasnt been an issue more often. Im guessing most people with icl will use the icl header and the rest just havnt noticed the
problem. The exact issue is that the msvc headers define NAN as ( INFINITY * 0.0 ) which the optimizers in icl just replace with 0. So it will work fine if you disable optimization and force floating point compliance but this defeats the purpose of an optimizing compiler like icl and whats the point of a release build if it cant even be compiled using -O2. Optimizing out multiplies by zero is standard procedure by all compilers not just icl so im not sure whos to blame here intel or microsoft. Msvc also optimizes
The values you quote are known at compile time, theres nothing to optimize out here. There would be no *0 at runtime either way [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I have often repented speaking, but never of holding my tongue. -- Xenocrates
On Sat, May 10, 2014 at 7:44 PM, Matt Oliver <protogonoi@gmail.com> wrote:
On 11 May 2014 03:03, Reimar Döffinger <Reimar.Doeffinger@gmx.de> wrote:
On 07.05.2014, at 15:52, Matt Oliver <protogonoi@gmail.com> wrote:
icl when run under msvc12 currently generates incorrect code. This is due to an issue with msvc12's C99 additions to math.h. Several defines in msvc's math.h are not compatible with icl and will compile but generate erroneous results during runtime. An obvious example is that NAN from math.h evaluates to 0.0 under icl which is clearly not a correct value for NaN. This cause several errors that completly break FFmpeg.
There are several ways to fix this. 1) Force icl's floating point compliance but negatively impact performance in doing so. 2) Add compiler specific code into FFmpeg or 3) Have icl use its own native math header (which can also improve performance).
To be honest I find this quite ridiculous. Almost two years should be enough for Intel to get the standard system headers to work correctly. Or to put it differently: Using ICL starts to sound like a horribly bad idea if it already miscompiles basic system headers, which in turn IMHO raises some questions how many hacks we should accept for it.
True, Im amazed this hasnt been an issue more often. Im guessing most people with icl will use the icl header and the rest just havnt noticed the problem. The exact issue is that the msvc headers define NAN as ( INFINITY * 0.0 ) which the optimizers in icl just replace with 0.
I doubt there is actual exceptions in the compiler for any particular header, more likely those other compilers are smart enough to know that 0 * inf is not 0. Its not the first case of ICL being overzealous and its "optimization" causing problems. Is an ICL build even faster than a GCC build? Or why are you using it? MSVC builds are significantly slower than GCC builds, even if I disable inline assembly in the GCC build - and in my experience ICL doesn't provide *that* much of a bonus. - Hendrik
On 10.05.2014, at 19:44, Matt Oliver <protogonoi@gmail.com> wrote:
On 11 May 2014 03:03, Reimar Döffinger <Reimar.Doeffinger@gmx.de> wrote:
On 07.05.2014, at 15:52, Matt Oliver <protogonoi@gmail.com> wrote:
icl when run under msvc12 currently generates incorrect code. This is due to an issue with msvc12's C99 additions to math.h. Several defines in msvc's math.h are not compatible with icl and will compile but generate erroneous results during runtime. An obvious example is that NAN from math.h evaluates to 0.0 under icl which is clearly not a correct value for NaN. This cause several errors that completly break FFmpeg.
There are several ways to fix this. 1) Force icl's floating point compliance but negatively impact performance in doing so. 2) Add compiler specific code into FFmpeg or 3) Have icl use its own native math header (which can also improve performance).
To be honest I find this quite ridiculous. Almost two years should be enough for Intel to get the standard system headers to work correctly. Or to put it differently: Using ICL starts to sound like a horribly bad idea if it already miscompiles basic system headers, which in turn IMHO raises some questions how many hacks we should accept for it.
True, Im amazed this hasnt been an issue more often. Im guessing most people with icl will use the icl header and the rest just havnt noticed the problem. The exact issue is that the msvc headers define NAN as ( INFINITY * 0.0 ) which the optimizers in icl just replace with 0. So it will work fine if you disable optimization and force floating point compliance but this defeats the purpose of an optimizing compiler like icl
No, it really doesn't. Floating-point code is not the only thing in a program and e.g. /fp:precise still allows plenty of (problematic) invalid optimizations. The problem is that Intel decide to enable by default very dangerous and wrong optimizations. If you search for the gcc "equivalent" -ffast-math you should find lots of examples. These optimizations are _dangerous_ and honestly you'd have to be insane to enable them _globally_. Floating-point is problematic enough even if you can rely on compliance. We do not support/enable it on gcc for good reasons, and I object to supporting it for other compilers just because the vendor though fast is better than safe. If we want it, make it an option and add -ffast-math for gcc as well. Anything else is just setting people up for wrong benchmarks (by allowing only some compilers to ignore the rules) and other bad surprises.
Optimizing out multiplies by zero is standard procedure by all compilers not just icl
No it isn't. Just try writing a function that multiplies a float value by 0 and look at the generated assembler code.
Msvc also optimizes out multiplies by zero
I'm fairly certain it does not with default options.
Msvc also optimizes out multiplies by zero I'm fairly certain it does not with default options.
Correct, my apologies for not being clearer. I was referring to optimizing out multiplies by zero with fp:fast optimizations which icl defaults to. So I was just trying to draw a comparison at those settings but as your right the default for msvc is fp:precise so at that setting the comparison is incorrect.
No, it really doesn't. Floating-point code is not the only thing in a program and e.g. /fp:precise still allows plenty of (problematic) invalid optimizations. The problem is that Intel decide to enable by default very dangerous and wrong optimizations. If you search for the gcc "equivalent" -ffast-math you should find lots of examples. These optimizations are _dangerous_ and honestly you'd have to be insane to enable them _globally_. Floating-point is problematic enough even if you can rely on compliance. We do not support/enable it on gcc for good reasons, and I object to supporting it for other compilers just because the vendor though fast is better than safe. If we want it, make it an option and add -ffast-math for gcc as well. Anything else is just setting people up for wrong benchmarks (by allowing only some compilers to ignore the rules) and other bad surprises.
icl provides two fast math optimization levels fp:fast=1/fp:fast=2 and by default uses the former. fp:fast=2 does heavy float optimizations and that one can as you say be _dangerous_/insane. Clearly fp:fast is not suitable either so given that msvc is using fp:precise it would be more consistent to patch icl to do the same. Adding a -fast-math option would be interesting but as it breaks stuff (this being case in point) then your right in that its probably not a good idea.
Is an ICL build even faster than a GCC build? Or why are you using it? MSVC builds are significantly slower than GCC builds, even if I disable inline assembly in the GCC build - and in my experience ICL doesn't provide *that* much of a bonus.
ICL is at times significantly faster than msvc depending on the code (SIMD heavy stuff benefits more). Comparing to gcc the benefits aren't that significant performance wise. However on Windows the comparison is with MinGW and the advantage that icl has is that its a native toolchain that can directly use the latest native windows headers/libs and integrates/debugs nicely with visual studio projects. So on non-Windows I have no problems with gcc but when it comes to native windows the comparison is between icl and msvc and there are advantages to icl. But at any rate icl support was in FFmpeg before I did anything. So dont shoot the messenger so to speak as I didnt create the issues im just trying to fix them ;) Attached is a new patch that should be more acceptable.
On 05/10/14 10:42 PM, Matt Oliver wrote:
Is an ICL build even faster than a GCC build? Or why are you using it?
MSVC builds are significantly slower than GCC builds, even if I disable inline assembly in the GCC build - and in my experience ICL doesn't provide*that* much of a bonus.
ICL is at times significantly faster than msvc depending on the code (SIMD heavy stuff benefits more). Comparing to gcc the benefits aren't that significant performance wise. However on Windows the comparison is with MinGW and the advantage that icl has is that its a native toolchain that can directly use the latest native windows headers/libs
Is it also true that non-intel processors also benefit from ICL? Dave
On 11 May 2014 18:17, Dave Yeo <daveryeo@telus.net> wrote:
On 05/10/14 10:42 PM, Matt Oliver wrote:
Is an ICL build even faster than a GCC build? Or why are you using it?
MSVC builds are significantly slower than GCC builds, even if I disable inline assembly in the GCC build - and in my experience ICL doesn't provide*that* much of a bonus.
ICL is at times significantly faster than msvc depending on the code (SIMD heavy stuff benefits more). Comparing to gcc the benefits aren't that significant performance wise. However on Windows the comparison is with MinGW and the advantage that icl has is that its a native toolchain that can directly use the latest native windows headers/libs
Is it also true that non-intel processors also benefit from ICL? Dave
There are different optimizations that can be enabled with icl. For instance with vectorization -arch:SSE2 will enable SSE2 optimizations for all SSE2 capable cpus (non-intel included). Then theres the intel Qx:SSE2 which enables SSE2 optimization that are faster again but these only work on intel cpus. FFmpeg doesnt and should never use the intel cpu specific optimization settings (which are always disabled by default). But any general purpose optimizations made by icl are also applicable to any capable cpu (amd included). ICL has been criticized in the past for favouring intel cpus over amd ones but that is mostly with things like Qx/Qax settings and the MKL libs which non of are used in FFmpeg so arent as relevant to us. So to answer your question since we dont use intel cpu specific optimization settings then any performance improvement over msvc is also generally applicable to non-intel cpus as well. I think the patch is good, and I'd also say the patch to use the other math
header can still be interesting, but only if an actual performance benefit can be shown (most likely in some of the less-optimized float audio codecs like on2avc).
Well I think your right in that for consistency icl should be fp:precise by default as in the last patch. Using the intel math header is potentially interesting but i ran some benchmarks over the main codecs and as they are mostly hand tuned asm there is no difference. It would only affect those less-optimized codecs that rely more on libs math funxtions but I was not familiar enough to know which ones would be likely candidates, So thanks for the heads up about on2avc ill look into potentially doing some benchmarks at some point and see if its worth it. In the mean time though if there are no objections we should apply the fp:precise patch as that keeps things consistent and fixes an error that has actually been in FFmpeg for several years now.
On 11.05.2014, at 07:42, Matt Oliver <protogonoi@gmail.com> wrote:
to patch icl to do the same. Adding a -fast-math option would be interesting but as it breaks stuff (this being case in point) then your right in that its probably not a good idea.
I think in the end nobody could show a significant performance benefit for fast-math anyway. It also has the nasty property (just like ICL) that output depens on what optimizations the compiler does. E.g. 0*inf is 0 if the compiler sees the constant 0, but it is NaN if it doesn't. That is very non-fun for debugging. I think the patch is good, and I'd also say the patch to use the other math header can still be interesting, but only if an actual performance benefit can be shown (most likely in some of the less-optimized float audio codecs like on2avc).
On Sun, May 11, 2014 at 03:42:30PM +1000, Matt Oliver wrote:
Msvc also optimizes out multiplies by zero I'm fairly certain it does not with default options.
Correct, my apologies for not being clearer. I was referring to optimizing out multiplies by zero with fp:fast optimizations which icl defaults to. So I was just trying to draw a comparison at those settings but as your right the default for msvc is fp:precise so at that setting the comparison is incorrect.
No, it really doesn't. Floating-point code is not the only thing in a program and e.g. /fp:precise still allows plenty of (problematic) invalid optimizations. The problem is that Intel decide to enable by default very dangerous and wrong optimizations. If you search for the gcc "equivalent" -ffast-math you should find lots of examples. These optimizations are _dangerous_ and honestly you'd have to be insane to enable them _globally_. Floating-point is problematic enough even if you can rely on compliance. We do not support/enable it on gcc for good reasons, and I object to supporting it for other compilers just because the vendor though fast is better than safe. If we want it, make it an option and add -ffast-math for gcc as well. Anything else is just setting people up for wrong benchmarks (by allowing only some compilers to ignore the rules) and other bad surprises.
icl provides two fast math optimization levels fp:fast=1/fp:fast=2 and by default uses the former. fp:fast=2 does heavy float optimizations and that one can as you say be _dangerous_/insane. Clearly fp:fast is not suitable either so given that msvc is using fp:precise it would be more consistent to patch icl to do the same. Adding a -fast-math option would be interesting but as it breaks stuff (this being case in point) then your right in that its probably not a good idea.
Is an ICL build even faster than a GCC build? Or why are you using it? MSVC builds are significantly slower than GCC builds, even if I disable inline assembly in the GCC build - and in my experience ICL doesn't provide *that* much of a bonus.
ICL is at times significantly faster than msvc depending on the code (SIMD heavy stuff benefits more). Comparing to gcc the benefits aren't that significant performance wise. However on Windows the comparison is with MinGW and the advantage that icl has is that its a native toolchain that can directly use the latest native windows headers/libs and integrates/debugs nicely with visual studio projects. So on non-Windows I have no problems with gcc but when it comes to native windows the comparison is between icl and msvc and there are advantages to icl.
But at any rate icl support was in FFmpeg before I did anything. So dont shoot the messenger so to speak as I didnt create the issues im just trying to fix them ;)
Attached is a new patch that should be more acceptable.
configure | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 6f47345edbdacaa5056da4c8ac763b700556852e configure-Fix-floating-point-conformance-ith-icl.patch From 5ea7b950e416069aae5985858580fde44de8ef91 Mon Sep 17 00:00:00 2001 From: Matt Oliver <protogonoi@gmail.com> Date: Sun, 11 May 2014 15:07:13 +1000 Subject: [PATCH] configure: Fix floating point conformance ith icl.
applied thx [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Many things microsoft did are stupid, but not doing something just because microsoft did it is even more stupid. If everything ms did were stupid they would be bankrupt already.
On 5/11/2014 6:42 AM, Matt Oliver wrote:
However on Windows the comparison is with MinGW and the advantage that icl has is that its a native toolchain that can directly use the latest native windows headers/libs and integrates/debugs nicely with visual studio projects.
I don't think this is true anymore. All the latest ICLs ship with gdb. - Derek
On 12 May 2014 23:48, Derek Buitenhuis <derek.buitenhuis@gmail.com> wrote:
On 5/11/2014 6:42 AM, Matt Oliver wrote:
However on Windows the comparison is with MinGW and the advantage that icl has is that its a native toolchain that can directly use the latest native windows headers/libs and integrates/debugs nicely with visual studio projects.
I don't think this is true anymore. All the latest ICLs ship with gdb.
I use it daily through Visual Studio so can vouch that its true. ICL comes with Visual Studio integration and the generated libs can be used as drop in replacements for msvc ones (and vice-versa). ICL generated libs/programs will debug fine with cdb and even generates .pdbs for "edit and continue" debugging. And ICL uses pretty much all of the msvc libraries directly by default. True ICL does currently ship with gdb but that is used for debugging Intel MIC processors (Xeon Phi).
On 5/12/2014 3:14 PM, Matt Oliver wrote:
True ICL does currently ship with gdb but that is used for debugging Intel MIC processors (Xeon Phi).
Ah OK. - Derek
participants (7)
-
Dave Yeo -
Derek Buitenhuis -
Hendrik Leppkes -
Matt Oliver -
Michael Niedermayer -
Reimar Döffinger -
Timothy Gu