[Rcpp-devel] use of auxiliary functions
Davor Cubranic
cubranic at stat.ubc.ca
Mon Aug 9 20:46:45 CEST 2010
I tried compiling this function with various gcc optimization switches. The results of my experiment are mixed. For most commonly-used optimization switches (-On for n=0..3, even -fast), I could see multiple calls to 'cos' with repeated same arguments, directly corresponding to the expressions in the source code. Only once I added '-ffast-math' did the compiler extract common subexpressions (e.g., 'cos(phi)', etc.) so that they were only calculated once.
The catch is that I doubt anyone ever turns this switch on. I didn't even know about it until today, and only found it in the man page as I was just about to throw in the towel and admit I was proven wrong. :-)
This is with GCC 4.2.1 from Apple's developer kit on OS X 10.6. If anyone has a highly optimizing compiler like Intel's 'icc', I'd love to hear the results you get.
Davor
On 2010-08-07, at 6:21 PM, Davor Cubranic wrote:
> On August 6, 2010 02:44:00 pm Douglas Bates wrote:
>> On Fri, Aug 6, 2010 at 3:14 PM, baptiste auguie
>>
>>> arma::mat euler(double phi, double theta, double psi)
>>> {
>>> arma::mat Rot(3,3);
>>> Rot(0,0) = cos(psi)*cos(phi) - cos(theta)*sin(phi)*sin(psi);
>>> Rot(0,1) = cos(psi)*sin(phi) + cos(theta)*cos(phi)*sin(psi);
>>> Rot(0,2) = sin(psi)*sin(theta);
>>>
>>> Rot(1,0) = -sin(psi)*cos(phi) - cos(theta)*sin(phi)*cos(psi);
>>> Rot(1,1) = -sin(psi)*sin(phi) + cos(theta)*cos(phi)*cos(psi);
>>> Rot(1,2) = cos(psi)*sin(theta);
>>>
>>> Rot(2,0) = sin(phi)*sin(theta);
>>> Rot(2,1) = -cos(phi)*sin(theta);
>>> Rot(2,2) = cos(theta);
>>> return (Rot);
>>> }
>>
>> It is probably not a big deal now but you are recalculating the same
>> trigonometric functions several times in there. Why not calculate
>> the cos and sin of the each of the angles just once and then form
>> the elements of the rotation matrix as linear combinations? (Once
>> upon a time we used to keep track of every floating point
>> calculation and try to work out ways to minimize that number.)
>
> True, but I wonder whether in this case any modern compiler will catch
> the common intermediate calculations and optimize them for you so that
> they're only calculated once.
>
> Easy enough to check with gcc and its '-s' argument, which will give you
> the assembly output. :-)
>
> Davor
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
More information about the Rcpp-devel
mailing list