Fortran to C++ Conversion
Fortran to C++ conversions are performed using a sophisticated system Objexx has developed and extended over a number of years. This conversion process preserves the syntax and semantics of the Fortran within fully ANSI/ISO compliant C++11 to retain the value of the code and existing documentation and to allow a wide range of developers to maintain the code.
Conversion Benefits and Tradeoffs
Conversion to C++ can be the right choice for a project in a number of situations. C++ is a more powerful language with better object-oriented design support and a much larger pool of developers to draw from. C++ also has a vast range of libraries and excellent GUI, visualization, and plotting packages to choose from.
Conversion vs. Wrapping
It is possible to get many of these benefits by wrapping a Fortran computational core in a modern C++ application, and Objexx uses this approach for some projects. But the burden of maintaining the C++—Fortran interface and the additional complexity of a hybrid application are often not warranted.
The performance of the converted C++ code may initially be slower than the Fortran but can be brought up to parity with a modest tuning effort. The ObjexxFCL arrays have a built-in fast lookup method that can go a long way towards bringing the speed back to that of the Fortran, or in some cases exceeding it. Objexx can do further performance enhancement of converted code, typically achieving speeds well beyond that of the Fortran.
When Performance considerations are important Objexx can do a small conversion+tuning study on sample code to demonstrate the performance of the C++ vs. that of the Fortran.
Conversion to C++ is often a first step in a code evolution plan. Objexx can perform refactoring and/or provide team mentoring to help clients migrate the converted C++ to exploit the capabilities of the language, such as improved modularity via object-oriented design and improved testability.
Some of the technical aspects of our conversion include:
- Reference (not pointer!) argument passing for clean code that corresponds to the Fortran.
- Variable names and comments are preserved.
- Fortran-like array template classes provide column-major arrays that preserve the element indexing of the Fortran and Fortran's array passing "tricks".
- Fortran-like string and substring classes are provided to encapsulate the distinct properties of Fortran strings and allow these strings to work smoothly with C++ strings.
- DATA statements become constructor initializers.
- PARAMETER, SAVE, VOLATILE, INTENT and other Fortran declaration attribute semantics are preserved.
- COMMON blocks are converted to namespaces.
- EQUIVALENCEs and COMMONs that can introduce aliasing are detected and converted as appropriate for the intent of the usage.
The Objexx Fortran Compatibility Library (ObjexxFCL) that provides the array, string, and intrinsic function support is open source: it is included in C++ source form with licensing that allows modification by the clients. The ObjexxFCL is in use in major codes and has been heavily tested and vetted for correctness. The ObjexxFCL arrays provide high-performance tuning capabilities and some unique dynamic sizing capabilities that can be exploited to great effect in meeting post-conversion modernization and refactoring goals.
This conversion of a simple Fortran 77 routine to C++ (shown at right) gives some sense of our approach. The philosophy of this method is to maintain the syntax and semantics of the Fortran as much as possible, thereby allowing both C++ and Fortran developers to maintain the code.
Objexx converted the U.S. Dept. of Energy EnergyPlus application from Fortran 90/2003 to C++ under contract with Autodesk. EnergyPlus is open source and the code is in the EnergyPlus GitHub repository. The ObjexxFCL library is in the ObjexxFCL directory of that repository. The C++ EnergyPlus was quickly validated and tuned to exceed the performance of the Fortran version and is under continued active development. The Fortran and C++ conversion sources can be provided for review.