Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28602 Discussions

Application execution speed: c++ versus Fortran

jbdamas
Beginner
1,115 Views

Assuming efficient fortran and c++ sorce codes, I need to decide whether to use c++ or fortran.

For building scientific numerically intensive applications involving sparse matrixoperations from scratch (i.e., not using the Intel's Math Kernel Library), are there any speed benchmark advantage to using Intel's FORTRAN compiler over using Intel's C++compiler?

Are there recent studies to this subject?

0 Kudos
7 Replies
TimP
Honored Contributor III
1,115 Views
Quoting - jbdamas

Assuming efficient fortran and c++ sorce codes, I need to decide whether to use c++ or fortran.

For building scientific numerically intensive applications involving sparse matrixoperations from scratch (i.e., not using the Intel's Math Kernel Library), are there any speed benchmark advantage to using Intel's FORTRAN compiler over using Intel's C++compiler?

Are there recent studies to this subject?

http://software.intel.com/en-us/forums/showthread.php?t=61470

shows an effort to get best performance on Fortran, C, and C++ on the same benchmark. You will note that some of the techniques for getting full performance from C++ aren't as idiomatic as in Fortran. You can try the Intel autoparallel option in place of OpenMP; I believe you will find it is not as effective as in the C and Fortran, while the OpenMP is fully effective in C++. You have compromises to make, impossible to answer your question definitively. Your assumption of efficiency in each case is hardly a given.

0 Kudos
jbdamas
Beginner
1,115 Views
Quoting - jbdamas

Assuming efficient fortran and c++ sorce codes, I need to decide whether to use c++ or fortran.

For building scientific numerically intensive applications involving sparse matrixoperations from scratch (i.e., not using the Intel's Math Kernel Library), are there any speed benchmark advantage to using Intel's FORTRAN compiler over using Intel's C++compiler?

Are there recent studies to this subject?

Thanks for the response.

However, after visiting the links, I am still confused. Perhaps if I rephrase my question it could help.

Around the late 1990s, it was definite that (for numerical computations) Fortran (mostly F77) programs ran faster than c++ programs. Since then, many things happened:

(1) better c++ compilers emerged.

(2) Fortran 95 and 2003 adopted many of the features of c++, which increased their programming power but at the expense of reducing some the execution speed efficiency.

Hence, after reviewing the literature on the web (and most of it are old studies.. e,g. 1997), its seems that there is no consensus which numerically intensive programs (c++ or fortran) will run faster. Some say c++ may run 20% slower, others say c++ is 10 times slower. others even claim that c++ will run slightly faster. Others, surprisingly, claim Java runs runs the fastest (.... well not likely).

Hence, I thought that Intel may have recent benchmark studies (preferably on their c++ and fortran compilers) that can help me decide whether c++ or fortran is more appropriate for building my applications.

Note: Naturally, I am assuming that the speed benchmark study properly use the optimal speedoptions on both of the c++ and fortran compilers.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,115 Views

As to which execution speed is faster (C++/Fortran) will depend on your programming style within a language. C++ programming style leans towards Object Oriented whereas Fortran programming style leans towares Array Oriented. And you can program either in the other leaning direction.

C++ has improved in the area of vector code generation and Fortran was an earlier adopter of vector code generation.

Fortran has more flexibility in array descriptors, but it comes at an expense of more overhead. Array indexing (outside of optimized loops) in C++ has less overhead than Array indexing in Fortran. However, the trend in C++ is to use containers and these add overhead on the C++ side of the teter-totter.

Since it is relatively easy to produce mixed language applications, feel free to use the best of each language to produce a faster application.

Jim Dempsey

0 Kudos
jbdamas
Beginner
1,115 Views

As to which execution speed is faster (C++/Fortran) will depend on your programming style within a language. C++ programming style leans towards Object Oriented whereas Fortran programming style leans towares Array Oriented. And you can program either in the other leaning direction.

C++ has improved in the area of vector code generation and Fortran was an earlier adopter of vector code generation.

Fortran has more flexibility in array descriptors, but it comes at an expense of more overhead. Array indexing (outside of optimized loops) in C++ has less overhead than Array indexing in Fortran. However, the trend in C++ is to use containers and these add overhead on the C++ side of the teter-totter.

Since it is relatively easy to produce mixed language applications, feel free to use the best of each language to produce a faster application.

Jim Dempsey

Many thanks Jim. I like your suggestion to use multiple languages ... thus combining their efficiencies

0 Kudos
Matthieu_Brucher
Beginner
1,115 Views
Hi,

Quoting - jimdempseyatthecove
Fortran has more flexibility in array descriptors, but it comes at an expense of more overhead. Array indexing (outside of optimized loops) in C++ has less overhead than Array indexing in Fortran. However, the trend in C++ is to use containers and these add overhead on the C++ side of the teter-totter.

This would mean that if you are using a "optimized" container (in fact just a smart pointer), you might expect the same speed as Fortran (provided that you do not make something stupid) ?
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,115 Views
Quoting - matthieubrucher
Hi,

Quoting - jimdempseyatthecove
Fortran has more flexibility in array descriptors, but it comes at an expense of more overhead. Array indexing (outside of optimized loops) in C++ has less overhead than Array indexing in Fortran. However, the trend in C++ is to use containers and these add overhead on the C++ side of the teter-totter.

This would mean that if you are using a "optimized" container (in fact just a smart pointer), you might expect the same speed as Fortran (provided that you do not make something stupid) ?

Let me see if I can explain this better.

C++ programming style leans towards object oriented (although you don't have to program that way). Fortran style of programming, old style, might be Array based and where each array contains an attribute of a larger "object". New style of Fortran programming leans towards object (user defined types).

In old style Fortran programming (you can do this in C++ too) you might have

REAL(8) :: POSITION(OBJMAX3), VELOCITY(OBJMAX3), ACCELERATION(OBJMAX3), ACCGRAV(OBJMAX3)
REAL(8) :: FORCETOT(OBJMAX3), FORCEEXTERNAL(OBJMAX3), FORCEAERO(OBJMAX3), ...
REAL(8) :: DELTAV(OBJMAX3), MASS(OBJMAX), etc...

i.e. you have a number of objects that is <= OBJMAX and the above arrays express a vector(3) attribute beginning at (ObjectNumber-1)*3. (mass is a single value and not a vector(3))

The disadvantage of this is it is not Object Oriented Programming

The advantage of this is it lends itself to highly optimized SSE3 programming

FORCETOT(1:nObjs) = FORCEEXTERNAL(1:nObjs) + FORCEAERO(1:nObjs) + ...
ACCELERATION(1:nObjs) = (FORCETOT(1:nObjs) / MASS(1:nObjs)) + ACCGRAV(1:nObjs)
DELTAV(1:nObjs) =ACCELERATION(1:nObjs) * T
POSITION(1:nObjs) = (VELOCITY(1:nObjs)+ (DELTAV(1:nObjs) / 2.0D)) * DeltaT
VELOCITY(1:nObjs) = VELOCITY(1:nObjs) + DELTAV(1:nObjs)

or as do loop

do i=1,nObjs
FORCETOT(i) = FORCEEXTERNAL(i) + FORCEAERO(i) + ...
...

Effectively you are re-thinking old programming style as new style stream programming.

Note, C++ can encapsulate the seperate arraysin a larger abstractlist of objects.

When performance matters, you adjustyour programming style accordingly.

Jim Dempsey

0 Kudos
Les_Neilson
Valued Contributor II
1,115 Views


As has been hinted at in other posts, basically you should program in whichever language you are most competent in. But for efficiency - algorithm choice and design is more important thansimple manual optimisation,especially since you mentioned you were going to code the algorithms yourself.

Les
0 Kudos
Reply