Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29282 Discussions

Which is more efficient internal Read() or ANINT()?

gelarimer
Beginner
923 Views

Is it more efficient to use Read() below to obtain JI rounded to same value asrepresented by string cJI, or is it better to use the intrinsic function ANINT()? Thanks for any information.

Write(cJI, '(F5.3)', IOSTAT=iErr) JI

! convert back so that values used in program execution are the same as
! displayed in string cJI, for example JI = 0.9867, cJI = '0.987', and after Read() below JI = 0.987
Read(cJI, '(F5.3)', IOSTAT=iErr) JI

Or is it more efficient to use the followingcode in place of
Read() above?

JI = ANINT(JI*1000.0)/1000.0

0 Kudos
7 Replies
TimP
Honored Contributor III
923 Views
Apparently, you have no concern about overflow or other corner cases where the results might differ. Then, the ANINT method has to be far faster, even though it has to distinguish between the IEEE "banker's" rounding directly supported in hardware, and the Fortran method of rounding half-way values away from 0. If you perform this operation in a loop, without setting -Qprec-div, ifort would likely change /1000. to *.001
0 Kudos
jparsly1
New Contributor I
923 Views

This would depend on lot on your compiler, but my guess would be that the ANINT would be faster.

The one concrete example I know about was a situation using CVF in which in one

of the deepest layers of a numerical model there was an internal write statement which

by itself accounted for about 10% of the execution time of the model. I was shocked.

0 Kudos
Steven_L_Intel1
Employee
923 Views
Any type of I/O statement is going to be signfiicantly slower than referencing an intrinsic function, ESPECIALLY if it is formatted I/O.
0 Kudos
gelarimer
Beginner
923 Views

Thanks for the replies.

tim 18wrote:

"Then, the ANINT method has to be far faster, even though it has to distinguish between the IEEE "banker's" rounding directly supported in hardware, and the Fortran method of rounding half-way values away from 0."

I understand what "banker's"and Fortran rounding are, butdo not understand why ANINT() has to distinguish between the two methods. I assume that ANINT() uses the Fortran method of rounding. Is the "banker's" rounding associated with multiplying and dividing by 1000.0?

0 Kudos
TimP
Honored Contributor III
923 Views
ANINT() could be implemented in a short fast instruction sequence, if Fortran accepted IEEE style rounding. Differences occur only for values +- 0.5, 2.5, 4.5, .... bankers' IEEE style rounding rounds those values to nearest even integral value, while Fortran rounds to the larger magnitude integer.
0 Kudos
Steven_L_Intel1
Employee
923 Views
Or to put it another way- the Fortran language defines ANINT as AINT(x+0.5) for positive values and AINT(x-0.5) for negative values. This is not the same as default IEEE "round to even", also called "bankers' rounding". So the code to do ANINT is a bit more complicated than it might otherwise need to be, yet still far less than an internal WRITE.
0 Kudos
gelarimer
Beginner
923 Views
Thanks Steve and tim18 for the information!
0 Kudos
Reply