Fortran 77代码转换为C ++ [英] Conversion of Fortran 77 code to C++

查看:157
本文介绍了Fortran 77代码转换为C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人将Fortran 77代码的大型(我们的是550,000行)程序转换为C ++?你遇到了什么陷阱?转换是否成功?您是否使用 for_c http:// www .cobalt-blue.com / fc / fcmain.htm )?

Has anyone converted a large (ours is 550,000 lines) program of Fortran 77 code to C++ ? What pitfalls did you run into ? Was the conversion a success ? Did you use a tool like for_c ( http://www.cobalt-blue.com/fc/fcmain.htm ) ? Was the resulting C++ code significantly faster or slower ?

推荐答案

有很多事情需要考虑。

真的有两个路径。一种是直接逐行转换为c。我的意思是每个fortran语句到一个相等的c语句。

There are really two paths to take. One is to do a direct line by line conversion to c. By that I mean every fortran statement to an equalivant c statement.

另一个路径是一个重写,与500k loc +将是更多的工作。

The other path to take is a rewrite, and with 500k loc+ that would be much more work. With that kind of size, I certainly would look for a tool, to do the translation, like f2c.

对于一个直接端口的问题...

Issues for a straight port...

gotos直接翻译,您需要为goto目标创建标签。

gotos translate directly, you will need to create labels for the goto targets.

label10:;

    goto label10;

数组下标是一个电位问题。
c是基于零的,fortran是基于1的,因此数组需要在fortran代码中设置一个更大的维数。

Array subscripting is a potiential problem. c is zero based, fortran is 1 based, so arrays need to be dimensioned one larger in the fortran code.

real * ,20)成为成为

real*4 a(10,20) becomes

#define XMAX 10 + 1
#define YMAX 20 + 1 
float a[XMAX][YMAX];

允许循环写成这样。

for (x = 1; x <= XMAX; x++)
    for (y = 1; y <= YMAX; y++)
        a[x][y] = 0.0f;

c数组访问在行主要顺序中,而fortran是列主要。
这可能是一个性能问题。如果它确实成为一个问题,你可能能够
解决它与某种宏定义,颠倒顺序或数组下标。事实上,你也可以让宏从每个下标中减去一个,以使它看起来像一个基于数组,实际上映射到一个基于零的数组。

c array access is in row major order, while fortran is column major. that can be a performance problem. if it does become a problem, you may be able to solve it with some sort of macro definition which reverses the order or the array subscripts. In fact you could also have the macro subtract one off of each of the subscripts in order to make it look like a one based array, actually map to a zero based array.

real * 8 a(XMAX,YMAX)
a(4,2)= 3.14

#define A(X,Y)  a[Y][X]
double a[XMAX][YMAX];
A(4,2) = 3.14;

fortran单元io可以使用stdio类型文件进行模拟。如果您使用单元19,则

fortran unit io can be simulated with stdio type files. if you are using unit 19, then

FILE *fp19 = fopen("file","mode");

如果在文件中使用fortran支架控制,则可能会出现托架控制的问题。单元5和6可以简单地用stdin引用,stdout不用fopen。

There may be issues with carriage control if you are using fortran carriage control in your files. Units 5 and 6 can be simply referenced with stdin, and stdout without fopen.

很多格式都可以用printf函数系列处理。你可能需要添加额外的循环来处理一些fortran数组io。

A lot of formats can be handled with the printf family of functions. You may have to add additional loops to deal with some of the fortran array io.

WRITE(6,200)(PROJ(z,4),z = 1,20)

int z;
for (z = 1, z <= 20; z++)
    printf("%lf ", proj[z][4]);


o使用f2c可能是最快的方法。然后你被卡住了它的rtl。

o做直接端口是一个可行的事情。耗时,但可行

o如果你想长期维护它,我建议重写。非常耗时,可能更快,但是长期更可维护


o using f2c is probably the fastest way to do it. Then you are stuck with its rtl.
o doing a direct port is a feasable thing to do. Time consuming, but doable
o if you want to maintain it long term, I would recommend a rewrite. Very time consuming, probably faster, but more maintainable in the long run

令人高兴的是,在任何情况下,您都有原始程序用作基线来进行一组单元测试以帮助开发工作。

Happily in any case you have the original program to use as a baseline to make a set of unit tests to help in the development effort.

这篇关于Fortran 77代码转换为C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆